Mercurial > molko
changeset 35:bdb51709fd55
core: implement error functions, closes #2453
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 13 Jan 2020 13:53:18 +0100 |
parents | e15361e5a46a |
children | 798baf7f3ec0 |
files | .hgignore Makefile src/error.c src/error.h src/main.c tests/test-error.c |
diffstat | 6 files changed, 183 insertions(+), 81 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Mon Jan 13 13:38:14 2020 +0100 +++ b/.hgignore Mon Jan 13 13:53:18 2020 +0100 @@ -14,6 +14,7 @@ ^libmolko\.a$ ^molko(\.exe)?$ ^tests/test-color$ +^tests/test-error$ ^tools/molko-map$ # doxygen stuff.
--- a/Makefile Mon Jan 13 13:38:14 2020 +0100 +++ b/Makefile Mon Jan 13 13:53:18 2020 +0100 @@ -25,6 +25,7 @@ SRCS= src/animation.c \ src/clock.c \ src/event.c \ + src/error.c \ src/font.c \ src/map.c \ src/message.c \ @@ -47,7 +48,8 @@ JANSSON_CFLAGS= `pkg-config --cflags jansson` JANSSON_LDFLAGS=`pkg-config --libs jansson` -TESTS= tests/test-color.c +TESTS= tests/test-color.c \ + tests/test-error.c TESTS_INCS= -I extern/libgreatest -I src ${SDL_CFLAGS} TESTS_LIBS= ${LIB} ${SDL_LDFLAGS} ${LDFLAGS} TESTS_OBJS= ${TESTS:.c=}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/error.c Mon Jan 13 13:53:18 2020 +0100 @@ -0,0 +1,63 @@ +/* + * error.c -- error routines + * + * 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> + +#include "error.h" + +static char buffer[2048]; + +const char * +error(void) +{ + return buffer; +} + +void +error_printf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + error_vprintf(fmt, ap); + va_end(ap); +} + +void +error_vprintf(const char *fmt, va_list ap) +{ + vsnprintf(buffer, sizeof (buffer), fmt, ap); +} + +noreturn void +error_fatalf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + error_vfatalf(fmt, ap); + va_end(ap); +} + +noreturn void +error_vfatalf(const char *fmt, va_list ap) +{ + error_vprintf(fmt, ap); + exit(1); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/error.h Mon Jan 13 13:53:18 2020 +0100 @@ -0,0 +1,72 @@ +/* + * error.h -- error routines + * + * 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_ERROR_H +#define MOLKO_ERROR_H + +/** + * \file error.h + * \brief Error routines. + */ + +#include <stdarg.h> +#include <stdnoreturn.h> + +/** + * Get the last error returned. + * + * \return the error string + */ +const char * +error(void); + +/** + * Set the game error with a printf-like format. + * + * \param fmt the format string + */ +void +error_printf(const char *fmt, ...); + +/** + * \Similar to \a error_printf. + * + * \param fmt the format stinrg + * \param ap the variadic arguments pointer + */ +void +error_vprintf(const char *fmt, va_list ap); + +/** + * Similar to \a error_printf but close the application. + * + * \param fmt the format string + */ +noreturn void +error_fatalf(const char *fmt, ...); + +/** + * Similar to \a error_fatalf + * + * \param fmt the format string + * \param ap the variadic arguments pointer + */ +noreturn void +error_vfatalf(const char *fmt, va_list ap); + +#endif /* !MOLKO_ERROR_H */
--- a/src/main.c Mon Jan 13 13:38:14 2020 +0100 +++ b/src/main.c Mon Jan 13 13:53:18 2020 +0100 @@ -16,91 +16,11 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include <stdio.h> -#include <stdlib.h> - -#include "animation.h" -#include "clock.h" -#include "event.h" -#include "font.h" -#include "image.h" -#include "map.h" -#include "message.h" -#include "sprite.h" -#include "sys.h" -#include "texture.h" -#include "window.h" -#include "painter.h" - -#include <SDL.h> - int main(int argc, char **argv) { (void)argc; (void)argv; - struct map map; - struct font *font; - - sys_init(); - if (!window_init("Molko's Adventure", 1280, 720)) { - printf("%s\n", SDL_GetError()); - exit(1); - } - - printf("----> [%s]\n", sys_datapath("default.map")); - exit(0); - - if (!map_open(&map, "default.map")) - exit(1); - - if (!(font = font_openf("assets/fonts/DejaVuSansCondensed.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: - return 0; - case EVENT_MOUSE: - printf("mouse moved to %d, %d, state: %d\n", ev.mouse.x, ev.mouse.y, ev.mouse.buttons); - break; - case EVENT_CLICKDOWN: - printf("mouse click on %d, %d, which: %d\n", ev.click.x, ev.click.y, ev.click.button); - break; - default: - break; - } - } - - painter_set_color(0x667788ff); - painter_clear(); -#if 0 - message_update(&welcome, elapsed); - message_draw(&welcome); -#endif - map_draw(&map); - painter_present(); - } - - sys_close(); - map_close(&map); - font_close(font); - return 0; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-error.c Mon Jan 13 13:53:18 2020 +0100 @@ -0,0 +1,44 @@ +/* + * test-error.c -- test error routines + * + * 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 <greatest.h> + +#include <error.h> + +TEST +simple(void) +{ + error_printf("Failed: %d", 123); + ASSERT_STR_EQ("Failed: 123", error()); + PASS(); +} + +SUITE(errors) +{ + RUN_TEST(simple); +} + +GREATEST_MAIN_DEFS(); + +int +main(int argc, char **argv) +{ + GREATEST_MAIN_BEGIN(); + RUN_SUITE(errors); + GREATEST_MAIN_END(); +}