diff --git a/.gitignore b/.gitignore index 997a6bb..a108108 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ cmake-build-debug .vscode cmake-build-release build +.DS_Store diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f1cf78..86dff94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,9 @@ add_executable(Pycron src/main.cpp src/Graphics/Font.cpp src/Graphics/Font.h src/States/EditorState.cpp - src/States/EditorState.h) + src/States/EditorState.h + src/Graphics/PycronImage.h + src/Graphics/PycronImage.cpp) add_subdirectory(dependencies/pocketpy) # Declaring our executable diff --git a/resources/Sprite-0001.png b/resources/Sprite-0001.png new file mode 100644 index 0000000..6580b3c Binary files /dev/null and b/resources/Sprite-0001.png differ diff --git a/resources/pc.png b/resources/pc.png new file mode 100644 index 0000000..e0a1457 Binary files /dev/null and b/resources/pc.png differ diff --git a/resources/plane.png b/resources/plane.png new file mode 100644 index 0000000..6f593d4 Binary files /dev/null and b/resources/plane.png differ diff --git a/resources/plane2.png b/resources/plane2.png new file mode 100644 index 0000000..962b41c Binary files /dev/null and b/resources/plane2.png differ diff --git a/resources/rgb.png b/resources/rgb.png new file mode 100644 index 0000000..7d64924 Binary files /dev/null and b/resources/rgb.png differ diff --git a/resources/the-cosmos-32x.png b/resources/the-cosmos-32x.png new file mode 100644 index 0000000..b91d617 Binary files /dev/null and b/resources/the-cosmos-32x.png differ diff --git a/src/Graphics/Graphics.cpp b/src/Graphics/Graphics.cpp index 4a7457b..6302d03 100644 --- a/src/Graphics/Graphics.cpp +++ b/src/Graphics/Graphics.cpp @@ -3,10 +3,15 @@ // #include "Graphics.h" +#include "PycronImage.h" +#include "Font.h" #include "../Utilities.h" -#include #include "../StateManager.h" + #include +#include + + Graphics::Graphics(int screenWidth, int screenHeight, int startupScale) : m_screenWidth(screenWidth), m_screenHeight(screenHeight){ @@ -51,7 +56,7 @@ void Graphics::draw(StateManager* stateManager) { calculateScreenPositionInWindow(); } - stateManager->Draw(this); + stateManager->Draw(); copyBufferToGPU(); renderVirtualScreen(); } @@ -540,6 +545,30 @@ void Graphics::Triangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t } +PycronImage* Graphics::loadImage(std::string path) { + Image img = LoadImage(path.c_str()); + ImageRotateCW(&img); + ImageFlipHorizontal(&img); + if(img.width == 0 && img.height == 0) std::cerr << "Image at path " << path << " is empty\n"; + Color* colors = LoadImageColors(img); + auto* idImg = new PycronImage(img.width, img.height); + for (int i = 0; i < idImg->data.size(); ++i) { + if(colors[i].a == 0){ + idImg->data[i] = -1; + }else{ + idImg->data[i] = (int8_t)rgbToID(colors[i].r, colors[i].g, colors[i].b); + } + } + return idImg; +} + +void Graphics::Img(PycronImage* img, int x, int y) { + for (int i = 0; i < img->data.size(); ++i) { + if(img->data[i] == -1) continue; + Pixel(x + i / img->width, y + i % img->width, img->data[i]); + } +} + diff --git a/src/Graphics/Graphics.h b/src/Graphics/Graphics.h index e0bf99c..8edede1 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 "Font.h" #include #include @@ -10,6 +9,8 @@ class StateManager; +class PycronImage; +class PixelFont; class Graphics { @@ -46,18 +47,18 @@ public: std::unordered_map m_paletteByID; // - - public: + Graphics(int screenWidth, int screenHeight, int startupScale); ~Graphics(); void draw(StateManager* stateManager); int rgbToID(int r, int g, int b); - void copyBufferToGPU(); + PycronImage* loadImage(std::string path); + void updateVMVars(pkpy::VM* vm); void loadPalette(std::string path); @@ -76,7 +77,7 @@ public: void RectBorder(int x, int y, int width, int height, int paletteIndex); void Text(const std::string& s, int x, int y, int paletteIndex); void Triangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, int paletteIndex); - + void Img(PycronImage* img, int x, int y); int GetPixel(int x, int y); diff --git a/src/Graphics/PycronImage.cpp b/src/Graphics/PycronImage.cpp new file mode 100644 index 0000000..8841a38 --- /dev/null +++ b/src/Graphics/PycronImage.cpp @@ -0,0 +1,8 @@ +// +// Created by Bobby Lucero on 5/22/24. +// + +#include "PycronImage.h" + +PycronImage::PycronImage(int width, int height) : width(width), height(height), data(width * height) { +} diff --git a/src/Graphics/PycronImage.h b/src/Graphics/PycronImage.h new file mode 100644 index 0000000..1957091 --- /dev/null +++ b/src/Graphics/PycronImage.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +struct PycronImage { + int width; + int height; + std::vector data; + + PycronImage(int width, int height); +}; + diff --git a/src/Pycron.cpp b/src/Pycron.cpp index 79fd60d..b87d26a 100644 --- a/src/Pycron.cpp +++ b/src/Pycron.cpp @@ -32,7 +32,7 @@ Pycron::Pycron() { m_graphics->loadPalette("resources/thecosmos.hex"); m_graphics->bindMethods(m_vm); m_graphics->updateVMVars(m_vm); - m_stateManager = new StateManager(this); + m_stateManager = new StateManager(this, m_graphics); diff --git a/src/State.h b/src/State.h index f124255..3202da0 100644 --- a/src/State.h +++ b/src/State.h @@ -8,7 +8,7 @@ protected: State(){} public: virtual ~State() = default; - virtual void Draw(Graphics* graphics) = 0; + virtual void Draw() = 0; virtual void OnEnter() = 0; virtual void OnExit() = 0; virtual void onKeyPressed(int key) = 0; diff --git a/src/StateManager.cpp b/src/StateManager.cpp index ebca8ec..29fe132 100644 --- a/src/StateManager.cpp +++ b/src/StateManager.cpp @@ -4,9 +4,9 @@ #include "StateManager.h" -StateManager::StateManager(Pycron *pycron) : m_pycron(pycron){ - m_gameState = new GameState(m_pycron->m_vm); - m_editorState = new EditorState(m_pycron->m_vm); +StateManager::StateManager(Pycron *pycron, Graphics *graphics) : m_pycron(pycron){ + m_gameState = new GameState(m_pycron->m_vm, graphics); + m_editorState = new EditorState(m_pycron->m_vm, graphics); m_currentState = nullptr; RequestStateChange(GAME); } @@ -14,6 +14,8 @@ StateManager::StateManager(Pycron *pycron) : m_pycron(pycron){ StateManager::~StateManager() { m_currentState = nullptr; delete m_gameState; + delete m_editorState; + delete m_graphics; } @@ -40,7 +42,7 @@ void StateManager::RequestStateChange(StateManager::StateType state) { } } -void StateManager::Draw(Graphics *graphics) { +void StateManager::Draw() { if(IsKeyPressed(KEY_ENTER)){ if(m_currentState == m_gameState){ @@ -56,11 +58,11 @@ void StateManager::Draw(Graphics *graphics) { m_pycron->m_graphics->Text(m_gameState->m_previousError, 2, 2, 59); } else{ - m_currentState->Draw(graphics); + m_currentState->Draw(); } } else{ - m_currentState->Draw(graphics); + m_currentState->Draw(); } } } diff --git a/src/StateManager.h b/src/StateManager.h index 14117ce..62920d4 100644 --- a/src/StateManager.h +++ b/src/StateManager.h @@ -22,10 +22,11 @@ private: Pycron* m_pycron; GameState* m_gameState; EditorState* m_editorState; + Graphics* m_graphics; public: - explicit StateManager(Pycron* pycron); + explicit StateManager(Pycron* pycron, Graphics* graphics); ~StateManager(); void RequestStateChange(StateType state); @@ -34,7 +35,7 @@ public: void RequestRunGame(); void RequestStopGame(); - void Draw(Graphics* graphics); + void Draw(); }; diff --git a/src/States/EditorState.cpp b/src/States/EditorState.cpp index b210ef9..a7bf9f0 100644 --- a/src/States/EditorState.cpp +++ b/src/States/EditorState.cpp @@ -3,21 +3,28 @@ // #include "EditorState.h" +#include "../Graphics/PycronImage.h" -EditorState::EditorState(pkpy::VM *vm) : m_vm(vm){ - +EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphics(graphics){ + test = m_graphics->loadImage("resources/plane2.png"); } -void EditorState::Draw(Graphics *graphics) { - graphics->Clear(1); +EditorState::~EditorState() { + delete test; +} + +void EditorState::Draw() { + m_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); + m_graphics->Text(text, m_graphics->m_screenWidth / 2 - (text.size() / 2 * 6), m_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())); + m_graphics->Pixel(x, y, m_graphics->rgbToID(x * 2, y * 2, m_graphics->mouseX())); } } + std::cout << test->width << "\n"; + m_graphics->Img(test, m_graphics->mouseX() - (test->height / 2), m_graphics->mouseY() - (test->width / 2)); } void EditorState::OnEnter() { diff --git a/src/States/EditorState.h b/src/States/EditorState.h index bcdacc7..00ee759 100644 --- a/src/States/EditorState.h +++ b/src/States/EditorState.h @@ -1,18 +1,23 @@ #pragma once #include "../State.h" +class PycronImage; + class EditorState : public State{ public: - EditorState(pkpy::VM* vm); + EditorState(pkpy::VM* vm, Graphics* graphics); + ~EditorState(); - void Draw(Graphics* graphics) override; + void Draw() override; void OnEnter() override; void OnExit() override; void onKeyPressed(int key) override; private: pkpy::VM* m_vm; + PycronImage* test; + Graphics* m_graphics; }; diff --git a/src/States/GameState.cpp b/src/States/GameState.cpp index f2e76d1..d836d79 100644 --- a/src/States/GameState.cpp +++ b/src/States/GameState.cpp @@ -8,12 +8,12 @@ #include #include "../Pycron.h" -GameState::GameState(pkpy::VM* vm) :m_vm(vm){ +GameState::GameState(pkpy::VM* vm, Graphics* graphics) :m_vm(vm), m_graphics(graphics){ m_updateFunction = nullptr; m_previousError = ""; } -void GameState::Draw(Graphics *graphics) { +void GameState::Draw() { try{ if(m_updateFunction != nullptr){ m_vm->call(m_updateFunction); diff --git a/src/States/GameState.h b/src/States/GameState.h index 1188871..3e96fec 100644 --- a/src/States/GameState.h +++ b/src/States/GameState.h @@ -9,9 +9,9 @@ public: std::string m_previousError; bool m_errorThrown = false; - GameState(pkpy::VM* vm); + GameState(pkpy::VM* vm, Graphics* graphics); - void Draw(Graphics* graphics) override; + void Draw() override; void OnEnter() override; void OnExit() override; void onKeyPressed(int key) override; @@ -20,7 +20,7 @@ private: pkpy::VM* m_vm; pkpy::PyObject* m_updateFunction; - + Graphics* m_graphics; std::unordered_map readPythonFiles(const std::string& dir); void loadPythonModules(std::unordered_map& fileContents);