prepare implementation of primitives

Tue, 10 Jun 2025 19:29:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 10 Jun 2025 19:29:07 +0200
changeset 143
4db4f00493ad
parent 142
27c9bbd765d0
child 144
43636d6a6e25

prepare implementation of primitives

src/2d.c file | annotate | diff | comparison | revisions
src/Makefile file | annotate | diff | comparison | revisions
src/ascension/2d.h file | annotate | diff | comparison | revisions
src/ascension/2d/sprite.h file | annotate | diff | comparison | revisions
src/ascension/sprite.h file | annotate | diff | comparison | revisions
src/ascension/ui/text.h file | annotate | diff | comparison | revisions
src/scene.c file | annotate | diff | comparison | revisions
src/sprite.c file | annotate | diff | comparison | revisions
test/snake/Makefile file | annotate | diff | comparison | revisions
test/snake/snake.c file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/2d.c	Tue Jun 10 19:29:07 2025 +0200
@@ -0,0 +1,29 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ * Copyright 2025 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "ascension/2d.h"
+
--- a/src/Makefile	Tue Jun 10 19:17:21 2025 +0200
+++ b/src/Makefile	Tue Jun 10 19:29:07 2025 +0200
@@ -29,7 +29,7 @@
 
 SRC = context.c glcontext.c filesystem.c error.c \
       window.c shader.c mesh.c texture.c \
-      sprite.c \
+      sprite.c 2d.c \
       camera.c scene.c scene_node.c behavior.c \
       font.c text.c
 
@@ -44,6 +44,10 @@
 
 FORCE:
 
+$(BUILD_DIR)/2d.o: 2d.c ascension/2d.h
+	@echo "Compiling $<"
+	$(CC) -o $@ $(CFLAGS) -c $<
+
 $(BUILD_DIR)/behavior.o: behavior.c ascension/behavior.h \
  ascension/scene_node.h ascension/datatypes.h ascension/transform.h \
  ascension/error.h ascension/scene.h ascension/camera.h
@@ -100,10 +104,9 @@
  ascension/datatypes.h ascension/window.h ascension/glcontext.h \
  ascension/scene.h ascension/scene_node.h ascension/transform.h \
  ascension/camera.h ascension/input.h ascension/ui/font.h \
- ascension/scene.h ascension/behavior.h ascension/shader.h ascension/2d.h \
- ascension/2d/sprite.h ascension/2d/../scene_node.h \
- ascension/2d/../mesh.h ascension/2d/../datatypes.h \
- ascension/2d/../texture.h ascension/2d/../shader.h
+ ascension/scene.h ascension/behavior.h ascension/shader.h \
+ ascension/sprite.h ascension/mesh.h ascension/texture.h \
+ ascension/shader.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
@@ -123,15 +126,13 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
-$(BUILD_DIR)/sprite.o: sprite.c ascension/2d/sprite.h \
- ascension/2d/../scene_node.h ascension/2d/../datatypes.h \
- ascension/2d/../transform.h ascension/2d/../mesh.h \
- ascension/2d/../texture.h ascension/2d/../shader.h \
- ascension/2d/../camera.h ascension/context.h ascension/datatypes.h \
- ascension/window.h ascension/glcontext.h ascension/scene.h \
- ascension/scene_node.h ascension/camera.h ascension/input.h \
- ascension/ui/font.h ascension/glcontext.h ascension/error.h \
- ascension/mesh.h ascension/constants.h
+$(BUILD_DIR)/sprite.o: sprite.c ascension/sprite.h ascension/scene_node.h \
+ ascension/datatypes.h ascension/transform.h ascension/mesh.h \
+ ascension/texture.h ascension/shader.h ascension/camera.h \
+ ascension/context.h ascension/window.h ascension/glcontext.h \
+ ascension/scene.h ascension/input.h ascension/ui/font.h \
+ ascension/glcontext.h ascension/error.h ascension/mesh.h \
+ ascension/constants.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
@@ -139,10 +140,10 @@
  ascension/datatypes.h ascension/window.h ascension/glcontext.h \
  ascension/scene.h ascension/scene_node.h ascension/transform.h \
  ascension/camera.h ascension/input.h ascension/ui/font.h \
