libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx.hpp
Go to the documentation of this file.
1/**
2 * @file mx.hpp
3 * @brief Central SDL2 application window class and related includes.
4 *
5 * Pulls in all core libmx subsystems and provides mxWindow, which manages
6 * the SDL2 window, renderer, event loop, and optional mixer.
7 */
8#ifndef __MX__H___
9#define __MX__H___
10
11#ifdef __EMSCRIPTEN__
12#include"config.hpp"
13#else
14#include"config.h"
15#endif
16
17#include"SDL.h"
18#include"SDL_ttf.h"
19#include<iostream>
20#include<string>
21#include"util.hpp"
22#include"object.hpp"
23#include"font.hpp"
24#include"tee_stream.hpp"
25#include"texture.hpp"
26#include"exception.hpp"
27#include"joystick.hpp"
28#include"wrapper.hpp"
29#include"loadpng.hpp"
30#include"input.hpp"
31#include<memory>
32
33#ifdef WITH_MIXER
34#include"sound.hpp"
35#endif
36
37namespace mx {
38
39/**
40 * @class mxWindow
41 * @brief Main SDL2 application window.
42 *
43 * Creates and manages the SDL2 window and renderer. Derived classes must
44 * implement event() and draw() to provide application-specific behaviour.
45 * The window runs a blocking loop() that drives the frame and event cycle.
46 */
47 class mxWindow {
48 public:
49 std::unique_ptr<obj::Object> object; ///< Optional attached game object.
50
51 mxWindow() = delete;
52
53 /**
54 * @brief Create the SDL2 window.
55 * @param name Window title.
56 * @param w Window width in pixels.
57 * @param h Window height in pixels.
58 * @param full If @c true, start in fullscreen mode.
59 */
60 mxWindow(const std::string &name, int w, int h, bool full = false);
61
62 /** @brief Destructor — destroys the SDL2 window and renderer. */
63 virtual ~mxWindow();
64
65 mxWindow(const mxWindow&) = delete;
66 mxWindow& operator=(const mxWindow&) = delete;
67 mxWindow(mxWindow&&) = delete;
69
70 /**
71 * @brief Process a single SDL event (pure virtual).
72 * @param e The SDL event to handle.
73 */
74 virtual void event(SDL_Event &e) = 0;
75
76 /**
77 * @brief Draw the current frame (pure virtual).
78 * @param ren Active SDL renderer.
79 */
80 virtual void draw(SDL_Renderer *ren) = 0;
81
82 /**
83 * @brief Create a streaming SDL_Texture of the given size.
84 * @param w Width in pixels.
85 * @param h Height in pixels.
86 * @return New SDL_Texture (caller owns it).
87 */
88 SDL_Texture *createTexture(int w, int h);
89
90 /**
91 * @brief Enable or disable the main loop.
92 * @param a @c true to keep running, @c false to stop.
93 */
94 void setActive(bool a) { active = a; }
95
96 /** @brief Run the event+draw loop until setActive(false) is called. */
97 void loop();
98
99 /** @brief Process one iteration of events and drawing. */
100 void proc();
101
102 /** @brief Stop the loop on the next cycle (sets active = false). */
103 void destroy() { setActive(false); }
104
105 /**
106 * @brief Prepend the asset directory to all relative file paths.
107 * @param path Asset directory path.
108 */
109 void setPath(const std::string &path) { util.path = path; }
110
111 /**
112 * @brief Set the window icon from an SDL_Surface.
113 * @param icon Surface to use as the icon.
114 */
115 void setIcon(SDL_Surface *icon);
116
117 /**
118 * @brief Set the window icon from an image file.
119 * @param icon Image file path.
120 */
121 void setIcon(const std::string &icon);
122
123 /**
124 * @brief Toggle fullscreen mode.
125 * @param full @c true to enable fullscreen.
126 */
127 void setFullScreen(bool full);
128
129 /** @brief Request the SDL event loop to quit. */
130 void quit();
131
132 mxUtil util; ///< Utility/asset helper.
133 Text text; ///< 2D text renderer.
134 SDL_Renderer *renderer = nullptr; ///< Active SDL renderer.
135 int width = 0; ///< Window width in pixels.
136 int height = 0; ///< Window height in pixels.
137
138 /**
139 * @brief Attach an obj::Object to receive draw/event callbacks.
140 * @param o Pointer to the object (window does not take ownership).
141 */
142 void setObject(obj::Object *o);
143
144 SDL_Window *window = nullptr; ///< Underlying SDL window handle.
145
146 /**
147 * @brief Change the window title bar text.
148 * @param title New title string.
149 */
150 void setWindowTitle(const std::string &title);
151
152#ifdef WITH_MIXER
153 Mixer mixer; ///< Audio mixer (only when built with WITH_MIXER).
154#endif
155 protected:
156 /**
157 * @brief Internal helper: create and initialise the SDL window+renderer.
158 * @param name Title string.
159 * @param w Width.
160 * @param h Height.
161 * @param full Fullscreen flag.
162 */
163 void create_window(const std::string &name, int w, int h, bool full);
164 bool active = false; ///< Controls the main loop.
165 SDL_Event e; ///< Reusable SDL event structure.
166 };
167}
168
169#endif
SDL_mixer–based audio manager.
Definition sound.hpp:31
Helper for rendering text strings with SDL_Renderer.
Definition font.hpp:117
Utility bag for SDL-based applications.
Definition util.hpp:33
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 destroy()
Stop the loop on the next cycle (sets active = false).
Definition mx.hpp:103
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
Mixer mixer
Audio mixer (only when built with WITH_MIXER).
Definition mx.hpp:153
mxUtil util
Utility/asset helper.
Definition mx.hpp:132
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 setActive(bool a)
Enable or disable the main loop.
Definition mx.hpp:94
mxWindow(mxWindow &&)=delete
void setWindowTitle(const std::string &title)
Change the window title bar text.
Definition mx.cpp:133
mxWindow & operator=(const mxWindow &)=delete
mxWindow & operator=(mxWindow &&)=delete
SDL_Window * window
Underlying SDL window handle.
Definition mx.hpp:144
int width
Window width in pixels.
Definition mx.hpp:135
std::unique_ptr< obj::Object > object
Optional attached game object.
Definition mx.hpp:49
Text text
2D text renderer.
Definition mx.hpp:133
void setPath(const std::string &path)
Prepend the asset directory to all relative file paths.
Definition mx.hpp:109
SDL_Texture * createTexture(int w, int h)
Create a streaming SDL_Texture of the given size.
Definition mx.cpp:52
mxWindow(const mxWindow &)=delete
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
Compile-time feature flags and version macros for libmx.
Exception type used throughout libmx.
SDL_ttf font and 2D text rendering wrappers.
High-level game controller input abstraction built on Controller.
SDL joystick and game controller RAII wrappers.
PNG image loading and saving utilities via SDL.
Definition cfg.cpp:11
Abstract game-object interface for the SDL window loop.
SDL_mixer audio subsystem wrapper (requires WITH_MIXER build flag).
Tee stream utilities that duplicate output to two sinks simultaneously.
SDL_Texture RAII wrapper with loading, saving, and flip utilities.
General SDL utility helpers, file I/O, and compression utilities.
Type-safe nullable pointer wrapper inspired by Rust's Option<T>.