From 5845ad2e8dba8cabac9585cd29d290b0e0fa3b34 Mon Sep 17 00:00:00 2001 From: Bobby Lucero Date: Fri, 26 Apr 2024 16:21:17 -0400 Subject: [PATCH] Errors output to screen, python text function accepts all types now --- dependencies/pocketpy/include/pocketpy/vm.h | 2 +- python/ball.py | 4 +- python/main.py | 119 ++++++++++++-------- src/Graphics/Graphics.cpp | 13 ++- src/Graphics/Graphics.h | 2 +- src/Pycron.cpp | 4 +- src/StateManager.cpp | 17 ++- src/States/GameState.cpp | 2 +- 8 files changed, 101 insertions(+), 62 deletions(-) diff --git a/dependencies/pocketpy/include/pocketpy/vm.h b/dependencies/pocketpy/include/pocketpy/vm.h index ab04baa..f952367 100644 --- a/dependencies/pocketpy/include/pocketpy/vm.h +++ b/dependencies/pocketpy/include/pocketpy/vm.h @@ -229,7 +229,7 @@ public: PyObject* call_method(PyObject* self, PyObject* callable, Args&&... args){ PUSH(callable); PUSH(self); - _push_varargs(args...); + _push_varargs(static_cast(args)...); return vectorcall(sizeof...(args)); } diff --git a/python/ball.py b/python/ball.py index 8582f71..62bd531 100644 --- a/python/ball.py +++ b/python/ball.py @@ -1,4 +1,4 @@ -class ball: +class Ball: def __init__(self): self.x = rnd(10, 100) self.y = rnd(10, 100) @@ -32,5 +32,3 @@ class ball: circle(self.x, self.y, 3, self.color) - - diff --git a/python/main.py b/python/main.py index 5017374..55b0656 100644 --- a/python/main.py +++ b/python/main.py @@ -1,59 +1,82 @@ -import ball +# import ball -linecol = 15 -def draw_line(x0, y0, x1, y1, c): - dx = abs(x1 - x0) - dy = abs(y1 - y0) - sx = 1 if x0 < x1 else -1 - sy = 1 if y0 < y1 else -1 - err = dx - dy +# linecol = 15 +# def draw_line(x0, y0, x1, y1, c): +# dx = abs(x1 - x0) +# dy = abs(y1 - y0) +# sx = 1 if x0 < x1 else -1 +# sy = 1 if y0 < y1 else -1 +# err = dx - dy - while x0 != x1 or y0 != y1: - pixel(x0, y0, c) - e2 = 2 * err - if e2 > -dy: - err -= dy - x0 += sx - if e2 < dx: - err += dx - y0 += sy +# while x0 != x1 or y0 != y1: +# pixel(x0, y0, c) +# e2 = 2 * err +# if e2 > -dy: +# err -= dy +# x0 += sx +# if e2 < dx: +# err += dx +# y0 += sy -balls = [] +# balls = [] -for i in range(2): - balls.append(ball.ball()) +# for i in range(2): +# balls.append(ball.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): +# pixel(i * 4, j, i) +# pixel(i * 4 + 1, j, i) +# pixel(i * 4 + 2, j, i) +# pixel(i * 4 + 3, j, i) + +# 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) + + +# clear(0) +# text("test", 2,2, 32) +import easing +import time +funcs = [easing.InSine, easing.OutSine, easing.InOutSine, easing.InQuad, easing.OutQuad, easing.InOutQuad, easing.InBounce, easing.OutBounce, easing.InOutBounce, easing.InElastic, easing.OutElastic, easing.InOutElastic] + +test = {"1" : 0, "2" : 4} counter = 0 - +x = 0 def update(): - global linecol + cls() + global x global counter - counter += 1 - #clear(0) - draw_line(balls[0].x, balls[0].y, balls[1].x, balls[1].y, linecol) + counter += 0.008 + x += 0.5 + if(counter > 1): + counter = 0 + + for i in range(len(funcs)): + pos = funcs[i](counter) + circle(100 + (pos * 200), 10 + (14 * i), 5, i + 35) - for ball in balls: - ball.draw() + text("The FPS: " + str(fps()), 2, 2, 9) + - 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) - - -clear(0) -text("test", 2,2, 32) \ No newline at end of file +def cls(): + for i in range(width): + for j in range(height): + pixel(i , j, int(i + j + x) % 64) + \ No newline at end of file diff --git a/src/Graphics/Graphics.cpp b/src/Graphics/Graphics.cpp index b70e872..a34eb7c 100644 --- a/src/Graphics/Graphics.cpp +++ b/src/Graphics/Graphics.cpp @@ -148,14 +148,17 @@ void Graphics::bindMethods(pkpy::VM *vm) { 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(); + vm->bind(vm->builtins, "text(t: string, x: int, y: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){ + pkpy::PyObject* func_str = vm->builtins->attr("str"); + pkpy::PyObject* result = vm->call(func_str, args[0]); + std::string s = pkpy::py_cast(vm, result).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(int paletteIndex) { @@ -183,9 +186,11 @@ void Graphics::endDraw() { EndTextureMode(); } -void Graphics::updateVMMouse(pkpy::VM* vm) { +void Graphics::updateVMVars(pkpy::VM* vm) { vm->builtins->attr().set("mouseX", pkpy::py_var(vm, mouseX())); vm->builtins->attr().set("mouseY", pkpy::py_var(vm, mouseY())); + vm->builtins->attr().set("width", pkpy::py_var(vm, m_screenWidth)); + vm->builtins->attr().set("height", pkpy::py_var(vm, m_screenHeight)); } diff --git a/src/Graphics/Graphics.h b/src/Graphics/Graphics.h index fdf7553..4af319e 100644 --- a/src/Graphics/Graphics.h +++ b/src/Graphics/Graphics.h @@ -46,7 +46,7 @@ public: void beginDraw(); void endDraw(); - void updateVMMouse(pkpy::VM* vm); + void updateVMVars(pkpy::VM* vm); void loadPalette(std::string path); int mouseX(); diff --git a/src/Pycron.cpp b/src/Pycron.cpp index d006034..3291bba 100644 --- a/src/Pycron.cpp +++ b/src/Pycron.cpp @@ -31,7 +31,7 @@ Pycron::Pycron() { m_graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale}; m_graphics->loadPalette("../resources/palette2.hex"); m_graphics->bindMethods(m_vm); - m_graphics->updateVMMouse(m_vm); + m_graphics->updateVMVars(m_vm); m_stateManager = new StateManager(this); @@ -54,7 +54,7 @@ void Pycron::StartGameLoop() { if (IsKeyPressed(KEY_F)) { m_graphics->toggleFullScreen(); } - m_graphics->updateVMMouse(m_vm); + m_graphics->updateVMVars(m_vm); m_graphics->draw(this->m_stateManager); } } diff --git a/src/StateManager.cpp b/src/StateManager.cpp index 7ced653..98ba16a 100644 --- a/src/StateManager.cpp +++ b/src/StateManager.cpp @@ -20,9 +20,12 @@ void StateManager::RequestStateChange(StateManager::StateType state) { if(m_currentState){ // Game state needs ability to draw during code loading - if(state == GAME){ + if(m_currentState == m_gameState){ m_pycron->m_graphics->beginDraw(); m_currentState->OnEnter(); + if(m_gameState->m_errorThrown){ + m_pycron->m_graphics->Text(m_gameState->m_previousError, 2, 2, 59); + } m_pycron->m_graphics->endDraw(); }else{ m_currentState->OnEnter(); @@ -32,7 +35,17 @@ void StateManager::RequestStateChange(StateManager::StateType state) { void StateManager::Draw(Graphics *graphics) { if(m_currentState){ - m_currentState->Draw(graphics); + if(m_currentState == m_gameState){ + if(m_gameState->m_errorThrown){ + m_pycron->m_graphics->Text(m_gameState->m_previousError, 2, 2, 59); + } + else{ + m_currentState->Draw(graphics); + } + } + else{ + m_currentState->Draw(graphics); + } } } diff --git a/src/States/GameState.cpp b/src/States/GameState.cpp index 266b852..039bf17 100644 --- a/src/States/GameState.cpp +++ b/src/States/GameState.cpp @@ -42,8 +42,8 @@ void GameState::PreProcessScripts() { loadPythonModules(pythonSources); std::string main = pythonSources[MAIN_FILE]; - pkpy::CodeObject_ code = m_vm->compile(main, MAIN_FILE, pkpy::EXEC_MODE, false); try{ + pkpy::CodeObject_ code = m_vm->compile(main, MAIN_FILE, pkpy::EXEC_MODE, false); m_vm->_exec(code, m_vm->_main); m_updateFunction = m_vm->eval("update"); // if(m_updateFunction == nullptr){