From 26d32eb059ac8d5ad4bead967e492251c35d6a59 Mon Sep 17 00:00:00 2001 From: OmniaX-Dev Date: Thu, 5 Mar 2026 21:45:05 +0100 Subject: [PATCH] Ported PixelRenderer class from DragonVM --- CMakeLists.txt | 1 + other/build.nr | 2 +- src/ogfx/PixelRenderer.cpp | 70 ++++++++++++++++++++++++++++++++++++++ src/ogfx/PixelRenderer.hpp | 29 ++++++++++++++++ src/ogfx/WindowBase.hpp | 6 ++-- 5 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 src/ogfx/PixelRenderer.cpp create mode 100644 src/ogfx/PixelRenderer.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e20a938..1a3d305 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,7 @@ list(APPEND OGFX_SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/RawTextInput.cpp ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/Image.cpp ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/WindowBaseOutputHandler.cpp + ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/PixelRenderer.cpp ) list(APPEND TEST_SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/src/test.cpp diff --git a/other/build.nr b/other/build.nr index 761051d..ec3cc3d 100644 --- a/other/build.nr +++ b/other/build.nr @@ -1 +1 @@ -1966 +1969 diff --git a/src/ogfx/PixelRenderer.cpp b/src/ogfx/PixelRenderer.cpp new file mode 100644 index 0000000..baba8ce --- /dev/null +++ b/src/ogfx/PixelRenderer.cpp @@ -0,0 +1,70 @@ +#include "PixelRenderer.hpp" +#include "WindowBase.hpp" +#include + +namespace ogfx +{ + PixelRenderer::~PixelRenderer(void) + { + if (isInvalid()) return; + ostd::Utils::destroyArray(m_pixels); + SDL_DestroyTexture(m_texture); + } + + void PixelRenderer::initialize(WindowBase& parent) + { + if (isValid()) return; //TODO: Error + if (!parent.isValid() || !parent.isInitialized()) + return; //TODO: Error + m_parent = &parent; + m_pixels = ostd::Utils::createArray(parent.getWindowWidth() * parent.getWindowHeight()); + m_texture = SDL_CreateTexture(parent.getSDLRenderer(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, parent.getWindowWidth(), parent.getWindowHeight()); + m_windowWidth = parent.getWindowWidth(); + m_windowHeight = parent.getWindowHeight(); + setTypeName("ogfx::PixelRenderer"); + enableSignals(); + connectSignal(ostd::tBuiltinSignals::WindowResized); + validate(); + } + + void PixelRenderer::handleSignal(ostd::tSignal& signal) + { + if (isInvalid()) return; + if (signal.ID == ostd::tBuiltinSignals::WindowResized) + { + m_pixels = ostd::Utils::resizeArray(m_pixels, m_parent->getWindowWidth() * m_parent->getWindowHeight()); + SDL_DestroyTexture(m_texture); + m_texture = SDL_CreateTexture(m_parent->getSDLRenderer(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, m_parent->getWindowWidth(), m_parent->getWindowHeight()); + m_windowWidth = m_parent->getWindowWidth(); + m_windowHeight = m_parent->getWindowHeight(); + updateBuffer(); + displayBuffer(); + } + } + + void PixelRenderer::updateBuffer(void) + { + if (isInvalid()) return; + SDL_UpdateTexture(m_texture, NULL, m_pixels, m_windowWidth * 4); + } + + void PixelRenderer::displayBuffer(void) + { + if (isInvalid()) return; + SDL_Rect rect { 0, 0, m_windowWidth, m_windowHeight }; + SDL_RenderCopy(m_parent->getSDLRenderer(), m_texture, NULL, &rect); + } + + void PixelRenderer::clear(const ostd::Color& color) + { + if (isInvalid()) return; + for (int32_t y = 0; y < m_windowHeight; y++) + { + for (uint32_t x = 0; x < m_windowWidth; x++) + { + int32_t index = CONVERT_2D_1D(x, y, m_windowWidth); + m_pixels[index] = color.asInteger(ostd::Color::eColorFormat::ARGB); + } + } + } +} diff --git a/src/ogfx/PixelRenderer.hpp b/src/ogfx/PixelRenderer.hpp new file mode 100644 index 0000000..7fe14b6 --- /dev/null +++ b/src/ogfx/PixelRenderer.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +namespace ogfx +{ + class WindowBase; + class PixelRenderer : public ostd::BaseObject + { + public: + inline PixelRenderer(void) { invalidate(); } + ~PixelRenderer(void); + void initialize(WindowBase& parent); + void handleSignal(ostd::tSignal& signal) override; + void updateBuffer(void); + void displayBuffer(void); + inline uint32_t* getScreenPixels(void) { return m_pixels; } + + void clear(const ostd::Color& color); + + private: + uint32_t* m_pixels { nullptr }; + SDL_Texture* m_texture { nullptr }; + WindowBase* m_parent { nullptr }; + int32_t m_windowWidth { 0 }; + int32_t m_windowHeight { 0 }; + }; +} diff --git a/src/ogfx/WindowBase.hpp b/src/ogfx/WindowBase.hpp index 68e68e8..3a0a35a 100644 --- a/src/ogfx/WindowBase.hpp +++ b/src/ogfx/WindowBase.hpp @@ -31,8 +31,9 @@ namespace ogfx inline bool isInitialized(void) const { return m_initialized; } inline bool isRunning(void) const { return m_running; } - inline void hide(void) { SDL_HideWindow(m_window); } - inline void show(void) { SDL_ShowWindow(m_window); } + inline bool isVisible(void) const { return m_visible; } + inline void hide(void) { SDL_HideWindow(m_window); m_visible = false; } + inline void show(void) { SDL_ShowWindow(m_window); m_visible = true; } inline ostd::String getTitle(void) const { return m_title; } inline int32_t getFPS(void) const { return m_fps; } inline int32_t getWindowWidth(void) const { return m_windowWidth; } @@ -69,6 +70,7 @@ namespace ogfx bool m_deagEventEnabled { false }; bool m_running { false }; bool m_initialized { false }; + bool m_visible { true }; SDL_Cursor* m_cursor_IBeam { nullptr }; SDL_Cursor* m_cursor_Arrow { nullptr };