From cb6369ad3061ee2fba433cdd02ab7c27597bebe2 Mon Sep 17 00:00:00 2001 From: Bobby Lucero Date: Mon, 22 Apr 2024 13:07:23 -0400 Subject: [PATCH] New binding method that allows c++ to call graphics functions as well as python. Testing setting vm vars like mouseX and mouseY --- dependencies/pocketpy | 2 +- python/main.py | 22 +++++++++++++ src/Graphics/Graphics.cpp | 65 ++++++++++++++++++++++++++++++--------- src/Graphics/Graphics.h | 7 +++-- src/Pycron.cpp | 13 +++++--- src/Pycron.h | 2 +- 6 files changed, 87 insertions(+), 24 deletions(-) diff --git a/dependencies/pocketpy b/dependencies/pocketpy index c665273..8361c7d 160000 --- a/dependencies/pocketpy +++ b/dependencies/pocketpy @@ -1 +1 @@ -Subproject commit c6652736f323f96a2f9634055aef50dd9e2fd24f +Subproject commit 8361c7d4a181bf94931b0f1841032d58cdeee481 diff --git a/python/main.py b/python/main.py index eaa6f90..f8423a4 100644 --- a/python/main.py +++ b/python/main.py @@ -70,12 +70,34 @@ balls = [] for i in range(2): balls.append(ball()) +counter = 0 + def update(): global linecol + global counter + counter += 1 #clear(0) draw_line(balls[0].x, balls[0].y, balls[1].x, balls[1].y, linecol) for ball in balls: 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) diff --git a/src/Graphics/Graphics.cpp b/src/Graphics/Graphics.cpp index 80aff05..6817c87 100644 --- a/src/Graphics/Graphics.cpp +++ b/src/Graphics/Graphics.cpp @@ -33,6 +33,9 @@ Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : screen 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(); if (IsWindowResized()) { @@ -49,6 +52,9 @@ void Graphics::draw(pkpy::VM* vm) { } catch(pkpy::Exception e){ std::cout << e.summary() << std::endl; } + + Circle(150,100,50,1); + Text("Hello from C++", 120, 95, 9); ////////// EndTextureMode(); @@ -111,41 +117,70 @@ void Graphics::toggleFullScreen() { windowHeight = startupScreenHeight; } else { int monitor = GetCurrentMonitor(); - SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor)); windowWidth = GetMonitorWidth(monitor); windowHeight = GetMonitorHeight(monitor); + SetWindowSize(windowWidth, windowHeight); ToggleFullscreen(); + } calculateScreenPositionInWindow(); } void Graphics::bindMethods(pkpy::VM *vm) { - vm->bind(vm->builtins, "clear(color: int)", reinterpret_cast(Clear)); - vm->bind(vm->builtins, "pixel(x: int, y: int, color: int)", reinterpret_cast(Pixel)); - vm->bind(vm->builtins, "circle(x: int, y: int, radius: float, color: int)", reinterpret_cast(Circle)); +// vm->bind(vm->builtins, "clear(color: int)", reinterpret_cast(Clear)); +// vm->bind(vm->builtins, "pixel(x: int, y: int, color: int)", reinterpret_cast(Pixel)); +// vm->bind(vm->builtins, "circle(x: int, y: int, radius: float, color: int)", reinterpret_cast(Circle)); + + vm->bind(vm->builtins, "clear(color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){{ + int index = pkpy::py_cast(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(vm, args[0]); + float y = pkpy::py_cast(vm, args[1]); + float paletteIndex = pkpy::py_cast(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(vm, args[0]); + float y = pkpy::py_cast(vm, args[1]); + float radius = pkpy::py_cast(vm, args[2]); + float paletteIndex = pkpy::py_cast(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(vm, args[0]).str(); + float x = pkpy::py_cast(vm, args[1]); + float y = pkpy::py_cast(vm, args[2]); + float paletteIndex = pkpy::py_cast(vm, args[3]); + this->Text(s, x, y, paletteIndex); + return vm->None; + }}); } -void Graphics::Clear(pkpy::VM* vm, pkpy::ArgsView args) { - int paletteIndex = pkpy::py_cast(vm, args[0]); +void Graphics::Clear(int paletteIndex) { if(paletteIndex < 0 || paletteIndex >= palette.size()) paletteIndex = 0; ClearBackground(palette[paletteIndex]); } -void Graphics::Pixel(pkpy::VM* vm, pkpy::ArgsView args) { - int x = pkpy::py_cast(vm, args[0]); - int y = pkpy::py_cast(vm, args[1]); - int paletteIndex = pkpy::py_cast(vm, args[2]); +void Graphics::Pixel(int x, int y, int paletteIndex) { DrawPixel(x, y, palette[paletteIndex]); } -void Graphics::Circle(pkpy::VM* vm, pkpy::ArgsView args) { - float x = pkpy::py_cast(vm, args[0]); - float y = pkpy::py_cast(vm, args[1]); - float radius = pkpy::py_cast(vm, args[2]); - int paletteIndex = pkpy::py_cast(vm, args[3]); +void Graphics::Circle(int x, int y, int radius, int 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) { updateFunction = vm->eval("update"); if(updateFunction == nullptr){ diff --git a/src/Graphics/Graphics.h b/src/Graphics/Graphics.h index 16b3bdd..82da443 100644 --- a/src/Graphics/Graphics.h +++ b/src/Graphics/Graphics.h @@ -54,9 +54,10 @@ public: void searchForDrawFunc(pkpy::VM* vm); - static void Clear(pkpy::VM* vm, pkpy::ArgsView args); - static void Pixel(pkpy::VM* vm, pkpy::ArgsView args); - static void Circle(pkpy::VM* vm, pkpy::ArgsView args); + void Clear(int paletteIndex); + void Pixel(int x, int y, int paletteIndex); + void Circle(int x, int y, int radius, int paletteIndex); + void Text(std::string s, int x, int y, int paletteIndex); }; diff --git a/src/Pycron.cpp b/src/Pycron.cpp index 0f440d2..fb05f17 100644 --- a/src/Pycron.cpp +++ b/src/Pycron.cpp @@ -29,7 +29,6 @@ Pycron::Pycron() { graphics->loadPalette("../resources/palette2.hex"); vm = new pkpy::VM(); - bindMethods(vm); graphics->bindMethods(vm); @@ -37,6 +36,7 @@ Pycron::Pycron() { std::string python = loadFileToString("../python/main.py"); graphics->beginDraw(); + graphics->Clear(0); try { pkpy::CodeObject_ code = vm->compile(python, "main.py", pkpy::EXEC_MODE, false); vm->_exec(code, vm->_main); @@ -66,9 +66,10 @@ void Pycron::StartGameLoop() { } void Pycron::bindMethods(pkpy::VM *vm) { - vm->bind(vm->builtins, "rnd(min: int, max: int) -> int", reinterpret_cast(getRandomNumber)); - vm->bind(vm->builtins, "sin(num: float) -> float", reinterpret_cast(getSin)); - vm->bind(vm->builtins, "cos(num: float) -> float", reinterpret_cast(getCos)); + vm->bind(vm->builtins, "rnd(min: int, max: int) -> int", getRandomNumber); + vm->bind(vm->builtins, "sin(num: float) -> float", getSin); + 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)); } +pkpy::PyObject* Pycron::getFPS(pkpy::VM* vm, pkpy::ArgsView args) { + return pkpy::py_var(vm, GetFPS()); +} + diff --git a/src/Pycron.h b/src/Pycron.h index 4247132..2740899 100644 --- a/src/Pycron.h +++ b/src/Pycron.h @@ -22,6 +22,6 @@ public: static pkpy::PyObject* getRandomNumber(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* getFPS(pkpy::VM* vm, pkpy::ArgsView args); };