changeset 5:b5c649b6367b

core: implement sprites, closes #2440
author David Demelier <markand@malikania.fr>
date Mon, 06 Jan 2020 21:14:35 +0100
parents cd58eabb7fb4
children 3054723e53d7
files Makefile src/image.c src/main.c src/sprite.c src/sprite.h
diffstat 5 files changed, 142 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Mon Jan 06 20:22:22 2020 +0100
+++ b/Makefile	Mon Jan 06 21:14:35 2020 +0100
@@ -23,6 +23,7 @@
 CPPFLAGS=       -MMD
 SRCS=           src/image.c \
                 src/main.c \
+                src/sprite.c \
                 src/texture.c \
                 src/window.c
 OBJS=           ${SRCS:.c=.o}
--- a/src/image.c	Mon Jan 06 20:22:22 2020 +0100
+++ b/src/image.c	Mon Jan 06 21:14:35 2020 +0100
@@ -16,6 +16,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <assert.h>
 #include <stdbool.h>
 
 #include <SDL_image.h>
@@ -25,12 +26,16 @@
 struct texture *
 image_openf(const char *path)
 {
+	assert(path);
+
 	return texture_from_surface(IMG_Load(path));
 }
 
 struct texture *
 image_openb(const char *buffer, size_t size)
 {
+	assert(buffer);
+
 	SDL_RWops *ops = SDL_RWFromConstMem(buffer, size);
 
 	if (!ops)
--- a/src/main.c	Mon Jan 06 20:22:22 2020 +0100
+++ b/src/main.c	Mon Jan 06 21:14:35 2020 +0100
@@ -21,6 +21,9 @@
 #include "window.h"
 #include "image.h"
 #include "texture.h"
+#include "sprite.h"
+
+#include <SDL.h>
 
 int
 main(int argc, char **argv)
@@ -29,6 +32,7 @@
 	(void)argv;
 
 	struct texture *logo;
+	struct sprite sprite;
 
 	window_init("Molko's Adventure", 640, 480);
 	window_set_color(0x667788ff);
@@ -37,8 +41,10 @@
 	window_draw_line(50, 50, 100, 100);
 	window_draw_point(60, 60);
 	window_draw_rectangle(true, 20, 20, 70, 10);
-	logo = image_openf("E:\\dev\\molko\\logo.png");
-	texture_draw_ex(logo, 0, 0, 500, 500, 200, 200, 32, 32, 90);
+	logo = image_openf("E:\\Charactervector.png");
+	sprite_init(&sprite, logo, 65, 100);
+	sprite_draw(&sprite, 1, 2, 400, 400);
+
 	window_present();
 	SDL_Delay(5000);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sprite.c	Mon Jan 06 21:14:35 2020 +0100
@@ -0,0 +1,53 @@
+/*
+ * sprite.c -- image sprites
+ *
+ * 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 "sprite.h"
+#include "texture_p.h"
+#include "texture.h"
+
+void
+sprite_init(struct sprite *sprite, struct texture *tex, uint8_t cellw, uint8_t cellh)
+{
+	assert(sprite);
+	assert(tex);
+
+	sprite->texture = tex;
+	sprite->cellw = cellw;
+	sprite->cellh = cellh;
+}
+
+void
+sprite_draw(struct sprite *sprite, unsigned r, unsigned c, int x, int y)
+{
+	assert(sprite);
+
+	texture_draw_ex(
+		sprite->texture,
+		r * sprite->cellw,      /* src x */
+		c * sprite->cellh,      /* src y */
+		sprite->cellw,          /* src width */
+		sprite->cellh,          /* src height */
+		x,                      /* dst x */
+		y,                      /* dst y */
+		sprite->cellw,          /* dst width */
+		sprite->cellh,          /* dst height */
+		0.0                     /* angle */
+	);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sprite.h	Mon Jan 06 21:14:35 2020 +0100
@@ -0,0 +1,75 @@
+/*
+ * sprite.h -- image sprites
+ *
+ * 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_SPRITE_H
+#define MOLKO_SPRITE_H
+
+/**
+ * \file sprite.h
+ * \brief Image sprites.
+ */
+
+#include <stdint.h>
+
+struct texture;
+
+/**
+ * \brief Sprite structure.
+ */
+struct sprite {
+	struct texture *texture;        /* Texture to access (RO) */
+	uint8_t cellw;                  /* Width per cell (RW) */
+	uint8_t cellh;                  /* Hieight per cell (RW) */
+};
+
+/**
+ * Initialize a sprite.
+ *
+ * The sprite does not take ownership of texture and must be valid until the
+ * sprite is no longer used.
+ *
+ * This function is only provided as convenience, user may initialize the
+ * sprite by itself if wanted.
+ *
+ * \pre sprite != NULL
+ * \pre tex != NULL
+ * \param sprite the sprite to initialize
+ * \param tex the texture
+ * \param cellw the width per cell in pixels
+ * \param cellh the height per cell in pixels
+ */
+void
+sprite_init(struct sprite *sprite,
+            struct texture *tex,
+            uint8_t cellw,
+            uint8_t cellh);
+
+/**
+ * Draw the sprite component from row `r' and column `c'.
+ *
+ * \pre sprite != NULL
+ * \param r the row number
+ * \param c the column number
+ * \param x the X destination
+ * \param y the Y destination
+ * \warning No bounds checking
+ */
+void
+sprite_draw(struct sprite *sprite, unsigned r, unsigned c, int x, int y);
+
+#endif /* !MOLKO_SPRITE_H */