From c1583392c4370e16899952152c4b251e433a3b69 Mon Sep 17 00:00:00 2001
From: Dominic Masters <dominic@domsplace.com>
Date: Mon, 3 Jan 2022 08:12:28 -0800
Subject: [PATCH] Writing a few more asserts.

---
 src/assert/assert.h                  |  2 +-
 src/display/animation/queue.c        |  4 +++
 src/display/animation/timeline.c     |  2 ++
 src/display/shaderprogram.c          | 23 ++++++++++++++
 src/display/shaderprogram.h          |  1 +
 src/display/shaders/standardshader.c |  1 +
 src/display/shaders/standardshader.h | 47 ++++++++++++++++++++++++++++
 src/display/texture.c                | 18 +++++++++++
 src/display/texture.h                |  1 +
 9 files changed, 98 insertions(+), 1 deletion(-)

diff --git a/src/assert/assert.h b/src/assert/assert.h
index cc03a8f6..53c78cf3 100644
--- a/src/assert/assert.h
+++ b/src/assert/assert.h
@@ -19,7 +19,7 @@
 #define ASSERT_NOT_EQUAL(x, y) ASSERT_TRUE(x != y)
 
 #define ASSERT_LESS_THAN(x, y) ASSERT_TRUE(x < y)
-#define ASSERT_LESS_THAN_EQUAL_TO (x, y) ASSERT_TRUE(x <= y)
+#define ASSERT_LESS_THAN_EQUAL_TO(x, y) ASSERT_TRUE(x <= y)
 
 #define ASSERT_GREATER_THAN(x, y) ASSERT_TRUE(x > y)
 #define ASSERT_GREATER_THAN_EQUAL_TO(x, y) ASSERT_TRUE(x >= y)
