Mercurial > molko
changeset 2:832c20d6cce9
core: make window singleton for convenience (textures)
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 06 Jan 2020 20:20:58 +0100 |
parents | 03f6d572fd17 |
children | c013f41a72a5 |
files | src/window.c src/window.h src/window_p.h |
diffstat | 3 files changed, 71 insertions(+), 89 deletions(-) [+] |
line wrap: on
line diff
--- a/src/window.c Mon Jan 06 13:06:52 2020 +0100 +++ b/src/window.c Mon Jan 06 20:20:58 2020 +0100 @@ -21,77 +21,63 @@ #include <SDL.h> #include "window.h" +#include "window_p.h" -struct window { - SDL_Window *win; - SDL_Renderer *renderer; +/* global object, used by textures */ +struct window win = { + .win = NULL, + .renderer = NULL }; -struct window * +bool window_init(const char *title, unsigned width, unsigned height) { - struct window *w; - assert(title); - if (!(w = calloc(1, sizeof (struct window)))) - return NULL; if (SDL_CreateWindowAndRenderer(width, height, SDL_WINDOW_OPENGL, - &w->win, &w->renderer) < 0) - goto fail; + &win.win, &win.renderer) < 0) + return false; - SDL_SetWindowTitle(w->win, title); - - return w; + SDL_SetWindowTitle(win.win, title); -fail: - free(w); - return NULL; + return true; } uint32_t -window_get_color(const struct window *w) +window_get_color(void) { - assert(w); - Uint8 r = 0, g = 0, b = 0, a = 0; - SDL_GetRenderDrawColor(w->renderer, &r, &g, &b, &a); + SDL_GetRenderDrawColor(win.renderer, &r, &g, &b, &a); return r << 24 | g << 16 | b << 8 | a; } void -window_set_color(struct window *w, uint32_t color) +window_set_color(uint32_t color) { - assert(w); - const Uint8 r = color >> 24 & 0xff; const Uint8 g = color >> 16 & 0xff; const Uint8 b = color >> 8 & 0xff; const Uint8 a = color & 0xff; - SDL_SetRenderDrawColor(w->renderer, r, g, b, a); + SDL_SetRenderDrawColor(win.renderer, r, g, b, a); } void -window_draw_line(struct window *w, int x1, int y1, int x2, int y2) +window_draw_line(int x1, int y1, int x2, int y2) { - assert(w); - - SDL_RenderDrawLine(w->renderer, x1, y1, x2, y2); + SDL_RenderDrawLine(win.renderer, x1, y1, x2, y2); } void -window_draw_point(struct window *w, int x1, int y1) +window_draw_point(int x1, int y1) { - assert(w); - - SDL_RenderDrawPoint(w->renderer, x1, y1); + SDL_RenderDrawPoint(win.renderer, x1, y1); } void -window_draw_rectangle(struct window *w, bool fill, int x, int y, unsigned width, unsigned height) +window_draw_rectangle(bool fill, int x, int y, unsigned width, unsigned height) { const SDL_Rect rect = { .w = width, @@ -99,37 +85,30 @@ .x = x, .y = y }; - + assert(w); if (fill) - SDL_RenderFillRect(w->renderer, &rect); + SDL_RenderFillRect(win.renderer, &rect); else - SDL_RenderDrawRect(w->renderer, &rect); + SDL_RenderDrawRect(win.renderer, &rect); } void -window_clear(struct window *window) +window_clear(void) { - assert(window); - - SDL_RenderClear(window->renderer); + SDL_RenderClear(win.renderer); } void -window_present(struct window *window) +window_present(void) { - assert(window); - - SDL_RenderPresent(window->renderer); + SDL_RenderPresent(win.renderer); } void -window_close(struct window *w) +window_close(void) { - assert(window); - - SDL_DestroyRenderer(w->renderer); - SDL_DestroyWindow(w->win); - free(w); + SDL_DestroyRenderer(win.renderer); + SDL_DestroyWindow(win.win); }
--- a/src/window.h Mon Jan 06 13:06:52 2020 +0100 +++ b/src/window.h Mon Jan 06 20:20:58 2020 +0100 @@ -28,71 +28,56 @@ #include <stdint.h> /** - * \brief Window opaque object. - */ -struct window; - -/** * Initialize window. * * \pre title != NULL * \param title the window title * \param width the desired width * \param height the desired height - * \return the window or NULL on error + * \return true on success */ -struct window * +bool window_init(const char *title, unsigned width, unsigned height); /** * Get the current drawing color. * - * \pre window != NULL - * \param window the window * \return the color in RRGGBBAA format */ uint32_t -window_get_color(const struct window *window); +window_get_color(void); /** * Set the rendering drawing color. * - * \pre window != NULL - * \param window the window * \param color in RRGGBBAA format */ void -window_set_color(struct window *window, uint32_t color); +window_set_color(uint32_t color); /** * Draw a line. * - * \pre window != NULL - * \param window the window * \param x1 first X coordinate * \param y1 first Y coordinate * \param x2 second X coordinate * \param y2 second Y coordinate */ void -window_draw_line(struct window *window, int x1, int y1, int x2, int y2); +window_draw_line(int x1, int y1, int x2, int y2); /** * Draw a pixel point. * - * \pre window != NULL - * \param window the window * \param x the X coordinate * \param y the Y coordinate */ void -window_draw_point(struct window *window, int x, int y); +window_draw_point(int x, int y); /** * Draw a rectangle * - * \pre window != NULL - * \param window the window * \param fill set to true to fill the rectangle * \param x the X coordinate * \param y the Y coordinate @@ -100,39 +85,25 @@ * \param h the rectangle height */ void -window_draw_rectangle(struct window *window, - bool fill, - int x, - int y, - unsigned w, - unsigned h); +window_draw_rectangle(bool fill, int x, int y, unsigned w, unsigned h); /** * Clear the window. - * - * \pre window != NULL - * \param window the window */ void -window_clear(struct window *window); +window_clear(void); /** * Present the window, only call this function one time in the main loop. - * - * \pre window != NULL - * \param window the window */ void -window_present(struct window *window); +window_present(void); /** * Close the window and destroy associated resources, do not use pointer * afterwards. - * - * \pre window != NULL - * \param window the window */ void -window_close(struct window *window); +window_close(void); #endif /* !MOLKO_WINDOW_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/window_p.h Mon Jan 06 20:20:58 2020 +0100 @@ -0,0 +1,32 @@ +/* + * window.c -- (PRIVATE) basic window management + * + * Copyright (c) 2020 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MOLKO_WINDOW_P_H +#define MOLKO_WINDOW_P_H + +#include <SDL.h> + +struct window { + SDL_Window *win; + SDL_Renderer *renderer; +}; + +/* Global window object */ +extern struct window win; + +#endif /* !MOLKO_WINDOW_P_H */