summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pjm@nac.net>2013-11-16 01:02:17 (EST)
committer P. J. McDermott <pjm@nac.net>2013-11-16 01:02:17 (EST)
commit48cce04f85c7b49ab817c0001c636d9d4ffd299f (patch)
tree45c7ea56ed5b230402c7127e71dba551992b03be
parentaf6c4ffbccfbdfe3f9e260ba679886364c727c41 (diff)
downloadoverworld-rpg-48cce04f85c7b49ab817c0001c636d9d4ffd299f.zip
overworld-rpg-48cce04f85c7b49ab817c0001c636d9d4ffd299f.tar.gz
overworld-rpg-48cce04f85c7b49ab817c0001c636d9d4ffd299f.tar.bz2
Add code to load and initialize games.
-rw-r--r--src/game.c50
-rw-r--r--src/game.h36
-rw-r--r--src/local.mk2
-rw-r--r--src/main.c5
4 files changed, 93 insertions, 0 deletions
diff --git a/src/game.c b/src/game.c
new file mode 100644
index 0000000..ab58eb4
--- /dev/null
+++ b/src/game.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2013 Patrick "P. J." McDermott
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include "script.h"
+#include "logging.h"
+#include "game.h"
+
+struct game *
+game_load(const char *dirname)
+{
+ struct game *g;
+ char *path;
+
+ g = malloc(sizeof(*g));
+ if (g == NULL) {
+ err(1, "Failed to allocate game");
+ }
+ g->dirname = dirname;
+
+ path = malloc(strlen(dirname) + strlen("init.lua") + 2);
+ if (path == NULL) {
+ err(1, "Failed to allocate game");
+ }
+ sprintf(path, "%s/%s", dirname, "init.lua");
+ g->init_script = script_load(path);
+ free(path);
+
+ return g;
+}
+
+void
+game_run(struct game *g)
+{
+ script_call(g->init_script, "on_init");
+}
diff --git a/src/game.h b/src/game.h
new file mode 100644
index 0000000..e2ec169
--- /dev/null
+++ b/src/game.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2013 Patrick "P. J." McDermott
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GAME_H
+#define GAME_H
+
+#include "script.h"
+#include "viewport.h"
+/*#include "clock.h"*/
+
+struct game {
+ const char *dirname;
+ struct script *init_script;
+ struct viewport *vp;
+ /*struct clock *ck;*/
+};
+
+struct game *game_load(const char *dirname);
+void game_run(struct game *g);
+
+#endif
diff --git a/src/local.mk b/src/local.mk
index 1950fee..ceb4557 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -9,6 +9,8 @@ sdlex_SOURCES += \
src/compression.h \
src/demo.c \
src/demo.h \
+ src/game.c \
+ src/game.h \
src/init.c \
src/init.h \
src/logging.c \
diff --git a/src/main.c b/src/main.c
index 93ac007..9be003b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -21,6 +21,7 @@
#include "init.h"
#include "logging.h"
#include "demo.h"
+#include "game.h"
static void usage(const char *program_name, FILE *stream);
@@ -28,6 +29,7 @@ int
main(int argc, char **argv)
{
const char *game;
+ struct game *g;
if (argc != 2) {
usage(argv[0], stderr);
@@ -39,6 +41,9 @@ main(int argc, char **argv)
if (strcmp(game, "demo") == 0) {
demo();
+ } else {
+ g = game_load(argv[1]);
+ game_run(g);
}
quit(0);