improve datatypes

Sat, 03 May 2025 13:49:24 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 03 May 2025 13:49:24 +0200
changeset 105
2b7f92ff2c15
parent 104
2ca88ec29953
child 106
895f92cff6b8

improve datatypes

make/gcc.mk file | annotate | diff | comparison | revisions
src/Makefile file | annotate | diff | comparison | revisions
src/ascension/camera.h file | annotate | diff | comparison | revisions
src/ascension/datatypes.h file | annotate | diff | comparison | revisions
src/ascension/scene_node.h file | annotate | diff | comparison | revisions
src/ascension/transform.h file | annotate | diff | comparison | revisions
src/ascension/window.h file | annotate | diff | comparison | revisions
src/camera.c file | annotate | diff | comparison | revisions
src/context.c file | annotate | diff | comparison | revisions
src/glcontext.c file | annotate | diff | comparison | revisions
src/scene.c file | annotate | diff | comparison | revisions
src/scene_node.c file | annotate | diff | comparison | revisions
src/sprite.c file | annotate | diff | comparison | revisions
src/text.c file | annotate | diff | comparison | revisions
src/window.c file | annotate | diff | comparison | revisions
test/snake/snake.c file | annotate | diff | comparison | revisions
--- a/make/gcc.mk	Sat May 03 13:30:04 2025 +0200
+++ b/make/gcc.mk	Sat May 03 13:49:24 2025 +0200
@@ -3,6 +3,6 @@
  #
 
  CFLAGS =
- DEBUG_CFLAGS = -g -Wall -Wextra -Werror -pedantic
+ DEBUG_CFLAGS = -g -Wall -Wextra -Werror
  RELEASE_CFLAGS = -O3 -DNDEBUG
  LDFLAGS =
\ No newline at end of file
--- a/src/Makefile	Sat May 03 13:30:04 2025 +0200
+++ b/src/Makefile	Sat May 03 13:49:24 2025 +0200
@@ -45,7 +45,12 @@
 
 FORCE:
 
-$(BUILD_DIR)/camera.o: camera.c ascension/camera.h ascension/datatypes.h
+$(BUILD_DIR)/camera.o: camera.c ascension/context.h ascension/datatypes.h \
+ ascension/window.h ascension/glcontext.h ascension/primitives.h \
+ ascension/mesh.h ascension/shader.h ascension/texture.h \
+ ascension/scene.h ascension/scene_node.h ascension/transform.h \
+ ascension/camera.h ascension/input.h ascension/ui/font.h \
+ ascension/camera.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
--- a/src/ascension/camera.h	Sat May 03 13:30:04 2025 +0200
+++ b/src/ascension/camera.h	Sat May 03 13:49:24 2025 +0200
@@ -32,8 +32,8 @@
 
 typedef struct AscCamera AscCamera;
 
-typedef asc_recti(*asc_camera_viewport_update_func)(asc_vec2i window_size);
-typedef void(*asc_camera_projection_update_func)(AscCamera*, asc_vec2i window_size);
+typedef asc_recti(*asc_camera_viewport_update_func)(asc_vec2u window_size);
+typedef void(*asc_camera_projection_update_func)(AscCamera*, asc_vec2u window_size);
 
 struct AscCamera {
     asc_mat4f projection;
@@ -60,9 +60,9 @@
         struct {
             asc_recti rect;
         } ortho;
-        struct {
-            // TODO: implement
-        } perspective;
+        /*struct {
+            TODO: implement
+        } perspective;*/
     };
     asc_camera_viewport_update_func viewport_update_func;
     asc_camera_projection_update_func projection_update_func;
@@ -86,6 +86,6 @@
  * @param size the new size
  */
 __attribute__((__nonnull__))
-void asc_camera_ortho_update_size(AscCamera *camera, asc_vec2i size);
+void asc_camera_ortho_update_size(AscCamera *camera, asc_vec2u size);
 
 #endif //ASCENSION_CAMERA_H
--- a/src/ascension/datatypes.h	Sat May 03 13:30:04 2025 +0200
+++ b/src/ascension/datatypes.h	Sat May 03 13:49:24 2025 +0200
@@ -56,11 +56,20 @@
     struct { int width, height; };
     int data[2];
 } asc_vec2i;
+#define asc_vec2i_new(x, y) (asc_vec2i){{(int)x, (int)y}}
+
+typedef union asc_vec2u {
+    struct { unsigned x, y; };
+    struct { unsigned width, height; };
+    unsigned data[2];
+} asc_vec2u;
+#define asc_vec2u_new(x, y) (asc_vec2u){{(unsigned)x, (unsigned)y}}
 
 typedef struct asc_recti {
     asc_vec2i pos;
-    asc_vec2i size; // TODO: shouldn't sizes be unsigned?
+    asc_vec2u size;
 } asc_recti;
