changeset 97:58133933ea17

window: expose some data to avoid functions
author David Demelier <markand@malikania.fr>
date Mon, 30 Mar 2020 20:10:00 +0200
parents 04011a942e3b
children c7e993455985
files doxygen/page-howto-initialization.c src/adventure/main.c src/adventure/mainmenu_state.c src/adventure/panic_state.c src/adventure/splashscreen_state.c src/core/map.c src/core/map_state.c src/core/message.c src/core/painter.c src/core/sys.c src/core/sys.h src/core/texture.c src/core/window.c src/core/window.h src/core/window_p.h tests/test-map.c
diffstat 16 files changed, 75 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/doxygen/page-howto-initialization.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/doxygen/page-howto-initialization.c	Mon Mar 30 20:10:00 2020 +0200
@@ -17,10 +17,10 @@
  * The following table summarize the functions to be used at the beginning and
  * the end of your game.
  *
- * | System  | Init function    | Close function    | Remarks                |
- * |---------|------------------|-------------------|------------------------|
- * | General | \ref sys_init    | \ref sys_close    | Required for most API  |
- * | Window  | \ref window_init | \ref window_close | Required by some parts |
+ * | System  | Init function    | Close function     | Remarks                |
+ * |---------|------------------|--------------------|------------------------|
+ * | General | \ref sys_init    | \ref sys_finish    | Required for most API  |
+ * | Window  | \ref window_init | \ref window_finish | Required by some parts |
  *
  * All init functions set an error code if any and you're encouraged to test the
  * result and check the error if any.
--- a/src/adventure/main.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/adventure/main.c	Mon Mar 30 20:10:00 2020 +0200
@@ -130,8 +130,8 @@
 static void
 close(void)
 {
-	window_close();
-	sys_close();
+	window_finish();
+	sys_finish();
 }
 
 int
--- a/src/adventure/mainmenu_state.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/adventure/mainmenu_state.c	Mon Mar 30 20:10:00 2020 +0200
@@ -68,12 +68,12 @@
 	substate = SUBSTATE_MOVING;
 	x = splashscreen_state_data.x;
 	y = splashscreen_state_data.y;
-	destination = window_height() / 4;
+	destination = window.h / 4;
 
 	/* TODO: change continue color if no game exists. */
 	font_render(&font, &items[0].texture, "New game");
-	items[0].x = (window_width() / 2) - (items[0].texture.w / 2);
-	items[0].y = window_height() * 0.75;
+	items[0].x = (window.w / 2) - (items[0].texture.w / 2);
+	items[0].y = window.h * 0.75;
 
 	font_render(&font, &items[1].texture, "Continue");
 	items[1].x = items[0].x;
--- a/src/adventure/panic_state.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/adventure/panic_state.c	Mon Mar 30 20:10:00 2020 +0200
@@ -192,7 +192,7 @@
 	texture_draw(&lerror.texture, PADDING, y + PADDING);
 
 	/* Bottom. */
-	y = window_height() - PADDING;
+	y = window.h - PADDING;
 	y -= bottom[0].texture.h;
 
 	for (size_t i = 0; i < NELEM(bottom); ++i) {
--- a/src/adventure/splashscreen_state.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/adventure/splashscreen_state.c	Mon Mar 30 20:10:00 2020 +0200
@@ -51,8 +51,8 @@
 	const unsigned int w = splashscreen_state_data.text.w;
 	const unsigned int h = splashscreen_state_data.text.h;
 
-	splashscreen_state_data.x = (window_width() / 2) - (w / 2);
-	splashscreen_state_data.y = (window_height() / 2) - (h / 2);
+	splashscreen_state_data.x = (window.w / 2) - (w / 2);
+	splashscreen_state_data.y = (window.h / 2) - (h / 2);
 
 	font_finish(&font);
 }
--- a/src/core/map.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/core/map.c	Mon Mar 30 20:10:00 2020 +0200
@@ -190,12 +190,12 @@
 		&map->picture,
 		srcx,
 		srcy,
-		window_width(),
-		window_height(),
+		window.w,
+		window.h,
 		0,
 		0,
-		window_width(),
-		window_height(),
+		window.w,
+		window.h,
 		0
 	);
 }
--- a/src/core/map_state.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/core/map_state.c	Mon Mar 30 20:10:00 2020 +0200
@@ -127,8 +127,8 @@
 	map_state_data.map.h = m->picture.h;
 
 	/* Adjust view. */
