changeset 43:291be94202c7

core: add PAINTER_(BEGIN|END) helpers, closes #2456
author David Demelier <markand@malikania.fr>
date Wed, 15 Jan 2020 20:54:53 +0100
parents 22a09a5ee476
children c97fe725fdeb
files src/main.c src/map.c src/painter.h
diffstat 3 files changed, 50 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.c	Wed Jan 15 20:33:21 2020 +0100
+++ b/src/main.c	Wed Jan 15 20:54:53 2020 +0100
@@ -29,6 +29,7 @@
 #include "walksprite.h"
 #include "window.h"
 #include "util.h"
+#include "texture.h"
 
 int
 main(int argc, char **argv)
@@ -36,6 +37,27 @@
 	(void)argc;
 	(void)argv;
 
+	struct texture *tex;
+
+	if (!sys_init())
+		error_fatal();
+	if (!window_init("Molko's Adventure", 1024, 576))
+		error_fatal();
+	if (!(tex = texture_new(100, 100)))
+		error_fatal();
+
+	painter_set_color(0xffffffff);
+	painter_clear();
+
+	PAINTER_BEGIN(tex);
+	painter_set_color(0xff0000ff);
+	painter_clear();
+	PAINTER_END();
+
+	texture_draw(tex, 10, 10);
+	painter_present();
+	delay(5000);
+
 #if 0
 
 	struct clock clock;
--- a/src/map.c	Wed Jan 15 20:33:21 2020 +0100
+++ b/src/map.c	Wed Jan 15 20:54:53 2020 +0100
@@ -200,13 +200,10 @@
 void
 map_repaint(struct map *map)
 {
-	struct texture *old;
-
-	old = painter_get_target();
-	painter_set_target(map->picture);
+	PAINTER_BEGIN(map->picture);
 	draw_layer(map, &map->layers[0]);
 	draw_layer(map, &map->layers[1]);
-	painter_set_target(old);
+	PAINTER_END();
 }
 
 void
--- a/src/painter.h	Wed Jan 15 20:33:21 2020 +0100
+++ b/src/painter.h	Wed Jan 15 20:54:53 2020 +0100
@@ -48,6 +48,8 @@
  * If texture is NULL, use default context aka the window.
  *
  * \param tex the texture
+ * \see PAINTER_BEGIN
+ * \see PAINTER_END
  */
 void
 painter_set_target(struct texture *tex);
@@ -112,4 +114,28 @@
 void
 painter_present(void);
 
+/**
+ * Use this macro to start painting on the texture to store the current
+ * rendering context and put it back afterwards.
+ *
+ * \pre tex != NULL
+ * \param tex the texture to use
+ * \see PAINTER_END
+ */
+#define PAINTER_BEGIN(tex    )                                          \
+do {                                                                    \
+        struct texture *__current_texture__;                            \
+                                                                        \
+        __current_texture__ = painter_get_target();                     \
+        painter_set_target((tex))
+
+/**
+ * Use this macro at the end of rendering into a given texture.
+ *
+ * \see PAINTER_BEGIN
+ */
+#define PAINTER_END()                                                   \
+        painter_set_target(__current_texture__);                        \
+} while (0)
+
 #endif /* !MOLKO_PAINTER_H */