SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
WindowHelper.hpp
Go to the documentation of this file.
1#ifndef _WINDOWHELPER_HPP_
2#define _WINDOWHELPER_HPP_
3
4#include <Core/Logger.hpp>
5#include <SDL3/SDL.h>
6#include <iostream>
7
8namespace Sleak {
9
10 /// SDL message box icon/style, maps directly to SDL_MessageBoxFlags.
11 /// @ingroup core
12 enum MessageBoxType : unsigned int {
13 Info = 0x00000040u,
14 Warning = 0x00000020u,
15 Error = 0x00000010u
16 };
17
18 /// Which button the user picked in ShowMessageBox().
19 /// @ingroup core
21 Ok = 0,
22 Yes = 1,
23 No = 2,
25 };
26
27 /// RGB palette for a ShowMessageBox() dialog's background, text, and buttons.
28 /// @ingroup core
30 {
31 Uint8 Background[3] = { 255, 0, 0 }; // Background
32 Uint8 Text[3] = { 255, 255, 255 }; // Text
33 Uint8 ButtonBorder[3] = { 0, 255, 0 }; // ButtonBorder
34 Uint8 ButtonBackground[3] = { 0, 0, 255 }; // ButtonBackground
35 Uint8 ButtonText[3] = { 255, 255, 0 }; // ButtonText
36 };
37
38 /// Fire-and-forget OS message box; no return value, no custom buttons.
39 void MessageBox(const char* Title, const char* Message, MessageBoxType Type) {
40 if(SDL_WasInit(0) == 0)
41 {
42 SLEAK_WARN("SDL is not initialized yet! Cannot display message box.");
43 return;
44 }
45
46 if(!SDL_ShowSimpleMessageBox(Type,Title,Message,nullptr))
47 {
48 SLEAK_ERROR("Failed to create message box! {}",SDL_GetError());
49 }
50 }
51
52 /// Blocking Yes/No/Cancel dialog with a custom color scheme; returns the pressed button.
53 MessageBoxReturn ShowMessageBox(const char* Title, const char* Message, MessageBoxType Type, MessageBoxColorScheme scheme = MessageBoxColorScheme()) {
54 if(SDL_WasInit(0) == 0)
55 {
56 SLEAK_WARN("SDL is not initialized yet! Cannot display message box.");
58 }
59
60 SDL_MessageBoxColorScheme Colorscheme;
61 Colorscheme.colors[0].r = scheme.Background[0];
62 Colorscheme.colors[0].g = scheme.Background[1];
63 Colorscheme.colors[0].b = scheme.Background[2];
64
65 Colorscheme.colors[1].r = scheme.Text[0];
66 Colorscheme.colors[1].g = scheme.Text[1];
67 Colorscheme.colors[1].b = scheme.Text[2];
68
69 Colorscheme.colors[2].r = scheme.ButtonBorder[0];
70 Colorscheme.colors[2].g = scheme.ButtonBorder[1];
71 Colorscheme.colors[2].b = scheme.ButtonBorder[2];
72
73 Colorscheme.colors[3].r = scheme.ButtonBackground[0];
74 Colorscheme.colors[3].g = scheme.ButtonBackground[1];
75 Colorscheme.colors[3].b = scheme.ButtonBackground[2];
76
77 Colorscheme.colors[4].r = scheme.ButtonText[0];
78 Colorscheme.colors[4].g = scheme.ButtonText[1];
79 Colorscheme.colors[4].b = scheme.ButtonText[2];
80
81 const SDL_MessageBoxButtonData Buttons[] = {
82 {0, MessageBoxReturn::Yes, "Yes"}, // First button
83 {SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, MessageBoxReturn::No, "No"},
84 {SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, MessageBoxReturn::Cancel, "Cancel"}
85 };
86
87 SDL_MessageBoxData data;
88 data.flags = (SDL_MessageBoxButtonFlags)Type;
89 data.title = Title;
90 data.message = Message;
91 data.window = nullptr;
92 data.buttons = Buttons;
93 data.numbuttons = SDL_arraysize(Buttons);
94 data.colorScheme = &Colorscheme;
95
96 int button = 0;
97
98 if(!SDL_ShowMessageBox(&data,&button))
99 {
100 SLEAK_ERROR("Failed to create message box! {}",SDL_GetError());
101 }
102
103 return (MessageBoxReturn)button;
104 }
105
106
107};
108#endif
#define SLEAK_ERROR(...)
Definition Logger.hpp:22
#define SLEAK_WARN(...)
Definition Logger.hpp:21
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
MessageBoxReturn ShowMessageBox(const char *Title, const char *Message, MessageBoxType Type, MessageBoxColorScheme scheme=MessageBoxColorScheme())
Blocking Yes/No/Cancel dialog with a custom color scheme; returns the pressed button.
void MessageBox(const char *Title, const char *Message, MessageBoxType Type)
Fire-and-forget OS message box; no return value, no custom buttons.