Skip to content

Instantly share code, notes, and snippets.

@veganaize
Created December 22, 2023 23:45
Show Gist options
  • Save veganaize/7bb6612839c1bce92c342f615acddeaa to your computer and use it in GitHub Desktop.
Save veganaize/7bb6612839c1bce92c342f615acddeaa to your computer and use it in GitHub Desktop.
sdl2++
#ifndef PLUSPLUSSDL2_HPP_
#define PLUSPLUSSDL2_HPP_
#include <SDL2/SDL.h>
namespace sdl {
const Uint32 window_shown = SDL_WINDOW_SHOWN;
const int windowpos_undefined = SDL_WINDOWPOS_UNDEFINED;
typedef SDL_Event event;
typedef SDL_Surface surface;
typedef SDL_Window window;
class init {
public:
enum : Uint32 {
timer = SDL_INIT_TIMER,
audio = SDL_INIT_AUDIO,
video = SDL_INIT_VIDEO,
joystick = SDL_INIT_JOYSTICK,
haptic = SDL_INIT_HAPTIC,
gamecontroller = SDL_INIT_GAMECONTROLLER,
events = SDL_INIT_EVENTS,
sensor = SDL_INIT_SENSOR,
noparachute = SDL_INIT_NOPARACHUTE,
everything = SDL_INIT_EVERYTHING
};
};
class event_type {
public:
enum {
firstevent = SDL_FIRSTEVENT,
quit = SDL_QUIT,
app_terminating = SDL_APP_TERMINATING,
app_lowmemory = SDL_APP_LOWMEMORY,
app_willenterbackground = SDL_APP_WILLENTERBACKGROUND,
app_didenterbackground = SDL_APP_DIDENTERBACKGROUND,
app_willenterforeground = SDL_APP_WILLENTERFOREGROUND,
app_didenterforeground = SDL_APP_DIDENTERFOREGROUND,
localechanged = SDL_LOCALECHANGED,
displayevent = SDL_DISPLAYEVENT,
windowevent = SDL_WINDOWEVENT,
syswmevent = SDL_SYSWMEVENT,
keydown = SDL_KEYDOWN,
keyup = SDL_KEYUP,
textediting = SDL_TEXTEDITING,
textinput = SDL_TEXTINPUT,
keymapchanged = SDL_KEYMAPCHANGED,
textediting_ext = SDL_TEXTEDITING_EXT,
mousemotion = SDL_MOUSEMOTION,
mousebuttondown = SDL_MOUSEBUTTONDOWN,
mousebuttonup = SDL_MOUSEBUTTONUP,
mousewheel = SDL_MOUSEWHEEL,
joyaxismotion = SDL_JOYAXISMOTION,
joyballmotion = SDL_JOYBALLMOTION,
joyhatmotion = SDL_JOYHATMOTION,
joybuttondown = SDL_JOYBUTTONDOWN,
joybuttonup = SDL_JOYBUTTONUP,
joydeviceadded = SDL_JOYDEVICEADDED,
joydeviceremoved = SDL_JOYDEVICEREMOVED,
joybatteryupdated = SDL_JOYBATTERYUPDATED,
controlleraxismotion = SDL_CONTROLLERAXISMOTION,
controllerbuttondown = SDL_CONTROLLERBUTTONDOWN,
controllerbuttonup = SDL_CONTROLLERBUTTONUP,
controllerdeviceadded = SDL_CONTROLLERDEVICEADDED,
controllerdeviceremoved = SDL_CONTROLLERDEVICEREMOVED,
controllerdeviceremapped = SDL_CONTROLLERDEVICEREMAPPED,
controllertouchpaddown = SDL_CONTROLLERTOUCHPADDOWN,
controllertouchpadmotion = SDL_CONTROLLERTOUCHPADMOTION,
controllertouchpadup = SDL_CONTROLLERTOUCHPADUP,
controllersensorupdate = SDL_CONTROLLERSENSORUPDATE,
fingerdown = SDL_FINGERDOWN,
fingerup = SDL_FINGERUP,
fingermotion = SDL_FINGERMOTION,
dollargesture = SDL_DOLLARGESTURE,
dollarrecord = SDL_DOLLARRECORD,
multigesture = SDL_MULTIGESTURE,
clipboardupdate = SDL_CLIPBOARDUPDATE,
dropfile = SDL_DROPFILE,
droptext = SDL_DROPTEXT,
dropbegin = SDL_DROPBEGIN,
dropcomplete = SDL_DROPCOMPLETE,
audiodeviceadded = SDL_AUDIODEVICEADDED,
audiodeviceremoved = SDL_AUDIODEVICEREMOVED,
sensorupdate = SDL_SENSORUPDATE,
render_targets_reset = SDL_RENDER_TARGETS_RESET,
render_device_reset = SDL_RENDER_DEVICE_RESET,
pollsentinel = SDL_POLLSENTINEL,
userevent = SDL_USEREVENT,
lastevent = SDL_LASTEVENT
}; // enum
}; // event_type
class window_flags {
enum {
fullscreen = SDL_WINDOW_FULLSCREEN,
opengl = SDL_WINDOW_OPENGL,
shown = SDL_WINDOW_SHOWN,
hidden = SDL_WINDOW_HIDDEN,
borderless = SDL_WINDOW_BORDERLESS,
resizable = SDL_WINDOW_RESIZABLE,
minimized = SDL_WINDOW_MINIMIZED,
maximized = SDL_WINDOW_MAXIMIZED,
mouse_grabbed = SDL_WINDOW_MOUSE_GRABBED,
input_focus = SDL_WINDOW_INPUT_FOCUS,
mouse_focus = SDL_WINDOW_MOUSE_FOCUS,
fullscreen_desktop = SDL_WINDOW_FULLSCREEN_DESKTOP,
foreign = SDL_WINDOW_FOREIGN,
allow_highdpi = SDL_WINDOW_ALLOW_HIGHDPI,
mouse_capture = SDL_WINDOW_MOUSE_CAPTURE,
always_on_top = SDL_WINDOW_ALWAYS_ON_TOP,
skip_taskbar = SDL_WINDOW_SKIP_TASKBAR,
window_utility = SDL_WINDOW_UTILITY,
tooltip = SDL_WINDOW_TOOLTIP,
popup_menu = SDL_WINDOW_POPUP_MENU,
keyboard_grabbed = SDL_WINDOW_KEYBOARD_GRABBED,
vulcan = SDL_WINDOW_VULKAN,
metal = SDL_WINDOW_METAL,
input_grabbed = SDL_WINDOW_INPUT_GRABBED
}; // enum
}; // window_flags
////// SDL catergory: init
int
init(Uint32 flags) { return SDL_Init(flags); }
int
init_sub_system(Uint32 flags) { return SDL_InitSubSystem(flags); }
void
quit(void) { SDL_Quit(); }
void
quit_sub_system(Uint32 flags) { SDL_QuitSubSystem(flags); }
void
set_main_ready(void) { SDL_SetMainReady(); }
Uint32
was_init(Uint32 flags) { return SDL_WasInit(flags); }
/*
*int
*win_rt_run_app(SDL_main_func mainFunction, void* reserved)
*{
* return SDL_WinRTRunApp(mainFunction, reserved);
*}
*/
////// end category: init
int blit_surface(SDL_Surface* src,
const SDL_Rect* srcrect,
SDL_Surface* dst,
SDL_Rect* dstrect)
{
return SDL_BlitSurface(src, srcrect, dst, dstrect);
}
SDL_Window*
create_window(const char *title, int x, int y, int w, int h, Uint32 flags)
{
return SDL_CreateWindow(title, x, y, w, h, flags);
}
void
destroy_window(SDL_Window* sdl_window)
{
return SDL_DestroyWindow(sdl_window);
}
const char*
get_error(void)
{
return SDL_GetError();
}
SDL_Surface*
get_window_surface(SDL_Window* sdl_window)
{
return SDL_GetWindowSurface(sdl_window);
}
int
fill_rect(SDL_Surface* sdl_surface, const SDL_Rect* sdl_rect, Uint32 color)
{
return SDL_FillRect(sdl_surface, sdl_rect, color);
}
void free_surface(SDL_Surface* surface)
{
SDL_FreeSurface(surface);
}
SDL_Surface*
load_bmp(const char* file)
{
return SDL_LoadBMP(file);
}
Uint32
map_rgb(const SDL_PixelFormat* sdl_pixel_format, Uint8 r, Uint8 g, Uint8 b)
{
return SDL_MapRGB(sdl_pixel_format, r, g, b);
}
int
poll_event(sdl::event* sdl_event)
{
return SDL_PollEvent(sdl_event);
}
int
update_window_surface(SDL_Window* sdl_window)
{
return SDL_UpdateWindowSurface(sdl_window);
}
////// Object-oriented classes...
void
Init(Uint32 flags = sdl::init::everything)
{
if (sdl::init(flags) < 0) {
std::cerr << "\nFailed to initialize SDL...\n"
<< sdl::get_error()
<< std::endl;
exit(1);
}
}
class Surface {
public:
sdl::surface *surface;
~Surface()
{
sdl::free_surface(this->surface);
this->surface = NULL;
}
Surface*
blit(sdl::surface* destination_surface)
{
sdl::blit_surface(
this->surface,
NULL,
destination_surface,
NULL);
return this;
}
Surface*
load_bmp(const char* file)
{
this->surface = SDL_LoadBMP(file);
if(this->surface == NULL) {
std::cerr << "\nFailed loading BMP...\n"
<< SDL_GetError();
exit(1);
}
return this;
}
};
class Window {
public:
sdl::window *window;
//sdl::surface* surface;
Window(const char* title = "SDL Window",
int x = sdl::windowpos_undefined,
int y = sdl::windowpos_undefined,
int w = 640,
int h = 480,
Uint32 flags = sdl::window_shown)
{
this->window = sdl::create_window(
title,
x, y,
w, h,
flags);
if (this->window == NULL) {
std::cerr << "\nFailed to create window...\n"
<< sdl::get_error()
<< std::endl;
exit(1);
}
this->get_surface();
}
~Window()
{
this->destroy();
}
void
destroy()
{
sdl::destroy_window(this->window);
}
int
fill_rect(Uint8 r = 0x00, Uint8 g = 0x00, Uint8 b = 0x00)
{
return sdl::fill_rect(
this->get_surface(),
NULL,
sdl::map_rgb(this->get_surface()->format, r, g, b));
}
/**
* After resizing the window this method
* must be called again.
*/
sdl::surface*
get_surface()
{
return sdl::get_window_surface(this->window);
}
int
update_surface()
{
return sdl::update_window_surface(this->window);
}
}; // Window class
class Event {
public:
sdl::event event;
sdl::event*
poll()
{
sdl::poll_event(&this->event);
return &this->event;
}
}; // Event class
}; // sdl namespace
#endif // PLUSPLUSSDL2_HPP_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment