Renaming stuff, it was confusing without

This commit is contained in:
Bobby Lucero 2024-04-26 00:05:07 -04:00
parent 631f9e3612
commit 581ed013f3
10 changed files with 99 additions and 95 deletions

View File

@ -33,8 +33,8 @@ add_executable(Pycron src/main.cpp
src/StateManager.cpp src/StateManager.cpp
src/StateManager.h src/StateManager.h
src/State.h src/State.h
src/GameState.cpp src/States/GameState.cpp
src/GameState.h) src/States/GameState.h)
add_subdirectory(dependencies/pocketpy) add_subdirectory(dependencies/pocketpy)
# Declaring our executable # Declaring our executable

View File

@ -6,22 +6,22 @@
#include "Graphics.h" #include "Graphics.h"
#include "../Utilities.h" #include "../Utilities.h"
std::vector<Color> Graphics::palette; std::vector<Color> Graphics::Palette;
Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : screenWidth(screenWidth), screenHeight(screenHeight){ Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : m_screenWidth(screenWidth), m_screenHeight(screenHeight){
startupScreenWidth = screenWidth * startupScale; m_startupScreenWidth = screenWidth * startupScale;
startupScreenHeight = screenHeight * startupScale; m_startupScreenHeight = screenHeight * startupScale;
windowWidth = startupScreenWidth; m_windowWidth = m_startupScreenWidth;
windowHeight = startupScreenHeight; m_windowHeight = m_startupScreenHeight;
SetConfigFlags(FLAG_WINDOW_RESIZABLE); SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(startupScreenWidth, startupScreenHeight, "test"); InitWindow(m_startupScreenWidth, m_startupScreenHeight, "test");
SetTargetFPS(60); SetTargetFPS(60);
virtualScreen = LoadRenderTexture(screenWidth, screenHeight); m_virtualScreen = LoadRenderTexture(screenWidth, screenHeight);
virtualScreenLocalBounds = {0.0f, 0.0f, (float)virtualScreen.texture.width, -(float)virtualScreen.texture.height }; m_virtualScreenLocalBounds = {0.0f, 0.0f, (float)m_virtualScreen.texture.width, -(float)m_virtualScreen.texture.height };
virtualScreenWindowBounds = {0.0f, 0.0f, (float)windowWidth, (float)windowHeight}; m_virtualScreenWindowBounds = {0.0f, 0.0f, (float)m_windowWidth, (float)m_windowHeight};
updateFunction = nullptr; updateFunction = nullptr;
calculateScreenPositionInWindow(); calculateScreenPositionInWindow();
} }
@ -31,15 +31,15 @@ void Graphics::draw(StateManager* stateManager) {
// 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()));
windowShouldClose = WindowShouldClose(); m_windowShouldClose = WindowShouldClose();
if (IsWindowResized()) { if (IsWindowResized()) {
windowWidth = GetScreenWidth(); m_windowWidth = GetScreenWidth();
windowHeight = GetScreenHeight(); m_windowHeight = GetScreenHeight();
calculateScreenPositionInWindow(); calculateScreenPositionInWindow();
} }
BeginTextureMode(virtualScreen); BeginTextureMode(m_virtualScreen);
// ////////// // //////////
// try{ // try{
// if(updateFunction != nullptr) // if(updateFunction != nullptr)
@ -61,8 +61,8 @@ void Graphics::draw(StateManager* stateManager) {
void Graphics::renderVirtualScreen() { void Graphics::renderVirtualScreen() {
BeginDrawing(); BeginDrawing();
//ClearBackground(palette[16]); ClearBackground(BLACK);
DrawTexturePro(virtualScreen.texture, virtualScreenLocalBounds, virtualScreenWindowBounds, origin, 0.0f, WHITE); DrawTexturePro(m_virtualScreen.texture, m_virtualScreenLocalBounds, m_virtualScreenWindowBounds, m_origin, 0.0f, WHITE);
EndDrawing(); EndDrawing();
} }
@ -72,52 +72,52 @@ void Graphics::loadPalette(std::string path) {
if(paletteFile.is_open()){ if(paletteFile.is_open()){
while(getline(paletteFile, line)){ while(getline(paletteFile, line)){
palette.push_back(Utilities::ColorFromHex(stoi(line, nullptr, 16))); Palette.push_back(Utilities::ColorFromHex(stoi(line, nullptr, 16)));
} }
paletteFile.close(); paletteFile.close();
} }
} }
void Graphics::calculateScreenPositionInWindow() { void Graphics::calculateScreenPositionInWindow() {
float virtualAspectRatio = (float)screenWidth / (float)screenHeight; float virtualAspectRatio = (float)m_screenWidth / (float)m_screenHeight;
float windowAspectRatio = (float)windowWidth / (float)windowHeight; float windowAspectRatio = (float)m_windowWidth / (float)m_windowHeight;
if(windowAspectRatio > virtualAspectRatio) { if(windowAspectRatio > virtualAspectRatio) {
virtualScreenWindowBounds.height = (float)windowHeight; m_virtualScreenWindowBounds.height = (float)m_windowHeight;
virtualScreenWindowBounds.width = virtualScreenWindowBounds.height * virtualAspectRatio; m_virtualScreenWindowBounds.width = m_virtualScreenWindowBounds.height * virtualAspectRatio;
origin.x = -(windowWidth / 2.0f - (virtualScreenWindowBounds.width / 2.0f)); m_origin.x = -(m_windowWidth / 2.0f - (m_virtualScreenWindowBounds.width / 2.0f));
origin.y = 0; m_origin.y = 0;
}else { }else {
virtualScreenWindowBounds.width = (float)windowWidth; m_virtualScreenWindowBounds.width = (float)m_windowWidth;
virtualScreenWindowBounds.height = virtualScreenWindowBounds.width / virtualAspectRatio; m_virtualScreenWindowBounds.height = m_virtualScreenWindowBounds.width / virtualAspectRatio;
origin.x = 0; m_origin.x = 0;
origin.y = -(windowHeight / 2.0f - (virtualScreenWindowBounds.height / 2.0f)); m_origin.y = -(m_windowHeight / 2.0f - (m_virtualScreenWindowBounds.height / 2.0f));
} }
} }
int Graphics::mouseX() { int Graphics::mouseX() {
float x = GetMouseX(); float x = GetMouseX();
float adjX = x + origin.x; float adjX = x + m_origin.x;
return (int)(adjX / virtualScreenWindowBounds.width * screenWidth); return (int)(adjX / m_virtualScreenWindowBounds.width * m_screenWidth);
} }
int Graphics::mouseY() { int Graphics::mouseY() {
float y = GetMouseY(); float y = GetMouseY();
float adjY = y + origin.y; float adjY = y + m_origin.y;
return (int)(adjY / virtualScreenWindowBounds.height * screenHeight); return (int)(adjY / m_virtualScreenWindowBounds.height * m_screenHeight);
} }
void Graphics::toggleFullScreen() { void Graphics::toggleFullScreen() {
if (IsWindowFullscreen()) { if (IsWindowFullscreen()) {
ToggleFullscreen(); ToggleFullscreen();
SetWindowSize(startupScreenWidth, startupScreenHeight); SetWindowSize(m_startupScreenWidth, m_startupScreenHeight);
windowWidth = startupScreenWidth; m_windowWidth = m_startupScreenWidth;
windowHeight = startupScreenHeight; m_windowHeight = m_startupScreenHeight;
} else { } else {
int monitor = GetCurrentMonitor(); int monitor = GetCurrentMonitor();
windowWidth = GetMonitorWidth(monitor); m_windowWidth = GetMonitorWidth(monitor);
windowHeight = GetMonitorHeight(monitor); m_windowHeight = GetMonitorHeight(monitor);
SetWindowSize(windowWidth, windowHeight); SetWindowSize(m_windowWidth, m_windowHeight);
ToggleFullscreen(); ToggleFullscreen();
} }
@ -163,20 +163,20 @@ void Graphics::bindMethods(pkpy::VM *vm) {
} }
void Graphics::Clear(int paletteIndex) { void Graphics::Clear(int paletteIndex) {
if(paletteIndex < 0 || paletteIndex >= palette.size()) paletteIndex = 0; if(paletteIndex < 0 || paletteIndex >= Palette.size()) paletteIndex = 0;
ClearBackground(palette[paletteIndex]); ClearBackground(Palette[paletteIndex]);
} }
void Graphics::Pixel(int x, int y, int 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) { 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) { 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) { void Graphics::searchForDrawFunc(pkpy::VM* vm) {
@ -187,7 +187,7 @@ void Graphics::searchForDrawFunc(pkpy::VM* vm) {
} }
void Graphics::beginDraw() { void Graphics::beginDraw() {
BeginTextureMode(virtualScreen); BeginTextureMode(m_virtualScreen);
} }
void Graphics::endDraw() { void Graphics::endDraw() {

View File

@ -11,16 +11,17 @@ class Graphics {
private: private:
// initial spawn size, used to exit full screen as well // initial spawn size, used to exit full screen as well
int startupScreenWidth; int m_startupScreenWidth;
int startupScreenHeight; int m_startupScreenHeight;
// current size // current size
int windowWidth; int m_windowWidth;
int windowHeight; int m_windowHeight;
Rectangle virtualScreenWindowBounds; // size of rect texture on window Rectangle m_virtualScreenWindowBounds; // size of rect texture on window
Vector2 origin; // position of rect texture on window Vector2 m_origin; // position of rect texture on window
Rectangle virtualScreenLocalBounds; // virtual screen bounds Rectangle m_virtualScreenLocalBounds; // virtual screen bounds
RenderTexture2D virtualScreen; // actual pixel screen RenderTexture2D m_virtualScreen; // actual pixel screen
pkpy::PyObject* updateFunction;
private: private:
@ -29,14 +30,13 @@ private:
public: public:
// virtual screen // virtual screen
int screenWidth; int m_screenWidth;
int screenHeight; int m_screenHeight;
bool windowShouldClose; bool m_windowShouldClose;
static std::vector<Color> palette; static std::vector<Color> Palette;
pkpy::PyObject* updateFunction;
public: public:

View File

@ -25,53 +25,54 @@ std::string loadFileToString(const std::string& filename) {
Pycron::Pycron() { Pycron::Pycron() {
SetTraceLogLevel(LOG_ERROR); SetTraceLogLevel(LOG_ERROR);
graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale}; m_graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale};
graphics->loadPalette("../resources/palette2.hex"); m_graphics->loadPalette("../resources/palette2.hex");
stateManager = new StateManager(this); m_stateManager = new StateManager(this);
vm = new pkpy::VM(); m_vm = new pkpy::VM();
bindMethods(vm); bindMethods();
graphics->bindMethods(vm); m_graphics->bindMethods(m_vm);
std::string python = loadFileToString("../python/main.py"); std::string python = loadFileToString("../python/main.py");
graphics->beginDraw(); m_graphics->beginDraw();
graphics->Clear(0); m_graphics->Clear(0);
try { try {
pkpy::CodeObject_ code = vm->compile(python, "main.py", pkpy::EXEC_MODE, false); pkpy::CodeObject_ code = m_vm->compile(python, "main.py", pkpy::EXEC_MODE, false);
vm->_exec(code, vm->_main); m_vm->_exec(code, m_vm->_main);
graphics->searchForDrawFunc(vm); m_graphics->searchForDrawFunc(m_vm);
}catch (pkpy::Exception e) { }catch (pkpy::Exception e) {
std::cout << e.summary() << std::endl; std::cout << e.summary() << std::endl;
} }
graphics->endDraw(); m_graphics->endDraw();
} }
Pycron::~Pycron(){ Pycron::~Pycron(){
CloseWindow(); CloseWindow();
delete vm; delete m_vm;
delete graphics; delete m_graphics;
delete m_stateManager;
} }
void Pycron::StartGameLoop() { void Pycron::StartGameLoop() {
while (!graphics->windowShouldClose) { while (!m_graphics->m_windowShouldClose) {
if (IsKeyPressed(KEY_F)) { 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) { void Pycron::bindMethods() {
vm->bind(vm->builtins, "rnd(min: int, max: int) -> int", getRandomNumber); m_vm->bind(m_vm->builtins, "rnd(min: int, max: int) -> int", getRandomNumber);
vm->bind(vm->builtins, "sin(num: float) -> float", getSin); m_vm->bind(m_vm->builtins, "sin(num: float) -> float", getSin);
vm->bind(vm->builtins, "cos(num: float) -> float", getCos); m_vm->bind(m_vm->builtins, "cos(num: float) -> float", getCos);
vm->bind(vm->builtins, "fps() -> int", getFPS); m_vm->bind(m_vm->builtins, "fps() -> int", getFPS);
} }

View File

@ -13,16 +13,16 @@ private:
const int virtualScreenWidth = 360; const int virtualScreenWidth = 360;
const int virtualScreenHeight = 203; const int virtualScreenHeight = 203;
const int initialScale = 3; const int initialScale = 3;
Graphics* graphics; Graphics* m_graphics;
StateManager* stateManager; StateManager* m_stateManager;
pkpy::VM* vm; pkpy::VM* m_vm;
public: public:
Pycron(); Pycron();
~Pycron(); ~Pycron();
void StartGameLoop(); void StartGameLoop();
void bindMethods(pkpy::VM* vm); void bindMethods();
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);

View File

@ -8,9 +8,9 @@ class StateManager;
class State { class State {
private: private:
StateManager* stateManager; StateManager* m_stateManager;
protected: protected:
explicit State(StateManager* stateManager) : stateManager(stateManager){} explicit State(StateManager* stateManager) : m_stateManager(stateManager){}
public: public:
virtual ~State() = default; virtual ~State() = default;
virtual void Draw(Graphics* graphics) = 0; virtual void Draw(Graphics* graphics) = 0;

View File

@ -5,9 +5,8 @@
#include "StateManager.h" #include "StateManager.h"
StateManager::StateManager(Pycron *pycron) : m_pycron(pycron){ StateManager::StateManager(Pycron *pycron) : m_pycron(pycron){
gameState = new GameState(this); m_gameState = new GameState(this);
m_currentState = gameState; RequestStateChange(GAME);
} }
void StateManager::RequestStateChange(StateManager::StateType state) { void StateManager::RequestStateChange(StateManager::StateType state) {
@ -16,7 +15,7 @@ void StateManager::RequestStateChange(StateManager::StateType state) {
} }
if(state == StateType::GAME){ if(state == StateType::GAME){
m_currentState = gameState; m_currentState = m_gameState;
} }
if(m_currentState){ if(m_currentState){
@ -30,3 +29,8 @@ void StateManager::Draw(Graphics *graphics) {
m_currentState->Draw(graphics); m_currentState->Draw(graphics);
} }
} }
StateManager::~StateManager() {
m_currentState = nullptr;
delete m_gameState;
}

View File

@ -3,7 +3,7 @@
#include "State.h" #include "State.h"
#include "Pycron.h" #include "Pycron.h"
#include "Graphics/Graphics.h" #include "Graphics/Graphics.h"
#include "GameState.h" #include "States/GameState.h"
class Pycron; class Pycron;
class State; class State;
@ -20,12 +20,11 @@ private:
State* m_currentState; State* m_currentState;
Pycron* m_pycron; Pycron* m_pycron;
GameState* m_gameState;
GameState* gameState;
public: public:
explicit StateManager(Pycron* pycron); explicit StateManager(Pycron* pycron);
~StateManager();
void RequestStateChange(StateType state); void RequestStateChange(StateType state);

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "State.h" #include "../State.h"
class GameState : public State { class GameState : public State {
public: public: