From 6f4631c8f82a2a631730767cad70c0b6ae02e0ad Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Mon, 02 Aug 2021 20:50:54 -0400 Subject: splash: New (WIP) screen --- (limited to 'src') diff --git a/src/defs.h b/src/defs.h index 7fa89b4..0b7a47a 100644 --- a/src/defs.h +++ b/src/defs.h @@ -20,7 +20,40 @@ #ifndef MF_DEFS_H_ #define MF_DEFS_H_ +/* Window dimensions */ #define MF_WINDOW_W 640 /* Window width */ #define MF_WINDOW_H 480 /* Window height */ +/* Splash menu */ +#define MF_SPLASH_MAZE_CELL_W 16 /* Background maze cell width */ +#define MF_SPLASH_WINDOW_P 24 /* Window padding */ +#define MF_SPLASH_FORM_P 24 /* Form padding */ +#define MF_SPLASH_TITLE_M 24 /* Margin under title */ +#define MF_SPLASH_ROW_M 16 /* Margin between rows */ +#define MF_SPLASH_COL_M 16 /* Margin between labels and entries */ +#define MF_SPLASH_LBL_M 8 /* Margin between radio buttons and labels */ +#define MF_SPLASH_BOX_W 16 /* Radio button and check box width */ +#define MF_SPLASH_BOX_P 2 /* Radio button and check box padding */ +#define MF_SPLASH_BTN_P 8 /* Button padding */ +#define MF_SPLASH_TITLE_FONT_S 48 /* Title font size */ +#define MF_SPLASH_TEXT_FONT_S 16 /* Regular text font size */ + +/* Colors */ +#define MF_COLOR_BACK_R 0xAF /* Background color */ +#define MF_COLOR_BACK_G 0xAF +#define MF_COLOR_BACK_B 0xAF +#define MF_COLOR_BACK_A 0xFF +#define MF_COLOR_MAZE_R 0x00 /* Maze wall color */ +#define MF_COLOR_MAZE_G 0x00 +#define MF_COLOR_MAZE_B 0x00 +#define MF_COLOR_MAZE_A 0xFF +#define MF_COLOR_FORM_R 0xDF /* Text/button color */ +#define MF_COLOR_FORM_G 0xDF +#define MF_COLOR_FORM_B 0xDF +#define MF_COLOR_FORM_A 0xDF +#define MF_COLOR_FORE_R 0x00 /* Text/button color */ +#define MF_COLOR_FORE_G 0x00 +#define MF_COLOR_FORE_B 0x00 +#define MF_COLOR_FORE_A 0xFF + #endif /* MF_DEFS_H_ */ diff --git a/src/local.mk b/src/local.mk index 891139c..bdde089 100644 --- a/src/local.mk +++ b/src/local.mk @@ -5,5 +5,7 @@ mazefight_SOURCES += \ %reldir%/main.c \ %reldir%/maze.c \ %reldir%/maze.h \ + %reldir%/splash.c \ + %reldir%/splash.h \ %reldir%/util.c \ %reldir%/util.h diff --git a/src/main.c b/src/main.c index 2180655..ff248f1 100644 --- a/src/main.c +++ b/src/main.c @@ -22,6 +22,7 @@ #include #include "defs.h" #include "dirs.h" +#include "splash.h" static SDL_Window *_mf_window; static SDL_Renderer *_mf_renderer; @@ -87,6 +88,8 @@ int main(int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) { + int e; + /* Find data directories */ mf_find_dirs(argv[0]); @@ -95,8 +98,13 @@ main(int argc __attribute__((__unused__)), return EXIT_FAILURE; } + e = EXIT_SUCCESS; + if (mf_splash(_mf_renderer) < 0) { + e = EXIT_FAILURE; + } + /* Quit SDL libraries */ _mf_quit(); - return EXIT_SUCCESS; + return e; } diff --git a/src/splash.c b/src/splash.c new file mode 100644 index 0000000..f662a0d --- /dev/null +++ b/src/splash.c @@ -0,0 +1,165 @@ +/* + * 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 . + */ + +#include +#include +#include +#include +#include "defs.h" +#include "dirs.h" +#include "maze.h" +#include "splash.h" +#include "util.h" + +static SDL_Texture * +_mf_splash_text(TTF_Font *font, const char *text, SDL_Color *color, + SDL_Renderer *renderer, SDL_Rect *rect) +{ + SDL_Surface *surface; + SDL_Texture *texture; + + surface = TTF_RenderUTF8_Blended(font, text, *color); + if (surface == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't create surface: %s", + TTF_GetError()); + return NULL; + } + + texture = SDL_CreateTextureFromSurface(renderer, surface); + if (texture == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't create texture: %s", + SDL_GetError()); + return NULL; + } + + rect->w = surface->w; + rect->h = surface->h; + + SDL_FreeSurface(surface); + + return texture; +} + +int +mf_splash(SDL_Renderer *renderer) +{ + char *font_path; + SDL_Color text_color; + TTF_Font *font; + SDL_Rect title_rect; + SDL_Texture *title_texture; + SDL_Rect form_rect; + struct mf_maze *maze; + SDL_Color maze_color; + SDL_Event event; + + font_path = mf_strcat(mf_get_fonts_dir(), "/FifteenTwenty-Regular.ttf"); + + 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; + + /* Render title text */ + font = TTF_OpenFont(font_path, MF_SPLASH_TITLE_FONT_S); + if (font == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't open font: %s", + TTF_GetError()); + free(font_path); + return -1; + } + title_texture = _mf_splash_text(font, "Maze Fight", &text_color, + renderer, &title_rect); + if (title_texture == NULL) { + goto err; + } + TTF_CloseFont(font); + font = NULL; + title_rect.x = (MF_WINDOW_W - title_rect.w) / 2; + title_rect.y = MF_SPLASH_WINDOW_P + MF_SPLASH_FORM_P; + + /* TODO: Widgets */ + + /* TODO: Size to widgets and center in window */ + form_rect.x = MF_SPLASH_WINDOW_P; + form_rect.y = MF_SPLASH_WINDOW_P; + form_rect.w = MF_WINDOW_W - MF_SPLASH_WINDOW_P*2; + form_rect.h = MF_WINDOW_H - MF_SPLASH_WINDOW_P*2; + + free(font_path); + font_path = NULL; + + /* Create maze */ + maze = mf_maze_new(time(NULL), + MF_WINDOW_W / MF_SPLASH_MAZE_CELL_W, + MF_WINDOW_H / MF_SPLASH_MAZE_CELL_W); + if (maze == NULL) { + goto err; + } + maze_color.r = MF_COLOR_MAZE_R; + maze_color.g = MF_COLOR_MAZE_G; + maze_color.b = MF_COLOR_MAZE_B; + maze_color.a = MF_COLOR_MAZE_A; + + SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); + + while (SDL_WaitEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + goto quit; + default: + break; + } + SDL_SetRenderDrawColor(renderer, + MF_COLOR_BACK_R, MF_COLOR_BACK_G, + MF_COLOR_BACK_B, MF_COLOR_BACK_A); + SDL_RenderClear(renderer); + mf_maze_render(maze, renderer, &maze_color, + MF_SPLASH_MAZE_CELL_W); + SDL_SetRenderDrawColor(renderer, + MF_COLOR_FORM_R, MF_COLOR_FORM_G, + MF_COLOR_FORM_B, MF_COLOR_FORM_A); + SDL_RenderFillRect(renderer, &form_rect); + SDL_RenderCopy(renderer, title_texture, NULL, &title_rect); + SDL_RenderPresent(renderer); + } + + quit: + SDL_DestroyTexture(title_texture); + title_texture = NULL; + + mf_maze_destroy(&maze); + + return 0; + + err: + if (font_path != NULL) { + free(font_path); + } + if (font != NULL) { + TTF_CloseFont(font); + } + if (title_texture != NULL) { + SDL_DestroyTexture(title_texture); + } + return -1; +} diff --git a/src/splash.h b/src/splash.h new file mode 100644 index 0000000..9b76db5 --- /dev/null +++ b/src/splash.h @@ -0,0 +1,28 @@ +/* + * 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 . + */ + +#ifndef MF_SPLASH_H_ +#define MF_SPLASH_H_ + +#include + +int +mf_splash(SDL_Renderer *renderer); + +#endif /* MF_SPLASH_H_ */ -- cgit v0.9.1