- ascension/ui/text.h ascension/ui/font.h ascension/ui/../2d/sprite.h \
- ascension/ui/../2d/../scene_node.h ascension/ui/../2d/../mesh.h \
- ascension/ui/../2d/../datatypes.h ascension/ui/../2d/../texture.h \
- ascension/ui/../2d/../shader.h ascension/ui/../2d/../camera.h
+ ascension/ui/text.h ascension/ui/font.h ascension/ui/../sprite.h \
+ ascension/ui/../scene_node.h ascension/ui/../mesh.h \
+ ascension/ui/../datatypes.h ascension/ui/../texture.h \
+ ascension/ui/../shader.h ascension/ui/../camera.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
--- a/src/ascension/2d.h	Tue Jun 10 19:17:21 2025 +0200
+++ b/src/ascension/2d.h	Tue Jun 10 19:29:07 2025 +0200
@@ -28,7 +28,6 @@
 #ifndef ASCENSION_2D_H
 #define ASCENSION_2D_H
 
-#include "2d/sprite.h"
 
 #endif /* ASCENSION_2D_H */
 
--- a/src/ascension/2d/sprite.h	Tue Jun 10 19:17:21 2025 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,90 +0,0 @@
-/*
-* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- * Copyright 2025 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef ASCENSION_SPRITE_H
-#define ASCENSION_SPRITE_H
-
-#include "../scene_node.h"
-#include "../mesh.h"
-#include "../texture.h"
-#include "../shader.h"
-
-typedef struct AscSprite {
-    AscSceneNode data;
-    AscMesh mesh;
-    unsigned width;
-    unsigned height;
-    AscTexture *texture;
-    enum asc_texture_scale_mode texture_scale_mode;
-    asc_vec2f texture_scale;
-} AscSprite;
-
-struct asc_sprite_create_args {
-    const char* name;
-    AscTexture *texture;
-    int x;
-    int y;
-    /**
-     * Optional width for re-scaling.
-     * If zero, the texture width will be used.
-     */
-    unsigned width;
-    /**
-     * Optional height for re-scaling.
-     * If zero, the texture height will be used.
-     */
-    unsigned height;
-    enum asc_texture_scale_mode texture_scale_mode;
-    float texture_scale_x;
-    float texture_scale_y;
-    bool opaque;
-};
-
-AscSceneNode *asc_sprite_create(struct asc_sprite_create_args args);
-
-#define asc_sprite(...) \
-    asc_sprite_create((struct asc_sprite_create_args) { __VA_ARGS__ })
-
-void asc_sprite_set_size(AscSceneNode *node, unsigned width, unsigned height);
-
-void asc_sprite_draw(const AscShaderProgram *shader, const AscSprite *node);
-
-/**
- * Returns a shader program that can draw sprites with rectangle textures.
- *
- * @return the shader program
- */
-const AscShaderProgram *asc_sprite_shader_rect(void);
-
-/**
- * Returns a shader program that can draw sprites with 2D textures.
- *
- * @return the shader program
- */
-const AscShaderProgram *asc_sprite_shader_uv(void);
-
-#endif //ASCENSION_SPRITE_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ascension/sprite.h	Tue Jun 10 19:29:07 2025 +0200
@@ -0,0 +1,90 @@
+/*
+* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ * Copyright 2025 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef ASCENSION_SPRITE_H
+#define ASCENSION_SPRITE_H
+
+#include "scene_node.h"
+#include "mesh.h"
+#include "texture.h"
+#include "shader.h"
+
+typedef struct AscSprite {
+    AscSceneNode data;
+    AscMesh mesh;
+    unsigned width;
+    unsigned height;
+    AscTexture *texture;
+    enum asc_texture_scale_mode texture_scale_mode;
+    asc_vec2f texture_scale;
+} AscSprite;
+
+struct asc_sprite_create_args {
+    const char* name;
+    AscTexture *texture;
+    int x;
+    int y;
+    /**
+     * Optional width for re-scaling.
+     * If zero, the texture width will be used.
+     */
+    unsigned width;
+    /**
+     * Optional height for re-scaling.
+     * If zero, the texture height will be used.
+     */
+    unsigned height;
+    enum asc_texture_scale_mode texture_scale_mode;
+    float texture_scale_x;
+    float texture_scale_y;
+    bool opaque;
+};
+
+AscSceneNode *asc_sprite_create(struct asc_sprite_create_args args);
+
+#define asc_sprite(...) \
+    asc_sprite_create((struct asc_sprite_create_args) { __VA_ARGS__ })
+
+void asc_sprite_set_size(AscSceneNode *node, unsigned width, unsigned height);
+
+void asc_sprite_draw(const AscShaderProgram *shader, const AscSprite *node);
+
+/**
+ * Returns a shader program that can draw sprites with rectangle textures.
+ *
+ * @return the shader program
+ */
+const AscShaderProgram *asc_sprite_shader_rect(void);
+
+/**
+ * Returns a shader program that can draw sprites with 2D textures.
+ *
+ * @return the shader program
+ */
+const AscShaderProgram *asc_sprite_shader_uv(void);
+
+#endif //ASCENSION_SPRITE_H
--- a/src/ascension/ui/text.h	Tue Jun 10 19:17:21 2025 +0200
+++ b/src/ascension/ui/text.h	Tue Jun 10 19:29:07 2025 +0200
@@ -29,11 +29,12 @@
 #define ASCENSION_UI_TEXT_H
 
 #include "font.h"
