New binding method that allows c++ to call graphics functions as well as python. Testing setting vm vars like mouseX and mouseY

This commit is contained in:
Bobby Lucero 2024-04-22 13:07:23 -04:00
parent 65ffe5d39b
commit cb6369ad30
6 changed files with 87 additions and 24 deletions

@ -1 +1 @@
Subproject commit c6652736f323f96a2f9634055aef50dd9e2fd24f Subproject commit 8361c7d4a181bf94931b0f1841032d58cdeee481

View File

@ -70,12 +70,34 @@ balls = []
for i in range(2): for i in range(2):
balls.append(ball()) balls.append(ball())
counter = 0
def update(): def update():
global linecol global linecol
global counter
counter += 1
#clear(0) #clear(0)
draw_line(balls[0].x, balls[0].y, balls[1].x, balls[1].y, linecol) draw_line(balls[0].x, balls[0].y, balls[1].x, balls[1].y, linecol)
for ball in balls: for ball in balls:
ball.draw() ball.draw()
for i in range(64):
for j in range(20):
if(i == 0 or i == 63 or j == 0 or j == 20):
pixel(i * 4, j, i)
pixel(i * 4 + 1, j, i)
pixel(i * 4 + 2, j, i)
pixel(i * 4 + 3, j, i)
else:
pixel(i * 4, j, 0)
pixel(i * 4 + 1, j, 0)
pixel(i * 4 + 2, j, 0)
pixel(i * 4 + 3, j, 0)
t = "Hello from python FPS:" + str(fps()) + " X:" + str(mouseX) + " Y:" + str(mouseY)
for i in range(len(t)):
text(t[i], 4 + (i * 7), 4, 20 + i)
circle(mouseX, mouseY, 10, counter % 64)

View File

