From 4c35be70dfbcdbe99310e1a8485eabcf640e4087 Mon Sep 17 00:00:00 2001 From: Bobby Lucero Date: Sun, 21 Apr 2024 14:06:56 -0400 Subject: [PATCH] Refactor --- CMakeLists.txt | 6 +- src/Graphics/Graphics.cpp | 119 +++++++++++++++++++++++++++++++++ src/Graphics/Graphics.h | 44 +++++++++++++ src/Pycron.cpp | 135 +++----------------------------------- src/Pycron.h | 29 ++------ src/Utilities.cpp | 21 ++++++ src/Utilities.h | 7 ++ 7 files changed, 209 insertions(+), 152 deletions(-) create mode 100644 src/Graphics/Graphics.cpp create mode 100644 src/Graphics/Graphics.h create mode 100644 src/Utilities.cpp create mode 100644 src/Utilities.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dbb827d..37eaaa8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,11 @@ set(PROJECT_INCLUDE "${CMAKE_CURRENT_LIST_DIR}/sources/") # Define PROJECT_INCLU add_executable(UnnamedMicroPythonConsole src/main.cpp src/Pycron.cpp - src/Pycron.h) + src/Pycron.h + src/Utilities.cpp + src/Utilities.h + src/Graphics/Graphics.cpp + src/Graphics/Graphics.h) add_subdirectory(dependencies/pocketpy) # Declaring our executable diff --git a/src/Graphics/Graphics.cpp b/src/Graphics/Graphics.cpp new file mode 100644 index 0000000..80475fd --- /dev/null +++ b/src/Graphics/Graphics.cpp @@ -0,0 +1,119 @@ +// +// Created by Bobby Lucero on 4/20/24. +// + +#include +#include "Graphics.h" +#include "../Utilities.h" + +Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : screenWidth(screenWidth), screenHeight(screenHeight){ + startupScreenWidth = screenWidth * startupScale; + startupScreenHeight = screenHeight * startupScale; + windowWidth = startupScreenWidth; + windowHeight = startupScreenHeight; + + SetConfigFlags(FLAG_WINDOW_RESIZABLE); + InitWindow(startupScreenWidth, startupScreenHeight, "test"); + SetTargetFPS(60); + virtualScreen = LoadRenderTexture(screenWidth, screenHeight); + virtualScreenLocalBounds = {0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height }; + calculateScreenPositionInWindow(); +} + +void Graphics::draw() { + + windowShouldClose = WindowShouldClose(); + + if (IsWindowResized()) { + windowWidth = GetScreenWidth(); + windowHeight = GetScreenHeight(); + calculateScreenPositionInWindow(); + } + + BeginTextureMode(virtualScreen); + ////////// + ClearBackground(palette[1]); + DrawText(("Hello World " + std::to_string(GetFPS()) + " FPS").c_str(), 5, 5, 5, RAYWHITE); + + DrawRectangle(3, 19, 33, 33, BLACK); + + for (int i = 0; i < 8; ++i) { + for (int j = 0; j < 8; ++j) { + DrawRectangle(4 + i * 4, 20 + j * 4, 3, 3, palette[i + j * 8]); + } + } + + DrawCircleLines(mouseX(), mouseY(),3, palette[18]); + ////////// + EndTextureMode(); + + renderVirtualScreen(); +} + +void Graphics::renderVirtualScreen() { + BeginDrawing(); + ClearBackground(palette[0]); + DrawTexturePro(virtualScreen.texture, virtualScreenLocalBounds, virtualScreenWindowBounds, origin, 0.0f, WHITE); + EndDrawing(); +} + +void Graphics::loadPalette(std::string &path) { + std::ifstream paletteFile(path); + std::string line; + + if(paletteFile.is_open()){ + while(getline(paletteFile, line)){ + palette.push_back(Utilities::ColorFromHex(stoi(line, nullptr, 16))); + } + paletteFile.close(); + } +} + +void Graphics::calculateScreenPositionInWindow() { + float virtualAspectRatio = (float)screenWidth / (float)screenHeight; + float windowAspectRatio = (float)windowWidth / (float)windowHeight; + + if(windowAspectRatio > virtualAspectRatio) { + virtualScreenWindowBounds.height = (float)windowHeight; + virtualScreenWindowBounds.width = virtualScreenWindowBounds.height * virtualAspectRatio; + origin.x = -(windowWidth / 2.0f - (virtualScreenWindowBounds.width / 2.0f)); + origin.y = 0; + }else { + virtualScreenWindowBounds.width = (float)windowWidth; + virtualScreenWindowBounds.height = virtualScreenWindowBounds.width / virtualAspectRatio; + origin.x = 0; + origin.y = -(windowHeight / 2.0f - (virtualScreenWindowBounds.height / 2.0f)); + } +} + +int Graphics::mouseX() { + float x = GetMouseX(); + float adjX = x + origin.x; + return (int)(adjX / virtualScreenWindowBounds.width * screenWidth); +} + +int Graphics::mouseY() { + float y = GetMouseY(); + float adjY = y + origin.y; + return (int)(adjY / virtualScreenWindowBounds.height * screenHeight); +} + +void Graphics::toggleFullScreen() { + if (IsWindowFullscreen()) { + ToggleFullscreen(); + SetWindowSize(startupScreenWidth, startupScreenHeight); + windowWidth = startupScreenWidth; + windowHeight = startupScreenHeight; + } else { + int monitor = GetCurrentMonitor(); + SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor)); + windowWidth = GetMonitorWidth(monitor); + windowHeight = GetMonitorHeight(monitor); + ToggleFullscreen(); + } + calculateScreenPositionInWindow(); +} + + + + diff --git a/src/Graphics/Graphics.h b/src/Graphics/Graphics.h new file mode 100644 index 0000000..493dd0a --- /dev/null +++ b/src/Graphics/Graphics.h @@ -0,0 +1,44 @@ +#pragma once +#include "raylib.h" +#include + +class Graphics { + +private: + // initial spawn size, used to exit full screen as well + int startupScreenWidth; + int startupScreenHeight; + // current size + int windowWidth; + int windowHeight; + + Rectangle virtualScreenWindowBounds; // size of rect texture on window + Vector2 origin; // position of rect texture on window + Rectangle virtualScreenLocalBounds; // virtual screen bounds + RenderTexture2D virtualScreen; // actual pixel screen + + std::vector palette; + +private: + void renderVirtualScreen(); + void calculateScreenPositionInWindow(); + +public: + // virtual screen + int screenWidth; + int screenHeight; + + bool windowShouldClose; + +public: + Graphics(int screenWidth, int screenHeight, int startupScale); + + void draw(); + + void loadPalette(std::string& path); + int mouseX(); + int mouseY(); + void toggleFullScreen(); + + +}; diff --git a/src/Pycron.cpp b/src/Pycron.cpp index d83fed5..a62691b 100644 --- a/src/Pycron.cpp +++ b/src/Pycron.cpp @@ -2,24 +2,17 @@ // Created by Bobby Lucero on 4/20/24. // #include "Pycron.h" +#include "Utilities.h" +#include "Graphics/Graphics.h" 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(); + + graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale}; + graphics->loadPalette((std::string &) "../resources/palette2.hex"); vm = new pkpy::VM(); -// Texture2D logo = LoadTexture("../resources/img.png"); - mouse = LoadTexture("../resources/mouse.png"); -// -// - loadPalette((std::string &) "../resources/palette2.hex"); - try { pkpy::CodeObject_ code = vm->compile("return 'test'", "main.py", pkpy::EXEC_MODE, false); pkpy::PyObject* obj = vm->_exec(code, vm->_main); @@ -29,131 +22,21 @@ Pycron::Pycron() { } - HideCursor(); } Pycron::~Pycron(){ CloseWindow(); delete vm; + delete graphics; } -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; - int b = hexValue & 0xFF; - - // Normalize the color components to the range [0, 1] - float rf = static_cast(r) / 255.0f; - float gf = static_cast(g) / 255.0f; - float bf = static_cast(b) / 255.0f; - - // Create and return the color - return ColorFromNormalized({ rf, gf, bf, 1.0f }); // Alpha is set to 1.0 (fully opaque) -} - -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; - - if(nar > ar) { - virtualScreenBounds.height = screenHeight; - virtualScreenBounds.width = virtualScreenBounds.height * ar; - origin.x = -(screenWidth / 2.0f - (virtualScreenBounds.width / 2.0f)); - origin.y = 0; - }else { - virtualScreenBounds.width = screenWidth; - virtualScreenBounds.height = virtualScreenBounds.width / ar; - origin.x = 0; - origin.y = -(screenHeight / 2.0f - (virtualScreenBounds.height / 2.0f)); - } -} void Pycron::StartGameLoop() { - while (!shouldClose) { - - - if (IsWindowResized() && !IsWindowFullscreen()) { - screenWidth = GetScreenWidth(); - screenHeight = GetScreenHeight(); - calculateBounds(); - } + while (!graphics->windowShouldClose) { if (IsKeyPressed(KEY_F)) { - if (IsWindowFullscreen()) { - ToggleFullscreen(); - SetWindowSize(startupScreenWidth, startupScreenHeight); - screenWidth = startupScreenWidth; - screenHeight = startupScreenHeight; - } else { - int monitor = GetCurrentMonitor(); - SetWindowSize(GetMonitorWidth(monitor), GetMonitorHeight(monitor)); - screenWidth = GetMonitorWidth(monitor); - screenHeight = GetMonitorHeight(monitor); - ToggleFullscreen(); - } - calculateBounds(); - }else if(IsKeyPressed(KEY_ESCAPE)){ - shouldClose = true; + graphics->toggleFullScreen(); } - - BeginTextureMode(virtualScreen); - - ClearBackground(palette[1]); - 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]); - DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 9, palette[15]); - DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 7, palette[51]); - DrawCircle(virtualScreenWidth / 2, virtualScreenHeight / 2, 5, palette[47]); - - DrawRectangle(3, 19, 33, 33, BLACK); - - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 8; ++j) { - DrawRectangle(4 + i * 4, 20 + j * 4, 3, 3, palette[i + j * 8]); - } - } - - 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(); - - BeginDrawing(); - ClearBackground(BLACK); - DrawTexturePro(virtualScreen.texture, sourceRec, virtualScreenBounds, origin, 0.0f, WHITE); - EndDrawing(); + graphics->draw(); } } \ No newline at end of file diff --git a/src/Pycron.h b/src/Pycron.h index ca74850..be367bd 100644 --- a/src/Pycron.h +++ b/src/Pycron.h @@ -1,43 +1,22 @@ #pragma once -#include #include - -#include "raylib.h" #include "pocketpy.h" +#include "Graphics/Graphics.h" class Pycron { -public: +private: 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; - + Graphics* graphics; pkpy::VM* vm; +public: Pycron(); ~Pycron(); - void calculateBounds(); void StartGameLoop(); - Color ColorFromHex(int hexValue); - void loadPalette(std::string& path); - int mouseX(); - int mouseY(); }; diff --git a/src/Utilities.cpp b/src/Utilities.cpp new file mode 100644 index 0000000..5a42308 --- /dev/null +++ b/src/Utilities.cpp @@ -0,0 +1,21 @@ +// +// Created by Bobby Lucero on 4/20/24. +// + +#include "Utilities.h" +namespace Utilities { + Color ColorFromHex(int hexValue) { + // Extract red, green, blue, and alpha components from the hexadecimal value + int r = (hexValue >> 16) & 0xFF; + int g = (hexValue >> 8) & 0xFF; + int b = hexValue & 0xFF; + + // Normalize the color components to the range [0, 1] + float rf = static_cast(r) / 255.0f; + float gf = static_cast(g) / 255.0f; + float bf = static_cast(b) / 255.0f; + + // Create and return the color + return ColorFromNormalized({ rf, gf, bf, 1.0f }); // Alpha is set to 1.0 (fully opaque) + } +} diff --git a/src/Utilities.h b/src/Utilities.h new file mode 100644 index 0000000..a7adf5d --- /dev/null +++ b/src/Utilities.h @@ -0,0 +1,7 @@ +#pragma once +#include "raylib.h" + +namespace Utilities { + Color ColorFromHex(int hexValue); + +}