7#include<emscripten/emscripten.h>
13 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) != 0) {
14 mx::system_err <<
"mx: Error initalizing SDL: " << SDL_GetError() <<
"\n";
25 text.setColor({255,255,255,255});
53 SDL_Texture *tex = SDL_CreateTexture(
renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
55 mx::system_err <<
"mx: Error creating texture: " << SDL_GetError() <<
"\n";
63 window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, full ==
true ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_SHOWN);
65 mx::system_err <<
"mx: Error initalzing Window: " << SDL_GetError() <<
"\n";
72 renderer = SDL_CreateRenderer(
window, -1, SDL_RENDERER_ACCELERATED);
74 mx::system_err <<
"mx: Error Creating Renderer: " << SDL_GetError() <<
"\n";
84 SDL_SetWindowFullscreen(
window, SDL_WINDOW_FULLSCREEN);
86 SDL_SetWindowFullscreen(
window, 0);
96 throw Exception(
"Requires you set an Object");
107 throw Exception(
"Requires an active Object");
110 while(SDL_PollEvent(&
e)) {
111 if(
e.type == SDL_QUIT || (
e.type == SDL_KEYDOWN &&
112e.key.keysym.sym == SDLK_ESCAPE)) {
118 object->event(
this,
e);
124 SDL_SetWindowIcon(
window, icon);
130 SDL_FreeSurface(ico);
134 SDL_SetWindowTitle(
window, title.c_str());
General-purpose exception class for the mx library.
void setObject(obj::Object *o)
Attach an obj::Object to receive draw/event callbacks.
void setIcon(SDL_Surface *icon)
Set the window icon from an SDL_Surface.
void setFullScreen(bool full)
Toggle fullscreen mode.
void create_window(const std::string &name, int w, int h, bool full)
Internal helper: create and initialise the SDL window+renderer.
void loop()
Run the event+draw loop until setActive(false) is called.
void proc()
Process one iteration of events and drawing.
SDL_Renderer * renderer
Active SDL renderer.
virtual void event(SDL_Event &e)=0
Process a single SDL event (pure virtual).
virtual ~mxWindow()
Destructor — destroys the SDL2 window and renderer.
SDL_Event e
Reusable SDL event structure.
void quit()
Request the SDL event loop to quit.
virtual void draw(SDL_Renderer *ren)=0
Draw the current frame (pure virtual).
void setWindowTitle(const std::string &title)
Change the window title bar text.
SDL_Window * window
Underlying SDL window handle.
int width
Window width in pixels.
Text text
2D text renderer.
SDL_Texture * createTexture(int w, int h)
Create a streaming SDL_Texture of the given size.
int height
Window height in pixels.
bool active
Controls the main loop.
Abstract base class for all drawable game objects.
Central SDL2 application window class and related includes.
void redirect()
Redirect std::cout and std::cerr through the tee streams.
TeeStream system_out(std::cout, log_file)
Global stdout + log tee stream.
TeeStream system_err(std::cerr, error_file)
Global stderr + log tee stream.
SDL_Surface * LoadPNG(const char *file)
Load a PNG file into an SDL_Surface.