Thanks to visit codestin.com
Credit goes to canreader.github.io
SleakEngine
1.0.0
C++23 multi-backend game engine
Toggle main menu visibility
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
8
namespace
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
20
enum
MessageBoxReturn
{
21
Ok
= 0,
22
Yes
= 1,
23
No
= 2,
24
Cancel
= 3
25
};
26
27
/// RGB palette for a ShowMessageBox() dialog's background, text, and buttons.
28
/// @ingroup core
29
struct
MessageBoxColorScheme
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."
);
57
return
MessageBoxReturn::Cancel
;
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
Logger.hpp
SLEAK_ERROR
#define SLEAK_ERROR(...)
Definition
Logger.hpp:22
SLEAK_WARN
#define SLEAK_WARN(...)
Definition
Logger.hpp:21
Sleak::MessageBoxReturn
MessageBoxReturn
Definition
WindowHelper.hpp:20
Sleak::MessageBoxType
MessageBoxType
Definition
WindowHelper.hpp:12
Sleak::Cancel
@ Cancel
Definition
WindowHelper.hpp:24
Sleak::No
@ No
Definition
WindowHelper.hpp:23
Sleak::Yes
@ Yes
Definition
WindowHelper.hpp:22
Sleak::Ok
@ Ok
Definition
WindowHelper.hpp:21
Sleak::Error
@ Error
Definition
WindowHelper.hpp:15
Sleak::Warning
@ Warning
Definition
WindowHelper.hpp:14
Sleak::Info
@ Info
Definition
WindowHelper.hpp:13
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::ShowMessageBox
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.
Definition
WindowHelper.hpp:53
Sleak::MessageBox
void MessageBox(const char *Title, const char *Message, MessageBoxType Type)
Fire-and-forget OS message box; no return value, no custom buttons.
Definition
WindowHelper.hpp:39
Sleak::MessageBoxColorScheme
Definition
WindowHelper.hpp:30
Sleak::MessageBoxColorScheme::ButtonBorder
Uint8 ButtonBorder[3]
Definition
WindowHelper.hpp:33
Sleak::MessageBoxColorScheme::Background
Uint8 Background[3]
Definition
WindowHelper.hpp:31
Sleak::MessageBoxColorScheme::ButtonText
Uint8 ButtonText[3]
Definition
WindowHelper.hpp:35
Sleak::MessageBoxColorScheme::ButtonBackground
Uint8 ButtonBackground[3]
Definition
WindowHelper.hpp:34
Sleak::MessageBoxColorScheme::Text
Uint8 Text[3]
Definition
WindowHelper.hpp:32
include
public
Core
WindowHelper.hpp
Generated on
for SleakEngine by
1.18.0