changeset 28:783841af4033

core: implement painter API, closes #2451 @1h
author David Demelier <markand@malikania.fr>
date Mon, 13 Jan 2020 12:21:31 +0100
parents 607bd90aba63
children 781c612edff1
files Makefile src/main.c src/map.c src/map.h src/message.c src/painter.c src/painter.h src/texture.c src/texture.h src/window.c src/window.h
diffstat 11 files changed, 234 insertions(+), 134 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun Jan 12 21:04:49 2020 +0100
+++ b/Makefile	Mon Jan 13 12:21:31 2020 +0100
@@ -31,6 +31,7 @@
                 src/image.c \
                 src/sprite.c \
                 src/sys.c \
+                src/painter.c \
                 src/texture.c \
                 src/window.c
 OBJS=           ${SRCS:.c=.o}
--- a/src/main.c	Sun Jan 12 21:04:49 2020 +0100
+++ b/src/main.c	Mon Jan 13 12:21:31 2020 +0100
@@ -30,6 +30,7 @@
 #include "sys.h"
 #include "texture.h"
 #include "window.h"
+#include "painter.h"
 
 #include <SDL.h>
 
@@ -84,14 +85,14 @@
 			}
 		}
 
-		window_set_color(0x667788ff);
-		window_clear();
+		painter_set_color(0x667788ff);
+		painter_clear();
 #if 0
 		message_update(&welcome, elapsed);
 		message_draw(&welcome);
 #endif
 		map_draw(&map);
-		window_present();
+		painter_present();
 	}
 
 	sys_close();
--- a/src/map.c	Sun Jan 12 21:04:49 2020 +0100
+++ b/src/map.c	Mon Jan 13 12:21:31 2020 +0100
@@ -148,8 +148,8 @@
 void
 map_draw(struct map *map)
 {
-	draw(map, &map->layers[0]);
-	draw(map, &map->layers[1]);
+	draw_layer(map, &map->layers[0]);
+	draw_layer(map, &map->layers[1]);
 }
 
 void
--- a/src/map.h	Sun Jan 12 21:04:49 2020 +0100
+++ b/src/map.h	Mon Jan 13 12:21:31 2020 +0100
@@ -52,6 +52,9 @@
 map_open(struct map *map, const char *path);
 
 void
+map_draw(struct map *map);
+
+void
 map_close(struct map *map);
 
 #endif /* !MOLKO_MAP_H */
--- a/src/message.c	Sun Jan 12 21:04:49 2020 +0100
+++ b/src/message.c	Mon Jan 13 12:21:31 2020 +0100
@@ -19,10 +19,10 @@
 #include <assert.h>
 
 #include "font.h"
+#include "message.h"
+#include "painter.h"
+#include "sprite.h"
 #include "texture.h"
-#include "message.h"
-#include "sprite.h"
-#include "window.h"
 
 #define MESSAGE_SPEED   1000    /* Time delay for animations */
 #define MESSAGE_TIMEOUT 5000    /* Time for auto-closing */
@@ -69,8 +69,8 @@
 	int x = 160;
 	int y = 80;
 
