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

+14 -4
View File
@@ -5,6 +5,7 @@
#include "SDL.h"
#include "SDL_error.h"
#include "SDL_events.h"
#include "SDL_hints.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
#include "SDL_pixels.h"
@@ -12,6 +13,7 @@
#include "SDL_surface.h"
#include "SDL_timer.h"
#include "SDL_ttf.h"
#include "SDL_video.h"
#include "Window.h"
#include "constants.h"
#include "engine_build_config.h"
@@ -35,11 +37,13 @@ INTERNAL struct game_engine {
AssetManager *asset_manager;
} GameEngine;
bool init_engine(const char *game_name, uint32_t window_width,
uint32_t window_height, SDL_Color window_background_color,
bool full_screen, uint8_t audio_tracks) {
bool init_engine(const char *game_name, WindowSize window_size,
SDL_Color window_background_color, bool full_screen,
uint8_t audio_tracks) {
bool initialisation_complete = true;
SDL_SetHint(SDL_HINT_VIDEODRIVER, "wayland,x11");
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
#ifdef DEBUG_ENGINE
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)) {
#ifdef DEBUG_ENGINE
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; }
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,
UpdateFuncPtr update_func, RenderFuncPtr render_func) {
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) {
if (os->event_handlers[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) {
if (os->update_funcs[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;
}
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) {
if (!renderer || !(renderer->SDL_base_renderer)) {
return;
+13 -8
View File
@@ -1,13 +1,14 @@
#include "Window.h"
#include "Renderer.h"
#include "SDL_video.h"
#include "engine_build_config.h"
#include <stdbool.h>
#ifdef DEBUG_ENGINE
#include <stdio.h>
#ifdef 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) {
if (!wnd) {
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;
if (full_screen) {
window_flags |= SDL_WINDOW_FULLSCREEN;
window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
wnd->SDL_base_window =
SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height, window_flags);
size.width, size.height, window_flags);
if (!wnd->SDL_base_window) {
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;
@@ -39,10 +41,13 @@ bool init_window(Window *wnd, const char *title, int width, int height,
return false;
}
get_drawable_size(wnd->renderer, &(wnd->drawable_size.width),
&(wnd->drawable_size.height));
#ifdef DEBUG_ENGINE
printf("Renderer initialised successfully\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 */
return true;
@@ -77,8 +82,8 @@ void destroy_window(Window *wnd) {
SDL_DestroyWindow(wnd->SDL_base_window);
wnd->SDL_base_window = NULL;
wnd->width = 0;
wnd->height = 0;
wnd->defined_size = (WindowSize){0, 0};
wnd->drawable_size = (WindowSize){0, 0};
wnd->background_color = (SDL_Color){0, 0, 0, 255};
#ifdef DEBUG_ENGINE