diff libmlk-adventure/adventure/state/continue.c @ 281:87b8c7510717

rpg: implement load/save for characters
author David Demelier <markand@malikania.fr>
date Sun, 20 Dec 2020 10:55:53 +0100
parents
children a15f77eda9a4
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmlk-adventure/adventure/state/continue.c	Sun Dec 20 10:55:53 2020 +0100
@@ -0,0 +1,109 @@
+/*
+ * continue.c -- select save to continue the game
+ *
+ * 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 <core/alloc.h>
+#include <core/event.h>
+#include <core/game.h>
+#include <core/painter.h>
+#include <core/state.h>
+
+#include <adventure/dialog/save.h>
+
+#include "mainmenu.h"
+#include "continue.h"
+
+struct self {
+	struct state state;
+	struct dialog_save dialog;
+};
+
+static void
+start(struct state *state)
+{
+	struct self *self = state->data;
+
+	dialog_save_init(&self->dialog);
+}
+
+static void
+handle(struct state *state, const union event *ev)
+{
+	struct self *self = state->data;
+	bool selected = false;
+
+	switch (ev->type) {
+	case EVENT_QUIT:
+		game_quit();
+		break;
+	case EVENT_KEYDOWN:
+		if (ev->key.key == KEY_ESCAPE)
+			game_switch(mainmenu_state_new(), false);
+		else
+			selected = dialog_save_handle(&self->dialog, ev);
+		break;
+	default:
+		selected = dialog_save_handle(&self->dialog, ev);
+		break;
+	}
+
+	if (selected)
+		game_quit();
+}
+
+static void
+update(struct state *state, unsigned int ticks)
+{
+	struct self *self = state->data;
+
+	dialog_save_update(&self->dialog, ticks);
+}
+
+static void
+draw(struct state *state)
+{
+	struct self *self = state->data;
+
+	painter_set_color(0xffffffff);
+	painter_clear();
+	dialog_save_draw(&self->dialog);
+	painter_present();
+}
+
+static void
+finish(struct state *state)
+{
+	struct self *self = state->data;
+
+	dialog_save_finish(&self->dialog);
+}
+
+struct state *
+continue_state_new(void)
+{
+	struct self *self;
+
+	self = alloc_new0(sizeof (*self));
+	self->state.data = self;
+	self->state.start = start;
+	self->state.handle = handle;
+	self->state.update = update;
+	self->state.draw = draw;
+	self->state.finish = finish;
+
+	return &self->state;
+}