-	map_state_data.view.w = window_width();
-	map_state_data.view.h = window_height();
+	map_state_data.view.w = window.w;
+	map_state_data.view.h = window.h;
 
 	/* Adjust margin. */
 	cache.margin.w = map_state_data.view.w - (MARGIN_WIDTH * 2);
--- a/src/core/message.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/core/message.c	Mon Mar 30 20:10:00 2020 +0200
@@ -217,7 +217,7 @@
 
 	const unsigned int w = msg->frame->w;
 	const unsigned int h = msg->frame->h;
-	const unsigned int x = (window_width() / 2) - (w / 2);
+	const unsigned int x = (window.w / 2) - (w / 2);
 	const unsigned int y = 80;
 	const unsigned int avgh = average(msg);
 	const unsigned int gapy = (h - (avgh * 6)) / 7;
--- a/src/core/painter.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/core/painter.c	Mon Mar 30 20:10:00 2020 +0200
@@ -19,8 +19,10 @@
 #include "color.h"
 #include "painter.h"
 #include "texture.h"
+#include "window.h"
 #include "window_p.h"
 
+/* Current texture renderer. */
 static struct texture *renderer;
 
 struct texture *
@@ -33,7 +35,7 @@
 painter_set_target(struct texture *tex)
 {
 	renderer = tex;
-	SDL_SetRenderTarget(win.renderer, tex ? tex->handle : NULL);
+	SDL_SetRenderTarget(RENDERER(), tex ? tex->handle : NULL);
 }
 
 unsigned long
@@ -41,7 +43,7 @@
 {
 	Uint8 r = 0, g = 0, b = 0, a = 0;
 
-	SDL_GetRenderDrawColor(win.renderer, &r, &g, &b, &a);
+	SDL_GetRenderDrawColor(RENDERER(), &r, &g, &b, &a);
 
 	return COLOR_HEX(r, g, b, a);
 }
@@ -50,7 +52,7 @@
 painter_set_color(unsigned long color)
 {
 	SDL_SetRenderDrawColor(
-		win.renderer,
+		RENDERER(),
 		COLOR_R(color),
 		COLOR_G(color),
 		COLOR_B(color),
@@ -61,13 +63,13 @@
 void
 painter_draw_line(int x1, int y1, int x2, int y2)
 {
-	SDL_RenderDrawLine(win.renderer, x1, y1, x2, y2);
+	SDL_RenderDrawLine(RENDERER(), x1, y1, x2, y2);
 }
 
 void
 painter_draw_point(int x1, int y1)
 {
-	SDL_RenderDrawPoint(win.renderer, x1, y1);
+	SDL_RenderDrawPoint(RENDERER(), x1, y1);
 }
 
 void
@@ -80,17 +82,17 @@
 		.y = y
 	};
 
-	SDL_RenderFillRect(win.renderer, &rect);
+	SDL_RenderFillRect(RENDERER(), &rect);
 }
 
 void
 painter_clear(void)
 {
-	SDL_RenderClear(win.renderer);
+	SDL_RenderClear(RENDERER());
 }
 
 void
 painter_present(void)
 {
-	SDL_RenderPresent(win.renderer);
+	SDL_RenderPresent(RENDERER());
 }
--- a/src/core/sys.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/core/sys.c	Mon Mar 30 20:10:00 2020 +0200
@@ -170,7 +170,7 @@
 }
 
 void
