Ported PixelRenderer class from DragonVM
This commit is contained in:
parent
8fea21fb58
commit
26d32eb059
5 changed files with 105 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
1966
|
||||
1969
|
||||
|
|
|
|||
70
src/ogfx/PixelRenderer.cpp
Normal file
70
src/ogfx/PixelRenderer.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#include "PixelRenderer.hpp"
|
||||
#include "WindowBase.hpp"
|
||||
#include <ostd/Utils.hpp>
|
||||
|
||||
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<uint32_t>(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<uint32_t>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
29
src/ogfx/PixelRenderer.hpp
Normal file
29
src/ogfx/PixelRenderer.hpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#pragma once
|
||||
|
||||
#include <ostd/Color.hpp>
|
||||
#include <ogfx/SDLInclude.hpp>
|
||||
|
||||
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 };
|
||||
};
|
||||
}
|
||||
|
|
@ -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 };
|
||||
|
|
|
|||
Loading…
Reference in a new issue