+#define asc_recti_new(x, y, w, h) (asc_recti){asc_vec2i_new(x,y), asc_vec2u_new(w,h)}
 
 typedef union asc_vec3f {
     struct { float x, y, z; };
@@ -68,14 +77,17 @@
     struct { float pitch, yaw, roll; };
     float data[3];
 } asc_vec3f;
+#define asc_vec3f_new(x, y, z) (asc_vec3f){{(float)x, (float)y, (float)(z)}}
 
 typedef struct asc_col4i {
     asc_ubyte red, green, blue, alpha;
 } asc_col4i;
+#define asc_col4i_new(r, g, b, a) (asc_col4i){(asc_ubyte)r, (asc_ubyte)g, (asc_ubyte)b, (asc_ubyte)a}
 
 typedef struct asc_col4f {
     float red, green, blue, alpha;
 } asc_col4f;
+#define asc_col4f_new(r, g, b, a) (asc_col4f){(float)r, (float)g, (float)b, (float)a}
 
 typedef float asc_mat4f[16];
 
@@ -89,6 +101,12 @@
     return v;
 }
 
+static inline unsigned asc_clamp_u(unsigned v, unsigned min, unsigned max) {
+    if (v < min) return min;
+    if (v > max) return max;
+    return v;
+}
+
 /**
  * Converts a float color (0.0f to 1.0f) to an int color (0 to 255).
  *
@@ -99,15 +117,15 @@
  * @return the same color using ints
  */
 static inline asc_col4i asc_col_ftoi(asc_col4f c) {
-    int red = (int)(255*c.red);
-    int green = (int)(255*c.green);
-    int blue = (int)(255*c.blue);
-    int alpha = (int)(255*c.alpha);
+    unsigned red = (unsigned)(255*c.red);
+    unsigned green = (unsigned)(255*c.green);
+    unsigned blue = (unsigned)(255*c.blue);
+    unsigned alpha = (unsigned)(255*c.alpha);
     asc_col4i r;
-    r.red = (asc_ubyte)asc_clamp_i(red, 0, 255);
-    r.green = (asc_ubyte)asc_clamp_i(green, 0, 255);
-    r.blue = (asc_ubyte)asc_clamp_i(blue, 0, 255);
-    r.alpha = (asc_ubyte)asc_clamp_i(alpha, 0, 255);
+    r.red = asc_clamp_u(red, 0, 255);
+    r.green = asc_clamp_u(green, 0, 255);
+    r.blue = asc_clamp_u(blue, 0, 255);
+    r.alpha = asc_clamp_u(alpha, 0, 255);
     return r;
 }
 
--- a/src/ascension/scene_node.h	Sat May 03 13:30:04 2025 +0200
+++ b/src/ascension/scene_node.h	Sat May 03 13:49:24 2025 +0200
@@ -197,7 +197,7 @@
 
 __attribute__((__nonnull__)) static inline
 asc_vec2i asc_get_position2d(AscSceneNode *node) {
-    return (asc_vec2i) {(int) node->position.x, (int) node->position.y};
+    return asc_vec2i_new(node->position.x, node->position.y);
 }
 
 __attribute__((__nonnull__)) static inline
@@ -208,16 +208,4 @@
     asc_node_update_transform(node);
 }
 
-__attribute__((__nonnull__)) static inline
-void asc_set_scale2d(AscSceneNode *node, int width, int height) {
-    node->scale.width = (float)width;
-    node->scale.height = (float)height;
-    asc_node_update_transform(node);
-}
-
-__attribute__((__nonnull__)) static inline
-asc_vec2i asc_get_scale2d(AscSceneNode *node) {
-    return (asc_vec2i) {(int) node->scale.width, (int) node->scale.height};
-}
-
 #endif
--- a/src/ascension/transform.h	Sat May 03 13:30:04 2025 +0200
+++ b/src/ascension/transform.h	Sat May 03 13:49:24 2025 +0200
@@ -76,8 +76,8 @@
 }
 
 ASC_TRANFORM_FUNC void asc_transform_rotate(
-        asc_transform transform,
-        asc_vec3f vec
+        __attribute__((__unused__)) asc_transform transform,
+        __attribute__((__unused__)) asc_vec3f vec
 ) {
     // TODO: implement
 }
--- a/src/ascension/window.h	Sat May 03 13:30:04 2025 +0200
+++ b/src/ascension/window.h	Sat May 03 13:49:24 2025 +0200
@@ -56,7 +56,7 @@
     bool resized;
     bool focused;
     SDL_Window* window;
-    asc_vec2i dimensions;
+    asc_vec2u dimensions;
     AscGLContext glctx;
     AscScene ui;
     AscScene scenes[ASC_MAX_SCENES];
--- a/src/camera.c	Sat May 03 13:30:04 2025 +0200
+++ b/src/camera.c	Sat May 03 13:49:24 2025 +0200
@@ -56,6 +56,6 @@
     asc_mat4f_ortho(camera->projection, left, right, bottom, top, -1, 1);
 }
 
-void asc_camera_ortho_update_size(AscCamera *camera, asc_vec2i size) {
+void asc_camera_ortho_update_size(AscCamera *camera, asc_vec2u size) {
     asc_mat4f_ortho_update_size(camera->projection, (float)size.width, (float)size.height);
 }
--- a/src/context.c	Sat May 03 13:30:04 2025 +0200
+++ b/src/context.c	Sat May 03 13:49:24 2025 +0200
@@ -211,9 +211,9 @@
 static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) {
     unsigned int i = asc_window_index(id);
     if (i < ASC_MAX_WINDOWS) {
-        asc_vec2i dimensions = (asc_vec2i) {width, height};
         asc_context.windows[i].resized = true;
-        asc_context.windows[i].dimensions = dimensions;
+        asc_context.windows[i].dimensions.width = (unsigned) width;
+        asc_context.windows[i].dimensions.height = (unsigned) height;
     }
 }
 
--- a/src/glcontext.c	Sat May 03 13:30:04 2025 +0200
+++ b/src/glcontext.c	Sat May 03 13:49:24 2025 +0200
@@ -35,7 +35,7 @@
 static void asc_gl_debug_callback(
         GLenum source, GLenum type, GLuint id, GLenum severity,
         GLsizei length, const GLchar* message,
-        const void* userParam
+        __attribute__((__unused__)) const void* userParam
 ) {
     if (type == GL_DEBUG_TYPE_ERROR) {
         asc_error("OpenGL source = %d, id = %u, type = %d, severity= %d, message = %.*s",
--- a/src/scene.c	Sat May 03 13:30:04 2025 +0200
+++ b/src/scene.c	Sat May 03 13:49:24 2025 +0200
@@ -63,7 +63,7 @@
 
     // if the window resized, we must update the viewport
     if (asc_active_window->resized) {
-        asc_vec2i window_size = asc_active_window->dimensions;
+        asc_vec2u window_size = asc_active_window->dimensions;
         if (scene->camera.viewport_update_func == NULL) {
             // this assumes the viewport was initialized with zeros!
             scene->camera.viewport.size = window_size;
--- a/src/scene_node.c	Sat May 03 13:30:04 2025 +0200
+++ b/src/scene_node.c	Sat May 03 13:49:24 2025 +0200
@@ -105,6 +105,7 @@
     if (node->behaviors == NULL) {
         node->behaviors = cxLinkedListCreateSimple(CX_STORE_POINTERS);
     }
+    // TODO: create AscBehavior struct with more stuff than just a fptr
     cxListAdd(node->behaviors, behavior);
 }
 
--- a/src/sprite.c	Sat May 03 13:30:04 2025 +0200
+++ b/src/sprite.c	Sat May 03 13:49:24 2025 +0200
@@ -52,12 +52,12 @@
                              : ASC_RENDER_GROUP_SPRITE_BLEND;
     node->free_func = asc_sprite_free;
 
-    node->position.x = (float) args.x;
-    node->position.y = (float) args.y;
-    node->position.z = ASC_SCENE_2D_DEPTH_OFFSET;
-    node->scale.width = (float) (args.width == 0 ? args.texture->width : args.width);
-    node->scale.height = (float) (args.height == 0 ? args.texture->height : args.height);
-    node->scale.depth = 1.f;
+    node->position = asc_vec3f_new(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET);
+    node->scale = asc_vec3f_new(
+        args.width == 0 ? args.texture->width : args.width,
+        args.height == 0 ? args.texture->height : args.height,
+        1
+    );
     asc_node_update_transform(node);
 
     return node;
--- a/src/text.c	Sat May 03 13:30:04 2025 +0200
+++ b/src/text.c	Sat May 03 13:49:24 2025 +0200
@@ -53,7 +53,8 @@
         asc_error("Rendering TTF surface failed: %s", SDL_GetError());
         return;
     }
-    asc_set_scale2d(node, surface->w, surface->h);
+    // TODO: don't use scale - create an own fitting mesh
+    asc_set_scale(node, (float)surface->w, (float)surface->h, 1.f);
     if (asc_test_flag(text->base.data.flags, ASC_TEXT_CENTERED_FLAG)) {
         unsigned short newoffx = surface->w / 2;
         asc_vec2i pos = asc_get_position2d(node);
@@ -87,9 +88,7 @@
     node->update_func = asc_text_update;
 
     node->flags = args.alignment;
-    node->position.x = (float) args.x;
-    node->position.y = (float) args.y;
-    node->position.z = ASC_SCENE_2D_DEPTH_OFFSET;
+    node->position = asc_vec3f_new(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET);
     node->scale.depth = 1.f;
     text->max_width = args.max_width;
     text->font = asc_active_font;
--- a/src/window.c	Sat May 03 13:30:04 2025 +0200
+++ b/src/window.c	Sat May 03 13:49:24 2025 +0200
@@ -69,18 +69,17 @@
     }
 
     window->id = SDL_GetWindowID(window->window);
-    SDL_GetWindowSize(window->window,
-            &window->dimensions.width,
-            &window->dimensions.height
-    );
+    {
+        Sint32 w, h;
+        SDL_GetWindowSize(window->window, &w, &h);
+        window->dimensions = asc_vec2u_new(w, h);
+    }
     window->resized = true; // count initial sizing as resize
 
     if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) {
         asc_scene_init(&window->ui, (AscCameraParams){
             .type = ASC_CAMERA_ORTHO,
-            .ortho.rect = (asc_recti){
-                0, 0, window->dimensions.width, window->dimensions.height
-            },
+            .ortho.rect = asc_recti_new(0, 0, window->dimensions.width, window->dimensions.height),
             .projection_update_func = asc_camera_ortho_update_size
         });
         asc_dprintf("Window %u initialized at index %u", window->id, index);
--- a/test/snake/snake.c	Sat May 03 13:30:04 2025 +0200
+++ b/test/snake/snake.c	Sat May 03 13:49:24 2025 +0200
@@ -42,7 +42,7 @@
 #define BACKDROP_SCENE asc_window_scene(0)
 #define MAIN_SCENE asc_window_scene(1)
 
-static void destroy_textures(void *dummy) {
+static void destroy_textures(__attribute__((__unused__)) void *dummy) {
     asc_texture_destroy(tex2d, TEX2D_COUNT);
 }
 
@@ -70,12 +70,13 @@
     // tie to bottom right of the screen
     if (asc_test_flag(node->flags, ASC_SCENE_NODE_GRAPHICS_UPDATED)
         || asc_active_window->resized) {
-        asc_vec2i bottom_right = asc_active_window->dimensions;
-        asc_vec2i scale = asc_get_scale2d(node);
+        asc_vec2u bottom_right = asc_active_window->dimensions;
+        // TODO: replace scale with asc_text_get_size()
+        asc_vec3f text_size = node->scale;
         asc_set_position2d(
                 node,
-                bottom_right.x - scale.width - 10,
-                bottom_right.y - scale.height - 10
+                (int)bottom_right.x - (int)text_size.width - 10,
+                (int)bottom_right.y - (int)text_size.height - 10
         );
     }
 }
@@ -83,8 +84,9 @@
 static void scale_backdrop(AscSceneNode *node) {
     // scale the backdrop to the size of the window
     if (asc_active_window->resized) {
-        asc_vec2i window_size = asc_active_window->dimensions;
-        asc_set_scale2d(node, window_size.width, window_size.height);
+        asc_vec2u window_size = asc_active_window->dimensions;
+        // TODO: replace scale with asc_sprite_set_size()
+        asc_set_scale(node, (float)window_size.width, (float)window_size.height, 1);
         // TODO: implement texture repetition
     }
 }
@@ -126,9 +128,9 @@
     // TODO: return something
 }
 
-static asc_recti update_viewport_for_window_resize(asc_vec2i window_size) {
+static asc_recti update_viewport_for_window_resize(asc_vec2u window_size) {
     // Compute scaling and offsets
-    int viewport_size, offset_x = 0, offset_y = 0;
+    unsigned viewport_size, offset_x = 0, offset_y = 0;
     if (window_size.width > window_size.height) {
         // Wider window: letterbox (black bars on top/bottom)
         offset_x = (window_size.width - window_size.height) / 2;
@@ -140,15 +142,10 @@
     }
 
     // Set the viewport to the scaled and centered region
-    return (asc_recti){
-        offset_x,
-        offset_y,
-        viewport_size,
-        viewport_size
-    };
+    return asc_recti_new(offset_x, offset_y, viewport_size, viewport_size);
 }
 
-int main(int argc, char** argv) {
+int main(void) {
     asc_context_initialize();
     if (asc_has_error()) {
         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
@@ -178,7 +175,7 @@
     });
     asc_scene_init(MAIN_SCENE, (AscCameraParams) {
         .type = ASC_CAMERA_ORTHO,
-        .ortho.rect = (asc_recti){0, 0, game_field_size, game_field_size},
+        .ortho.rect = asc_recti_new(0, 0, game_field_size, game_field_size),
         .viewport_update_func = update_viewport_for_window_resize
     });
 

mercurial