-#include "../2d/sprite.h"
+#include "../sprite.h"
 
 #include <cx/string.h>
 
 typedef struct AscText {
+    // TODO: try to remove the explicit dependency to a sprite
     AscSprite base;
     cxmutstr text;
     AscFont font;
--- a/src/scene.c	Tue Jun 10 19:17:21 2025 +0200
+++ b/src/scene.c	Tue Jun 10 19:29:07 2025 +0200
@@ -30,7 +30,7 @@
 #include "ascension/scene.h"
 #include "ascension/behavior.h"
 #include "ascension/shader.h"
-#include "ascension/2d.h"
+#include "ascension/sprite.h"
 
 #include <cx/tree.h>
 #include <cx/array_list.h>
@@ -95,6 +95,7 @@
 
     // TODO: implement interleaving by depth
     if (cxIteratorValid(iter_opaque_rect)) {
+        // TODO: add abstraction, because otherwise this will explode really fast when we start adding primitive shaders
         glDisable(GL_BLEND);
         const AscShaderProgram *shader = asc_sprite_shader_rect();
         asc_shader_use(shader, &scene->camera);
--- a/src/sprite.c	Tue Jun 10 19:17:21 2025 +0200
+++ b/src/sprite.c	Tue Jun 10 19:29:07 2025 +0200
@@ -25,7 +25,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "ascension/2d/sprite.h"
+#include "ascension/sprite.h"
 
 #include "ascension/context.h"
 #include "ascension/glcontext.h"
--- a/test/snake/Makefile	Tue Jun 10 19:17:21 2025 +0200
+++ b/test/snake/Makefile	Tue Jun 10 19:29:07 2025 +0200
@@ -52,13 +52,12 @@
  ../../src/ascension/camera.h ../../src/ascension/input.h \
  ../../src/ascension/ui/font.h ../../src/ascension/behavior.h \
  ../../src/ascension/ui.h ../../src/ascension/ui/text.h \
- ../../src/ascension/ui/font.h ../../src/ascension/ui/../2d/sprite.h \
- ../../src/ascension/ui/../2d/../scene_node.h \
- ../../src/ascension/ui/../2d/../mesh.h \
- ../../src/ascension/ui/../2d/../datatypes.h \
- ../../src/ascension/ui/../2d/../texture.h \
- ../../src/ascension/ui/../2d/../shader.h \
- ../../src/ascension/ui/../2d/../camera.h
+ ../../src/ascension/ui/font.h ../../src/ascension/ui/../sprite.h \
+ ../../src/ascension/ui/../scene_node.h ../../src/ascension/ui/../mesh.h \
+ ../../src/ascension/ui/../datatypes.h \
+ ../../src/ascension/ui/../texture.h ../../src/ascension/ui/../shader.h \
+ ../../src/ascension/ui/../camera.h ../../src/ascension/sprite.h \
+ ../../src/ascension/2d.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
--- a/test/snake/snake.c	Tue Jun 10 19:17:21 2025 +0200
+++ b/test/snake/snake.c	Tue Jun 10 19:29:07 2025 +0200
@@ -27,6 +27,8 @@
 
 #include <ascension/core.h>
 #include <ascension/ui.h>
+#include <ascension/sprite.h>
+#include <ascension/2d.h>
 
 #include <cx/printf.h>
 

mercurial