various changes, read the code. too lazy to write description

This commit is contained in:
Bobby Lucero 2024-10-10 23:26:54 -04:00
parent 9f04ac67ba
commit 028348691a
12 changed files with 167 additions and 38 deletions

38
python/blobs.py Normal file
View File

@ -0,0 +1,38 @@
from scene import Scene
# Palette class (inherits from scene)
class Blobs(Scene):
def __init__(self):
super().__init__()
self.title = "Squares"
self.t = 0
self.dir = True
def update(self):
pass
def draw(self):
clear(0)
for i in range(width):
for j in range(height):
c = cos(i / 10) + (sin(j / 10) + 2)
#c2 = cos(i / 5) + sin(j / 5)
pixel(i, j, 2 * c + self.t)
self.t += (1/5) * (1 if self.dir else - 1)
if(self.t > 64):
self.t = 64
self.dir = not self.dir
if(self.t < 0):
self.t = 0
self.dir = not self.dir

View File

@ -1,6 +1,9 @@
from scene import Scene from scene import Scene
from particles import Particles from particles import Particles
from triangles import Triangles from triangles import Triangles
from squares import Squares
from blobs import Blobs
from mechanical import Mechanical
# Palette class (inherits from scene) # Palette class (inherits from scene)
class Palette(Scene): class Palette(Scene):
@ -17,7 +20,7 @@ class Palette(Scene):
for i in range(8): for i in range(8):
for j in range(8): for j in range(8):
rectangle(10 + (i * 20), 10 + (j * 20), 18, 18, (j * 8 + i)) rect(10 + (i * 20), 10 + (j * 20), 18, 18, (j * 8 + i))
text(str(j * 8 + i), 11 + (i * 20), 10 + (j * 20), 63) text(str(j * 8 + i), 11 + (i * 20), 10 + (j * 20), 63)
class RGBTest(Scene): class RGBTest(Scene):
@ -26,7 +29,7 @@ class RGBTest(Scene):
self.title = "RGB To Palette" self.title = "RGB To Palette"
scenes = [Particles(), Triangles(), Palette()] scenes = [Particles(), Triangles(), Palette(), Squares(), Blobs()]
current_scene = 0 current_scene = 0
@ -50,10 +53,3 @@ def update():
if(not scenes[current_scene].paused): if(not scenes[current_scene].paused):
scenes[current_scene].update() scenes[current_scene].update()
scenes[current_scene].draw() scenes[current_scene].draw()

29
python/mechanical.py Normal file
View File

@ -0,0 +1,29 @@
from scene import Scene
s = 128
s2 = 120-12
l = 15
a = 80
t = 0
# Palette class (inherits from scene)
class Mechanical(Scene):
def __init__(self):
super().__init__()
self.title = "Squares"
def update(self):
pass
def draw(self):
clear(0)
rect(120 - s/2, 68 - s/2, s, s, 14)
triangle(120-s/2, 68 - s/2, 120-s/2, 68+s/2,
120 + s/2, 68+s/2, 15)
line(119 + s / 2, 68 + s / 2, 119 + s2 / 2, 68 - s2/2, 15)

View File

@ -1537,4 +1537,3 @@ class Particles(Scene):
# triangle(mouseX, mouseY, x2, y2, x3, y3, 9) # triangle(mouseX, mouseY, x2, y2, x3, y3, 9)
# lineTri(mouseX, mouseY, x2, y2, x3, y3, 8) # lineTri(mouseX, mouseY, x2, y2, x3, y3, 8)

25
python/squares.py Normal file
View File

@ -0,0 +1,25 @@
from scene import Scene
# Palette class (inherits from scene)
class Squares(Scene):
def __init__(self):
super().__init__()
self.title = "Squares"
def update(self):
pass
def draw(self):
clear(0)
for i in range(50):
for j in range(27):
rect(8 * i, 8 * j, 8, 8, (i + j) % 64)
rectBorder(8 * i, 8 * j, 8, 8, (i + j + 1) % 64)

View File

@ -77,7 +77,7 @@ class Triangles(Scene):
y = rnd(0, height) y = rnd(0, height)
c = 0 c = 0
if(rnd(0,9) < 5): if(rnd(0,9) < 5):
c = get_pixel(x -3 ,y-3) c = getPixel(x -3 ,y-3)
else: else:
c = 0 c = 0