-sys_close(void)
+sys_finish(void)
 {
 	Mix_Quit();
 	TTF_Quit();
--- a/src/core/sys.h	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/core/sys.h	Mon Mar 30 20:10:00 2020 +0200
@@ -78,6 +78,6 @@
  * Close the system.
  */
 void
-sys_close(void);
+sys_finish(void);
 
 #endif /* !MOLKO_SYS_H */
--- a/src/core/texture.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/core/texture.c	Mon Mar 30 20:10:00 2020 +0200
@@ -23,6 +23,7 @@
 #include "texture.h"
 #include "texture_p.h"
 #include "util.h"
+#include "window.h"
 #include "window_p.h"
 
 bool
@@ -30,7 +31,7 @@
 {
 	assert(tex);
 
-	tex->handle = SDL_CreateTexture(win.renderer,
+	tex->handle = SDL_CreateTexture(RENDERER(),
 	    SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
 
 	if (!tex->handle) {
@@ -64,7 +65,7 @@
 		.h = tex->h
 	};
 
-	SDL_RenderCopy(win.renderer, tex->handle, NULL, &dst);
+	SDL_RenderCopy(RENDERER(), tex->handle, NULL, &dst);
 }
 
 void
@@ -92,7 +93,7 @@
 		.h = dst_h
 	};
 
-	SDL_RenderCopyEx(win.renderer, tex->handle, &src, &dst, angle, NULL, SDL_FLIP_NONE);
+	SDL_RenderCopyEx(RENDERER(), tex->handle, &src, &dst, angle, NULL, SDL_FLIP_NONE);
 }
 
 void
@@ -114,7 +115,7 @@
 	assert(tex);
 	assert(surface);
 
-	if (!(tex->handle = SDL_CreateTextureFromSurface(win.renderer, surface))) {
+	if (!(tex->handle = SDL_CreateTextureFromSurface(RENDERER(), surface))) {
 		tex->w = tex->h = 0;
 		return error_sdl();
 	}
--- a/src/core/window.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/core/window.c	Mon Mar 30 20:10:00 2020 +0200
@@ -24,49 +24,37 @@
 #include "window.h"
 #include "window_p.h"
 
-/* global object, used by textures */
-struct window win = {
+static struct window_handle handle = {
 	.win = NULL,
 	.renderer = NULL
 };
 
+struct window window = {
+	.handle = &handle
+};
+
 bool
-window_init(const char *title, unsigned int width, unsigned int height)
+window_init(const char *title, unsigned int w, unsigned int h)
 {
 	assert(title);
 
-	if (SDL_CreateWindowAndRenderer(width, height, SDL_WINDOW_OPENGL,
-	    &win.win, &win.renderer) < 0)
+	if (SDL_CreateWindowAndRenderer(w, h, SDL_WINDOW_OPENGL,
+	    &handle.win, &handle.renderer) < 0)
 		return error_sdl();
 
-	SDL_SetWindowTitle(win.win, title);
+	SDL_SetWindowTitle(handle.win, title);
+
+	window.w = w;
+	window.h = h;
 
 	return true;
 }
 
-unsigned int
-window_width(void)
-{
-	int width;
-
-	SDL_GetWindowSize(win.win, &width, NULL);
-
-	return width;
-}
-
-unsigned int
-window_height(void)
+void
+window_finish(void)
 {
-	int height;
-
-	SDL_GetWindowSize(win.win, NULL, &height);
-
-	return height;
+	if (handle.renderer)
+		SDL_DestroyRenderer(handle.renderer);
+	if (handle.win)
+		SDL_DestroyWindow(handle.win);
 }
-
-void
-window_close(void)
-{
-	SDL_DestroyRenderer(win.renderer);
-	SDL_DestroyWindow(win.win);
-}
--- a/src/core/window.h	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/core/window.h	Mon Mar 30 20:10:00 2020 +0200
@@ -28,6 +28,20 @@
 #include <stdbool.h>
 
 /**
+ * \brief Global exposed window structure.
+ */
+struct window {
+	unsigned int w;         /*!< (RO) Window width. */
+	unsigned int h;         /*!< (RO) Window height. */
+	void *handle;           /*!< (RO) Native handle. */
+};
+
+/**
+ * \brief Access to global window structure.
+ */
+extern struct window window;
+
+/**
  * Initialize window.
  *
  * \pre title != NULL
@@ -40,25 +54,9 @@
 window_init(const char *title, unsigned int width, unsigned int height);
 
 /**
- * Get the current window's width.
- *
- * \return the width
- */
-unsigned int
-window_width(void);
-
-/**
- * Get the current window's height.
- *
- * \return the height
- */
-unsigned int
-window_height(void);
-
-/**
  * Close the window and destroy associated resources.
  */
 void
-window_close(void);
+window_finish(void);
 
 #endif /* !MOLKO_WINDOW_H */
--- a/src/core/window_p.h	Mon Mar 30 20:05:00 2020 +0200
+++ b/src/core/window_p.h	Mon Mar 30 20:10:00 2020 +0200
@@ -21,12 +21,13 @@
 
 #include <SDL.h>
 
-struct window {
+struct window_handle {
 	SDL_Window *win;
 	SDL_Renderer *renderer;
 };
 
-/* Global window object */
-extern struct window win;
+/* Convenient macros to access the native handle from global window object. */
+#define WINDOW()        (((struct window_handle *)window.handle)->win)
+#define RENDERER()      (((struct window_handle *)window.handle)->renderer)
 
 #endif /* !MOLKO_WINDOW_P_H */
--- a/tests/test-map.c	Mon Mar 30 20:05:00 2020 +0200
+++ b/tests/test-map.c	Mon Mar 30 20:10:00 2020 +0200
@@ -40,8 +40,8 @@
 {
 	(void)data;
 
-	window_close();
-	sys_close();
+	window_finish();
+	sys_finish();
 }
 
 TEST