diff --git a/CMakeLists.txt b/CMakeLists.txt index c880988..1398b1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,8 +33,8 @@ add_executable(Pycron src/main.cpp src/StateManager.cpp src/StateManager.h src/State.h - src/GameState.cpp - src/GameState.h) + src/States/GameState.cpp + src/States/GameState.h) add_subdirectory(dependencies/pocketpy) # Declaring our executable diff --git a/src/Graphics/Graphics.cpp b/src/Graphics/Graphics.cpp index 79b0c07..024bca8 100644 --- a/src/Graphics/Graphics.cpp +++ b/src/Graphics/Graphics.cpp @@ -6,22 +6,22 @@ #include "Graphics.h" #include "../Utilities.h" -std::vector Graphics::palette; +std::vector Graphics::Palette; -Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : screenWidth(screenWidth), screenHeight(screenHeight){ - startupScreenWidth = screenWidth * startupScale; - startupScreenHeight = screenHeight * startupScale; - windowWidth = startupScreenWidth; - windowHeight = startupScreenHeight; +Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : m_screenWidth(screenWidth), m_screenHeight(screenHeight){ + m_startupScreenWidth = screenWidth * startupScale; + m_startupScreenHeight = screenHeight * startupScale; + m_windowWidth = m_startupScreenWidth; + m_windowHeight = m_startupScreenHeight; SetConfigFlags(FLAG_WINDOW_RESIZABLE); - InitWindow(startupScreenWidth, startupScreenHeight, "test"); + InitWindow(m_startupScreenWidth, m_startupScreenHeight, "test"); SetTargetFPS(60); - virtualScreen = LoadRenderTexture(screenWidth, screenHeight); + m_virtualScreen = LoadRenderTexture(screenWidth, screenHeight); - virtualScreenLocalBounds = {0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height }; - virtualScreenWindowBounds = {0.0f, 0.0f, (float)windowWidth, (float)windowHeight}; + m_virtualScreenLocalBounds = {0.0f, 0.0f, (float)m_virtualScreen.texture.width, -(float)m_virtualScreen.texture.height }; + m_virtualScreenWindowBounds = {0.0f, 0.0f, (float)m_windowWidth, (float)m_windowHeight}; updateFunction = nullptr; calculateScreenPositionInWindow(); } @@ -31,15 +31,15 @@ void Graphics::draw(StateManager* stateManager) { // vm->builtins->attr().set("mouseX", pkpy::py_var(vm, mouseX())); // vm->builtins->attr().set("mouseY", pkpy::py_var(vm, mouseY())); - windowShouldClose = WindowShouldClose(); + m_windowShouldClose = WindowShouldClose(); if (IsWindowResized()) { - windowWidth = GetScreenWidth(); - windowHeight = GetScreenHeight(); + m_windowWidth = GetScreenWidth(); + m_windowHeight = GetScreenHeight(); calculateScreenPositionInWindow(); } - BeginTextureMode(virtualScreen); + BeginTextureMode(m_virtualScreen); // ////////// // try{ // if(updateFunction != nullptr) @@ -61,8 +61,8 @@ void Graphics::draw(StateManager* stateManager) { void Graphics::renderVirtualScreen() { BeginDrawing(); - //ClearBackground(palette[16]); - DrawTexturePro(virtualScreen.texture, virtualScreenLocalBounds, virtualScreenWindowBounds, origin, 0.0f, WHITE); + ClearBackground(BLACK); + DrawTexturePro(m_virtualScreen.texture, m_virtualScreenLocalBounds, m_virtualScreenWindowBounds, m_origin, 0.0f, WHITE); EndDrawing(); } @@ -72,52 +72,52 @@ void Graphics::loadPalette(std::string path) { if(paletteFile.is_open()){ while(getline(paletteFile, line)){ - palette.push_back(Utilities::ColorFromHex(stoi(line, nullptr, 16))); + 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; + float virtualAspectRatio = (float)m_screenWidth / (float)m_screenHeight; + float windowAspectRatio = (float)m_windowWidth / (float)m_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; + m_virtualScreenWindowBounds.height = (float)m_windowHeight; + m_virtualScreenWindowBounds.width = m_virtualScreenWindowBounds.height * virtualAspectRatio; + m_origin.x = -(m_windowWidth / 2.0f - (m_virtualScreenWindowBounds.width / 2.0f)); + m_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)); + m_virtualScreenWindowBounds.width = (float)m_windowWidth; + m_virtualScreenWindowBounds.height = m_virtualScreenWindowBounds.width / virtualAspectRatio; + m_origin.x = 0; + m_origin.y = -(m_windowHeight / 2.0f - (m_virtualScreenWindowBounds.height / 2.0f)); } } int Graphics::mouseX() { float x = GetMouseX(); - float adjX = x + origin.x; - return (int)(adjX / virtualScreenWindowBounds.width * screenWidth); + float adjX = x + m_origin.x; + return (int)(adjX / m_virtualScreenWindowBounds.width * m_screenWidth); } int Graphics::mouseY() { float y = GetMouseY(); - float adjY = y + origin.y; - return (int)(adjY / virtualScreenWindowBounds.height * screenHeight); + float adjY = y + m_origin.y; + return (int)(adjY / m_virtualScreenWindowBounds.height * m_screenHeight); } void Graphics::toggleFullScreen() { if (IsWindowFullscreen()) { ToggleFullscreen(); - SetWindowSize(startupScreenWidth, startupScreenHeight); - windowWidth = startupScreenWidth; - windowHeight = startupScreenHeight; + SetWindowSize(m_startupScreenWidth, m_startupScreenHeight); + m_windowWidth = m_startupScreenWidth; + m_windowHeight = m_startupScreenHeight; } else { int monitor = GetCurrentMonitor(); - windowWidth = GetMonitorWidth(monitor); - windowHeight = GetMonitorHeight(monitor); - SetWindowSize(windowWidth, windowHeight); + m_windowWidth = GetMonitorWidth(monitor); + m_windowHeight = GetMonitorHeight(monitor); + SetWindowSize(m_windowWidth, m_windowHeight); ToggleFullscreen(); } @@ -163,20 +163,20 @@ void Graphics::bindMethods(pkpy::VM *vm) { } void Graphics::Clear(int paletteIndex) { - if(paletteIndex < 0 || paletteIndex >= palette.size()) paletteIndex = 0; - ClearBackground(palette[paletteIndex]); + if(paletteIndex < 0 || paletteIndex >= Palette.size()) paletteIndex = 0; + ClearBackground(Palette[paletteIndex]); } void Graphics::Pixel(int x, int y, int paletteIndex) { - DrawPixel(x, y, palette[paletteIndex]); + DrawPixel(x, y, Palette[paletteIndex]); } void Graphics::Circle(int x, int y, int radius, int paletteIndex) { - DrawCircle(x, y, radius, palette[paletteIndex]); + DrawCircle(x, y, radius, Palette[paletteIndex]); } void Graphics::Text(std::string s, int x, int y, int paletteIndex) { - DrawText(s.c_str(), x, y, 5, palette[paletteIndex]); + DrawText(s.c_str(), x, y, 5, Palette[paletteIndex]); } void Graphics::searchForDrawFunc(pkpy::VM* vm) { @@ -187,7 +187,7 @@ void Graphics::searchForDrawFunc(pkpy::VM* vm) { } void Graphics::beginDraw() { - BeginTextureMode(virtualScreen); + BeginTextureMode(m_virtualScreen); } void Graphics::endDraw() { diff --git a/src/Graphics/Graphics.h b/src/Graphics/Graphics.h index b5c9652..c279af6 100644 --- a/src/Graphics/Graphics.h +++ b/src/Graphics/Graphics.h @@ -11,16 +11,17 @@ class Graphics { private: // initial spawn size, used to exit full screen as well - int startupScreenWidth; - int startupScreenHeight; + int m_startupScreenWidth; + int m_startupScreenHeight; // current size - int windowWidth; - int windowHeight; + int m_windowWidth; + int m_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 + Rectangle m_virtualScreenWindowBounds; // size of rect texture on window + Vector2 m_origin; // position of rect texture on window + Rectangle m_virtualScreenLocalBounds; // virtual screen bounds + RenderTexture2D m_virtualScreen; // actual pixel screen + pkpy::PyObject* updateFunction; private: @@ -29,14 +30,13 @@ private: public: // virtual screen - int screenWidth; - int screenHeight; + int m_screenWidth; + int m_screenHeight; - bool windowShouldClose; + bool m_windowShouldClose; - static std::vector palette; + static std::vector Palette; - pkpy::PyObject* updateFunction; public: diff --git a/src/Pycron.cpp b/src/Pycron.cpp index 0d482a7..221fc79 100644 --- a/src/Pycron.cpp +++ b/src/Pycron.cpp @@ -25,53 +25,54 @@ std::string loadFileToString(const std::string& filename) { Pycron::Pycron() { SetTraceLogLevel(LOG_ERROR); - graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale}; - graphics->loadPalette("../resources/palette2.hex"); + m_graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale}; + m_graphics->loadPalette("../resources/palette2.hex"); - stateManager = new StateManager(this); + m_stateManager = new StateManager(this); - vm = new pkpy::VM(); - bindMethods(vm); + m_vm = new pkpy::VM(); + bindMethods(); - graphics->bindMethods(vm); + m_graphics->bindMethods(m_vm); std::string python = loadFileToString("../python/main.py"); - graphics->beginDraw(); - graphics->Clear(0); + m_graphics->beginDraw(); + m_graphics->Clear(0); try { - pkpy::CodeObject_ code = vm->compile(python, "main.py", pkpy::EXEC_MODE, false); - vm->_exec(code, vm->_main); - graphics->searchForDrawFunc(vm); + pkpy::CodeObject_ code = m_vm->compile(python, "main.py", pkpy::EXEC_MODE, false); + m_vm->_exec(code, m_vm->_main); + m_graphics->searchForDrawFunc(m_vm); }catch (pkpy::Exception e) { std::cout << e.summary() << std::endl; } - graphics->endDraw(); + m_graphics->endDraw(); } Pycron::~Pycron(){ CloseWindow(); - delete vm; - delete graphics; + delete m_vm; + delete m_graphics; + delete m_stateManager; } void Pycron::StartGameLoop() { - while (!graphics->windowShouldClose) { + while (!m_graphics->m_windowShouldClose) { if (IsKeyPressed(KEY_F)) { - graphics->toggleFullScreen(); + m_graphics->toggleFullScreen(); } - graphics->draw(this->stateManager); + m_graphics->draw(this->m_stateManager); } } -void Pycron::bindMethods(pkpy::VM *vm) { - vm->bind(vm->builtins, "rnd(min: int, max: int) -> int", getRandomNumber); - vm->bind(vm->builtins, "sin(num: float) -> float", getSin); - vm->bind(vm->builtins, "cos(num: float) -> float", getCos); - vm->bind(vm->builtins, "fps() -> int", getFPS); +void Pycron::bindMethods() { + m_vm->bind(m_vm->builtins, "rnd(min: int, max: int) -> int", getRandomNumber); + 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, "fps() -> int", getFPS); } diff --git a/src/Pycron.h b/src/Pycron.h index 282f769..9ea7c79 100644 --- a/src/Pycron.h +++ b/src/Pycron.h @@ -13,16 +13,16 @@ private: const int virtualScreenWidth = 360; const int virtualScreenHeight = 203; const int initialScale = 3; - Graphics* graphics; - StateManager* stateManager; - pkpy::VM* vm; + Graphics* m_graphics; + StateManager* m_stateManager; + pkpy::VM* m_vm; public: Pycron(); ~Pycron(); void StartGameLoop(); - void bindMethods(pkpy::VM* vm); + void bindMethods(); static pkpy::PyObject* getRandomNumber(pkpy::VM* vm, pkpy::ArgsView args); static pkpy::PyObject* getSin(pkpy::VM* vm, pkpy::ArgsView args); diff --git a/src/State.h b/src/State.h index 3482de1..d795dc5 100644 --- a/src/State.h +++ b/src/State.h @@ -8,9 +8,9 @@ class StateManager; class State { private: - StateManager* stateManager; + StateManager* m_stateManager; protected: - explicit State(StateManager* stateManager) : stateManager(stateManager){} + explicit State(StateManager* stateManager) : m_stateManager(stateManager){} public: virtual ~State() = default; virtual void Draw(Graphics* graphics) = 0; diff --git a/src/StateManager.cpp b/src/StateManager.cpp index 1bb5b66..745b345 100644 --- a/src/StateManager.cpp +++ b/src/StateManager.cpp @@ -5,9 +5,8 @@ #include "StateManager.h" StateManager::StateManager(Pycron *pycron) : m_pycron(pycron){ - gameState = new GameState(this); - m_currentState = gameState; - + m_gameState = new GameState(this); + RequestStateChange(GAME); } void StateManager::RequestStateChange(StateManager::StateType state) { @@ -16,7 +15,7 @@ void StateManager::RequestStateChange(StateManager::StateType state) { } if(state == StateType::GAME){ - m_currentState = gameState; + m_currentState = m_gameState; } if(m_currentState){ @@ -30,3 +29,8 @@ void StateManager::Draw(Graphics *graphics) { m_currentState->Draw(graphics); } } + +StateManager::~StateManager() { + m_currentState = nullptr; + delete m_gameState; +} diff --git a/src/StateManager.h b/src/StateManager.h index c2061ca..7ee78b4 100644 --- a/src/StateManager.h +++ b/src/StateManager.h @@ -3,7 +3,7 @@ #include "State.h" #include "Pycron.h" #include "Graphics/Graphics.h" -#include "GameState.h" +#include "States/GameState.h" class Pycron; class State; @@ -20,12 +20,11 @@ private: State* m_currentState; Pycron* m_pycron; - - GameState* gameState; - + GameState* m_gameState; public: explicit StateManager(Pycron* pycron); + ~StateManager(); void RequestStateChange(StateType state); diff --git a/src/GameState.cpp b/src/States/GameState.cpp similarity index 100% rename from src/GameState.cpp rename to src/States/GameState.cpp diff --git a/src/GameState.h b/src/States/GameState.h similarity index 91% rename from src/GameState.h rename to src/States/GameState.h index 2c2e41e..d046add 100644 --- a/src/GameState.h +++ b/src/States/GameState.h @@ -1,5 +1,5 @@ #pragma once -#include "State.h" +#include "../State.h" class GameState : public State { public: