Mercurial > molko
changeset 45:e10fd1b6323f
core: implement splashscreen, closes #2458 @1h
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 15 Jan 2020 22:31:17 +0100 |
parents | c97fe725fdeb |
children | b815621df3e3 |
files | Makefile assets/fonts/knights-quest.ttf src/game.c src/game.h src/main.c src/splashscreen.c src/splashscreen.h src/window.c src/window.h |
diffstat | 9 files changed, 192 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Wed Jan 15 21:31:17 2020 +0100 +++ b/Makefile Wed Jan 15 22:31:17 2020 +0100 @@ -36,6 +36,7 @@ src/sys.c \ src/texture.c \ src/util.c \ + src/splashscreen.c \ src/walksprite.c \ src/window.c OBJS= ${SRCS:.c=.o}
--- a/src/game.c Wed Jan 15 21:31:17 2020 +0100 +++ b/src/game.c Wed Jan 15 22:31:17 2020 +0100 @@ -16,6 +16,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <assert.h> #include <stddef.h> #include "game.h" @@ -27,6 +28,14 @@ }; void +game_switch(struct state *state) +{ + assert(state); + + game.state_next = state; +} + +void game_handle(const union event *event) { if (game.state) @@ -44,6 +53,7 @@ game.state = game.state_next; game.state->enter(); + game.state_next = NULL; } if (game.state)
--- a/src/game.h Wed Jan 15 21:31:17 2020 +0100 +++ b/src/game.h Wed Jan 15 22:31:17 2020 +0100 @@ -43,6 +43,17 @@ extern struct game game; /** + * Request to change state. + * + * This function will only update state after the next \a game_update call. + * + * \pre state != NULL + * \param state the new state + */ +void +game_switch(struct state *state); + +/** * Handle input event. * * \param event the event
--- a/src/main.c Wed Jan 15 21:31:17 2020 +0100 +++ b/src/main.c Wed Jan 15 22:31:17 2020 +0100 @@ -17,9 +17,10 @@ */ #include "clock.h" -#include "game.h" #include "error.h" #include "event.h" +#include "game.h" +#include "splashscreen.h" #include "sys.h" #include "window.h" @@ -33,6 +34,9 @@ error_fatal(); if (!window_init("Molko's Adventure", WINDOW_WIDTH, WINDOW_HEIGHT)) error_fatal(); + + /* Default state is splash screen */ + game_switch(&splashscreen_state); } static void
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/splashscreen.c Wed Jan 15 22:31:17 2020 +0100 @@ -0,0 +1,97 @@ +/* + * splashscreen.c -- splash screen state + * + * 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. + */ + +#include <stdio.h> +#include <stdlib.h> // TODO: temporary + +#include "error.h" +#include "font.h" +#include "painter.h" +#include "splashscreen.h" +#include "state.h" +#include "sys.h" +#include "texture.h" +#include "window.h" + +#define DELAY 5000 + +static unsigned int elapsed; +static struct font *font; +static struct texture *text; +static int x; +static int y; + +static void +enter(void) +{ + if (!(font = font_openf(sys_datapath("fonts/knights-quest.ttf"), 100))) + error_fatal(); + if (!(text = font_render(font, "Molko's Adventure", 0x000000ff))) + error_fatal(); + + /* Compute position. */ + uint16_t w = 0; + uint16_t h = 0; + + if (!texture_get_size(text, &w, &h)) + error_fatal(); + + x = (window_width() / 2) - (w / 2); + y = (window_height() / 2) - (h / 2) - 100; +} + +static void +leave(void) +{ + font_close(font); +} + +static void +handle(const union event *event) +{ + (void)event; +} + +static void +update(unsigned int ticks) +{ + elapsed += ticks; + + /* TODO: change this once map is done. */ + if (elapsed >= DELAY) { + printf("splash finished!"); + exit(0); + } +} + +static void +draw(void) +{ + painter_set_color(0xffffffff); + painter_clear(); + texture_draw(text, x, y); + painter_present(); +} + +struct state splashscreen_state = { + .enter = enter, + .leave = leave, + .handle = handle, + .update = update, + .draw = draw +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/splashscreen.h Wed Jan 15 22:31:17 2020 +0100 @@ -0,0 +1,32 @@ +/* + * splashscreen.h -- splash screen state + * + * 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_SPLASHSCREEN_H +#define MOLKO_SPLASHSCREEN_H + +/** + * \file splashscreen.h + * \brief Splash screen state. + */ + +/** + * \brief Splash screen state. + */ +extern struct state splashscreen_state; + +#endif /* !MOLKO_SPLASHSCREEN_H */
--- a/src/window.c Wed Jan 15 21:31:17 2020 +0100 +++ b/src/window.c Wed Jan 15 22:31:17 2020 +0100 @@ -44,6 +44,26 @@ return true; } +unsigned +window_width(void) +{ + int width; + + SDL_GetWindowSize(win.win, &width, NULL); + + return width; +} + +unsigned +window_height(void) +{ + int height; + + SDL_GetWindowSize(win.win, NULL, &height); + + return height; +} + void window_close(void) {
--- a/src/window.h Wed Jan 15 21:31:17 2020 +0100 +++ b/src/window.h Wed Jan 15 22:31:17 2020 +0100 @@ -39,6 +39,22 @@ window_init(const char *title, unsigned width, unsigned height); /** + * Get the current window's width. + * + * \return the width + */ +unsigned +window_width(void); + +/** + * Get the current window's height. + * + * \return the height + */ +unsigned +window_height(void); + +/** * Close the window and destroy associated resources. */ void