diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-08-06 01:35:30 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-08-06 01:35:30 (EDT) |
commit | 592ee2f218f1886decc478c3353810c7c11b51d3 (patch) | |
tree | 91d8b4cd170bad380b2f34efd0fd3029ad18c30d /src | |
parent | ab7ea3c8367dad3101a250bfe6873753ba1875b1 (diff) | |
download | mazefight-592ee2f218f1886decc478c3353810c7c11b51d3.zip mazefight-592ee2f218f1886decc478c3353810c7c11b51d3.tar.gz mazefight-592ee2f218f1886decc478c3353810c7c11b51d3.tar.bz2 |
tk: Add window object
Diffstat (limited to 'src')
-rw-r--r-- | src/splash.c | 25 | ||||
-rw-r--r-- | src/tk.h | 18 | ||||
-rw-r--r-- | src/tk/local.mk | 3 | ||||
-rw-r--r-- | src/tk/widget.h | 19 | ||||
-rw-r--r-- | src/tk/window.c | 76 |
5 files changed, 116 insertions, 25 deletions
diff --git a/src/splash.c b/src/splash.c index 08eaeed..3c743a3 100644 --- a/src/splash.c +++ b/src/splash.c @@ -168,7 +168,7 @@ mf_splash(SDL_Renderer *renderer) TTF_Font *text_font = NULL; SDL_Color form_color; SDL_Color text_color; - struct mftk_widget *box; + struct mftk_window *win = NULL; SDL_Event event; /* Create maze */ @@ -204,17 +204,18 @@ mf_splash(SDL_Renderer *renderer) text_color.r = MF_COLOR_FORE_R, text_color.g = MF_COLOR_FORE_G; text_color.b = MF_COLOR_FORE_B, text_color.a = MF_COLOR_FORE_A; - box = mftk_box_new(MF_WINDOW_W, MF_WINDOW_H, 0, 0, MF_SPLASH_FORM_P, - &form_color, mftk_grid_new(2, 1, MF_SPLASH_TITLE_M, 0, - mftk_label_new(title_font, "Maze Fight", - &text_color, renderer), + win = mftk_window_new(mftk_box_new(MF_WINDOW_W, MF_WINDOW_H, 0, 0, + MF_SPLASH_FORM_P, &form_color, + mftk_grid_new(2, 1, MF_SPLASH_TITLE_M, 0, + mftk_label_new(title_font, "Maze Fight", + &text_color, renderer), MFTK_GRID_HALIGN_C|MFTK_GRID_VALIGN_T, - _mf_splash_form(renderer, text_font, - &text_color), + _mf_splash_form(renderer, text_font, + &text_color), MFTK_GRID_HALIGN_C|MFTK_GRID_VALIGN_T + ) ) ); - mftk_widget_layout(box); TTF_CloseFont(title_font); title_font = NULL; @@ -236,13 +237,13 @@ mf_splash(SDL_Renderer *renderer) SDL_RenderClear(renderer); mf_maze_render(maze, renderer, &maze_color, MF_SPLASH_MAZE_CELL_W); - mftk_widget_event(box, &event, 0, 0); - mftk_widget_render(box, renderer, 0, 0); + mftk_window_event(win, &event); + mftk_window_render(win, renderer); SDL_RenderPresent(renderer); } quit: - mftk_widget_destroy(&box); + mftk_window_destroy(&win); TTF_CloseFont(text_font); text_font = NULL; mf_maze_destroy(&maze); @@ -259,7 +260,7 @@ mf_splash(SDL_Renderer *renderer) if (text_font != NULL) { TTF_CloseFont(text_font); } - mftk_widget_destroy(&box); + mftk_window_destroy(&win); mf_maze_destroy(&maze); return -1; } @@ -23,26 +23,20 @@ #include <SDL.h> #include <SDL_ttf.h> +struct mftk_window; struct mftk_widget; -void -mftk_widget_set_visible(struct mftk_widget *w, int v); - -int -mftk_widget_get_visible(struct mftk_widget *w) - __attribute__((__pure__)); - -void -mftk_widget_layout(struct mftk_widget *w); +struct mftk_window * +mftk_window_new(struct mftk_widget *root); int -mftk_widget_event(struct mftk_widget *w, SDL_Event *e, int x, int y); +mftk_window_event(struct mftk_window *w, SDL_Event *e); int -mftk_widget_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y); +mftk_window_render(struct mftk_window *w, SDL_Renderer *renderer); void -mftk_widget_destroy(struct mftk_widget **w); +mftk_window_destroy(struct mftk_window **w_p); struct mftk_widget * mftk_label_new(TTF_Font *font, const char *text, SDL_Color *color, diff --git a/src/tk/local.mk b/src/tk/local.mk index 7522cc6..f5c3415 100644 --- a/src/tk/local.mk +++ b/src/tk/local.mk @@ -8,4 +8,5 @@ mazefight_SOURCES += \ %reldir%/radio.c \ %reldir%/text.c \ %reldir%/widget.c \ - %reldir%/widget.h + %reldir%/widget.h \ + %reldir%/window.c diff --git a/src/tk/widget.h b/src/tk/widget.h index b474f09..310dc56 100644 --- a/src/tk/widget.h +++ b/src/tk/widget.h @@ -50,6 +50,25 @@ mftk_widget_new(size_t size); t_w = (struct mftk_##name *) w; \ } while (0) +void +mftk_widget_set_visible(struct mftk_widget *w, int v); + +int +mftk_widget_get_visible(struct mftk_widget *w) + __attribute__((__pure__)); + +void +mftk_widget_layout(struct mftk_widget *w); + +int +mftk_widget_event(struct mftk_widget *w, SDL_Event *e, int x, int y); + +int +mftk_widget_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y); + +void +mftk_widget_destroy(struct mftk_widget **w); + struct mftk_widget * mftk_grid_new_a(int rows, int cols, int row_spacing, int col_spacing, struct mftk_widget **children, int *alignments); diff --git a/src/tk/window.c b/src/tk/window.c new file mode 100644 index 0000000..532a2e4 --- /dev/null +++ b/src/tk/window.c @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2021 P. J. McDermott + * + * This file is part of Maze Fight + * + * Maze Fight is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Maze Fight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Maze Fight. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <SDL.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include "../tk.h" +#include "widget.h" + +struct mftk_window { + struct mftk_widget *root; +}; + +struct mftk_window * +mftk_window_new(struct mftk_widget *root) +{ + struct mftk_window *w; + + w = calloc(1, sizeof(*w)); + if (w == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't create window: %s", + strerror(errno)); + return NULL; + } + + w->root = root; + + mftk_widget_layout(w->root); + + return w; +} + +int +mftk_window_event(struct mftk_window *w, SDL_Event *e) +{ + return mftk_widget_event(w->root, e, 0, 0); +} + +int +mftk_window_render(struct mftk_window *w, SDL_Renderer *renderer) +{ + return mftk_widget_render(w->root, renderer, 0, 0); +} + +void +mftk_window_destroy(struct mftk_window **w_p) +{ + struct mftk_window *w; + + if (w_p == NULL || *w_p == NULL) { + return; + } + w = *w_p; + + mftk_widget_destroy(&w->root); + free(w); + w = NULL; +} |