diff --git a/.gitignore b/.gitignore index 1faff06..997a6bb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ cmake-build-debug .vscode cmake-build-release +build diff --git a/CMakeLists.txt b/CMakeLists.txt index 590eb72..dbb827d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,9 @@ file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/s set(PROJECT_INCLUDE "${CMAKE_CURRENT_LIST_DIR}/sources/") # Define PROJECT_INCLUDE to be the path to the include directory of the project -add_executable(UnnamedMicroPythonConsole main.cpp) +add_executable(UnnamedMicroPythonConsole src/main.cpp + src/Pycron.cpp + src/Pycron.h) add_subdirectory(dependencies/pocketpy) # Declaring our executable diff --git a/main.cpp b/src/Pycron.cpp similarity index 57% rename from main.cpp rename to src/Pycron.cpp index facab74..d83fed5 100644 --- a/main.cpp +++ b/src/Pycron.cpp @@ -1,24 +1,55 @@ -#include -#include -#include "pocketpy.h" -#include "raylib.h" +// +// Created by Bobby Lucero on 4/20/24. +// +#include "Pycron.h" -using namespace std; +Pycron::Pycron() { + SetTraceLogLevel(LOG_ERROR); + SetConfigFlags(FLAG_WINDOW_RESIZABLE); + InitWindow(startupScreenWidth, startupScreenHeight, "test"); + SetTargetFPS(60); + virtualScreen = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight); + sourceRec = { 0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height }; + calculateBounds(); -const int virtualScreenWidth = 360; -const int virtualScreenHeight = 203; -const int initialScale = 3; + vm = new pkpy::VM(); -int startupScreenWidth = virtualScreenWidth * initialScale; -int startupScreenHeight = virtualScreenHeight * initialScale; +// Texture2D logo = LoadTexture("../resources/img.png"); + mouse = LoadTexture("../resources/mouse.png"); +// +// + loadPalette((std::string &) "../resources/palette2.hex"); -int screenWidth = startupScreenWidth; -int screenHeight = startupScreenHeight; + try { + pkpy::CodeObject_ code = vm->compile("return 'test'", "main.py", pkpy::EXEC_MODE, false); + pkpy::PyObject* obj = vm->_exec(code, vm->_main); -Rectangle virtualScreenBounds; -Vector2 origin; + auto& str = pkpy::py_cast(vm, obj); + }catch (pkpy::Exception e) { -Color ColorFromHex(int hexValue) { + } + + HideCursor(); +} + +Pycron::~Pycron(){ + CloseWindow(); + delete vm; +} + +int Pycron::mouseX(){ + int x = GetMouseX(); + float adjX = x + origin.x; + return adjX / virtualScreenBounds.width * virtualScreenWidth; +} + +int Pycron::mouseY(){ + int y = GetMouseY(); + float adjY = y + origin.y; + return adjY / virtualScreenBounds.height * virtualScreenHeight; +} + +Color Pycron::ColorFromHex(int hexValue) { // Extract red, green, blue, and alpha components from the hexadecimal value int r = (hexValue >> 16) & 0xFF; int g = (hexValue >> 8) & 0xFF; @@ -33,7 +64,21 @@ Color ColorFromHex(int hexValue) { return ColorFromNormalized({ rf, gf, bf, 1.0f }); // Alpha is set to 1.0 (fully opaque) } -void calculateBounds() { +void Pycron::loadPalette(std::string& path){ + // Load palette + + std::ifstream paletteFile(path); + std::string line; + + if(paletteFile.is_open()){ + while(getline(paletteFile, line)){ + palette.push_back(ColorFromHex(stoi(line, nullptr, 16))); + } + paletteFile.close(); + } +} + +void Pycron::calculateBounds() { float ar = (float)virtualScreenWidth / (float)virtualScreenHeight; float nar = (float)screenWidth / (float)screenHeight; @@ -51,79 +96,23 @@ void calculateBounds() { } } -int mouseX(){ - int x = GetMouseX(); - float adjX = x + origin.x; - return adjX / virtualScreenBounds.width * virtualScreenWidth; -} - -int mouseY(){ - int y = GetMouseY(); - float adjY = y + origin.y; - return adjY / virtualScreenBounds.height * virtualScreenHeight; -} - -int main() { - - SetTraceLogLevel(LOG_ERROR); - - bool flip = false; - - SetConfigFlags(FLAG_WINDOW_RESIZABLE); - - InitWindow(startupScreenWidth, startupScreenHeight, "test"); - SetTargetFPS(60); - - RenderTexture2D virtualScreen = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight); - Rectangle sourceRec = { 0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height }; - calculateBounds(); - - pkpy::VM* vm = new pkpy::VM(); - - Texture2D logo = LoadTexture("resources/img.png"); - Texture2D mouse = LoadTexture("resources/mouse.png"); - - // Load palette - vector palette; - - ifstream paletteFile("resources/palette2.hex"); - string line; - - if(paletteFile.is_open()){ - while(getline(paletteFile, line)){ - palette.push_back(ColorFromHex(stoi(line, nullptr, 16))); - } - paletteFile.close(); - } - - try { - pkpy::CodeObject_ code = vm->compile("return 'test'", "main.py", pkpy::EXEC_MODE, false); - pkpy::PyObject* obj = vm->_exec(code, vm->_main); - - pkpy::Str& str = pkpy::py_cast(vm, obj); - }catch (pkpy::Exception e) { - - } - - HideCursor(); - - while(!WindowShouldClose()) { +void Pycron::StartGameLoop() { + while (!shouldClose) { - if (IsWindowResized() && !IsWindowFullscreen()) - { + if (IsWindowResized() && !IsWindowFullscreen()) { screenWidth = GetScreenWidth(); screenHeight = GetScreenHeight(); calculateBounds(); } if (IsKeyPressed(KEY_F)) { - if(IsWindowFullscreen()){ + if (IsWindowFullscreen()) { ToggleFullscreen(); SetWindowSize(startupScreenWidth, startupScreenHeight); screenWidth = startupScreenWidth; screenHeight = startupScreenHeight; - }else{ + } else { int monitor = GetCurrentMonitor(); SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor)); screenWidth = GetMonitorWidth(monitor); @@ -131,15 +120,16 @@ int main() { ToggleFullscreen(); } calculateBounds(); - }else if(IsKeyPressed(KEY_I)){ - flip = !flip; + }else if(IsKeyPressed(KEY_ESCAPE)){ + shouldClose = true; } BeginTextureMode(virtualScreen); + ClearBackground(palette[1]); - DrawText(("Hello World " + to_string(GetFPS()) + " FPS").c_str(), 5, 5, 5, RAYWHITE); - DrawTexture(logo, 80 + cos(GetTime()) * (virtualScreenHeight / 4), (virtualScreenHeight / 2) + sin(GetTime()) * (virtualScreenHeight / 4) - 29, WHITE); - DrawLineBezier((Vector2){80, 20}, (Vector2){virtualScreenWidth - 5, virtualScreenHeight - 5}, 1, GREEN); + DrawText(("Hello World " + std::to_string(GetFPS()) + " FPS").c_str(), 5, 5, 5, RAYWHITE); + // DrawTexture(logo, 80 + cos(GetTime()) * (virtualScreenHeight / 4), (virtualScreenHeight / 2) + sin(GetTime()) * (virtualScreenHeight / 4) - 29, WHITE); + DrawLineBezier((Vector2) {80, 20}, (Vector2) {virtualScreenWidth - 5.0f, virtualScreenHeight - 5.0f}, 1, GREEN); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 15, palette[31]); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 13, palette[28]); DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 11, palette[16]); @@ -155,39 +145,15 @@ int main() { } } - DrawTexture(mouse, mouseX(), mouseY(), WHITE); - DrawText(to_string(mouseX()).c_str(), 2, 70, 5, palette[37]); - DrawText(to_string(mouseY()).c_str(), 2, 78, 5, palette[27]); + DrawTexture(mouse, mouseX(), mouseY(), WHITE); + DrawText(std::to_string(mouseX()).c_str(), 2, 70, 5, palette[37]); + DrawText(std::to_string(mouseY()).c_str(), 2, 78, 5, palette[27]); EndTextureMode(); - if(flip){ - // Get texture data - Image img = LoadImageFromTexture(virtualScreen.texture); - Color* pixels = LoadImageColors(img); - - // Modify pixel data - for (int y = 0; y < img.height; y++) { - for (int x = 0; x < img.width; x++) { - // Example: Invert colors - Color pixel = pixels[y * img.width + x]; - pixels[y * img.width + x] = { (unsigned char)(255 - pixel.g), (unsigned char)(255 - pixel.r), (unsigned char)(255 - pixel.b), pixel.a }; - } - } - - // Update texture with modified data - UpdateTexture(virtualScreen.texture, pixels); - } - BeginDrawing(); - ClearBackground(BLACK); - DrawTexturePro(virtualScreen.texture, sourceRec, virtualScreenBounds, origin, 0.0f, WHITE); + ClearBackground(BLACK); + DrawTexturePro(virtualScreen.texture, sourceRec, virtualScreenBounds, origin, 0.0f, WHITE); EndDrawing(); } - - CloseWindow(); - - delete vm; - - return 0; -} +} \ No newline at end of file diff --git a/src/Pycron.h b/src/Pycron.h new file mode 100644 index 0000000..ca74850 --- /dev/null +++ b/src/Pycron.h @@ -0,0 +1,43 @@ +#pragma once +#include +#include + +#include "raylib.h" +#include "pocketpy.h" + +class Pycron { + +public: + const int virtualScreenWidth = 360; + const int virtualScreenHeight = 203; + const int initialScale = 3; + + int startupScreenWidth = virtualScreenWidth * initialScale; + int startupScreenHeight = virtualScreenHeight * initialScale; + + int screenWidth = startupScreenWidth; + int screenHeight = startupScreenHeight; + + Rectangle virtualScreenBounds; + Vector2 origin; + std::vector palette; + RenderTexture2D virtualScreen; + Rectangle sourceRec; + Texture2D mouse; + + bool shouldClose = false; + + pkpy::VM* vm; + + Pycron(); + ~Pycron(); + + void calculateBounds(); + void StartGameLoop(); + Color ColorFromHex(int hexValue); + void loadPalette(std::string& path); + + int mouseX(); + int mouseY(); +}; + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c5f521a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,9 @@ +using namespace std; +#include "Pycron.h" +int main() { + + Pycron pycron_object; + pycron_object.StartGameLoop(); + + return 0; +}