Editor State foundation

This commit is contained in:
Bobby Lucero 2024-05-13 14:39:52 -04:00
parent cd8ce2e4a0
commit 798ac17939
11 changed files with 115 additions and 35 deletions

View File

@ -41,7 +41,9 @@ add_executable(Pycron src/main.cpp
src/States/GameState.cpp src/States/GameState.cpp
src/States/GameState.h src/States/GameState.h
src/Graphics/Font.cpp src/Graphics/Font.cpp
src/Graphics/Font.h) src/Graphics/Font.h
src/States/EditorState.cpp
src/States/EditorState.h)
add_subdirectory(dependencies/pocketpy) add_subdirectory(dependencies/pocketpy)
# Declaring our executable # Declaring our executable

View File

@ -19,6 +19,11 @@ class Palette(Scene):
rectangle(10 + (i * 20), 10 + (j * 20), 18, 18, (j * 8 + i)) rectangle(10 + (i * 20), 10 + (j * 20), 18, 18, (j * 8 + i))
text(str(j * 8 + i), 11 + (i * 20), 10 + (j * 20), 63) text(str(j * 8 + i), 11 + (i * 20), 10 + (j * 20), 63)
class RGBTest(Scene):
def __init__(self):
super().__init__()
self.title = "RGB To Palette"
scenes = [Particles(), Triangles(), Palette()] scenes = [Particles(), Triangles(), Palette()]
current_scene = 0 current_scene = 0
@ -43,9 +48,8 @@ def update():
if(not scenes[current_scene].paused): if(not scenes[current_scene].paused):
scenes[current_scene].update() scenes[current_scene].update()
scenes[current_scene].draw() scenes[current_scene].draw()
text("if(x != 22.252f && y % 2 == 0) { break; }", 2, 40, 32)
#text("Test" + scenes[current_scene].title, 2, 15, 32)

View File

