changeset 267:bead76f6c793

adventure: start a global assets registry
author David Demelier <markand@malikania.fr>
date Thu, 10 Dec 2020 16:28:30 +0100
parents e48ffbcf9bcc
children 3f710c68bb97
files libmlk-adventure/CMakeLists.txt libmlk-adventure/adventure/assets.c libmlk-adventure/adventure/assets.h libmlk-adventure/adventure/molko.c libmlk-adventure/adventure/state/mainmenu.c libmlk-data/sprites/cursor.png libmlk-data/sprites/ui-cursor.png
diffstat 7 files changed, 123 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/libmlk-adventure/CMakeLists.txt	Thu Dec 10 16:28:03 2020 +0100
+++ b/libmlk-adventure/CMakeLists.txt	Thu Dec 10 16:28:30 2020 +0100
@@ -25,6 +25,8 @@
 	${libadventure_SOURCE_DIR}/adventure/actions/teleport.c
 	${libadventure_SOURCE_DIR}/adventure/actions/teleport.h
 	${libadventure_SOURCE_DIR}/adventure/adventure_p.h
+	${libadventure_SOURCE_DIR}/adventure/assets.c
+	${libadventure_SOURCE_DIR}/adventure/assets.h
 	${libadventure_SOURCE_DIR}/adventure/molko.c
 	${libadventure_SOURCE_DIR}/adventure/molko.h
 	${libadventure_SOURCE_DIR}/adventure/trace_hud.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmlk-adventure/adventure/assets.c	Thu Dec 10 16:28:30 2020 +0100
@@ -0,0 +1,69 @@
+/*
+ * assets.c -- global atlas for every resources
+ *
+ * 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/image.h>
+#include <core/panic.h>
+#include <core/sprite.h>
+#include <core/texture.h>
+#include <core/util.h>
+
+#include <adventure/molko.h>
+
+#include "assets.h"
+
+#define SPRITE(which, file, w, h) { which, file, w, h }
+
+static struct {
+	enum assets_sprite index;
+	const char *path;
+	unsigned int cellw;
+	unsigned int cellh;
+	struct texture texture;
+	struct sprite sprite;
+} table_sprites[] = {
+	SPRITE(ASSETS_SPRITE_UI_CURSOR, "sprites/ui-cursor.png", 24, 24)
+};
+
+struct sprite *assets_sprites[ASSETS_SPRITE_NUM] = {0};
+
+static void
+init_sprites(void)
+{
+	for (size_t i = 0; i < UTIL_SIZE(table_sprites); ++i) {
+		if (!image_open(&table_sprites[i].texture, molko_path(table_sprites[i].path)))
+			panic();
+
+		sprite_init(&table_sprites[i].sprite, &table_sprites[i].texture,
+		    table_sprites[i].cellw, table_sprites[i].cellh);
+
+		assets_sprites[table_sprites[i].index] = &table_sprites[i].sprite;
+	}
+}
+
+void
+assets_init(void)
+{
+	init_sprites();
+}
+
+void
+assets_finish(void)
+{
+	for (size_t i = 0; i < UTIL_SIZE(table_sprites); ++i)
+		texture_finish(&table_sprites[i].texture);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmlk-adventure/adventure/assets.h	Thu Dec 10 16:28:30 2020 +0100
@@ -0,0 +1,38 @@
+/*
+ * assets.h -- global atlas for every resources
+ *
+ * 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_ADVENTURE_ASSETS_H
+#define MOLKO_ADVENTURE_ASSETS_H
+
+#include <core/sprite.h>
+
+enum assets_sprite {
+	/* UI elements. */
+	ASSETS_SPRITE_UI_CURSOR,
+	ASSETS_SPRITE_NUM
+};
+
+extern struct sprite *assets_sprites[ASSETS_SPRITE_NUM];
+
+void
+assets_init(void);
+
+void
+assets_finish(void);
+
+#endif /* !MOLKO_ADVENTURE_ASSETS_H */
--- a/libmlk-adventure/adventure/molko.c	Thu Dec 10 16:28:03 2020 +0100
+++ b/libmlk-adventure/adventure/molko.c	Thu Dec 10 16:28:30 2020 +0100
@@ -41,6 +41,7 @@
 #include <adventure/state/mainmenu.h>
 #include <adventure/state/map.h>
 
+#include "assets.h"
 #include "molko.h"
 
 #define WINDOW_WIDTH    1280
@@ -78,6 +79,9 @@
 	molko.panic = panic_state_new();
 	panic_handler = crash;
 
+	/* Init other stuff. */
+	assets_init();
+
 	/* Start to splash. */
 	game_switch(splashscreen_state_new(), true);
 }
--- a/libmlk-adventure/adventure/state/mainmenu.c	Thu Dec 10 16:28:03 2020 +0100
+++ b/libmlk-adventure/adventure/state/mainmenu.c	Thu Dec 10 16:28:30 2020 +0100
@@ -36,6 +36,7 @@
 #include <ui/label.h>
 #include <ui/theme.h>
 
+#include <adventure/assets.h>
 #include <adventure/molko.h>
 #include <adventure/adventure_p.h>
 
@@ -168,6 +169,8 @@
 draw(struct state *state)
 {
 	struct self *self = state->data;
+	struct sprite *cursor = assets_sprites[ASSETS_SPRITE_UI_CURSOR];
+	int x, y;
 
 	painter_set_color(0xffffffff);
 	painter_clear();
@@ -175,11 +178,13 @@
 	for (size_t i = 0; i < UTIL_SIZE(self->texts); ++i)
 		texture_draw(&self->texts[i].tex, self->texts[i].x, self->texts[i].y);
 
-	/* TODO: a sword here. */
-	painter_set_color(0x000000ff);
-	painter_draw_rectangle(
-	    self->texts[self->itemsel].x - 30,
-	    self->texts[self->itemsel].y + 11, 15, 15);
+	x  = self->texts[self->itemsel].x;
+	x -= cursor->cellw * 2;
+	y  = self->texts[self->itemsel].y;
+	y += self->texts[self->itemsel].tex.h / 2;
+	y -= cursor->cellh / 2;
+
+	sprite_draw(cursor, 1, 2, x, y);
 	painter_present();
 }
 
Binary file libmlk-data/sprites/cursor.png has changed
Binary file libmlk-data/sprites/ui-cursor.png has changed