diff --git a/SDLU/graphics/RenderWindow.cpp b/SDLU/graphics/RenderWindow.cpp index a3ba8e3..832c233 100644 --- a/SDLU/graphics/RenderWindow.cpp +++ b/SDLU/graphics/RenderWindow.cpp @@ -198,4 +198,33 @@ namespace sdlu { SDL_SetWindowGrab(m_pWindow, grabbed ? SDL_TRUE : SDL_FALSE); } + + void RenderWindow::SetIcon(Uint32 width, Uint32 height, const Uint8* pixels) + { + size_t size = static_cast(width) * static_cast(height) * 4; + void* _pixels = malloc(size); + memcpy(_pixels, pixels, size); + SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(_pixels, + width, height, 32, 32 * width, + SDL_PIXELFORMAT_RGBA8888); + + SDL_SetWindowIcon(m_pWindow, surface); + } + + void RenderWindow::SetIcon(Uint32 width, Uint32 height, const Uint32* pixels) + { + size_t size = static_cast(width) * static_cast(height) * 4; + void* _pixels = malloc(size); + memcpy(_pixels, pixels, size); + SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(_pixels, + width, height, 32, 4 * width, + SDL_PIXELFORMAT_RGBA8888); + + SDL_SetWindowIcon(m_pWindow, surface); + } + + void RenderWindow::SetIcon(SDL_Surface* icon) + { + SDL_SetWindowIcon(m_pWindow, icon); + } } \ No newline at end of file diff --git a/SDLU/graphics/RenderWindow.hpp b/SDLU/graphics/RenderWindow.hpp index ffe79ee..274a16c 100644 --- a/SDLU/graphics/RenderWindow.hpp +++ b/SDLU/graphics/RenderWindow.hpp @@ -198,6 +198,31 @@ namespace sdlu */ void SetMouseCursorGrabbed(bool grabbed); + /** + * @brief Sets the window icon to an array of RGBA values + * + * @param[in] width Width of the icon (in px) + * @param[in] height Height of the icon (in px) + * @param[in] pixels Array of color data (RGBA as seperate 8-Bit integer values) + */ + void SetIcon(Uint32 width, Uint32 height, const Uint8* pixels); + + /** + * @brief Sets the window icon to an array of RGBA values + * + * @param[in] width Width of the icon (in px) + * @param[in] height Height of the icon (in px) + * @param[in] pixels Array of color data (RGBA as one 32-Bit integer value) + */ + void SetIcon(Uint32 width, Uint32 height, const Uint32* pixels); + + /** + * @brief Sets the window icon to a SDL_Surface + * + * @param[in] icon A SDL_Surface* holding the icon data + */ + void SetIcon(SDL_Surface* icon); + protected: SDL_Window* m_pWindow; ///< A pointer to the window object SDL_Renderer* m_pRenderer; ///< A pointer to the renderer object diff --git a/SDLU_Example/main.cpp b/SDLU_Example/main.cpp index c4a5ece..5e219c4 100644 --- a/SDLU_Example/main.cpp +++ b/SDLU_Example/main.cpp @@ -4,9 +4,22 @@ int main(int argc, char** argv) { SDL_Init(SDL_INIT_VIDEO); + Uint32* icon_data = new Uint32[64 * 64]; + for (int y = 0; y < 64; y++) + { + for (int x = 0; x < 64; x++) + { + icon_data[64 * y + x] = 0x004400FF; + icon_data[64 * y + x] |= (((Uint32)((y / 64.f) * 255) << 24)); + icon_data[64 * y + x] |= (((Uint32)((x / 64.f) * 255) << 8)); + } + } + MyWindow window(800, 800, "Test"); SDL_SetWindowTitle(window.GetWindow(), "New Title"); + window.SetIcon(64, 64, icon_data); + SDL_Event event; float t = 0.f; while (window.IsOpen())