@ -33,6 +33,9 @@ Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : screen
void Graphics::draw(pkpy::VM* vm) { void Graphics::draw(pkpy::VM* vm) {
vm->builtins->attr().set("mouseX", pkpy::py_var(vm, mouseX()));
vm->builtins->attr().set("mouseY", pkpy::py_var(vm, mouseY()));
windowShouldClose = WindowShouldClose(); windowShouldClose = WindowShouldClose();
if (IsWindowResized()) { if (IsWindowResized()) {
@ -49,6 +52,9 @@ void Graphics::draw(pkpy::VM* vm) {
} catch(pkpy::Exception e){ } catch(pkpy::Exception e){
std::cout << e.summary() << std::endl; std::cout << e.summary() << std::endl;
} }
Circle(150,100,50,1);
Text("Hello from C++", 120, 95, 9);
////////// //////////
EndTextureMode(); EndTextureMode();
@ -111,41 +117,70 @@ void Graphics::toggleFullScreen() {
windowHeight = startupScreenHeight; windowHeight = startupScreenHeight;
} else { } else {
int monitor = GetCurrentMonitor(); int monitor = GetCurrentMonitor();
SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor));
windowWidth = GetMonitorWidth(monitor); windowWidth = GetMonitorWidth(monitor);
windowHeight = GetMonitorHeight(monitor); windowHeight = GetMonitorHeight(monitor);
SetWindowSize(windowWidth, windowHeight);
ToggleFullscreen(); ToggleFullscreen();
} }
calculateScreenPositionInWindow(); calculateScreenPositionInWindow();
} }
void Graphics::bindMethods(pkpy::VM *vm) { void Graphics::bindMethods(pkpy::VM *vm) {
vm->bind(vm->builtins, "clear(color: int)", reinterpret_cast<pkpy::NativeFuncC>(Clear)); // vm->bind(vm->builtins, "clear(color: int)", reinterpret_cast<pkpy::NativeFuncC>(Clear));
vm->bind(vm->builtins, "pixel(x: int, y: int, color: int)", reinterpret_cast<pkpy::NativeFuncC>(Pixel)); // vm->bind(vm->builtins, "pixel(x: int, y: int, color: int)", reinterpret_cast<pkpy::NativeFuncC>(Pixel));
vm->bind(vm->builtins, "circle(x: int, y: int, radius: float, color: int)", reinterpret_cast<pkpy::NativeFuncC>(Circle)); // vm->bind(vm->builtins, "circle(x: int, y: int, radius: float, color: int)", reinterpret_cast<pkpy::NativeFuncC>(Circle));
vm->bind(vm->builtins, "clear(color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){{
int index = pkpy::py_cast<int>(vm, args[0]);
Clear(index);
return vm->None;
}});
vm->bind(vm->builtins, "pixel(x: int, y: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){{
float x = pkpy::py_cast<float>(vm, args[0]);
float y = pkpy::py_cast<float>(vm, args[1]);
float paletteIndex = pkpy::py_cast<float>(vm, args[2]);
this->Pixel(x, y, paletteIndex);
return vm->None;
}});
vm->bind(vm->builtins, "circle(x: int, y: int, radius: float, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){{
float x = pkpy::py_cast<float>(vm, args[0]);
float y = pkpy::py_cast<float>(vm, args[1]);
float radius = pkpy::py_cast<float>(vm, args[2]);
float paletteIndex = pkpy::py_cast<float>(vm, args[3]);
this->Circle(x, y, radius, paletteIndex);
return vm->None;
}});
vm->bind(vm->builtins, "text(t: string, x: int, y: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){{
std::string s = pkpy::py_cast<pkpy::Str&>(vm, args[0]).str();
float x = pkpy::py_cast<float>(vm, args[1]);
float y = pkpy::py_cast<float>(vm, args[2]);
float paletteIndex = pkpy::py_cast<float>(vm, args[3]);
this->Text(s, x, y, paletteIndex);
return vm->None;
}});
} }
void Graphics::Clear(pkpy::VM* vm, pkpy::ArgsView args) { void Graphics::Clear(int paletteIndex) {
int paletteIndex = pkpy::py_cast<int>(vm, args[0]);
if(paletteIndex < 0 || paletteIndex >= palette.size()) paletteIndex = 0; if(paletteIndex < 0 || paletteIndex >= palette.size()) paletteIndex = 0;
ClearBackground(palette[paletteIndex]); ClearBackground(palette[paletteIndex]);
} }
void Graphics::Pixel(pkpy::VM* vm, pkpy::ArgsView args) { void Graphics::Pixel(int x, int y, int paletteIndex) {
int x = pkpy::py_cast<int>(vm, args[0]);
int y = pkpy::py_cast<int>(vm, args[1]);
int paletteIndex = pkpy::py_cast<int>(vm, args[2]);
DrawPixel(x, y, palette[paletteIndex]); DrawPixel(x, y, palette[paletteIndex]);
} }
void Graphics::Circle(pkpy::VM* vm, pkpy::ArgsView args) { void Graphics::Circle(int x, int y, int radius, int paletteIndex) {
float x = pkpy::py_cast<float>(vm, args[0]);
float y = pkpy::py_cast<float>(vm, args[1]);
float radius = pkpy::py_cast<float>(vm, args[2]);
int paletteIndex = pkpy::py_cast<int>(vm, args[3]);
DrawCircle(x, y, radius, palette[paletteIndex]); DrawCircle(x, y, radius, palette[paletteIndex]);
} }
void Graphics::Text(std::string s, int x, int y, int paletteIndex) {
DrawText(s.c_str(), x, y, 5, palette[paletteIndex]);
}
void Graphics::searchForDrawFunc(pkpy::VM* vm) { void Graphics::searchForDrawFunc(pkpy::VM* vm) {
updateFunction = vm->eval("update"); updateFunction = vm->eval("update");
if(updateFunction == nullptr){ if(updateFunction == nullptr){

View File

@ -54,9 +54,10 @@ public:
void searchForDrawFunc(pkpy::VM* vm); void searchForDrawFunc(pkpy::VM* vm);
static void Clear(pkpy::VM* vm, pkpy::ArgsView args); void Clear(int paletteIndex);
static void Pixel(pkpy::VM* vm, pkpy::ArgsView args); void Pixel(int x, int y, int paletteIndex);
static void Circle(pkpy::VM* vm, pkpy::ArgsView args); void Circle(int x, int y, int radius, int paletteIndex);
void Text(std::string s, int x, int y, int paletteIndex);
}; };

View File

@ -29,7 +29,6 @@ Pycron::Pycron() {
graphics->loadPalette("../resources/palette2.hex"); graphics->loadPalette("../resources/palette2.hex");
vm = new pkpy::VM(); vm = new pkpy::VM();
bindMethods(vm); bindMethods(vm);
graphics->bindMethods(vm); graphics->bindMethods(vm);
@ -37,6 +36,7 @@ Pycron::Pycron() {
std::string python = loadFileToString("../python/main.py"); std::string python = loadFileToString("../python/main.py");
graphics->beginDraw(); graphics->beginDraw();
graphics->Clear(0);
try { try {
pkpy::CodeObject_ code = vm->compile(python, "main.py", pkpy::EXEC_MODE, false); pkpy::CodeObject_ code = vm->compile(python, "main.py", pkpy::EXEC_MODE, false);
vm->_exec(code, vm->_main); vm->_exec(code, vm->_main);
@ -66,9 +66,10 @@ void Pycron::StartGameLoop() {
} }
void Pycron::bindMethods(pkpy::VM *vm) { void Pycron::bindMethods(pkpy::VM *vm) {
vm->bind(vm->builtins, "rnd(min: int, max: int) -> int", reinterpret_cast<pkpy::NativeFuncC>(getRandomNumber)); vm->bind(vm->builtins, "rnd(min: int, max: int) -> int", getRandomNumber);
vm->bind(vm->builtins, "sin(num: float) -> float", reinterpret_cast<pkpy::NativeFuncC>(getSin)); vm->bind(vm->builtins, "sin(num: float) -> float", getSin);
vm->bind(vm->builtins, "cos(num: float) -> float", reinterpret_cast<pkpy::NativeFuncC>(getCos)); vm->bind(vm->builtins, "cos(num: float) -> float", getCos);
vm->bind(vm->builtins, "fps() -> int", getFPS);
} }
@ -96,3 +97,7 @@ pkpy::PyObject* Pycron::getCos(pkpy::VM* vm, pkpy::ArgsView args) {
return pkpy::py_var(vm, cos(num)); return pkpy::py_var(vm, cos(num));
} }
pkpy::PyObject* Pycron::getFPS(pkpy::VM* vm, pkpy::ArgsView args) {
return pkpy::py_var(vm, GetFPS());
}

View File

@ -22,6 +22,6 @@ public:
static pkpy::PyObject* getRandomNumber(pkpy::VM* vm, pkpy::ArgsView args); static pkpy::PyObject* getRandomNumber(pkpy::VM* vm, pkpy::ArgsView args);
static pkpy::PyObject* getSin(pkpy::VM* vm, pkpy::ArgsView args); static pkpy::PyObject* getSin(pkpy::VM* vm, pkpy::ArgsView args);
static pkpy::PyObject* getCos(pkpy::VM* vm, pkpy::ArgsView args); static pkpy::PyObject* getCos(pkpy::VM* vm, pkpy::ArgsView args);
static pkpy::PyObject* getFPS(pkpy::VM* vm, pkpy::ArgsView args);
}; };