Mercurial > molko
changeset 24:4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 10 Jan 2020 13:30:01 +0100 |
parents | bc9637a2601b |
children | 0d5ecefcccd3 |
files | Makefile assets/fonts/DejaVuSans.ttf assets/fonts/DejaVuSansCondensed.ttf src/main.c src/message.c src/message.h |
diffstat | 6 files changed, 238 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Fri Jan 10 12:37:59 2020 +0100 +++ b/Makefile Fri Jan 10 13:30:01 2020 +0100 @@ -26,6 +26,7 @@ src/clock.c \ src/event.c \ src/font.c \ + src/message.c \ src/image.c \ src/sprite.c \ src/sys.c \
--- a/src/main.c Fri Jan 10 12:37:59 2020 +0100 +++ b/src/main.c Fri Jan 10 13:30:01 2020 +0100 @@ -17,16 +17,18 @@ */ #include <stdio.h> +#include <stdlib.h> #include "animation.h" #include "clock.h" #include "event.h" #include "font.h" #include "image.h" +#include "message.h" #include "sprite.h" +#include "sys.h" #include "texture.h" #include "window.h" -#include "sys.h" int main(int argc, char **argv) @@ -34,11 +36,29 @@ (void)argc; (void)argv; + struct font *font; + sys_init(); window_init("Molko's Adventure", 1280, 720); - window_set_color(0x667788ff); + + if (!(font = font_openf("assets/fonts/DejaVuSerifCondensed.ttf", 12))) + exit(1); + + struct message welcome = { + .text = { + "Welcome to this adventure Molko.", + "I'm pretty proud of you.", + "Now get of my house." + }, + .font = font + }; + struct clock clock; for (;;) { + uint64_t elapsed = clock_elapsed(&clock); + + clock_start(&clock); + for (union event ev; event_poll(&ev); ) { switch (ev.type) { case EVENT_QUIT: @@ -54,7 +74,10 @@ } } + window_set_color(0x667788ff); window_clear(); + message_update(&welcome, elapsed); + message_draw(&welcome); window_present(); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/message.c Fri Jan 10 13:30:01 2020 +0100 @@ -0,0 +1,101 @@ +/* + * message.c -- message dialog + * + * 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 <assert.h> + +#include "font.h" +#include "texture.h" +#include "message.h" +#include "sprite.h" +#include "window.h" + +#define MESSAGE_SPEED 1000 /* Time delay for animations */ +#define MESSAGE_TIMEOUT 5000 /* Time for auto-closing */ + +void +message_start(struct message *msg) +{ + assert(msg); + + msg->elapsed = 0; + msg->state = MESSAGE_OPENING; +} + +void +message_update(struct message *msg, unsigned ticks) +{ + assert(msg); + + msg->elapsed += ticks; + + switch (msg->state) { + case MESSAGE_OPENING: + if (msg->elapsed >= MESSAGE_SPEED) + msg->state = MESSAGE_SHOWING; + break; + case MESSAGE_SHOWING: + /* Do automatically switch state if requested by the user. */ + if (msg->flags & MESSAGE_AUTOMATIC && msg->elapsed >= MESSAGE_TIMEOUT) + msg->state = MESSAGE_HIDING; + + break; + default: + break; + } +} + +void +message_draw(struct message *msg) +{ + assert(msg); + + /* TODO: more constant variables. */ + struct texture *lines[3]; + int x = 160; + int y = 80; + + window_set_color(0xff0000ff); + window_draw_rectangle(true, x, y, 960, 160); + + for (int i = 0; msg->text[i]; ++i) { + lines[i] = font_render(msg->font, msg->text[i], 0xffffffff); + + if (!lines[i]) + continue; + + texture_draw(lines[i], x, y); + texture_close(lines[i]); + y += 53; + } +} + +void +message_hide(struct message *msg) +{ + assert(msg); + + msg->elapsed = 0; +} + +bool +message_is_complete(struct message *msg) +{ + assert(msg); + + return msg->state == MESSAGE_HIDING && msg->elapsed >= MESSAGE_SPEED; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/message.h Fri Jan 10 13:30:01 2020 +0100 @@ -0,0 +1,111 @@ +/* + * message.h -- message dialog + * + * 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_MESSAGE_H +#define MOLKO_MESSAGE_H + +/** + * \file message.h + * \brief Message dialog. + */ + +#include <stdbool.h> +#include <stdint.h> + +struct sprite; +struct font; + +/** + * \brief Message flags. + */ +enum message_flags { + MESSAGE_AUTOMATIC = (1 << 0) /*!< Message will automatically close */ +}; + +/** + * \brief Message state. + */ +enum message_state { + MESSAGE_OPENING, /*!< Message animation is opening */ + MESSAGE_SHOWING, /*!< Message is displaying */ + MESSAGE_HIDING /*!< Message animation for hiding */ +}; + +/** + * \brief Message object. + */ +struct message { + const char *text[3]; /*!< (RW) lines of text to show */ + struct sprite *theme; /*!< (RW) sprite to use for the frame */ + struct texture *avatar; /*!< (RW) optional avatar */ + struct font *font; /*!< (RW) font to use */ + enum message_flags flags; /*!< (RW) message flags */ + enum message_state state; /*!< (RO) current state */ + uint16_t elapsed; /*!< (RW) elapsed time while displaying */ +}; + +/** + * Start opening the message. This function will reset the message state and + * elapsed time. + * + * \pre msg != NULL + * \param msg the message + */ +void +message_start(struct message *msg); + +/** + * Update the message state and elapsed time.. + * + * \pre msg != NULL + * \param msg the message + * \param ticks the elapsed delay since last frame + */ +void +message_update(struct message *msg, unsigned ticks); + +/** + * Draw the message into the screen. + * + * \pre msg != NULL + * \param msg the message + */ +void +message_draw(struct message *msg); + +/** + * Start hiding the message. + * + * \pre msg != NULL + * \param msg the message + * \note You should still continue to draw the message as the animation is not + * finished! + */ +void +message_hide(struct message *msg); + +/** + * Tells if the message is complete. + * + * \pre msg != NULL + * \param msg the message + */ +bool +message_is_complete(struct message *msg); + +#endif /* !MOLKO_MESSAGE_H */