@ -5,7 +5,7 @@
#include "Graphics.h" #include "Graphics.h"
#include "../Utilities.h" #include "../Utilities.h"
#include <raymath.h> #include <raymath.h>
#include "../StateManager.h"
#include <fstream> #include <fstream>
@ -84,7 +84,6 @@ void Graphics::loadPalette(std::string path) {
int color = stoi(line, nullptr, 16); int color = stoi(line, nullptr, 16);
//Palette.push_back(); //Palette.push_back();
color = color << 8 | 0xFF; color = color << 8 | 0xFF;
m_paletteByColor.insert({color, idx});
m_paletteByID.insert({idx, color}); m_paletteByID.insert({idx, color});
idx++; idx++;
} }
@ -109,6 +108,30 @@ void Graphics::calculateScreenPositionInWindow() {
} }
} }
int Graphics::rgbToID(int r, int g, int b) {
r = Clamp(r, 0, 255);
g = Clamp(g, 0, 255);
b = Clamp(b, 0, 255);
uint8_t idx = 0;
Color c = GetColor(m_paletteByID[idx]);
double minDist = std::pow(r - c.r, 2) +
std::pow(g - c.g, 2) +
std::pow(b - c.b, 2);
for(const auto& paletteColor : m_paletteByID){
c = GetColor(paletteColor.second);
double dist = std::pow(r - c.r, 2) +
std::pow(g - c.g, 2) +
std::pow(b - c.b, 2);
if(dist < minDist){
minDist = dist;
idx = paletteColor.first;
}
}
return idx;
}
int Graphics::mouseX() { int Graphics::mouseX() {
float x = GetMouseX(); float x = GetMouseX();
float adjX = x + m_origin.x; float adjX = x + m_origin.x;
@ -312,9 +335,6 @@ void Graphics::EllipseBorder(int x, int y, int w, int h, int paletteIndex){
} }
void Graphics::Rect(int x, int y, int width, int height, int paletteIndex) { void Graphics::Rect(int x, int y, int width, int height, int paletteIndex) {
for (int i = 0; i < height; ++i) { for (int i = 0; i < height; ++i) {
h_line(x, y + i, x + width - 1, paletteIndex); h_line(x, y + i, x + width - 1, paletteIndex);
@ -520,12 +540,6 @@ void Graphics::Triangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t
} }
void Graphics::swap(float &a, float &b) {
float temp = a;
a = b;
b = temp;
}

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "raylib.h" #include "raylib.h"
#include "pocketpy/vm.h" #include "pocketpy/vm.h"
#include "../StateManager.h"
#include "Font.h" #include "Font.h"
#include <algorithm> #include <algorithm>
@ -31,9 +30,6 @@ private:
PixelFont* m_currentFont; PixelFont* m_currentFont;
PixelFont* m_NormalFont; PixelFont* m_NormalFont;
static void swap(float &a, float &b);
private: private:
void renderVirtualScreen(); void renderVirtualScreen();
void calculateScreenPositionInWindow(); void calculateScreenPositionInWindow();
@ -48,8 +44,6 @@ public:
bool m_windowShouldClose = false; bool m_windowShouldClose = false;
std::unordered_map<unsigned int, unsigned int> m_paletteByColor; // <hex color, id>
std::unordered_map<unsigned int, unsigned int> m_paletteByID; // <id, hex color> std::unordered_map<unsigned int, unsigned int> m_paletteByID; // <id, hex color>
@ -59,6 +53,8 @@ public:
~Graphics(); ~Graphics();
void draw(StateManager* stateManager); void draw(StateManager* stateManager);
int rgbToID(int r, int g, int b);
void copyBufferToGPU(); void copyBufferToGPU();

View File

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

View File

@ -5,7 +5,8 @@
#include "StateManager.h" #include "StateManager.h"
StateManager::StateManager(Pycron *pycron) : m_pycron(pycron){ StateManager::StateManager(Pycron *pycron) : m_pycron(pycron){
m_gameState = new GameState(this, m_pycron->m_vm); m_gameState = new GameState(m_pycron->m_vm);
m_editorState = new EditorState(m_pycron->m_vm);
m_currentState = nullptr; m_currentState = nullptr;
RequestStateChange(GAME); RequestStateChange(GAME);
} }
@ -23,6 +24,8 @@ void StateManager::RequestStateChange(StateManager::StateType state) {
if(state == StateType::GAME){ if(state == StateType::GAME){
m_currentState = m_gameState; m_currentState = m_gameState;
}else if(state == StateType::EDITOR){
m_currentState = m_editorState;
} }
if(m_currentState){ if(m_currentState){
@ -38,6 +41,15 @@ void StateManager::RequestStateChange(StateManager::StateType state) {
} }
void StateManager::Draw(Graphics *graphics) { void StateManager::Draw(Graphics *graphics) {
if(IsKeyPressed(KEY_ENTER)){
if(m_currentState == m_gameState){
RequestStateChange(EDITOR);
}else{
RequestStateChange(GAME);
}
}
if(m_currentState){ if(m_currentState){
if(m_currentState == m_gameState){ if(m_currentState == m_gameState){
if(m_gameState->m_errorThrown){ if(m_gameState->m_errorThrown){

View File

@ -1,14 +1,14 @@
#pragma once #pragma once
#include "State.h"
#include "Pycron.h" #include "Pycron.h"
#include "Graphics/Graphics.h" #include "Graphics/Graphics.h"
#include "States/GameState.h" #include "States/GameState.h"
#include "States/EditorState.h"
class Pycron; class Pycron;
class State;
class Graphics; class Graphics;
class GameState; class GameState;
class EditorState;
class StateManager { class StateManager {
private: private:
@ -21,6 +21,8 @@ private:
State* m_currentState; State* m_currentState;
Pycron* m_pycron; Pycron* m_pycron;
GameState* m_gameState; GameState* m_gameState;
EditorState* m_editorState;
public: public:
explicit StateManager(Pycron* pycron); explicit StateManager(Pycron* pycron);

View File

@ -0,0 +1,33 @@
//
// Created by Bobby on 5/13/2024.
//
#include "EditorState.h"
EditorState::EditorState(pkpy::VM *vm) : m_vm(vm){
}
void EditorState::Draw(Graphics *graphics) {
graphics->Clear(1);
std::string text = "Editor State";
graphics->Text(text, graphics->m_screenWidth / 2 - (text.size() / 2 * 6), graphics->m_screenHeight / 2 - (7 / 2), 5);
for (int y = 0; y < 128; ++y) {
for (int x = 0; x < 128; ++x) {
int c = x;
graphics->Pixel(x, y, graphics->rgbToID(x * 2, y * 2, graphics->mouseX()));
}
}
}
void EditorState::OnEnter() {
}
void EditorState::OnExit() {
}
void EditorState::onKeyPressed(int key) {
}

19
src/States/EditorState.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include "../State.h"
class EditorState : public State{
public:
EditorState(pkpy::VM* vm);
void Draw(Graphics* graphics) override;
void OnEnter() override;
void OnExit() override;
void onKeyPressed(int key) override;
private:
pkpy::VM* m_vm;
};

View File

@ -6,8 +6,9 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <filesystem> #include <filesystem>
#include "../Pycron.h"
GameState::GameState(StateManager *stateManager, pkpy::VM* vm) : State(stateManager), m_vm(vm){ GameState::GameState(pkpy::VM* vm) :m_vm(vm){
m_updateFunction = nullptr; m_updateFunction = nullptr;
m_previousError = ""; m_previousError = "";
} }
@ -29,7 +30,8 @@ void GameState::OnEnter() {
} }
void GameState::OnExit() { void GameState::OnExit() {
m_vm->_lazy_modules.clear();
m_updateFunction = m_vm->None;
} }
void GameState::onKeyPressed(int key) { void GameState::onKeyPressed(int key) {

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "../State.h" #include "../State.h"
class GameState : public State {
class GameState : public State {
public: public:
@ -8,7 +9,7 @@ public:
std::string m_previousError; std::string m_previousError;
bool m_errorThrown = false; bool m_errorThrown = false;
GameState(StateManager* stateManager, pkpy::VM* vm); GameState(pkpy::VM* vm);
void Draw(Graphics* graphics) override; void Draw(Graphics* graphics) override;
void OnEnter() override; void OnEnter() override;