Core
Core routines including initialization and the main loop.
Header: #include <asw/modules/core.h>
Namespace: asw::core
Functions
init
void init(int width, int height, int scale = 1);Initializes the core module. This must be called before using any other ASW functionality. It loads the config file named by ASW_CONFIG, if it is set, before it opens the window.
| Parameter | Type | Description |
|---|---|---|
width | int | The width of the window |
height | int | The height of the window |
scale | int | The scale of the window (default: 1) |
init_opengl
void init_opengl(int width, int height, int scale = 1);Initializes the core module with an OpenGL context instead of an SDL renderer. The parameters are the same as init. Use asw::display::swap_window() to present each frame.
update
void update();Updates core module functionality. Called each frame to process events, update input state and update the sound module (fades, ducking and positional sounds).
run
void run(const std::function<void()>& frame);Run a main loop until exit() is called. run calls frame once per frame. In frame, call update(), run your game logic, draw, and call asw::display::present().
- Desktop:
runis a plain loop. It returns afterexit()is called, so you can callshutdown()after it. - Browser (Emscripten): the browser runs the loop with
emscripten_set_main_loop.rundoes not return, and code after it does not run. Whenexit()is called, the loop stops andModule.onStopis called if the web page defines it. The page made byasw_add_web_target()uses this to show a restart message.
Use run instead of a while (!is_exiting()) loop, so that the same code works on desktop and in the browser. A while loop blocks the browser, so the page freezes. SceneManager::start() uses run in the browser too.
asw::core::run([&]() {
asw::core::update();
// game logic and drawing
asw::display::present();
});get_delta_time
float get_delta_time();Get the time in seconds between the last two calls to update(). The value is capped at 0.25 seconds, so a breakpoint or a stalled window does not make objects jump. Returns 0 before the second call to update().
Use this in a custom loop. The managed SceneManager::start() loop passes a fixed dt to your scenes.
print_info
void print_info();Prints information about the core module to the console.
exit
void exit();Exit the application. Sets the exiting flag, which causes the main loop to exit on the next update.
is_exiting
bool is_exiting();Returns true after exit() was called or the window was closed.
shutdown
void shutdown();Release the resources that the core module uses. Call this on application exit, after the main loop.
Example
#include <asw/asw.h>
int main() {
asw::core::init(640, 480, 2);
asw::core::run([&]() {
asw::core::update();
const float dt = asw::core::get_delta_time();
if (asw::input::get_key_down(asw::input::Key::Escape)) {
asw::core::exit();
}
// game logic here, scaled by dt
asw::display::present();
});
asw::core::shutdown(); // desktop only, run() does not return in the browser
return 0;
}