Skip to content

Commit 9f98ff9

Browse files
author
Michael Zahniser
committed
Added code to set the window icon.
1 parent b025063 commit 9f98ff9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

icon.png

4.1 KB
Loading

source/main.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ PARTICULAR PURPOSE. See the GNU General Public License for more details.
1919
#include "DataFile.h"
2020
#include "DataNode.h"
2121
#include "Dialog.h"
22+
#include "Files.h"
2223
#include "Font.h"
2324
#include "FrameTimer.h"
2425
#include "GameData.h"
26+
#include "ImageBuffer.h"
2527
#include "MenuPanel.h"
2628
#include "Panel.h"
2729
#include "PlayerInfo.h"
@@ -47,6 +49,7 @@ using namespace std;
4749

4850
void PrintHelp();
4951
void PrintVersion();
52+
void SetIcon(SDL_Window *window);
5053
void AdjustViewport(SDL_Window *window);
5154
int DoError(string message, SDL_Window *window = nullptr, SDL_GLContext context = nullptr);
5255
void Cleanup(SDL_Window *window, SDL_GLContext context);
@@ -183,6 +186,7 @@ int main(int argc, char *argv[])
183186
GameData::LoadShaders();
184187
// Make sure the screen size and viewport are set correctly.
185188
AdjustViewport(window);
189+
SetIcon(window);
186190
if(!isFullscreen)
187191
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
188192

@@ -344,6 +348,32 @@ void PrintVersion()
344348

345349

346350

351+
void SetIcon(SDL_Window *window)
352+
{
353+
// Load the icon file.
354+
ImageBuffer *buffer = ImageBuffer::Read(Files::Resources() + "icon.png");
355+
if(!buffer)
356+
return;
357+
if(!buffer->Pixels() || !buffer->Width() || !buffer->Height())
358+
{
359+
delete buffer;
360+
return;
361+
}
362+
363+
// Convert the icon to an SDL surface.
364+
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(buffer->Pixels(), buffer->Width(), buffer->Height(),
365+
32, 4 * buffer->Width(), 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
366+
if(surface)
367+
{
368+
SDL_SetWindowIcon(window, surface);
369+
SDL_FreeSurface(surface);
370+
}
371+
// Free the image buffer.
372+
delete buffer;
373+
}
374+
375+
376+
347377
void AdjustViewport(SDL_Window *window)
348378
{
349379
// Get the window's size in screen coordinates.

0 commit comments

Comments
 (0)