From 03b1d7b5f79d46b1fb2f012e68ca86644a2af321 Mon Sep 17 00:00:00 2001 From: OmniaX-Dev Date: Tue, 8 Oct 2024 23:42:33 +0200 Subject: [PATCH] Added ogfx::Image class --- CMakeLists.txt | 3 ++- build.nr | 2 +- src/ogfx/BasicRenderer.cpp | 16 ++++++++++++++-- src/ogfx/BasicRenderer.hpp | 5 ++++- src/ogfx/Image.cpp | 28 ++++++++++++++++++++++++++++ src/ogfx/Image.hpp | 28 ++++++++++++++++++++++++++++ src/ogfx/SDLInclude.hpp | 1 + 7 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 src/ogfx/Image.cpp create mode 100644 src/ogfx/Image.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1fb2dbd..dce1209 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ list(APPEND OGFX_SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/WindowBase.cpp ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/BasicRenderer.cpp ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/RawTextInput.cpp + ${CMAKE_CURRENT_LIST_DIR}/src/ogfx/Image.cpp ) list(APPEND TEST_SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/src/test.cpp @@ -88,7 +89,7 @@ if (UNIX) target_link_libraries(${OMNIA_STD_LIB} PUBLIC tinfo boost_regex) target_link_libraries(${OMNIA_GFX_LIB} PUBLIC xcb xcb-randr) endif (UNIX) -target_link_libraries(${OMNIA_GFX_LIB} PUBLIC SDL2main SDL2 SDL2_gfx SDL2_ttf) +target_link_libraries(${OMNIA_GFX_LIB} PUBLIC SDL2main SDL2 SDL2_gfx SDL2_ttf SDL2_image) target_link_libraries(${OMNIA_STD_LIB} PUBLIC ssl crypto) #----------------------------------------------------------------------------------------- diff --git a/build.nr b/build.nr index 0b90508..c34a5b9 100644 --- a/build.nr +++ b/build.nr @@ -1 +1 @@ -1883 +1885 diff --git a/src/ogfx/BasicRenderer.cpp b/src/ogfx/BasicRenderer.cpp index 2858290..608ba95 100644 --- a/src/ogfx/BasicRenderer.cpp +++ b/src/ogfx/BasicRenderer.cpp @@ -1,6 +1,6 @@ #include "BasicRenderer.hpp" #include "WindowBase.hpp" -#include +#include "SDLInclude.hpp" namespace ogfx { @@ -23,6 +23,18 @@ namespace ogfx m_ttfr.setFontSize(fontSize); } + void BasicRenderer2D::drawImage(const ogfx::Image& image, const ostd::Vec2& position) + { + if (!m_initialized) return; + if (!image.isLoaded()) return; + SDL_Rect texr; + texr.x = position.x; + texr.y = position.y; + texr.w = image.getSize().x; + texr.h = image.getSize().y; + SDL_RenderCopy(m_window->getSDLRenderer(), image.getSDLTexture(), nullptr, &texr); + } + void BasicRenderer2D::drawString(const ostd::String& str, const ostd::Vec2& position, const ostd::Color& color, int32_t fontSize) { if (!m_initialized) return; @@ -134,4 +146,4 @@ namespace ogfx drawCircle(center, radius, outlineColor, outlineThickness); } -} \ No newline at end of file +} diff --git a/src/ogfx/BasicRenderer.hpp b/src/ogfx/BasicRenderer.hpp index 4d923f1..7dbcea7 100644 --- a/src/ogfx/BasicRenderer.hpp +++ b/src/ogfx/BasicRenderer.hpp @@ -2,6 +2,7 @@ #include #include +#include namespace ogfx { @@ -19,6 +20,8 @@ namespace ogfx void setFont(const ostd::String& fontFilePath); void setFontSize(int32_t fontSize); + void drawImage(const ogfx::Image& image, const ostd::Vec2& position); + void drawString(const ostd::String& str, const ostd::Vec2& position, const ostd::Color& color, int32_t fontSize = 0); void drawCenteredString(const ostd::String& str, const ostd::Vec2& center, const ostd::Color& color, int32_t fontSize = 0); @@ -41,4 +44,4 @@ namespace ogfx WindowBase* m_window { nullptr }; bool m_initialized { false }; }; -} \ No newline at end of file +} diff --git a/src/ogfx/Image.cpp b/src/ogfx/Image.cpp new file mode 100644 index 0000000..55c86ce --- /dev/null +++ b/src/ogfx/Image.cpp @@ -0,0 +1,28 @@ +#include "Image.hpp" +#include +#include +#include + +namespace ogfx +{ + void Image::destroy(void) + { + SDL_DestroyTexture(m_sdl_texture); + m_loaded = false; + m_sdl_texture = nullptr; + m_width = 0; + m_height = 0; + } + + Image& Image::loadFromFile(const ostd::String& filePath, BasicRenderer2D& gfx) + { + if (!gfx.isInitialized()) + return *this; //TODO: Error + m_sdl_texture = IMG_LoadTexture(gfx.getWindow().getSDLRenderer(), filePath.c_str()); + SDL_QueryTexture(m_sdl_texture, nullptr, nullptr, &m_width, &m_height); + m_loaded = true; + setTypeName("ogfx::Image"); + validate(); + return *this; + } +} diff --git a/src/ogfx/Image.hpp b/src/ogfx/Image.hpp new file mode 100644 index 0000000..4544b5f --- /dev/null +++ b/src/ogfx/Image.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include + +namespace ogfx +{ + class BasicRenderer2D; + class Image : public ostd::BaseObject + { + public: + inline Image(void) { invalidate(); } + inline Image(const ostd::String& filePath, BasicRenderer2D& gfx) { loadFromFile(filePath, gfx); } + inline ~Image(void) { destroy(); } + void destroy(void); + Image& loadFromFile(const ostd::String& filePath, BasicRenderer2D& gfx); + inline ostd::IPoint getSize(void) const { return { m_width, m_height }; } + inline bool isLoaded(void) const { return m_loaded; } + inline SDL_Texture* getSDLTexture(void) const { return m_sdl_texture; } + + private: + SDL_Texture* m_sdl_texture { nullptr }; + int32_t m_width { 0 }; + int32_t m_height { 0 }; + bool m_loaded { false }; + }; +} diff --git a/src/ogfx/SDLInclude.hpp b/src/ogfx/SDLInclude.hpp index f068d47..687a169 100644 --- a/src/ogfx/SDLInclude.hpp +++ b/src/ogfx/SDLInclude.hpp @@ -8,3 +8,4 @@ #include #include #include +#include