Added support for desktop full screen with pillarboxing and letterboxing
and started adding some comments to the code
This commit is contained in:
1 parent
ffaa6e5ae7
commit
e110b1f2fd
10 files changed
+351
-77
No files matched your search
+82
-39
@@ -18,6 +18,7 @@
|
||||
#include "constants.h"
|
||||
#include "engine_build_config.h"
|
||||
#include "reusable_ids.h"
|
||||
#include <SDL2/SDL_pixels.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -130,10 +131,32 @@ bool init_engine(const char *game_name, WindowSize window_size,
|
||||
|
||||
bool game_running() { return GameEngine.running; }
|
||||
|
||||
WindowSize get_window_defined_size() { return GameEngine.window.defined_size; }
|
||||
void close_engine() {
|
||||
destroy_asset_manager(&(GameEngine.asset_manager));
|
||||
|
||||
WindowSize get_window_drawable_size() {
|
||||
return GameEngine.window.drawable_size;
|
||||
destroy_object_store(&(GameEngine.object_store));
|
||||
|
||||
destroy_window(&(GameEngine.window));
|
||||
|
||||
GameEngine.last_time = 0.0f;
|
||||
|
||||
GameEngine.delta = 0.0f;
|
||||
|
||||
GameEngine.running = false;
|
||||
|
||||
Mix_CloseAudio();
|
||||
|
||||
Mix_Quit();
|
||||
|
||||
TTF_Quit();
|
||||
|
||||
IMG_Quit();
|
||||
|
||||
SDL_Quit();
|
||||
|
||||
#ifdef DEBUG_ENGINE
|
||||
printf("Game closed successfully\n");
|
||||
#endif /* DEBUG_ENGINE */
|
||||
}
|
||||
|
||||
ObjectID add_object(void *object, EventHandlerPtr event_handler,
|
||||
@@ -162,6 +185,62 @@ AssetID load_asset(const char *path, AssetType type) {
|
||||
NULL);
|
||||
}
|
||||
|
||||
void close_asset(AssetID *id) {
|
||||
if (*id < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
remove_asset_from_manager(GameEngine.asset_manager, id);
|
||||
}
|
||||
|
||||
WindowSize get_window_defined_size() { return GameEngine.window.defined_size; }
|
||||
|
||||
WindowSize get_window_real_size() { return GameEngine.window.real_size; }
|
||||
|
||||
WindowDrawingArea get_window_drawing_area() {
|
||||
return GameEngine.window.drawing_area;
|
||||
}
|
||||
|
||||
void fill_rectangle(float x, float y, float w, float h, SDL_Color color) {
|
||||
fill_rect(GameEngine.window.renderer, color, x, y, w, h);
|
||||
}
|
||||
|
||||
void clip_render_rect_to_drawing_area(SDL_FRect *rect) {
|
||||
WindowDrawingArea area = get_window_drawing_area();
|
||||
|
||||
// If the rectangle is completely out of bounds, set its width and height to 0
|
||||
if (rect->x > area.x + area.width || rect->x + rect->w < area.x ||
|
||||
rect->y > area.y + area.height || rect->y + rect->h < area.y) {
|
||||
rect->w = 0.0f;
|
||||
rect->h = 0.0f;
|
||||
return;
|
||||
}
|
||||
|
||||
float x_diff = 0.0f;
|
||||
float y_diff = 0.0f;
|
||||
|
||||
// If the rectangle is partially out of the horizontal bounds of the drawing
|
||||
// area, calculate how much of its width will be clipped
|
||||
if (rect->x < area.x) {
|
||||
x_diff = area.x - rect->x;
|
||||
rect->x += x_diff;
|
||||
} else if (rect->x + rect->w > area.x + area.width) {
|
||||
x_diff = (rect->x + rect->w) - (area.x + area.width);
|
||||
}
|
||||
|
||||
// If the rectangle is partially out of the vertical bounds of the drawing
|
||||
// area, calculate how much of its height will be clipped
|
||||
if (rect->y < area.y) {
|
||||
y_diff = area.y - rect->y;
|
||||
rect->y += y_diff;
|
||||
} else if (rect->y + rect->h > area.y + area.height) {
|
||||
y_diff = (rect->y + rect->h) - (area.y + area.height);
|
||||
}
|
||||
|
||||
rect->w -= x_diff;
|
||||
rect->h -= y_diff;
|
||||
}
|
||||
|
||||
void render_texture_asset(AssetID id, float x, float y, float w, float h) {
|
||||
if (id < 0) {
|
||||
return;
|
||||
@@ -529,14 +608,6 @@ void set_playing_music_volume(int8_t volume) { Mix_VolumeMusic(volume); }
|
||||
|
||||
bool is_music_playing() { return Mix_PlayingMusic(); }
|
||||
|
||||
void close_asset(AssetID *id) {
|
||||
if (*id < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
remove_asset_from_manager(GameEngine.asset_manager, id);
|
||||
}
|
||||
|
||||
void handle_events() {
|
||||
if (!(GameEngine.running)) {
|
||||
return;
|
||||
@@ -586,31 +657,3 @@ void render() {
|
||||
|
||||
swap_render_buffers(&(GameEngine.window));
|
||||
}
|
||||
|
||||
void close_engine() {
|
||||
destroy_asset_manager(&(GameEngine.asset_manager));
|
||||
|
||||
destroy_object_store(&(GameEngine.object_store));
|
||||
|
||||
destroy_window(&(GameEngine.window));
|
||||
|
||||
GameEngine.last_time = 0.0f;
|
||||
|
||||
GameEngine.delta = 0.0f;
|
||||
|
||||
GameEngine.running = false;
|
||||
|
||||
Mix_CloseAudio();
|
||||
|
||||
Mix_Quit();
|
||||
|
||||
TTF_Quit();
|
||||
|
||||
IMG_Quit();
|
||||
|
||||
SDL_Quit();
|
||||
|
||||
#ifdef DEBUG_ENGINE
|
||||
printf("Game closed successfully\n");
|
||||
#endif /* DEBUG_ENGINE */
|
||||
}
|
||||
Reference in new issue
Block a user