Started working on figuring out how to properly support full screen and
high DPI
This commit is contained in:
1 parent
dbc8c88b89
commit
ffaa6e5ae7
8 files changed
+53
-22
No files matched your search
@@ -4,6 +4,7 @@
|
||||
#include "AssetManager.h"
|
||||
#include "ObjectStore.h"
|
||||
#include "SDL_pixels.h"
|
||||
#include "Window.h"
|
||||
#include "reusable_ids.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
@@ -12,10 +13,12 @@
|
||||
extern "C" {
|
||||
#endif // !__cplusplus
|
||||
|
||||
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 game_running();
|
||||
WindowSize get_window_defined_size();
|
||||
WindowSize get_window_drawable_size();
|
||||
ObjectID add_object(void *object, EventHandlerPtr eh, UpdateFuncPtr uf,
|
||||
RenderFuncPtr rf);
|
||||
void remove_object(ObjectID *id);
|
||||
|
||||
@@ -5,13 +5,14 @@
|
||||
#include "SDL_events.h"
|
||||
#include "engine_build_config.h"
|
||||
#include "reusable_ids.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // !__cplusplus
|
||||
|
||||
typedef void (*EventHandlerPtr)(const SDL_Event *event, void *object);
|
||||
typedef void (*UpdateFuncPtr)(float delta, void *object);
|
||||
typedef void (*EventHandlerPtr)(void *object, const SDL_Event *event);
|
||||
typedef void (*UpdateFuncPtr)(void *object, float delta);
|
||||
typedef void (*RenderFuncPtr)(void *object);
|
||||
|
||||
typedef struct object_store ObjectStore;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "SDL_surface.h"
|
||||
#include "SDL_video.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -15,6 +16,7 @@ typedef struct renderer Renderer;
|
||||
|
||||
Renderer *create_renderer();
|
||||
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);
|
||||
void render_clear(const Renderer *renderer, SDL_Color color);
|
||||
void render_surface(const Renderer *renderer, SDL_Surface *surface, float x,
|
||||
|
||||
+9
-3
@@ -5,20 +5,26 @@
|
||||
#include "SDL.h"
|
||||
#include "SDL_pixels.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // !__cplusplus
|
||||
|
||||
typedef struct window_size {
|
||||
int32_t width;
|
||||
int32_t height;
|
||||
} WindowSize;
|
||||
|
||||
typedef struct window {
|
||||
int width;
|
||||
int height;
|
||||
WindowSize defined_size;
|
||||
WindowSize drawable_size;
|
||||
Renderer *renderer;
|
||||
SDL_Window *SDL_base_window;
|
||||
SDL_Color background_color;
|
||||
} 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);
|
||||
void clear_window(const Window *wnd);
|
||||
void swap_render_buffers(const Window *wnd);
|
||||
|
||||
+14
-4
@@ -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
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
Reference in new issue
Block a user