-	window_set_color(0xff0000ff);
-	window_draw_rectangle(true, x, y, 960, 160);
+	painter_set_color(0xff0000ff);
+	painter_draw_rectangle(true, x, y, 960, 160);
 
 	for (int i = 0; msg->text[i]; ++i) {
 		lines[i] = font_render(msg->font, msg->text[i], 0xffffffff);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/painter.c	Mon Jan 13 12:21:31 2020 +0100
@@ -0,0 +1,91 @@
+/*
+ * painter.c -- basic drawing routines
+ *
+ * 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 "color.h"
+#include "painter.h"
+#include "texture_p.h"
+#include "window_p.h"
+
+void
+painter_use(struct texture *tex)
+{
+	SDL_SetRenderTarget(win.renderer, tex ? tex->handle : NULL);
+}
+
+uint32_t
+painter_get_color(void)
+{
+	Uint8 r = 0, g = 0, b = 0, a = 0;
+
+	SDL_GetRenderDrawColor(win.renderer, &r, &g, &b, &a);
+
+	return COLOR_HEX(r, g, b, a);
+}
+
+void
+painter_set_color(uint32_t color)
+{
+	SDL_SetRenderDrawColor(
+		win.renderer,
+		COLOR_R(color),
+		COLOR_G(color),
+		COLOR_B(color),
+		COLOR_A(color)
+	);
+}
+
+void
+painter_draw_line(int x1, int y1, int x2, int y2)
+{
+	SDL_RenderDrawLine(win.renderer, x1, y1, x2, y2);
+}
+
+void
+painter_draw_point(int x1, int y1)
+{
+	SDL_RenderDrawPoint(win.renderer, x1, y1);
+}
+
+void
+painter_draw_rectangle(bool fill, int x, int y, unsigned width, unsigned height)
+{
+	const SDL_Rect rect = {
+		.w = width,
+		.h = height,
+		.x = x,
+		.y = y
+	};
+
+	if (fill)
+		SDL_RenderFillRect(win.renderer, &rect);
+	else
+		SDL_RenderDrawRect(win.renderer, &rect);
+}
+
+void
+painter_clear(void)
+{
+	SDL_RenderClear(win.renderer);
+}
+
+void
+painter_present(void)
+{
+	SDL_RenderPresent(win.renderer);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/painter.h	Mon Jan 13 12:21:31 2020 +0100
@@ -0,0 +1,102 @@
+/*
+ * painter.h -- basic drawing routines
+ *
+ * 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_PAINTER_H
+#define MOLKO_PAINTER_H
+
+/**
+ * \file painter.h
+ * \brief Basic drawing routines.
+ */
+
+#include <stdbool.h>
+#include <stdint.h>
+
+struct texture;
+
+/**
+ * Set the rendering context to the given texture.
+ *
+ * If texture is NULL, use default context aka the window.
+ *
+ * \param tex the texture
+ */
+void
+painter_use(struct texture *tex);
+
+/**
+ * Get the current drawing color.
+ *
+ * \return the color in RRGGBBAA format
+ */
+uint32_t
+painter_get_color(void);
+
+/**
+ * Set the rendering drawing color.
+ *
+ * \param color in RRGGBBAA format
+ */
+void
+painter_set_color(uint32_t color);
+
+/**
+ * Draw a line.
+ *
+ * \param x1 first X coordinate
+ * \param y1 first Y coordinate
+ * \param x2 second X coordinate
+ * \param y2 second Y coordinate
+ */
+void
+painter_draw_line(int x1, int y1, int x2, int y2);
+
+/**
+ * Draw a pixel point.
+ *
+ * \param x the X coordinate
+ * \param y the Y coordinate
+ */
+void
+painter_draw_point(int x, int y);
+
+/**
+ * Draw a rectangle
+ *
+ * \param fill set to true to fill the rectangle
+ * \param x the X coordinate
+ * \param y the Y coordinate
+ * \param w the rectangle width
+ * \param h the rectangle height
+ */
+void
+painter_draw_rectangle(bool fill, int x, int y, unsigned w, unsigned h);
+
+/**
+ * Clear the window.
+ */
+void
+painter_clear(void);
+
+/**
+ * Present the window, only call this function one time in the main loop.
+ */
+void
+painter_present(void);
+
+#endif /* !MOLKO_PAINTER_H */
--- a/src/texture.c	Sun Jan 12 21:04:49 2020 +0100
+++ b/src/texture.c	Mon Jan 13 12:21:31 2020 +0100
@@ -22,6 +22,22 @@
 #include "texture_p.h"
 #include "window_p.h"
 
+struct texture *
+texture_new(uint16_t w, uint16_t h)
+{
+	struct texture *tex;
+
+	if (!(tex = malloc(sizeof (struct texture))))
+		return NULL;
+	if (!(tex->handle = SDL_CreateTexture(win.renderer,
+	    SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h))) {
+		free(tex);
+		return NULL;
+	}
+
+	return tex;
+}
+
 bool
 texture_get_size(struct texture *tex, uint16_t *w, uint16_t *h)
 {
--- a/src/texture.h	Sun Jan 12 21:04:49 2020 +0100
+++ b/src/texture.h	Mon Jan 13 12:21:31 2020 +0100
@@ -38,6 +38,16 @@
 struct texture;
 
 /**
+ * Create a new texture.
+ *
+ * \param w the width
+ * \param h the height
+ * \return the texture or NULL on error
+ */
+struct texture *
+texture_new(uint16_t w, uint16_t h);
+
+/**
  * Get texture size.
  *
  * \pre tex != NULL
--- a/src/window.c	Sun Jan 12 21:04:49 2020 +0100
+++ b/src/window.c	Mon Jan 13 12:21:31 2020 +0100
@@ -20,7 +20,6 @@
 
 #include <SDL.h>
 
-#include "color.h"
 #include "window.h"
 #include "window_p.h"
 
@@ -44,68 +43,6 @@
 	return true;
 }
 
-uint32_t
-window_get_color(void)
-{
-	Uint8 r = 0, g = 0, b = 0, a = 0;
-
-	SDL_GetRenderDrawColor(win.renderer, &r, &g, &b, &a);
-
-	return COLOR_HEX(r, g, b, a);
-}
-
-void
-window_set_color(uint32_t color)
-{
-	SDL_SetRenderDrawColor(
-		win.renderer,
-		COLOR_R(color),
-		COLOR_G(color),
-		COLOR_B(color),
-		COLOR_A(color)
-	);
-}
-
-void
-window_draw_line(int x1, int y1, int x2, int y2)
-{
-	SDL_RenderDrawLine(win.renderer, x1, y1, x2, y2);
-}
-
-void
-window_draw_point(int x1, int y1)
-{
-	SDL_RenderDrawPoint(win.renderer, x1, y1);
-}
-
-void
-window_draw_rectangle(bool fill, int x, int y, unsigned width, unsigned height)
-{
-	const SDL_Rect rect = {
-		.w = width,
-		.h = height,
-		.x = x,
-		.y = y
-	};
-
-	if (fill)
-		SDL_RenderFillRect(win.renderer, &rect);
-	else
-		SDL_RenderDrawRect(win.renderer, &rect);
-}
-
-void
-window_clear(void)
-{
-	SDL_RenderClear(win.renderer);
-}
-
-void
-window_present(void)
-{
-	SDL_RenderPresent(win.renderer);
-}
-
 void
 window_close(void)
 {
--- a/src/window.h	Sun Jan 12 21:04:49 2020 +0100
+++ b/src/window.h	Mon Jan 13 12:21:31 2020 +0100
@@ -25,7 +25,6 @@
  */
 
 #include <stdbool.h>
-#include <stdint.h>
 
 /**
  * Initialize window.
@@ -40,66 +39,6 @@
 window_init(const char *title, unsigned width, unsigned height);
 
 /**
- * Get the current drawing color.
- *
- * \return the color in RRGGBBAA format
- */
-uint32_t
-window_get_color(void);
-
-/**
- * Set the rendering drawing color.
- *
- * \param color in RRGGBBAA format
- */
-void
-window_set_color(uint32_t color);
-
-/**
- * Draw a line.
- *
- * \param x1 first X coordinate
- * \param y1 first Y coordinate
- * \param x2 second X coordinate
- * \param y2 second Y coordinate
- */
-void
-window_draw_line(int x1, int y1, int x2, int y2);
-
-/**
- * Draw a pixel point.
- *
- * \param x the X coordinate
- * \param y the Y coordinate
- */
-void
-window_draw_point(int x, int y);
-
-/**
- * Draw a rectangle
- *
- * \param fill set to true to fill the rectangle
- * \param x the X coordinate
- * \param y the Y coordinate
- * \param w the rectangle width
- * \param h the rectangle height
- */
-void
-window_draw_rectangle(bool fill, int x, int y, unsigned w, unsigned h);
-
-/**
- * Clear the window.
- */
-void
-window_clear(void);
-
-/**
- * Present the window, only call this function one time in the main loop.
- */
-void
-window_present(void);
-
-/**
  * Close the window and destroy associated resources.
  */
 void