Errors output to screen, python text function accepts all types now

This commit is contained in:
Bobby Lucero 2024-04-26 16:21:17 -04:00
parent 12ed3d3cb1
commit 5845ad2e8d
8 changed files with 101 additions and 62 deletions

View File

@ -229,7 +229,7 @@ public:
PyObject* call_method(PyObject* self, PyObject* callable, Args&&... args){ PyObject* call_method(PyObject* self, PyObject* callable, Args&&... args){
PUSH(callable); PUSH(callable);
PUSH(self); PUSH(self);
_push_varargs(args...); _push_varargs(static_cast<PyObject *>(args)...);
return vectorcall(sizeof...(args)); return vectorcall(sizeof...(args));
} }

View File

@ -1,4 +1,4 @@
class ball: class Ball:
def __init__(self): def __init__(self):
self.x = rnd(10, 100) self.x = rnd(10, 100)
self.y = rnd(10, 100) self.y = rnd(10, 100)
@ -32,5 +32,3 @@ class ball:
circle(self.x, self.y, 3, self.color) circle(self.x, self.y, 3, self.color)

View File

@ -1,59 +1,82 @@
import ball # import ball
linecol = 15 # linecol = 15
def draw_line(x0, y0, x1, y1, c): # def draw_line(x0, y0, x1, y1, c):
dx = abs(x1 - x0) # dx = abs(x1 - x0)
dy = abs(y1 - y0) # dy = abs(y1 - y0)
sx = 1 if x0 < x1 else -1 # sx = 1 if x0 < x1 else -1
sy = 1 if y0 < y1 else -1 # sy = 1 if y0 < y1 else -1
err = dx - dy # err = dx - dy
while x0 != x1 or y0 != y1: # while x0 != x1 or y0 != y1:
pixel(x0, y0, c) # pixel(x0, y0, c)
e2 = 2 * err # e2 = 2 * err
if e2 > -dy: # if e2 > -dy:
err -= dy # err -= dy
x0 += sx # x0 += sx
if e2 < dx: # if e2 < dx:
err += dx # err += dx
y0 += sy # y0 += sy
balls = [] # balls = []
for i in range(2): # for i in range(2):
balls.append(ball.ball()) # 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():
cls()
global x
global counter
counter += 0.008
x += 0.5
if(counter > 1):
counter = 0 counter = 0
def update(): for i in range(len(funcs)):
global linecol pos = funcs[i](counter)
global counter circle(100 + (pos * 200), 10 + (14 * i), 5, i + 35)
counter += 1
#clear(0)
draw_line(balls[0].x, balls[0].y, balls[1].x, balls[1].y, linecol)
for ball in balls: text("The FPS: " + str(fps()), 2, 2, 9)
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)
clear(0) def cls():
text("test", 2,2, 32) for i in range(width):
for j in range(height):
pixel(i , j, int(i + j + x) % 64)

View File

@ -148,14 +148,17 @@ void Graphics::bindMethods(pkpy::VM *vm) {
return vm->None; return vm->None;
}}); }});
vm->bind(vm->builtins, "text(t: string, x: int, y: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){{ 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(); pkpy::PyObject* func_str = vm->builtins->attr("str");
pkpy::PyObject* result = vm->call(func_str, args[0]);
std::string s = pkpy::py_cast<pkpy::Str&>(vm, result).str();
float x = pkpy::py_cast<float>(vm, args[1]); float x = pkpy::py_cast<float>(vm, args[1]);
float y = pkpy::py_cast<float>(vm, args[2]); float y = pkpy::py_cast<float>(vm, args[2]);
float paletteIndex = pkpy::py_cast<float>(vm, args[3]); float paletteIndex = pkpy::py_cast<float>(vm, args[3]);
this->Text(s, x, y, paletteIndex); this->Text(s, x, y, paletteIndex);
return vm->None; return vm->None;
}}); });
} }
void Graphics::Clear(int paletteIndex) { void Graphics::Clear(int paletteIndex) {
@ -183,9 +186,11 @@ void Graphics::endDraw() {
EndTextureMode(); 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("mouseX", pkpy::py_var(vm, mouseX()));
vm->builtins->attr().set("mouseY", pkpy::py_var(vm, mouseY())); 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));
} }

View File

@ -46,7 +46,7 @@ public:
void beginDraw(); void beginDraw();
void endDraw(); void endDraw();
void updateVMMouse(pkpy::VM* vm); void updateVMVars(pkpy::VM* vm);
void loadPalette(std::string path); void loadPalette(std::string path);
int mouseX(); int mouseX();

View File

@ -31,7 +31,7 @@ Pycron::Pycron() {
m_graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale}; m_graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale};
m_graphics->loadPalette("../resources/palette2.hex"); m_graphics->loadPalette("../resources/palette2.hex");
m_graphics->bindMethods(m_vm); m_graphics->bindMethods(m_vm);
m_graphics->updateVMMouse(m_vm); m_graphics->updateVMVars(m_vm);
m_stateManager = new StateManager(this); m_stateManager = new StateManager(this);
@ -54,7 +54,7 @@ void Pycron::StartGameLoop() {
if (IsKeyPressed(KEY_F)) { if (IsKeyPressed(KEY_F)) {
m_graphics->toggleFullScreen(); m_graphics->toggleFullScreen();
} }
m_graphics->updateVMMouse(m_vm); m_graphics->updateVMVars(m_vm);
m_graphics->draw(this->m_stateManager); m_graphics->draw(this->m_stateManager);
} }
} }

View File

@ -20,9 +20,12 @@ void StateManager::RequestStateChange(StateManager::StateType state) {
if(m_currentState){ if(m_currentState){
// Game state needs ability to draw during code loading // Game state needs ability to draw during code loading
if(state == GAME){ if(m_currentState == m_gameState){
m_pycron->m_graphics->beginDraw(); m_pycron->m_graphics->beginDraw();
m_currentState->OnEnter(); 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(); m_pycron->m_graphics->endDraw();
}else{ }else{
m_currentState->OnEnter(); m_currentState->OnEnter();
@ -32,9 +35,19 @@ void StateManager::RequestStateChange(StateManager::StateType state) {
void StateManager::Draw(Graphics *graphics) { void StateManager::Draw(Graphics *graphics) {
if(m_currentState){ if(m_currentState){
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); m_currentState->Draw(graphics);
} }
} }
else{
m_currentState->Draw(graphics);
}
}
}
StateManager::~StateManager() { StateManager::~StateManager() {
m_currentState = nullptr; m_currentState = nullptr;

View File

@ -42,8 +42,8 @@ void GameState::PreProcessScripts() {
loadPythonModules(pythonSources); loadPythonModules(pythonSources);
std::string main = pythonSources[MAIN_FILE]; std::string main = pythonSources[MAIN_FILE];
pkpy::CodeObject_ code = m_vm->compile(main, MAIN_FILE, pkpy::EXEC_MODE, false);
try{ try{
pkpy::CodeObject_ code = m_vm->compile(main, MAIN_FILE, pkpy::EXEC_MODE, false);
m_vm->_exec(code, m_vm->_main); m_vm->_exec(code, m_vm->_main);
m_updateFunction = m_vm->eval("update"); m_updateFunction = m_vm->eval("update");
// if(m_updateFunction == nullptr){ // if(m_updateFunction == nullptr){