changeset 3:c013f41a72a5

core: implement basic textures, closes #2442
author David Demelier <markand@malikania.fr>
date Mon, 06 Jan 2020 20:20:58 +0100
parents 832c20d6cce9
children cd58eabb7fb4
files src/texture.c src/texture.h src/texture_p.h
diffstat 3 files changed, 198 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/texture.c	Mon Jan 06 20:20:58 2020 +0100
@@ -0,0 +1,94 @@
+/*
+ * texture.c -- basic texture management
+ *
+ * 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 "texture.h"
+#include "texture_p.h"
+#include "window_p.h"
+
+void
+texture_draw(struct texture *tex, int x, int y)
+{
+	SDL_Rect dst = {
+		.x = x,
+		.y = y,
+		.w = 0,
+		.h = 0
+	};
+
+	assert(tex);
+
+	/* We need to determine size */
+	SDL_QueryTexture(tex->handle, NULL, NULL, &dst.w, &dst.h);
+	SDL_RenderCopy(win.renderer, tex->handle, NULL, &dst);
+}
+
+void
+texture_draw_ex(struct texture *tex,
+                int src_x,
+                int src_y,
+                unsigned src_w,
+                unsigned src_h,
+                int dst_x,
+                int dst_y,
+                unsigned dst_w,
+                unsigned dst_h,
+                double angle)
+{
+	const SDL_Rect src = {
+		.x = src_x,
+		.y = src_y,
+		.w = src_w,
+		.h = src_h
+	};
+	const SDL_Rect dst = {
+		.x = dst_x,
+		.y = dst_y,
+		.w = dst_w,
+		.h = dst_h
+	};
+
+	SDL_RenderCopyEx(win.renderer, tex->handle, &src, &dst, angle, NULL, SDL_FLIP_NONE);
+}
+
+void
+texture_close(struct texture *tex)
+{
+	assert(tex);
+
+	SDL_DestroyTexture(tex->handle);
+	free(tex);
+}
+
+/* private */
+
+struct texture *
+texture_from_surface(SDL_Surface *surface)
+{
+	struct texture *texture;
+
+	if (!(texture = calloc(1, sizeof (struct texture))))
+		return NULL;
+	if (!(texture->handle = SDL_CreateTextureFromSurface(win.renderer, surface))) {
+		free(texture);
+		return NULL;
+	}
+
+	return texture;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/texture.h	Mon Jan 06 20:20:58 2020 +0100
@@ -0,0 +1,73 @@
+/*
+ * texture.h -- basic texture management
+ *
+ * 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.
+ */
+
+struct texture;
+
+/**
+ * \file texture.h
+ * \brief Basic texture management.
+ *
+ * See also \a image.h for usage of textures.
+ */
+
+/**
+ * Simple texture drawing.
+ *
+ * \pre tex != NULL
+ * \param tex the texture
+ * \param x the X coordinate
+ * \param y the Y coordinate
+ */
+void
+texture_draw(struct texture *tex, int x, int y);
+
+/**
+ * Advanced texture drawing.
+ *
+ * \pre tex != NULL
+ * \param tex the texture
+ * \param src_x the source rectangle X coordinate
+ * \param src_y the source rectangle Y coordinate
+ * \param src_width the source rectangle width
+ * \param src_height the source rectangle height
+ * \param dst_x the destination rectangle X coordinate
+ * \param dst_y the destination rectangle Y coordinate
+ * \param dst_width the destination rectangle width
+ * \param dst_height the destination rectangle height
+ * \param angle the angle
+ */
+void
+texture_draw_ex(struct texture *tex,
+                int src_x,
+                int src_y,
+                unsigned src_w,
+                unsigned src_h,
+                int dst_x,
+                int dst_y,
+                unsigned dst_w,
+                unsigned dst_h,
+                double angle);
+
+/**
+ * Close the texture, do not use afterwards.
+ *
+ * \pre tex != NULL
+ * \param tex the texture
+ */
+void
+texture_close(struct texture *tex);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/texture_p.h	Mon Jan 06 20:20:58 2020 +0100
@@ -0,0 +1,31 @@
+/*
+ * texture_p.h -- (PRIVATE) basic texture management
+ *
+ * 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_TEXTURE_P_H
+#define MOLKO_TEXTURE_P_H
+
+#include <SDL.h>
+
+struct texture {
+	SDL_Texture *handle;
+};
+
+struct texture *
+texture_from_surface(SDL_Surface *surface);
+
+#endif /* !MOLKO_TEXTURE_P_H */