/* * 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" #include "widget.h" 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_Color butn_color; SDL_Rect play_btn_rect; SDL_Texture *play_btn_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; butn_color.r = MF_COLOR_BUTN_R; butn_color.g = MF_COLOR_BUTN_G; butn_color.b = MF_COLOR_BUTN_B; butn_color.a = MF_COLOR_BUTN_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_widget_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; font = TTF_OpenFont(font_path, MF_SPLASH_TEXT_FONT_S); if (font == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open font: %s", TTF_GetError()); free(font_path); return -1; } /* TODO: Widgets */ /* Render play button */ play_btn_texture = mf_widget_button(font, "Play", &text_color, &butn_color, 2, renderer, &play_btn_rect); play_btn_rect.x = (MF_WINDOW_W - play_btn_rect.w) / 2; play_btn_rect.y += title_rect.y + title_rect.h + MF_SPLASH_TITLE_M; TTF_CloseFont(font); font = NULL; /* 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_RenderCopy(renderer, play_btn_texture, NULL,&play_btn_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; }