View File

@ -64,7 +64,23 @@ void Graphics::draw(StateManager* stateManager) {
void Graphics::copyBufferToGPU() { void Graphics::copyBufferToGPU() {
Color* pixel_data = LoadImageColors(m_virtualScreenImageBuffer); Color* pixel_data = LoadImageColors(m_virtualScreenImageBuffer);
for (int i = 0; i < m_screenWidth * m_screenHeight; ++i) { for (int i = 0; i < m_screenWidth * m_screenHeight; ++i) {
pixel_data[i] = GetColor(m_paletteByID[m_virtualScreenColorBuffer[i]]); uint32_t rgb = m_paletteByID[m_virtualScreenColorBuffer[i]];
uint8_t r = rgb >> 16 & 0xFF;
uint8_t g = rgb >> 8 & 0xFF;
uint8_t b = rgb & 0xFF;
double dR = (double)r * 0.2126;
double dG = (double)g * 0.7152;
double dB = (double)b * 0.0722;
int gray = (int)(dR + dG + dB);
double naiveGray = (r + g + b) / 3.0;
//gray = (int)naiveGray;
unsigned int out = 255;
out |= gray << 8;
out |= gray << 16;
out |= gray << 24;
pixel_data[i] = GetColor(rgb);
} }
UpdateTexture(m_virtualScreen.texture, pixel_data); UpdateTexture(m_virtualScreen.texture, pixel_data);
UnloadImageColors(pixel_data); UnloadImageColors(pixel_data);
@ -181,7 +197,7 @@ void Graphics::bindMethods(pkpy::VM *vm) {
return vm->None; return vm->None;
}); });
vm->bind(vm->builtins, "get_pixel(x: int, y: int) -> int", [this](pkpy::VM* vm, pkpy::ArgsView args){ vm->bind(vm->builtins, "getPixel(x: int, y: int) -> int", [this](pkpy::VM* vm, pkpy::ArgsView args){
float x = pkpy::py_cast<float>(vm, args[0]); float x = pkpy::py_cast<float>(vm, args[0]);
float y = pkpy::py_cast<float>(vm, args[1]); float y = pkpy::py_cast<float>(vm, args[1]);
@ -197,7 +213,7 @@ void Graphics::bindMethods(pkpy::VM *vm) {
return vm->None; return vm->None;
}); });
vm->bind(vm->builtins, "rectangle(x: int, y: int, width: int, height: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){ vm->bind(vm->builtins, "rect(x: int, y: int, width: int, height: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){
float x = pkpy::py_cast<float>(vm, args[0]); float x = pkpy::py_cast<float>(vm, args[0]);
float y = pkpy::py_cast<float>(vm, args[1]); float y = pkpy::py_cast<float>(vm, args[1]);
float width = pkpy::py_cast<float>(vm, args[2]); float width = pkpy::py_cast<float>(vm, args[2]);
@ -207,6 +223,16 @@ void Graphics::bindMethods(pkpy::VM *vm) {
return vm->None; return vm->None;
}); });
vm->bind(vm->builtins, "rectBorder(x: int, y: int, width: int, height: 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 width = pkpy::py_cast<float>(vm, args[2]);
float height = pkpy::py_cast<float>(vm, args[3]);
float paletteIndex = pkpy::py_cast<float>(vm, args[4]);
this->RectBorder(x, y, width, height, paletteIndex);
return vm->None;
});
vm->bind(vm->builtins, "triangle(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){ vm->bind(vm->builtins, "triangle(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int, color: int)", [this](pkpy::VM* vm, pkpy::ArgsView args){
float x1 = pkpy::py_cast<float>(vm, args[0]); float x1 = pkpy::py_cast<float>(vm, args[0]);
float y1 = pkpy::py_cast<float>(vm, args[1]); float y1 = pkpy::py_cast<float>(vm, args[1]);
@ -388,15 +414,25 @@ void Graphics::RectBorder(int x, int y, int width, int height, int paletteIndex)
} }
void Graphics::Text(const std::string& s, int x, int y, int paletteIndex) { void Graphics::Text(const std::string& s, int x, int y, int paletteIndex) {
int currentX = 0;
int currentY = y;
for (int i = 0; i < s.size(); ++i) { for (int i = 0; i < s.size(); ++i) {
char c = s[i]; char c = s[i];
std::string bitData = m_currentFont->GetCharData((int)c); // Handle new lines
if(c != '\n') {
std::string bitData = m_currentFont->GetCharData((int) c);
for (int j = 0; j < bitData.size(); ++j) { for (int j = 0; j < bitData.size(); ++j) {
if(bitData[j] == '1') if (bitData[j] == '1'){
Pixel(x + (j % m_currentFont->GetWidth()) + ((m_currentFont->GetWidth() + 1) * i), y + (j / m_currentFont->GetWidth()), paletteIndex); Pixel(x + (j % m_currentFont->GetWidth()) + ((m_currentFont->GetWidth() + 1) * currentX),currentY + (j / m_currentFont->GetWidth()), paletteIndex);
}
} }
currentX++;
}else{
currentX = 0;
currentY += m_currentFont->GetHeight() + 1;
}
} }
} }

