Refactor, added state manager, abstract state, game state, implemented pipeline based on architecture drawing

work on relieving circular dependencies in the future
This commit is contained in:
Bobby Lucero 2024-04-25 20:16:58 -04:00
parent 16733fdb0e
commit 631f9e3612
13 changed files with 13619 additions and 17 deletions

View File

@ -29,7 +29,12 @@ add_executable(Pycron src/main.cpp
src/Utilities.cpp
src/Utilities.h
src/Graphics/Graphics.cpp
src/Graphics/Graphics.h)
src/Graphics/Graphics.h
src/StateManager.cpp
src/StateManager.h
src/State.h
src/GameState.cpp
src/GameState.h)
add_subdirectory(dependencies/pocketpy)
# Declaring our executable

13453
architecture.excalidraw Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 9.7 MiB

27
src/GameState.cpp Normal file
View File

@ -0,0 +1,27 @@
//
// Created by Bobby Lucero on 4/25/24.
//
#include "GameState.h"
GameState::GameState(StateManager *stateManager) : State(stateManager) {
}
void GameState::Draw(Graphics *graphics) {
graphics->Text("Test", 10, 10, 10);
}
void GameState::OnEnter() {
//TODO: Python preprocess scripts
}
void GameState::OnExit() {
}
void GameState::onKeyPressed(int key) {
}

13
src/GameState.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include "State.h"
class GameState : public State {
public:
GameState(StateManager* stateManager);
void Draw(Graphics* graphics) override;
void OnEnter() override;
void OnExit() override;
void onKeyPressed(int key) override;
};

View File

@ -26,10 +26,10 @@ Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : screen
calculateScreenPositionInWindow();
}
void Graphics::draw(pkpy::VM* vm) {
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()));
// vm->builtins->attr().set("mouseX", pkpy::py_var(vm, mouseX()));
// vm->builtins->attr().set("mouseY", pkpy::py_var(vm, mouseY()));
windowShouldClose = WindowShouldClose();
@ -40,17 +40,20 @@ void Graphics::draw(pkpy::VM* vm) {
}
BeginTextureMode(virtualScreen);
//////////
try{
if(updateFunction != nullptr)
vm->call(updateFunction);
} catch(pkpy::Exception e){
std::cout << e.summary() << std::endl;
}
// //////////
// try{
// if(updateFunction != nullptr)
// vm->call(updateFunction);
// } catch(pkpy::Exception e){
// std::cout << e.summary() << std::endl;
// }
//
// Circle(150,100,50,1);
// Text("Hello from C++", 120, 95, 9);
// //////////
stateManager->Draw(this);
Circle(150,100,50,1);
Text("Hello from C++", 120, 95, 9);
//////////
EndTextureMode();
renderVirtualScreen();

View File

@ -1,9 +1,12 @@
#pragma once
#include "raylib.h"
#include "pocketpy/vm.h"
#include "../StateManager.h"
#include <algorithm>
#include <vector>
class StateManager;
class Graphics {
private:
@ -39,7 +42,7 @@ public:
public:
Graphics(int screenWidth, int screenHeight, int startupScale);
void draw(pkpy::VM* vm);
void draw(StateManager* stateManager);
void beginDraw();
void endDraw();

View File

@ -28,6 +28,8 @@ Pycron::Pycron() {
graphics = new Graphics{virtualScreenWidth, virtualScreenHeight, initialScale};
graphics->loadPalette("../resources/palette2.hex");
stateManager = new StateManager(this);
vm = new pkpy::VM();
bindMethods(vm);
@ -61,7 +63,7 @@ void Pycron::StartGameLoop() {
if (IsKeyPressed(KEY_F)) {
graphics->toggleFullScreen();
}
graphics->draw(vm);
graphics->draw(this->stateManager);
}
}

View File

@ -1,8 +1,12 @@
#pragma once
#include <algorithm>
#include "pocketpy.h"
#include "StateManager.h"
#include "Graphics/Graphics.h"
class Graphics;
class StateManager;
class Pycron {
private:
@ -10,6 +14,7 @@ private:
const int virtualScreenHeight = 203;
const int initialScale = 3;
Graphics* graphics;
StateManager* stateManager;
pkpy::VM* vm;
public:

20
src/State.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include "Graphics/Graphics.h"
#include "StateManager.h"
class Graphics;
class StateManager;
class State {
private:
StateManager* stateManager;
protected:
explicit State(StateManager* stateManager) : stateManager(stateManager){}
public:
virtual ~State() = default;
virtual void Draw(Graphics* graphics) = 0;
virtual void OnEnter() = 0;
virtual void OnExit() = 0;
virtual void onKeyPressed(int key) = 0;
};

32
src/StateManager.cpp Normal file
View File

@ -0,0 +1,32 @@
//
// Created by Bobby Lucero on 4/25/24.
//
#include "StateManager.h"
StateManager::StateManager(Pycron *pycron) : m_pycron(pycron){
gameState = new GameState(this);
m_currentState = gameState;
}
void StateManager::RequestStateChange(StateManager::StateType state) {
if(m_currentState){
m_currentState->OnExit();
}
if(state == StateType::GAME){
m_currentState = gameState;
}
if(m_currentState){
m_currentState->OnEnter();
}
}
void StateManager::Draw(Graphics *graphics) {
if(m_currentState){
m_currentState->Draw(graphics);
}
}

40
src/StateManager.h Normal file
View File

@ -0,0 +1,40 @@
#pragma once
#include "State.h"
#include "Pycron.h"
#include "Graphics/Graphics.h"
#include "GameState.h"
class Pycron;
class State;
class Graphics;
class GameState;
class StateManager {
private:
enum StateType {
EDITOR,
MENU,
GAME
};
State* m_currentState;
Pycron* m_pycron;
GameState* gameState;
public:
explicit StateManager(Pycron* pycron);
void RequestStateChange(StateType state);
void RequestLoadGame();
void RequestRunGame();
void RequestStopGame();
void Draw(Graphics* graphics);
};

View File

@ -1,4 +1,3 @@
using namespace std;
#include "Pycron.h"
int main() {