libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx.cpp
Go to the documentation of this file.
1/**
2 * @file mx.cpp
3 * @brief Implementation of mx::mxWindow main SDL2/OpenGL window class.
4 */
5#include"mx.hpp"
6#ifdef FOR_WASM
7#include<emscripten/emscripten.h>
8#endif
9namespace mx {
10
11 mxWindow::mxWindow(const std::string &name, int w, int h, bool full) {
12 redirect();
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";
15 mx::system_err.flush();
16 exit(EXIT_FAILURE);
17 }
18 if(TTF_Init() < 0) {
19 mx::system_err << "mx: Error could not initalize SDL_ttf...\n";
20 mx::system_err.flush();
21 exit(EXIT_FAILURE);
22 }
23 create_window(name, w, h, full);
24 text.setRenderer(renderer);
25 text.setColor({255,255,255,255});
26 }
27
29 object.reset(o);
30 }
31
33 mx::system_out << "libmx2: mxWindow shutting down\n";
34 if(object) {
35 mx::system_out << "libmx2: releasing bound object\n";
36 object.reset();
37 }
38 if(renderer) {
39 mx::system_out << "libmx2: destroying SDL renderer\n";
40 SDL_DestroyRenderer(renderer);
41 }
42 if(window) {
43 mx::system_out << "libmx2: destroying SDL window\n";
44 SDL_DestroyWindow(window);
45 }
46 mx::system_out << "libmx2: shutting down SDL_ttf\n";
47 TTF_Quit();
48 mx::system_out << "libmx2: shutting down SDL\n";
49 SDL_Quit();
50 mx::system_out.flush();
51 }
52 SDL_Texture *mxWindow::createTexture(int w, int h) {
53 SDL_Texture *tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
54 if(!tex) {
55 mx::system_err << "mx: Error creating texture: " << SDL_GetError() << "\n";
56 mx::system_err.flush();
57 exit(EXIT_FAILURE);
58 }
59 return tex;
60 }
61
62 void mxWindow::create_window(const std::string &name, int w, int h, bool full) {
63 window = SDL_CreateWindow(name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, full == true ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_SHOWN);
64 if (!window) {
65 mx::system_err << "mx: Error initalzing Window: " << SDL_GetError() << "\n";
66 mx::system_err.flush();
67 SDL_Quit();
68 exit(EXIT_FAILURE);
69 }
70 width = w;
71 height = h;
72 renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
73 if (!renderer) {
74 mx::system_err << "mx: Error Creating Renderer: " << SDL_GetError() << "\n";
75 mx::system_err.flush();
76 SDL_DestroyWindow(window);
77 SDL_Quit();
78 exit(EXIT_FAILURE);
79 }
80 }
81
82 void mxWindow::setFullScreen(bool full) {
83 if(full) {
84 SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
85 } else {
86 SDL_SetWindowFullscreen(window, 0);
87 }
88 }
89
91 active = false;
92 }
93
95 if(!object) {
96 throw Exception("Requires you set an Object");
97 }
98 active = true;
99 while(active) {
100 proc();
101 }
102 }
103
105
106 if(!object) {
107 throw Exception("Requires an active Object");
108 }
109
110 while(SDL_PollEvent(&e)) {
111 if(e.type == SDL_QUIT || (e.type == SDL_KEYDOWN &&
112e.key.keysym.sym == SDLK_ESCAPE)) {
113 active = false;
114 return;
115 }
116
117 event(e);
118 object->event(this, e);
119 }
120 draw(renderer);
121 }
122
123 void mxWindow::setIcon(SDL_Surface *icon) {
124 SDL_SetWindowIcon(window, icon);
125 }
126
127 void mxWindow::setIcon(const std::string &icon) {
128 SDL_Surface *ico = png::LoadPNG(icon.c_str());
129 setIcon(ico);
130 SDL_FreeSurface(ico);
131 }
132
133 void mxWindow::setWindowTitle(const std::string &title) {
134 SDL_SetWindowTitle(window, title.c_str());
135 }
136
137}
General-purpose exception class for the mx library.
Definition exception.hpp:20
void setObject(obj::Object *o)
Attach an obj::Object to receive draw/event callbacks.
Definition mx.cpp:28
void setIcon(SDL_Surface *icon)
Set the window icon from an SDL_Surface.
Definition mx.cpp:123
void setFullScreen(bool full)
Toggle fullscreen mode.
Definition mx.cpp:82
void create_window(const std::string &name, int w, int h, bool full)
Internal helper: create and initialise the SDL window+renderer.
Definition mx.cpp:62
void loop()
Run the event+draw loop until setActive(false) is called.
Definition mx.cpp:94
void proc()
Process one iteration of events and drawing.
Definition mx.cpp:104
SDL_Renderer * renderer
Active SDL renderer.
Definition mx.hpp:134
virtual void event(SDL_Event &e)=0
Process a single SDL event (pure virtual).
mxWindow()=delete
virtual ~mxWindow()
Destructor — destroys the SDL2 window and renderer.
Definition mx.cpp:32
SDL_Event e
Reusable SDL event structure.
Definition mx.hpp:165
void quit()
Request the SDL event loop to quit.
Definition mx.cpp:90
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.
Definition mx.cpp:133
SDL_Window * window
Underlying SDL window handle.
Definition mx.hpp:144
int width
Window width in pixels.
Definition mx.hpp:135
Text text
2D text renderer.
Definition mx.hpp:133
SDL_Texture * createTexture(int w, int h)
Create a streaming SDL_Texture of the given size.
Definition mx.cpp:52
int height
Window height in pixels.
Definition mx.hpp:136
bool active
Controls the main loop.
Definition mx.hpp:164
Abstract base class for all drawable game objects.
Definition object.hpp:24
Central SDL2 application window class and related includes.
Definition cfg.cpp:11
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.
Definition loadpng.cpp:22