diff --git a/src/display/animation/queue.c b/src/display/animation/queue.c
index 1d55ca39..2dc8fb3e 100644
--- a/src/display/animation/queue.c
+++ b/src/display/animation/queue.c
@@ -41,6 +41,7 @@ queueaction_t * queueAdd(queue_t *queue) {
   queueaction_t *action;
 
   ASSERT_NOT_NULL(queue);
+  ASSERT_LESS_THAN(queue->count, ANIMATION_QUEUE_ITEM_MAX);
 
   action = queue->items + queue->count;
   action->index = queue->count;
@@ -79,9 +80,12 @@ void queueDispose(queue_t *queue) {
 
 void queueRestack(queue_t *queue) {
   uint8_t i;
+
   ASSERT_NOT_NULL(queue);
 
+
   // Rewind the array.
+  if(queue->current == 0) return;
   arrayRewind(sizeof(queueaction_t), queue->items, ANIMATION_QUEUE_START,
     queue->current, queue->count
   );
diff --git a/src/display/animation/timeline.c b/src/display/animation/timeline.c
index 667f85f2..431f6a85 100644
--- a/src/display/animation/timeline.c
+++ b/src/display/animation/timeline.c
@@ -120,7 +120,9 @@ timelineaction_t * timelineAddAction(timeline_t *timeline, float start,
   timelineaction_t *action;
 
   ASSERT_NOT_NULL(timeline);
+  ASSERT_LESS_THAN(timeline->actionCount, TIMELINE_ACTION_COUNT_MAX);
   ASSERT_GREATER_THAN_EQUAL_TO(start, 0);
+  ASSERT_IF(duration < 0, ASSERT_EQUAL(duration, -1));
 
   action = timeline->actions + (timeline->actionCount++);
   action->loop = false;
diff --git a/src/display/shaderprogram.c b/src/display/shaderprogram.c
index febca974..b4882a22 100644
--- a/src/display/shaderprogram.c
+++ b/src/display/shaderprogram.c
@@ -14,6 +14,10 @@ void shaderInit(shaderprogram_t *shader,
   char *error;
   GLuint shaderVertex, shaderFragment, shaderProgram;
 
+  ASSERT_NOT_NULL(shader);
+  ASSERT_NOT_NULL(vertexShaderSource);
+  ASSERT_NOT_NULL(fragmentShaderSource);
+
   // Load the vertex shader first
   shaderVertex = glCreateShader(GL_VERTEX_SHADER);
   glShaderSource(shaderVertex, 1, &vertexShaderSource, 0);
@@ -74,12 +78,15 @@ void shaderInit(shaderprogram_t *shader,
 }
 
 void shaderDispose(shaderprogram_t *shader) {
+  ASSERT_NOT_NULL(shader);
+
   glDeleteProgram(shader->shaderProgram);
   glDeleteShader(shader->shaderVertex);
   glDeleteShader(shader->shaderFrag);
 }
 
 void shaderUse(shaderprogram_t *shader) {
+  ASSERT_NOT_NULL(shader);
   glUseProgram(shader->shaderProgram);
 }
 
@@ -87,6 +94,11 @@ void shaderUseCamera(
   shaderprogram_t *shader, shaderuniform_t uniformView,
   shaderuniform_t uniformProjection, camera_t *camera
 ) {
+  ASSERT_NOT_NULL(shader);
+  ASSERT_NOT_NULL(camera);
+  ASSERT_GREATER_THAN_EQUAL_TO(uniformView, 0);
+  ASSERT_GREATER_THAN_EQUAL_TO(uniformProjection, 0);
+
   shaderUseMatrix(shader, uniformView, &camera->view);
   shaderUseMatrix(shader, uniformProjection, &camera->projection);
 }
@@ -94,18 +106,27 @@ void shaderUseCamera(
 void shaderUseTexture(
   shaderprogram_t *shader, shaderuniform_t uniform, textureslot_t slot
 ) {
+  ASSERT_NOT_NULL(shader);
+  ASSERT_GREATER_THAN_EQUAL_TO(uniform, 0);
+  ASSERT_GREATER_THAN_EQUAL_TO(slot, 0);
+  ASSERT_LESS_THAN(slot, TEXTURE_SLOTS_MAX);
   glUniform1i(uniform, slot);
 }
 
 void shaderUseMatrix(
   shaderprogram_t *shader, shaderuniform_t uniform, matrix_t *matrix
 ) {
+  ASSERT_NOT_NULL(shader);
+  ASSERT_NOT_NULL(matrix);
+  ASSERT_GREATER_THAN_EQUAL_TO(uniform, 0);
   glUniformMatrix4fv(uniform, 1, GL_FALSE, matrix->internalMatrix[0]);
 }
 
 void shaderUseColor(
   shaderprogram_t *shader, shaderuniform_t uniform, pixel_t color
 ) {
+  ASSERT_NOT_NULL(shader);
+  ASSERT_GREATER_THAN_EQUAL_TO(uniform, 0);
   glUniform4f(uniform,
     (float)color.r / 255.0f,
     (float)color.g / 255.0f,
@@ -115,5 +136,7 @@ void shaderUseColor(
 }
 
 shaderuniform_t shaderGetUniformByName(shaderprogram_t *sp, const char *name) {
+  ASSERT_NOT_NULL(sp);
+  ASSERT_NOT_NULL(name);
   return glGetUniformLocation(sp->shaderProgram, name);
 }
\ No newline at end of file
diff --git a/src/display/shaderprogram.h b/src/display/shaderprogram.h
index caf87a54..8c76ab4b 100644
--- a/src/display/shaderprogram.h
+++ b/src/display/shaderprogram.h
@@ -7,6 +7,7 @@
 
 #pragma once
 #include "../libs.h"
+#include "../assert/assert.h"
 #include "matrix.h"
 #include "camera.h"
 #include "texture.h"
diff --git a/src/display/shaders/standardshader.c b/src/display/shaders/standardshader.c
index 5724a107..14f52b3d 100644
--- a/src/display/shaders/standardshader.c
+++ b/src/display/shaders/standardshader.c
@@ -87,6 +87,7 @@ void standardShaderSetTexture(standardshader_t *stds, texture_t *texture) {
   ASSERT_NOT_NULL(texture);
   if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
 
+  textureBind(texture, 0);
   shaderUseTexture(STANDARD_SHADER_SHADER(stds), stds->uniformTexture, 0);
 }
 
diff --git a/src/display/shaders/standardshader.h b/src/display/shaders/standardshader.h
index ef734617..8c1fc523 100644
--- a/src/display/shaders/standardshader.h
+++ b/src/display/shaders/standardshader.h
@@ -35,10 +35,57 @@ typedef struct {
   shaderuniform_t uniformColor;
 } standardshader_t;
 
+/**
+ * Initialize the standard shader.
+ * 
+ * @param stds Standard Shader Instance.
+ * @param assetManager Asset Manager to load the shaders' assets.
+ */
 void standardShaderInit(standardshader_t *stds, assetmanager_t *assetManager);
+
+/**
+ * Bind the standard shader as the currently active shader.
+ * 
+ * @param stds Standard shader to bind.
+ */
 void standardShaderUse(standardshader_t *stds);
+
+/**
+ * Set the camera for the standard shader.
+ * 
+ * @param stds Standard shader instance.
+ * @param camera Camera to bind.
+ */
 void standardShaderSetCamera(standardshader_t *stds, camera_t *camera);
+
+/**
+ * Set the texture for the standard shader.
+ * 
+ * @param stds Standard shader instance.
+ * @param texture Texture to bind.
+ */
 void standardShaderSetTexture(standardshader_t *stds, texture_t *texture);
+
+/**
+ * Set the color for the standard shader.
+ * 
+ * @param stds Standard shader instance.
+ * @param color Color to bind.
+ */
 void standardShaderSetColor(standardshader_t *stds, pixel_t color);
+
+/**
+ * Set the position matrix for the standard shader.
+ * 
+ * @param stds Standard shader to use.
+ * @param matrix Matrix to use.
+ */
 void standardShaderSetPosition(standardshader_t *stds, matrix_t *matrix);
+
+/**
+ * Dispose/cleanup the standard shader.
+ * 
+ * @param stds Standard shader instance.
+ * @param assMan Asset manager instance.
+ */
 void standardShaderDispose(standardshader_t *stds, assetmanager_t *assMan);
\ No newline at end of file
diff --git a/src/display/texture.c b/src/display/texture.c
index 05340c73..2206855e 100644
--- a/src/display/texture.c
+++ b/src/display/texture.c
@@ -16,6 +16,10 @@
 void textureInit(texture_t *texture, int32_t width, int32_t height,
   pixel_t *pixels
 ) {
+  ASSERT_NOT_NULL(texture);
+  ASSERT_GREATER_THAN(width, 0);
+  ASSERT_GREATER_THAN(height, 0);
+
   texture->width = width;
   texture->height = height;
 
@@ -50,6 +54,10 @@ void textureInit(texture_t *texture, int32_t width, int32_t height,
 }
 
 void textureBind(texture_t *texture, textureslot_t slot) {
+  ASSERT_NOT_NULL(texture);
+  ASSERT_GREATER_THAN_EQUAL_TO(slot, 0);
+  ASSERT_LESS_THAN(slot, TEXTURE_SLOTS_MAX);
+
   glActiveTexture(GL_TEXTURE0 + slot);
   glBindTexture(GL_TEXTURE_2D, texture->id);
 }
@@ -57,6 +65,15 @@ void textureBind(texture_t *texture, textureslot_t slot) {
 void textureBufferPixels(texture_t *texture,
   int32_t x, int32_t y, int32_t width, int32_t height, pixel_t *pixels
 ) {
+  ASSERT_NOT_NULL(texture);
+  ASSERT_NOT_NULL(pixels);
+  ASSERT_GREATER_THAN_EQUAL_TO(x, 0);
+  ASSERT_GREATER_THAN_EQUAL_TO(y, 0);
+  ASSERT_GREATER_THAN(width, 0);
+  ASSERT_GREATER_THAN(height, 0);
+  ASSERT_LESS_THAN_EQUAL_TO(width, texture->width);
+  ASSERT_LESS_THAN_EQUAL_TO(height, texture->height);
+
   glBindTexture(GL_TEXTURE_2D, texture->id);
   glTexSubImage2D(GL_TEXTURE_2D, 0,
     x, y, width, height,
@@ -65,5 +82,6 @@ void textureBufferPixels(texture_t *texture,
 }
 
 void textureDispose(texture_t *texture) {
+  ASSERT_NOT_NULL(texture);
   glDeleteTextures(1, &texture->id);
 }
\ No newline at end of file
diff --git a/src/display/texture.h b/src/display/texture.h
index 7c278dc4..711ce68c 100644
--- a/src/display/texture.h
+++ b/src/display/texture.h
@@ -5,6 +5,7 @@
 
 #pragma once
 #include "../libs.h"
+#include "../assert/assert.h"
 
 #define TEXTURE_SLOTS_MAX 8