Mercurial > molko
view libmlk-adventure/adventure/molko.c @ 259:16be1ad3ddba
adventure: start working on maps and teleport
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 06 Dec 2020 11:22:03 +0100 |
parents | 71b3b7036de7 |
children | bfde372bf152 |
line wrap: on
line source
/* * molko.c -- main structure for Molko's Adventure * * 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 <stddef.h> #include <setjmp.h> #include <string.h> #include <locale.h> #include <core/clock.h> #include <core/core.h> #include <core/event.h> #include <core/image.h> #include <core/panic.h> #include <core/translate.h> #include <core/util.h> #include <core/sys.h> #include <core/window.h> #include <ui/ui.h> #include <rpg/rpg.h> #include <adventure/state/panic.h> #include <adventure/state/splashscreen.h> #include <adventure/state/mainmenu.h> #include <adventure/state/map.h> #include "molko.h" #define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 720 static jmp_buf panic_buf; struct molko molko; static void crash(void) { longjmp(panic_buf, 1); } static void loop(void) { struct clock clock = {0}; while (game.state) { unsigned int elapsed = clock_elapsed(&clock); clock_start(&clock); for (union event ev; event_poll(&ev); ) { switch (ev.type) { case EVENT_QUIT: return; default: game_handle(&ev); break; } } game_update(elapsed); game_draw(); if ((elapsed = clock_elapsed(&clock)) < 20) delay(20 - elapsed); } } void molko_init(void) { setlocale(LC_ALL, ""); if (!core_init("fr.malikania", "molko") || !ui_init() || !rpg_init()) panic(); translate_init("libmlk-adventure"); if (!window_open("Molko's Adventure", WINDOW_WIDTH, WINDOW_HEIGHT)) panic(); /* * From here, we can setup our panic state which requires a window * to be running. */ /* Init unrecoverable panic state. */ molko.panic = panic_state_new(); panic_handler = crash; /* Start to splash. */ game_switch(splashscreen_state_new(), true); } void molko_run(void) { if (setjmp(panic_buf) == 0) { /* Initial game run. */ loop(); } else { /* Clear event queue to avoid accidental key presses. */ for (union event ev; event_poll(&ev); ) continue; game_switch(molko.panic, true); loop(); } } void molko_teleport(const char *map, int origin_x, int origin_y) { game_switch(map_state_new(map, origin_x, origin_y), false); game.inhibit = INHIBIT_NONE; } const char * molko_path(const char *file) { assert(file); /* TODO: libmlk-adventure must be renamed. */ return pprintf("%s/libmlk-adventure/%s", sys_dir(SYS_DIR_DATA), file); } void molko_finish(void) { window_finish(); rpg_finish(); ui_finish(); core_finish(); }