From 11e0545d39d392b7ddb4e760682e2846cbee6c6a Mon Sep 17 00:00:00 2001
From: Dominic Masters <dominic@domsplace.com>
Date: Thu, 18 Mar 2021 22:23:39 +1100
Subject: [PATCH] Fixed primitive coordiantes being inaccurate

---
 assets/shaders/test.frag       |  2 --
 src/engine/display/primitive.c | 14 +++++++++-----
 src/engine/display/texture.h   |  2 +-
 src/engine/engine.c            | 17 ++++++++++-------
 src/engine/file/asset.h        |  3 +++
 5 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/assets/shaders/test.frag b/assets/shaders/test.frag
index 5ce15cad..945dbdd6 100644
--- a/assets/shaders/test.frag
+++ b/assets/shaders/test.frag
@@ -9,6 +9,4 @@ out vec4 FragColor;
 void main() {
   vec4 color = texture(u_Text, TexCoord);
   FragColor = color;
-  // FragColor = color + vec4(0.5, 0.5, 0.5, 1);
-  // FragColor = vec4(1, 1, 1, 1);
 }
\ No newline at end of file
diff --git a/src/engine/display/primitive.c b/src/engine/display/primitive.c
index 0843b63e..eb642abf 100644
--- a/src/engine/display/primitive.c
+++ b/src/engine/display/primitive.c
@@ -26,17 +26,21 @@ primitive_t * primitiveCreate(int32_t verticeCount, int32_t indiceCount) {
   glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer);
   glBufferData(GL_ARRAY_BUFFER, sizePositions+sizeCoordinates, 0, GL_DYNAMIC_DRAW);
 
+  size_t offset = 0;
+
   // Setup the attrib pointers
   glVertexAttribPointer(0, PRIMITIVE_POSITIONS_PER_VERTICE, GL_FLOAT,
-    GL_FALSE, 0, (void *)0
+    GL_FALSE, 0, (void *)offset
   );
+  offset += sizePositions;
+
   glVertexAttribPointer(1, PRIMITIVE_COORDINATES_PER_VERTICE, GL_FLOAT,
-    GL_FALSE, 0, (void *)0
+    GL_FALSE, 0, (void *)offset
   );
+
   glEnableVertexAttribArray(0);
   glEnableVertexAttribArray(1);
 
-
   return primitive;
 }
 
@@ -52,7 +56,7 @@ void primitiveBufferVertices(primitive_t *primitive,
   lengthPositions = sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * count;
   offsetPositions = sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * position;
 
-  lengthCoordinates  = sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * count;
+  lengthCoordinates  = sizeof(float) * PRIMITIVE_COORDINATES_PER_VERTICE * count;
   offsetCoordinates = (
     (sizeof(float) * PRIMITIVE_POSITIONS_PER_VERTICE * primitive->verticeCount)+
     (sizeof(float) * PRIMITIVE_COORDINATES_PER_VERTICE * position)
@@ -75,7 +79,7 @@ void primitiveBufferVertices(primitive_t *primitive,
   // Buffer the data into the GPU
   glBindBuffer(GL_ARRAY_BUFFER, primitive->vertexBuffer);
   glBufferSubData(GL_ARRAY_BUFFER, offsetPositions, lengthPositions, positions);
-  glBufferSubData(GL_ARRAY_BUFFER, offsetCoordinates, lengthCoordinates, positions);
+  glBufferSubData(GL_ARRAY_BUFFER, offsetCoordinates, lengthCoordinates, coordinates);
 
   // Free the vertices.
   free(positions);
diff --git a/src/engine/display/texture.h b/src/engine/display/texture.h
index 77736c3f..79ede17b 100644
--- a/src/engine/display/texture.h
+++ b/src/engine/display/texture.h
@@ -22,7 +22,7 @@ typedef struct {
 
 /** Information about a single pixel. */
 typedef struct {
-  uint8_t r, g, b, a;
+  uint8_t r, g, b, a ;
 } pixel_t;
 
 /**
diff --git a/src/engine/engine.c b/src/engine/engine.c
index 9afbd3b1..c41f666d 100644
--- a/src/engine/engine.c
+++ b/src/engine/engine.c
@@ -56,13 +56,16 @@ engine_t * engineInit(platform_t *platform, char *name, uint32_t inputCount) {
      1,  1, 1, 1
   );
 
-  texture = textureCreate(1, 1, NULL);
-  pixel_t *pixels = malloc(sizeof(pixel_t) * 1 * 1);
-  for(int32_t i = 0; i < 1*1; i++) {
-    pixels[i].r = 0xFF, pixels[i].g = 0x00, pixels[i].b = 0xFF;
-    pixels[i].a = 0xFF;
-  }
-  textureBufferPixels(texture, 0, 0, 1, 1, pixels);
+
+  texture = textureCreate(2, 2, NULL);
+  pixel_t *p = malloc(sizeof(pixel_t) * 2 * 2);
+  p[0].r=0xFF, p[0].g=0x00, p[0].b=0x00, p[0].a = 0xFF;
+  p[1].r=0x00, p[1].g=0xFF, p[1].b=0x00, p[1].a = 0xFF;
+  p[2].r=0x00, p[2].g=0x00, p[2].b=0xFF, p[2].a = 0xFF;
+  p[3].r=0xFF, p[3].g=0xFF, p[3].b=0xFF, p[3].a = 0xFF;
+
+
+  textureBufferPixels(texture, 0, 0, 2, 2, p);
   shaderUseTexture(shader, texture);
 
   return engine;
diff --git a/src/engine/file/asset.h b/src/engine/file/asset.h
index dcd09228..520ae468 100644
--- a/src/engine/file/asset.h
+++ b/src/engine/file/asset.h
@@ -11,7 +11,9 @@
 #include <stdint.h>
 #include <malloc.h>
 #include <string.h>
+
 #include "../display/shader.h"
+#include "../display/texture.h"
 
 /** Prefix of all asset load methods, may be customizable in future. */
 #define ASSET_PREFIX "../assets/"
@@ -67,6 +69,7 @@ int32_t assetBufferEnd(FILE *buffer);
 void assetBufferSkip(FILE *buffer, int32_t n);
 
 /**
+ * Load a shader program from a vertex and fragment shader file.
  * 
  * @param fileVertex The file path of the vertex shader
  * @param fileFragment The file path of the fragment shader