changeset 4:cd58eabb7fb4

core: implement basic images, closes #2441
author David Demelier <markand@malikania.fr>
date Mon, 06 Jan 2020 20:22:22 +0100
parents c013f41a72a5
children b5c649b6367b
files Makefile src/image.c src/image.h src/main.c
diffstat 4 files changed, 111 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Mon Jan 06 20:20:58 2020 +0100
+++ b/Makefile	Mon Jan 06 20:22:22 2020 +0100
@@ -21,12 +21,15 @@
 CC=             clang
 CFLAGS=         -O3 -DNDEBUG -std=c99 -Wall -Wextra
 CPPFLAGS=       -MMD
-SRCS=           src/main.c src/window.c
+SRCS=           src/image.c \
+                src/main.c \
+                src/texture.c \
+                src/window.c
 OBJS=           ${SRCS:.c=.o}
 DEPS=           ${SRCS:.c=.d}
 
-SDL_CFLAGS=     `pkg-config --cflags sdl2`
-SDL_LDFLAGS=    `pkg-config --libs sdl2`
+SDL_CFLAGS=     `pkg-config --cflags SDL2 SDL2_image`
+SDL_LDFLAGS=    `pkg-config --libs SDL2 SDL2_image`
 
 .SUFFIXES:
 .SUFFIXES: .c .o
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/image.c	Mon Jan 06 20:22:22 2020 +0100
@@ -0,0 +1,40 @@
+/*
+ * image.c -- basic image 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 <stdbool.h>
+
+#include <SDL_image.h>
+
+#include "texture_p.h"
+
+struct texture *
+image_openf(const char *path)
+{
+	return texture_from_surface(IMG_Load(path));
+}
+
+struct texture *
+image_openb(const char *buffer, size_t size)
+{
+	SDL_RWops *ops = SDL_RWFromConstMem(buffer, size);
+
+	if (!ops)
+		return NULL;
+
+	return texture_from_surface(IMG_Load_RW(ops, true));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/image.h	Mon Jan 06 20:22:22 2020 +0100
@@ -0,0 +1,52 @@
+/*
+ * image.h -- basic image 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_IMAGE_H
+#define MOLKO_IMAGE_H
+
+/**
+ * \file image.h
+ * \brief Basic image management.
+ */
+
+#include <stddef.h>
+
+struct texture;
+
+/**
+ * Open a file from a path.
+ *
+ * \pre path != NULL
+ * \param path the path to the file
+ * \return the texture or NULL on error
+ */
+struct texture *
+image_openf(const char *path);
+
+/**
+ * Open a file from a memory buffer.
+ *
+ * \pre buffer != NULL
+ * \param the memory buffer
+ * \return size the memory size
+ * \return the texture or NULL on error
+ */
+struct texture *
+image_openb(const char *buffer, size_t size);
+
+#endif /* !MOLKO_IMAGE_H */
--- a/src/main.c	Mon Jan 06 20:20:58 2020 +0100
+++ b/src/main.c	Mon Jan 06 20:22:22 2020 +0100
@@ -19,6 +19,8 @@
 #include <stdio.h>
 
 #include "window.h"
+#include "image.h"
+#include "texture.h"
 
 int
 main(int argc, char **argv)
@@ -26,15 +28,18 @@
 	(void)argc;
 	(void)argv;
 
-	struct window *w = window_init("Molko's Adventure", 640, 480);
+	struct texture *logo;
 
-	window_set_color(w, 0x667788ff);
-	window_clear(w);
-	window_set_color(w, 0xffffffff);
-	window_draw_line(w, 50, 50, 100, 100);
-	window_draw_point(w, 60, 60);
-	window_draw_rectangle(w, true, 20, 20, 70, 10);
-	window_present(w);
+	window_init("Molko's Adventure", 640, 480);
+	window_set_color(0x667788ff);
+	window_clear();
+	window_set_color(0xffffffff);
+	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);
+	window_present();
 	SDL_Delay(5000);
 
 	return 0;