Browse Source

Add some texture code.

Manoel Trapier 1 year ago
parent
commit
66b381725e
5 changed files with 106 additions and 11 deletions
  1. 6 7
      source/include/colour.h
  2. 4 4
      source/include/display.h
  3. 40 0
      source/include/texture.h
  4. 1 0
      source/main.c
  5. 55 0
      source/texture.c

+ 6 - 7
source/include/colour.h

@@ -1,14 +1,13 @@
 /*
- * 3D Engine 
- * colour.h: 
- * Based on pikuma.com 3D software renderer in C
- * Copyright (c) 2021 986-Studio. All rights reserved.
+ * Voxel-a-tord
+ * colour.h:
+ * Copyright (c) 2021-2022 986-Studio. All rights reserved.
  *
  * Created by Manoël Trapier on 15/03/2021.
  */
 
-#ifndef THREEDENGINE_SOURCE_INCLUDE_COLOUR_H
-#define THREEDENGINE_SOURCE_INCLUDE_COLOUR_H
+#ifndef VOXELATOR_SOURCE_INCLUDE_COLOUR_H
+#define VOXELATOR_SOURCE_INCLUDE_COLOUR_H
 
 /***********************************************************************************************************************
  * Data types
@@ -21,4 +20,4 @@ typedef uint32_t colour_t;
 #define MAKE_RGB(_r, _g, _b) ((0xFF000000) | ((_b & 0xFF) << 16) | ((_g & 0xFF) << 8) | ((_r & 0xFF)))
 #define MAKE_ARGB(_a, _r, _g, _b) (((_a & 0xFF) << 24) | ((_g & 0xFF) << 16) | ((_g & 0xFF) << 8) | ((_r & 0xFF)))
 
-#endif /* THREEDENGINE_SOURCE_INCLUDE_COLOUR_H */
+#endif /* VOXELATOR_SOURCE_INCLUDE_COLOUR_H */

+ 4 - 4
source/include/display.h

@@ -1,13 +1,13 @@
 /*
  * Voxel-a-tord
  * display.h: 
-* Copyright (c) 2021-2022 986-Studio. All rights reserved.
+ * Copyright (c) 2021-2022 986-Studio. All rights reserved.
  *
  * Created by Manoël Trapier on 01/03/2021.
  */
 
-#ifndef THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H
-#define THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H
+#ifndef VOXELATOR_SOURCE_INCLUDE_DISPLAY_H
+#define VOXELATOR_SOURCE_INCLUDE_DISPLAY_H
 
 #include <stdbool.h>
 #include <SDL.h>
@@ -52,4 +52,4 @@ void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, colour_t colour);
 void drawText(int32_t x, int32_t y, colour_t colour, const char *format, ...);
 
 
-#endif /* THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H */
+#endif /* VOXELATOR_SOURCE_INCLUDE_DISPLAY_H */

+ 40 - 0
source/include/texture.h

@@ -0,0 +1,40 @@
+/*
+ * Voxel-a-tord
+ * texture.h:
+ * Copyright (c) 2021-2022 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 07/03/2021.
+ */
+
+#ifndef VOXELATOR_SOURCE_INCLUDE_TEXTURE_H
+#define VOXELATOR_SOURCE_INCLUDE_TEXTURE_H
+
+#include <stdint.h>
+#include <upng.h>
+
+#include <colour.h>
+
+/***********************************************************************************************************************
+ * Data types
+ **********************************************************************************************************************/
+typedef struct tex2_t
+{
+    double u, v;
+} tex2_t;
+
+typedef struct texture_t
+{
+    upng_t *png;
+    colour_t *data;
+    int width;
+    int height;
+} texture_t;
+
+/***********************************************************************************************************************
+ * Prototypes
+ **********************************************************************************************************************/
+void loadTextureDateFromPng(texture_t *texture, const char *filepath);
+void textureCleanup(texture_t *texture);
+colour_t getTextureColour(texture_t *texture, tex2_t position);
+
+#endif /* VOXELATOR_SOURCE_INCLUDE_TEXTURE_H */

+ 1 - 0
source/main.c

@@ -15,6 +15,7 @@
 
 #include <log.h>
 #include <display.h>
+#include <texture.h>
 
 bool isRunning = false;
 int previousFrameTime = 0;

+ 55 - 0
source/texture.c

@@ -0,0 +1,55 @@
+/*
+ * 3D Engine 
+ * texture.c: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 07/03/2021.
+ */
+#include <stdlib.h>
+#include <upng.h>
+
+#include <texture.h>
+
+/***********************************************************************************************************************
+ * Public functions
+ **********************************************************************************************************************/
+
+void loadTextureDateFromPng(texture_t *tex, const char *filepath)
+{
+    if (tex->png != NULL)
+    {
+        upng_free(tex->png);
+        tex->png = NULL;
+    }
+
+    tex->png = upng_new_from_file(filepath);
+    if (tex->png != NULL)
+    {
+        upng_decode(tex->png);
+        if (upng_get_error(tex->png)  == UPNG_EOK)
+        {
+            tex->data = (colour_t *)upng_get_buffer(tex->png);
+            tex->width = upng_get_width(tex->png);
+            tex->height = upng_get_height(tex->png);
+        }
+    }
+}
+
+void textureCleanup(texture_t *tex)
+{
+    if (tex->png != NULL)
+    {
+        upng_free(tex->png);
+        tex->png = NULL;
+    }
+
+    /* uPNG do free the buffer for us. */
+    tex->data = NULL;
+}
+
+colour_t getTextureColour(texture_t *texture, tex2_t position)
+{
+
+    return 0;
+}