Editor State foundation
This commit is contained in:
parent
cd8ce2e4a0
commit
798ac17939
@ -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
|
||||
|
||||
@ -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
|
||||
@ -44,8 +49,7 @@ 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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include "Graphics.h"
|
||||
#include "../Utilities.h"
|
||||
#include <raymath.h>
|
||||
|
||||
#include "../StateManager.h"
|
||||
#include <fstream>
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
#include "raylib.h"
|
||||
#include "pocketpy/vm.h"
|
||||
#include "../StateManager.h"
|
||||
#include "Font.h"
|
||||
|
||||
#include <algorithm>
|
||||
@ -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<unsigned int, unsigned int> m_paletteByColor; // <hex color, id>
|
||||
std::unordered_map<unsigned int, unsigned int> m_paletteByID; // <id, hex color>
|
||||
|
||||
|
||||
@ -59,6 +53,8 @@ public:
|
||||
~Graphics();
|
||||
|
||||
void draw(StateManager* stateManager);
|
||||
int rgbToID(int r, int g, int b);
|
||||
|
||||
|
||||
void copyBufferToGPU();
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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){
|
||||
|
||||
@ -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);
|
||||
|
||||
33
src/States/EditorState.cpp
Normal file
33
src/States/EditorState.cpp
Normal 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
19
src/States/EditorState.h
Normal 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;
|
||||
|
||||
|
||||
};
|
||||
|
||||
@ -6,8 +6,9 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#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_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) {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "../State.h"
|
||||
|
||||
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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user