diff --git a/src/Graphics/Graphics.cpp b/src/Graphics/Graphics.cpp index 6302d03..8d3b591 100644 --- a/src/Graphics/Graphics.cpp +++ b/src/Graphics/Graphics.cpp @@ -74,7 +74,7 @@ void Graphics::renderVirtualScreen() { BeginDrawing(); ClearBackground(BLACK); DrawTexturePro(m_virtualScreen.texture, m_virtualScreenLocalBounds, m_virtualScreenWindowBounds, m_origin, 0.0f, WHITE); - DrawText(std::to_string(GetFPS()).c_str(), 10, 10, 30, YELLOW); + //DrawText(std::to_string(GetFPS()).c_str(), 10, 10, 30, YELLOW); EndDrawing(); } @@ -358,8 +358,6 @@ void Graphics::Text(const std::string& s, int x, int y, int paletteIndex) { for (int i = 0; i < s.size(); ++i) { char c = s[i]; std::string bitData = m_currentFont->GetCharData((int)c); - - //std::cout << c << ": " << (int)c << " = " << bitData << std::endl; for (int j = 0; j < bitData.size(); ++j) { if(bitData[j] == '1') Pixel(x + (j % m_currentFont->GetWidth()) + ((m_currentFont->GetWidth() + 1) * i), y + (j / m_currentFont->GetWidth()), paletteIndex); @@ -368,6 +366,15 @@ void Graphics::Text(const std::string& s, int x, int y, int paletteIndex) { } } +void Graphics::Char(char c, int x, int y, int paletteIndex) { + std::string bitData = m_currentFont->GetCharData((int)c); + for (int j = 0; j < bitData.size(); ++j) { + if(bitData[j] == '1') + Pixel(x + (j % m_currentFont->GetWidth()), y + (j / m_currentFont->GetWidth()), paletteIndex); + + } +} + void Graphics::updateVMVars(pkpy::VM* vm) { vm->builtins->attr().set("mouseX", pkpy::py_var(vm, mouseX())); vm->builtins->attr().set("mouseY", pkpy::py_var(vm, mouseY())); @@ -569,7 +576,12 @@ void Graphics::Img(PycronImage* img, int x, int y) { } } - +int Graphics::GetCurrentFontWidth(){ + return m_currentFont->GetWidth(); +} +int Graphics::GetCurrentFontHeight(){ + return m_currentFont->GetHeight(); +} diff --git a/src/Graphics/Graphics.h b/src/Graphics/Graphics.h index 8edede1..7b38ee7 100644 --- a/src/Graphics/Graphics.h +++ b/src/Graphics/Graphics.h @@ -22,14 +22,14 @@ private: int m_windowWidth; int m_windowHeight; - Rectangle m_virtualScreenWindowBounds; // size of rect texture on window - Vector2 m_origin; // position of rect texture on window - Rectangle m_virtualScreenLocalBounds; // virtual screen bounds - RenderTexture2D m_virtualScreen; + Rectangle m_virtualScreenWindowBounds; // size of rect texture on window + Vector2 m_origin; // position of rect texture on window + Rectangle m_virtualScreenLocalBounds; // virtual screen bounds + RenderTexture2D m_virtualScreen; std::vector m_virtualScreenColorBuffer; - Image m_virtualScreenImageBuffer; - PixelFont* m_currentFont; - PixelFont* m_NormalFont; + Image m_virtualScreenImageBuffer; + PixelFont* m_currentFont; + PixelFont* m_NormalFont; private: void renderVirtualScreen(); @@ -76,9 +76,13 @@ public: void Rect(int x, int y, int width, int height, int paletteIndex); 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 Char(char c, 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); + int GetCurrentFontWidth(); + int GetCurrentFontHeight(); + }; diff --git a/src/Graphics/PycronImage.h b/src/Graphics/PycronImage.h index 1957091..309c62b 100644 --- a/src/Graphics/PycronImage.h +++ b/src/Graphics/PycronImage.h @@ -1,6 +1,7 @@ #pragma once #include +#include struct PycronImage { int width; diff --git a/src/Pycron.cpp b/src/Pycron.cpp index b87d26a..2a84668 100644 --- a/src/Pycron.cpp +++ b/src/Pycron.cpp @@ -109,8 +109,8 @@ pkpy::PyObject* Pycron::getRandomNumber(pkpy::VM* vm, pkpy::ArgsView args) { float min = pkpy::py_cast(vm, args[0]); float max = pkpy::py_cast(vm, args[1]); // Seed the random number generator with a random device - std::random_device rd; - std::mt19937 gen(rd()); + static std::random_device rd; + static std::mt19937 gen(rd()); // Define a uniform distribution for the range [min, max] std::uniform_real_distribution distribution(min, max); diff --git a/src/State.h b/src/State.h index 3202da0..35cdb2f 100644 --- a/src/State.h +++ b/src/State.h @@ -11,5 +11,7 @@ public: virtual void Draw() = 0; virtual void OnEnter() = 0; virtual void OnExit() = 0; - virtual void onKeyPressed(int key) = 0; + virtual void OnKeyPressed(int key) = 0; + virtual void OnCharPressed(char character) = 0; + }; diff --git a/src/States/EditorState.cpp b/src/States/EditorState.cpp index a7bf9f0..48b9dc5 100644 --- a/src/States/EditorState.cpp +++ b/src/States/EditorState.cpp @@ -5,8 +5,23 @@ #include "EditorState.h" #include "../Graphics/PycronImage.h" + EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphics(graphics){ test = m_graphics->loadImage("resources/plane2.png"); + + m_horizontalLetterSpacing = 1; + m_verticalLetterSpacing = 1; + + m_charWidth = m_graphics->GetCurrentFontWidth() + m_horizontalLetterSpacing; // Final size of char with spacing. If the literal width of the font is n, the final width is n + spacing. + m_charHeight = m_graphics->GetCurrentFontHeight() + m_verticalLetterSpacing; + + m_textWidth = (int)(m_graphics->m_screenWidth / m_charWidth); + m_textHeight = (int)(m_graphics->m_screenHeight / m_charHeight); + + m_characterBuffer = std::vector(m_textWidth * m_textHeight); + + Clear(); + } EditorState::~EditorState() { @@ -16,15 +31,13 @@ EditorState::~EditorState() { void EditorState::Draw() { m_graphics->Clear(1); std::string text = "Editor State"; - 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; - m_graphics->Pixel(x, y, m_graphics->rgbToID(x * 2, y * 2, m_graphics->mouseX())); - } + + for (int i = 0; i < m_characterBuffer.size(); ++i) { + m_graphics->Char(m_characterBuffer[i], (i % m_textWidth) * m_charWidth, (i / m_textWidth) * m_charHeight, 5); } - std::cout << test->width << "\n"; - m_graphics->Img(test, m_graphics->mouseX() - (test->height / 2), m_graphics->mouseY() - (test->width / 2)); + Clear(); + Text("This is a big long test!!!@%)(!*@#%PALSKDGJ", ((float)m_graphics->mouseX() / m_graphics->m_screenWidth) * m_textWidth, ((float)m_graphics->mouseY() / m_graphics->m_screenHeight) * m_textHeight); + } void EditorState::OnEnter() { @@ -35,6 +48,30 @@ void EditorState::OnExit() { } -void EditorState::onKeyPressed(int key) { +void EditorState::OnKeyPressed(int key) { + std::cout << key << ". \n"; +} + +void EditorState::Clear() { + for (int i = 0; i < m_textWidth * m_textHeight; ++i) { + m_characterBuffer[i] = ' '; + } +} + +void EditorState::Text(const std::string &text, int x, int y) { + for (int i = 0; i < text.size(); ++i) { + int charX = x + i; + int charY = y; + if(charX < 0 || charY < 0 || charX >= m_textWidth || charY >= m_textHeight){ + return; + } + char c = text[i]; + m_characterBuffer[y * m_textWidth + (x + i)] = c; + } +} + +void EditorState::OnCharPressed(char character) { } + + diff --git a/src/States/EditorState.h b/src/States/EditorState.h index 00ee759..113d60a 100644 --- a/src/States/EditorState.h +++ b/src/States/EditorState.h @@ -12,12 +12,20 @@ public: void Draw() override; void OnEnter() override; void OnExit() override; - void onKeyPressed(int key) override; + void OnKeyPressed(int key) override; + void OnCharPressed(char character) override; private: pkpy::VM* m_vm; PycronImage* test; Graphics* m_graphics; + int m_charWidth, m_charHeight; + int m_textWidth, m_textHeight; + int m_horizontalLetterSpacing, m_verticalLetterSpacing; + std::vector m_characterBuffer; + + void Clear(); + void Text(const std::string& text, int x, int y); }; diff --git a/src/States/GameState.cpp b/src/States/GameState.cpp index d836d79..1e12fbb 100644 --- a/src/States/GameState.cpp +++ b/src/States/GameState.cpp @@ -34,7 +34,7 @@ void GameState::OnExit() { m_updateFunction = m_vm->None; } -void GameState::onKeyPressed(int key) { +void GameState::OnKeyPressed(int key) { } @@ -98,3 +98,7 @@ void GameState::loadPythonModules(std::unordered_map & } } +void GameState::OnCharPressed(char character) { + +} + diff --git a/src/States/GameState.h b/src/States/GameState.h index 3e96fec..8a100cc 100644 --- a/src/States/GameState.h +++ b/src/States/GameState.h @@ -14,7 +14,9 @@ public: void Draw() override; void OnEnter() override; void OnExit() override; - void onKeyPressed(int key) override; + void OnKeyPressed(int key) override; + void OnCharPressed(char character) override; + private: