Started working on figuring out how to properly support full screen and

high DPI
This commit is contained in:
abdelrahman committed 2022-09-25 23:31:53 +01:00
1 parent dbc8c88b89
commit ffaa6e5ae7
8 files changed
+53 -22

No files matched your search

+6 -3
View File
@@ -4,6 +4,7 @@
#include "AssetManager.h" #include "AssetManager.h"
#include "ObjectStore.h" #include "ObjectStore.h"
#include "SDL_pixels.h" #include "SDL_pixels.h"
#include "Window.h"
#include "reusable_ids.h" #include "reusable_ids.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@@ -12,10 +13,12 @@
extern "C" { extern "C" {
#endif // !__cplusplus #endif // !__cplusplus
bool init_engine(const char *game_name, uint32_t window_width, bool init_engine(const char *game_name, WindowSize window_size,
uint32_t window_height, SDL_Color window_background_color, SDL_Color window_background_color, bool full_screen,
bool full_screen, uint8_t audio_tracks); uint8_t audio_tracks);
bool game_running(); bool game_running();
WindowSize get_window_defined_size();
WindowSize get_window_drawable_size();
ObjectID add_object(void *object, EventHandlerPtr eh, UpdateFuncPtr uf, ObjectID add_object(void *object, EventHandlerPtr eh, UpdateFuncPtr uf,
RenderFuncPtr rf); RenderFuncPtr rf);
void remove_object(ObjectID *id); void remove_object(ObjectID *id);
+3 -2
View File
@@ -5,13 +5,14 @@
#include "SDL_events.h" #include "SDL_events.h"
#include "engine_build_config.h" #include "engine_build_config.h"
#include "reusable_ids.h" #include "reusable_ids.h"
#include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif // !__cplusplus #endif // !__cplusplus
typedef void (*EventHandlerPtr)(const SDL_Event *event, void *object); typedef void (*EventHandlerPtr)(void *object, const SDL_Event *event);
typedef void (*UpdateFuncPtr)(float delta, void *object); typedef void (*UpdateFuncPtr)(void *object, float delta);
typedef void (*RenderFuncPtr)(void *object); typedef void (*RenderFuncPtr)(void *object);
typedef struct object_store ObjectStore; typedef struct object_store ObjectStore;
+2
View File
@@ -6,6 +6,7 @@
#include "SDL_surface.h" #include "SDL_surface.h"
#include "SDL_video.h" #include "SDL_video.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -15,6 +16,7 @@ typedef struct renderer Renderer;
Renderer *create_renderer(); Renderer *create_renderer();
bool init_renderer(Renderer *renderer, SDL_Window *window); bool init_renderer(Renderer *renderer, SDL_Window *window);
void get_drawable_size(Renderer *renderer, int32_t *w, int32_t *h);
SDL_Texture *load_texture(const Renderer *renderer, const char *path); SDL_Texture *load_texture(const Renderer *renderer, const char *path);
void render_clear(const Renderer *renderer, SDL_Color color); void render_clear(const Renderer *renderer, SDL_Color color);
void render_surface(const Renderer *renderer, SDL_Surface *surface, float x, void render_surface(const Renderer *renderer, SDL_Surface *surface, float x,
+9 -3
View File
@@ -5,20 +5,26 @@
#include "SDL.h" #include "SDL.h"
#include "SDL_pixels.h" #include "SDL_pixels.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif // !__cplusplus #endif // !__cplusplus
typedef struct window_size {
int32_t width;
int32_t height;
} WindowSize;
typedef struct window { typedef struct window {
int width; WindowSize defined_size;
int height; WindowSize drawable_size;
Renderer *renderer; Renderer *renderer;
SDL_Window *SDL_base_window; SDL_Window *SDL_base_window;
SDL_Color background_color; SDL_Color background_color;
} Window; } Window;
bool init_window(Window *wnd, const char *title, int width, int height, bool init_window(Window *wnd, const char *title, WindowSize size,
SDL_Color color, bool full_screen); SDL_Color color, bool full_screen);
void clear_window(const Window *wnd); void clear_window(const Window *wnd);
void swap_render_buffers(const Window *wnd); void swap_render_buffers(const Window *wnd);
+14 -4
View File
@@ -5,6 +5,7 @@
#include "SDL.h" #include "SDL.h"
#include "SDL_error.h" #include "SDL_error.h"
#include "SDL_events.h" #include "SDL_events.h"
#include "SDL_hints.h"
#include "SDL_image.h" #include "SDL_image.h"
#include "SDL_mixer.h" #include "SDL_mixer.h"
#include "SDL_pixels.h" #include "SDL_pixels.h"
@@ -12,6 +13,7 @@
#include "SDL_surface.h" #include "SDL_surface.h"
#include "SDL_timer.h" #include "SDL_timer.h"
#include "SDL_ttf.h" #include "SDL_ttf.h"
#include "SDL_video.h"
#include "Window.h" #include "Window.h"
#include "constants.h" #include "constants.h"
#include "engine_build_config.h" #include "engine_build_config.h"
@@ -35,11 +37,13 @@ INTERNAL struct game_engine {
AssetManager *asset_manager; AssetManager *asset_manager;
} GameEngine; } GameEngine;
bool init_engine(const char *game_name, uint32_t window_width, bool init_engine(const char *game_name, WindowSize window_size,
uint32_t window_height, SDL_Color window_background_color, SDL_Color window_background_color, bool full_screen,
bool full_screen, uint8_t audio_tracks) { uint8_t audio_tracks) {
bool initialisation_complete = true; bool initialisation_complete = true;
SDL_SetHint(SDL_HINT_VIDEODRIVER, "wayland,x11");
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
#ifdef DEBUG_ENGINE #ifdef DEBUG_ENGINE
printf("Failed to initialise SDL. Error: %s\n", SDL_GetError()); printf("Failed to initialise SDL. Error: %s\n", SDL_GetError());
@@ -88,7 +92,7 @@ bool init_engine(const char *game_name, uint32_t window_width,
} }
} }
if (!init_window(&(GameEngine.window), game_name, window_width, window_height, if (!init_window(&(GameEngine.window), game_name, window_size,
window_background_color, full_screen)) { window_background_color, full_screen)) {
#ifdef DEBUG_ENGINE #ifdef DEBUG_ENGINE
printf("Failed to initialise window\n"); printf("Failed to initialise window\n");
@@ -126,6 +130,12 @@ bool init_engine(const char *game_name, uint32_t window_width,
bool game_running() { return GameEngine.running; } bool game_running() { return GameEngine.running; }
WindowSize get_window_defined_size() { return GameEngine.window.defined_size; }
WindowSize get_window_drawable_size() {
return GameEngine.window.drawable_size;
}
ObjectID add_object(void *object, EventHandlerPtr event_handler, ObjectID add_object(void *object, EventHandlerPtr event_handler,
UpdateFuncPtr update_func, RenderFuncPtr render_func) { UpdateFuncPtr update_func, RenderFuncPtr render_func) {
if (!(GameEngine.running) || !(GameEngine.object_store)) { if (!(GameEngine.running) || !(GameEngine.object_store)) {
+2 -2
View File
@@ -105,7 +105,7 @@ void handle_all_objects_events(ObjectStore *os, const SDL_Event *event) {
for (int i = 0; i < os->count; ++i) { for (int i = 0; i < os->count; ++i) {
if (os->event_handlers[i]) { if (os->event_handlers[i]) {
void *object = os->object_ptrs[i]; void *object = os->object_ptrs[i];
(os->event_handlers[i])(event, object); (os->event_handlers[i])(object, event);
} }
} }
} }
@@ -118,7 +118,7 @@ void update_all_objects(ObjectStore *os, float delta) {
for (int i = 0; i < os->count; ++i) { for (int i = 0; i < os->count; ++i) {
if (os->update_funcs[i]) { if (os->update_funcs[i]) {
void *object = os->object_ptrs[i]; void *object = os->object_ptrs[i];
(os->update_funcs[i])(delta, object); (os->update_funcs[i])(object, delta);
} }
} }
} }
+4
View File
@@ -32,6 +32,10 @@ bool init_renderer(Renderer *renderer, SDL_Window *window) {
return renderer->SDL_base_renderer ? true : false; return renderer->SDL_base_renderer ? true : false;
} }
void get_drawable_size(Renderer *renderer, int32_t *w, int32_t *h) {
SDL_GetRendererOutputSize(renderer->SDL_base_renderer, w, h);
}
void render_clear(const Renderer *renderer, SDL_Color color) { void render_clear(const Renderer *renderer, SDL_Color color) {
if (!renderer || !(renderer->SDL_base_renderer)) { if (!renderer || !(renderer->SDL_base_renderer)) {
return; return;
+13 -8
View File
@@ -1,13 +1,14 @@
#include "Window.h" #include "Window.h"
#include "Renderer.h" #include "Renderer.h"
#include "SDL_video.h"
#include "engine_build_config.h" #include "engine_build_config.h"
#include <stdbool.h> #include <stdbool.h>
#ifdef DEBUG_ENGINE
#include <stdio.h> #include <stdio.h>
#ifdef DEBUG_ENGINE
#endif /* DEBUG_ENGINE */ #endif /* DEBUG_ENGINE */
bool init_window(Window *wnd, const char *title, int width, int height, bool init_window(Window *wnd, const char *title, WindowSize size,
SDL_Color color, bool full_screen) { SDL_Color color, bool full_screen) {
if (!wnd) { if (!wnd) {
return false; return false;
@@ -16,18 +17,19 @@ bool init_window(Window *wnd, const char *title, int width, int height,
Uint32 window_flags = SDL_WINDOW_ALLOW_HIGHDPI; Uint32 window_flags = SDL_WINDOW_ALLOW_HIGHDPI;
if (full_screen) { if (full_screen) {
window_flags |= SDL_WINDOW_FULLSCREEN; window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
} }
wnd->SDL_base_window = wnd->SDL_base_window =
SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height, window_flags); size.width, size.height, window_flags);
if (!wnd->SDL_base_window) { if (!wnd->SDL_base_window) {
return false; return false;
} }
SDL_GetWindowSize(wnd->SDL_base_window, &(wnd->width), &(wnd->height)); wnd->defined_size.width = size.width;
wnd->defined_size.height = size.height;
wnd->background_color = color; wnd->background_color = color;
@@ -39,10 +41,13 @@ bool init_window(Window *wnd, const char *title, int width, int height,
return false; return false;
} }
get_drawable_size(wnd->renderer, &(wnd->drawable_size.width),
&(wnd->drawable_size.height));
#ifdef DEBUG_ENGINE #ifdef DEBUG_ENGINE
printf("Renderer initialised successfully\n"); printf("Renderer initialised successfully\n");
printf("Window initialised successfully with width %d and height %d\n", printf("Window initialised successfully with width %d and height %d\n",
wnd->width, wnd->height); wnd->drawable_size.width, wnd->drawable_size.height);
#endif /* DEBUG_ENGINE */ #endif /* DEBUG_ENGINE */
return true; return true;
@@ -77,8 +82,8 @@ void destroy_window(Window *wnd) {
SDL_DestroyWindow(wnd->SDL_base_window); SDL_DestroyWindow(wnd->SDL_base_window);
wnd->SDL_base_window = NULL; wnd->SDL_base_window = NULL;
wnd->width = 0; wnd->defined_size = (WindowSize){0, 0};
wnd->height = 0; wnd->drawable_size = (WindowSize){0, 0};
wnd->background_color = (SDL_Color){0, 0, 0, 255}; wnd->background_color = (SDL_Color){0, 0, 0, 255};
#ifdef DEBUG_ENGINE #ifdef DEBUG_ENGINE