Added SetIcon function to RenderWindow

This commit is contained in:
Robert 2020-05-18 16:27:03 +02:00
parent cea768fce1
commit 85ab65737a
3 changed files with 67 additions and 0 deletions

View file

@ -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<size_t>(width) * static_cast<size_t>(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<size_t>(width) * static_cast<size_t>(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);
}
}

View file

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

View file

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