Added ogfx::Image class

This commit is contained in:
OmniaX-Dev 2024-10-08 23:42:33 +02:00
parent 055aa4f01c
commit 03b1d7b5f7
7 changed files with 78 additions and 5 deletions

View file

@ -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)
#-----------------------------------------------------------------------------------------

View file

@ -1 +1 @@
1883
1885

View file

@ -1,6 +1,6 @@
#include "BasicRenderer.hpp"
#include "WindowBase.hpp"
#include <SDL2/SDL2_gfxPrimitives.h>
#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;

View file

@ -2,6 +2,7 @@
#include <ogfx/FontUtils.hpp>
#include <ostd/Geometry.hpp>
#include <ogfx/Image.hpp>
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);

28
src/ogfx/Image.cpp Normal file
View file

@ -0,0 +1,28 @@
#include "Image.hpp"
#include <SDL2/SDL_render.h>
#include <ogfx/BasicRenderer.hpp>
#include <ogfx/WindowBase.hpp>
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;
}
}

28
src/ogfx/Image.hpp Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include <ostd/BaseObject.hpp>
#include <ogfx/SDLInclude.hpp>
#include <ostd/Geometry.hpp>
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 };
};
}

View file

@ -8,3 +8,4 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>