Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
blueloveTH committed Aug 6, 2023
1 parent aa0b415 commit 985a4f4
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 192 deletions.
320 changes: 165 additions & 155 deletions include/imgui_bindings.hpp

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions include/miniaudio_bindings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ class AudioEngine{
}
};

struct PyAudioEngine: OpaquePointer<AudioEngine>{
struct PyAudioEngine {
PY_CLASS(PyAudioEngine, _ct, AudioEngine)
using OpaquePointer<AudioEngine>::OpaquePointer;

AudioEngine* ptr;
PyAudioEngine(AudioEngine* p): ptr(p){}

AudioEngine& _() { return *ptr;}

static void _register(VM* vm, PyObject* mod, PyObject* type){
vm->bind_notimplemented_constructor<PyAudioEngine>(type);
PK_REGISTER_PROPERTY(PyAudioEngine, volume, "float");
PK_REGISTER_PROPERTY(PyAudioEngine, "volume", _, get_volume, set_volume);
}
};

Expand Down
16 changes: 13 additions & 3 deletions include/template/imgui_bindings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

namespace pkpy{

template<typename T>
struct OpaquePointer{
T* ptr;
OpaquePointer(T* ptr): ptr(ptr){}

T* operator->(){ return ptr; }

T& _(){ return *ptr; }
};

inline PyObject* py_var(VM* vm, ImVec2 im_vec){
Vec2 vec(im_vec.x, im_vec.y);
return py_var(vm, vec);
Expand Down Expand Up @@ -61,12 +71,12 @@ struct Texture{
static void _register(VM* vm, PyObject* mod, PyObject* type){
vm->bind_notimplemented_constructor<Texture>(type);

type->attr().set("size", vm->property(
vm->bind_property(type, "size",
[](VM* vm, ArgsView args){
Texture& self = _CAST(Texture&, args[0]);
return VAR_T(PyVec2, Vec2(self.width, self.height));
}
));
);
}
};

Expand All @@ -78,7 +88,7 @@ struct PyImDrawList: OpaquePointer<ImDrawList>{
static void _register(VM* vm, PyObject* mod, PyObject* type){
vm->bind_notimplemented_constructor<PyImDrawList>(type);

PK_REGISTER_FIELD(PyImDrawList, Flags) // int
PK_REGISTER_FIELD(PyImDrawList, "Flags", _, Flags) // int

vm->bind(type, "AddLine(self, p1: vec2, p2: vec2, col: vec4, thickness: float = 1.0)", [](VM* vm, ArgsView args){
PyImDrawList& self = _CAST(PyImDrawList&, args[0]);
Expand Down
2 changes: 1 addition & 1 deletion moc/gen_imgui_pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def AddPolyline(self, points: list[vec2], col: vec4, flags: int = 0, thickness:
fn = 'PK_REGISTER_READONLY_FIELD'
else:
fn = 'PK_REGISTER_FIELD'
__fields.append(' '*8 + f'{fn}({__pycls}, {name})'.ljust(70) + f'// {type}')
__fields.append(' '*8 + f'{fn}({__pycls}, "{name}", _, {name})'.ljust(70) + f'// {type}')
__fields = '\n'.join(__fields)
result = struct_template.substitute(
__pycls=__pycls,
Expand Down
2 changes: 1 addition & 1 deletion pocketpy
57 changes: 30 additions & 27 deletions src/miniaudio_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,40 @@ namespace ct{
});

// looping
type->attr().set("looping", vm->property([](VM* vm, ArgsView args){
Sound& self = _CAST(Sound&, args[0]);
bool looping = ma_sound_is_looping(&self->sound);
return VAR(looping);
}, [](VM* vm, ArgsView args){
Sound& self = _CAST(Sound&, args[0]);
bool looping = CAST(bool, args[1]);
ma_sound_set_looping(&self->sound, looping);
return vm->None;
}));
vm->bind_property(type, "looping",
[](VM* vm, ArgsView args){
Sound& self = _CAST(Sound&, args[0]);
bool looping = ma_sound_is_looping(&self->sound);
return VAR(looping);
}, [](VM* vm, ArgsView args){
Sound& self = _CAST(Sound&, args[0]);
bool looping = CAST(bool, args[1]);
ma_sound_set_looping(&self->sound, looping);
return vm->None;
});

// volume
type->attr().set("volume", vm->property([](VM* vm, ArgsView args){
Sound& self = _CAST(Sound&, args[0]);
float volume = ma_sound_get_volume(&self->sound);
return VAR(volume);
}, [](VM* vm, ArgsView args){
Sound& self = _CAST(Sound&, args[0]);
float volume = CAST(float, args[1]);
ma_sound_set_volume(&self->sound, volume);
return vm->None;
}));
vm->bind_property(type, "volume",
[](VM* vm, ArgsView args){
Sound& self = _CAST(Sound&, args[0]);
float volume = ma_sound_get_volume(&self->sound);
return VAR(volume);
}, [](VM* vm, ArgsView args){
Sound& self = _CAST(Sound&, args[0]);
float volume = CAST(float, args[1]);
ma_sound_set_volume(&self->sound, volume);
return vm->None;
});

// duration (readonly)
type->attr().set("duration", vm->property([](VM* vm, ArgsView args){
Sound& self = _CAST(Sound&, args[0]);
float length;
ma_result res = ma_data_source_get_length_in_seconds(&self->decoder, &length);
ma_check(res);
return VAR(length);
}));
vm->bind_property(type, "duration",
[](VM* vm, ArgsView args){
Sound& self = _CAST(Sound&, args[0]);
float length;
ma_result res = ma_data_source_get_length_in_seconds(&self->decoder, &length);
ma_check(res);
return VAR(length);
});
}

void patch_module_ct_miniaudio(VM *vm, PyObject *mod){
Expand Down
2 changes: 0 additions & 2 deletions src/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "imgui.h"
#include "imgui_bindings.hpp"
#include "box2d_bindings.hpp"
#include "miniaudio_bindings.hpp"
#include "app_bindings.hpp"

Expand All @@ -19,7 +18,6 @@ void app_start(){
vm->_stderr = [](VM* vm, const Str& s){ platform_log_error(s); };

add_module_imgui(vm);
add_module_box2d(vm);

PyObject* _ct = vm->new_module("_ct");
ct::patch_module_ct(vm, _ct);
Expand Down

0 comments on commit 985a4f4

Please sign in to comment.