View File

@ -71,6 +71,7 @@ void Pycron::bindMethods() {
m_vm->bind(m_vm->builtins, "rnd(min: float, max: float) -> int", getRandomNumber); m_vm->bind(m_vm->builtins, "rnd(min: float, max: float) -> int", getRandomNumber);
m_vm->bind(m_vm->builtins, "sin(num: float) -> float", getSin); m_vm->bind(m_vm->builtins, "sin(num: float) -> float", getSin);
m_vm->bind(m_vm->builtins, "cos(num: float) -> float", getCos); m_vm->bind(m_vm->builtins, "cos(num: float) -> float", getCos);
m_vm->bind(m_vm->builtins, "tan(num: float) -> float", getTan);
m_vm->bind(m_vm->builtins, "fps() -> int", getFPS); m_vm->bind(m_vm->builtins, "fps() -> int", getFPS);
m_vm->bind(m_vm->builtins, "keyp(keycode: int) -> bool", getKeyPressed); m_vm->bind(m_vm->builtins, "keyp(keycode: int) -> bool", getKeyPressed);
m_vm->bind(m_vm->builtins, "key(keycode: int) -> bool", getKeyDown); m_vm->bind(m_vm->builtins, "key(keycode: int) -> bool", getKeyDown);
@ -137,6 +138,11 @@ 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::getTan(pkpy::VM* vm, pkpy::ArgsView args) {
auto num = pkpy::py_cast<double>(vm, args[0]);
return pkpy::py_var(vm, tan(num));
}
pkpy::PyObject* Pycron::getFPS(pkpy::VM* vm, pkpy::ArgsView args) { pkpy::PyObject* Pycron::getFPS(pkpy::VM* vm, pkpy::ArgsView args) {
return pkpy::py_var(vm, GetFPS()); return pkpy::py_var(vm, GetFPS());
} }

View File

@ -36,6 +36,7 @@ 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* getTan(pkpy::VM* vm, pkpy::ArgsView args);
static pkpy::PyObject* getFPS(pkpy::VM* vm, pkpy::ArgsView args); static pkpy::PyObject* getFPS(pkpy::VM* vm, pkpy::ArgsView args);
static pkpy::PyObject* getKeyPressed(pkpy::VM* vm, pkpy::ArgsView args); static pkpy::PyObject* getKeyPressed(pkpy::VM* vm, pkpy::ArgsView args);
static pkpy::PyObject* getKeyDown(pkpy::VM* vm, pkpy::ArgsView args); static pkpy::PyObject* getKeyDown(pkpy::VM* vm, pkpy::ArgsView args);

View File

@ -30,13 +30,9 @@ void StateManager::ChangeState(StateManager::StateType state) {
} }
if(m_currentState){ if(m_currentState){
if(m_currentState == m_gameState){
m_currentState->OnEnter();
if(m_gameState->m_errorThrown){
m_pycron->m_graphics->Text(m_gameState->m_previousError, 2, 2, 59);
}
}else{
m_currentState->OnEnter(); m_currentState->OnEnter();
if(m_currentState == m_gameState && m_gameState->m_errorThrown){
m_pycron->m_graphics->Text(m_gameState->m_previousError, 2, 2, 4);
} }
} }
} }
@ -52,13 +48,8 @@ void StateManager::Draw() {
} }
if(m_currentState){ if(m_currentState){
if(m_currentState == m_gameState){ if(m_currentState == m_gameState && m_gameState->m_errorThrown){
if(m_gameState->m_errorThrown){ m_pycron->m_graphics->Text(m_gameState->m_previousError, 2, 2, 4);
m_pycron->m_graphics->Text(m_gameState->m_previousError, 2, 2, 59);
}
else{
m_currentState->Draw();
}
} }
else{ else{
m_currentState->Draw(); m_currentState->Draw();

View File

@ -14,7 +14,7 @@
EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphics(graphics){ EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphics(graphics){
m_pythonTokenizer = new PythonTokenizer(); m_pythonTokenizer = new PythonTokenizer();
std::string randomSource = Pycron::loadFileToString("../python/particles.py"); std::string randomSource = Pycron::loadFileToString("../python/squares.py");
m_editorFrame = m_graphics->loadImage("../resources/EditorFrame.png"); m_editorFrame = m_graphics->loadImage("../resources/EditorFrame.png");
m_LineNumberDetailLeft = m_graphics->loadImage("../resources/LineNumberDetailLeft.png"); m_LineNumberDetailLeft = m_graphics->loadImage("../resources/LineNumberDetailLeft.png");
@ -202,6 +202,9 @@ void EditorState::Draw() {
// Line Numbers // Line Numbers
for (int i = 0; i < m_height; ++i) { for (int i = 0; i < m_height; ++i) {
if(i + m_scrollY >= m_text.size()) return;
int lineNum = i + m_scrollY + 1; int lineNum = i + m_scrollY + 1;
std::string lineNumber = std::to_string(lineNum); std::string lineNumber = std::to_string(lineNum);
int delta = std::max(0, 3 - (int)lineNumber.size()); int delta = std::max(0, 3 - (int)lineNumber.size());
@ -304,6 +307,10 @@ void EditorState::OnKeyPressed(int key) {
m_scrollY = m_text.size() - 1 - m_height; m_scrollY = m_text.size() - 1 - m_height;
} }
if(m_scrollY < 0){
m_scrollY = 0;
}
} }
@ -330,8 +337,10 @@ void EditorState::OnMousePressed(int button) {
x /= m_charWidth; x /= m_charWidth;
y /= m_charHeight; y /= m_charHeight;
m_cursorX = x; m_cursorY = std::min((int)m_text.size() - 1, y);
m_cursorY = y; m_cursorX = std::min((int)m_text[m_cursorY].size(), x);
m_cursorVisible = true; m_cursorVisible = true;
m_cursorBlinkTimer = 0; m_cursorBlinkTimer = 0;

View File

@ -26,6 +26,7 @@ void GameState::Draw() {
} }
void GameState::OnEnter() { void GameState::OnEnter() {
m_graphics->Clear(0);
PreProcessScripts(); PreProcessScripts();
} }
@ -49,9 +50,6 @@ void GameState::PreProcessScripts() {
pkpy::CodeObject_ code = m_vm->compile(main, MAIN_FILE, pkpy::EXEC_MODE, false); 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){
// m_previousError = "";
// }
}catch(pkpy::Exception e){ }catch(pkpy::Exception e){
m_previousError = e.summary(); m_previousError = e.summary();
std::cout << e.summary() << "\n"; std::cout << e.summary() << "\n";
@ -85,6 +83,7 @@ void GameState::loadPythonModules(std::unordered_map<std::string, std::string> &
for(const auto& pair : fileContents){ for(const auto& pair : fileContents){
try{ try{
if(pair.first != MAIN_FILE){ if(pair.first != MAIN_FILE){
// parse out file name as module ex: test.py is test
size_t pos = pair.first.find_last_of("."); size_t pos = pair.first.find_last_of(".");
if(pos == std::string::npos || pos == 0){ if(pos == std::string::npos || pos == 0){
throw pkpy::Exception("Invalid file name"); throw pkpy::Exception("Invalid file name");