diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dbc43a..2f1cf78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,9 @@ add_executable(Pycron src/main.cpp src/States/GameState.cpp src/States/GameState.h src/Graphics/Font.cpp - src/Graphics/Font.h) + src/Graphics/Font.h + src/States/EditorState.cpp + src/States/EditorState.h) add_subdirectory(dependencies/pocketpy) # Declaring our executable diff --git a/python/main.py b/python/main.py index a162207..abcc9f4 100644 --- a/python/main.py +++ b/python/main.py @@ -19,6 +19,11 @@ class Palette(Scene): rectangle(10 + (i * 20), 10 + (j * 20), 18, 18, (j * 8 + i)) 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()] current_scene = 0 @@ -43,9 +48,8 @@ def update(): if(not scenes[current_scene].paused): scenes[current_scene].update() - 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) + scenes[current_scene].draw() + diff --git a/src/Graphics/Graphics.cpp b/src/Graphics/Graphics.cpp index fb53514..4a7457b 100644 --- a/src/Graphics/Graphics.cpp +++ b/src/Graphics/Graphics.cpp @@ -5,7 +5,7 @@ #include "Graphics.h" #include "../Utilities.h" #include - +#include "../StateManager.h" #include @@ -84,7 +84,6 @@ void Graphics::loadPalette(std::string path) { int color = stoi(line, nullptr, 16); //Palette.push_back(); color = color << 8 | 0xFF; - m_paletteByColor.insert({color, idx}); m_paletteByID.insert({idx, color}); 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() { float x = GetMouseX(); 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) { for (int i = 0; i < height; ++i) { 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; -} - diff --git a/src/Graphics/Graphics.h b/src/Graphics/Graphics.h index 8b18e5d..e0bf99c 100644 --- a/src/Graphics/Graphics.h +++ b/src/Graphics/Graphics.h @@ -1,7 +1,6 @@ #pragma once #include "raylib.h" #include "pocketpy/vm.h" -#include "../StateManager.h" #include "Font.h" #include @@ -31,9 +30,6 @@ private: PixelFont* m_currentFont; PixelFont* m_NormalFont; - static void swap(float &a, float &b); - - private: void renderVirtualScreen(); void calculateScreenPositionInWindow(); @@ -48,8 +44,6 @@ public: bool m_windowShouldClose = false; - - std::unordered_map m_paletteByColor; // std::unordered_map m_paletteByID; // @@ -59,6 +53,8 @@ public: ~Graphics(); void draw(StateManager* stateManager); + int rgbToID(int r, int g, int b); + void copyBufferToGPU(); diff --git a/src/State.h b/src/State.h index d795dc5..f124255 100644 --- a/src/State.h +++ b/src/State.h @@ -1,16 +1,11 @@ #pragma once -#include "Graphics/Graphics.h" -#include "StateManager.h" -class Graphics; -class StateManager; +#include "Graphics/Graphics.h" class State { -private: - StateManager* m_stateManager; protected: - explicit State(StateManager* stateManager) : m_stateManager(stateManager){} + State(){} public: virtual ~State() = default; virtual void Draw(Graphics* graphics) = 0; diff --git a/src/StateManager.cpp b/src/StateManager.cpp index daaef33..ebca8ec 100644 --- a/src/StateManager.cpp +++ b/src/StateManager.cpp @@ -5,7 +5,8 @@ #include "StateManager.h" 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; RequestStateChange(GAME); } @@ -23,6 +24,8 @@ void StateManager::RequestStateChange(StateManager::StateType state) { if(state == StateType::GAME){ m_currentState = m_gameState; + }else if(state == StateType::EDITOR){ + m_currentState = m_editorState; } if(m_currentState){ @@ -38,6 +41,15 @@ void StateManager::RequestStateChange(StateManager::StateType state) { } 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 == m_gameState){ if(m_gameState->m_errorThrown){ diff --git a/src/StateManager.h b/src/StateManager.h index 7ee78b4..14117ce 100644 --- a/src/StateManager.h +++ b/src/StateManager.h @@ -1,14 +1,14 @@ #pragma once -#include "State.h" #include "Pycron.h" #include "Graphics/Graphics.h" #include "States/GameState.h" +#include "States/EditorState.h" class Pycron; -class State; class Graphics; class GameState; +class EditorState; class StateManager { private: @@ -21,6 +21,8 @@ private: State* m_currentState; Pycron* m_pycron; GameState* m_gameState; + EditorState* m_editorState; + public: explicit StateManager(Pycron* pycron); diff --git a/src/States/EditorState.cpp b/src/States/EditorState.cpp new file mode 100644 index 0000000..b210ef9 --- /dev/null +++ b/src/States/EditorState.cpp @@ -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) { + +} diff --git a/src/States/EditorState.h b/src/States/EditorState.h new file mode 100644 index 0000000..bcdacc7 --- /dev/null +++ b/src/States/EditorState.h @@ -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; + + +}; + diff --git a/src/States/GameState.cpp b/src/States/GameState.cpp index f1fc22e..f2e76d1 100644 --- a/src/States/GameState.cpp +++ b/src/States/GameState.cpp @@ -6,8 +6,9 @@ #include #include #include +#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_previousError = ""; } @@ -29,7 +30,8 @@ void GameState::OnEnter() { } void GameState::OnExit() { - + m_vm->_lazy_modules.clear(); + m_updateFunction = m_vm->None; } void GameState::onKeyPressed(int key) { diff --git a/src/States/GameState.h b/src/States/GameState.h index 332d78b..1188871 100644 --- a/src/States/GameState.h +++ b/src/States/GameState.h @@ -1,6 +1,7 @@ #pragma once #include "../State.h" -class GameState : public State { + +class GameState : public State { public: @@ -8,7 +9,7 @@ public: std::string m_previousError; bool m_errorThrown = false; - GameState(StateManager* stateManager, pkpy::VM* vm); + GameState(pkpy::VM* vm); void Draw(Graphics* graphics) override; void OnEnter() override;