More assertions.
This commit is contained in:
@ -43,6 +43,7 @@ int32_t main() {
|
||||
|
||||
// Prepare the game
|
||||
game = malloc(sizeof(game_t));
|
||||
ASSERT_NOT_NULL(game);
|
||||
GAME_STATE = game;
|
||||
input = &game->engine.input;
|
||||
|
||||
@ -72,9 +73,6 @@ int32_t main() {
|
||||
inputBind(input, INPUT_MOUSE_X, GLFW_PLATFORM_INPUT_MOUSE_X);
|
||||
inputBind(input, INPUT_MOUSE_Y, GLFW_PLATFORM_INPUT_MOUSE_Y);
|
||||
|
||||
// Set up the client
|
||||
game->engine.client.setTitle = &glfwClientSetTitle;
|
||||
|
||||
// Set up some GLFW stuff
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
glfwSetWindowTitle(window, game->engine.name);
|
||||
@ -112,13 +110,20 @@ int32_t main() {
|
||||
}
|
||||
|
||||
void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height) {
|
||||
ASSERT_NOT_NULL(window);
|
||||
ASSERT_GREATER_THAN(width, 0);
|
||||
ASSERT_GREATER_THAN(height, 0);
|
||||
|
||||
renderSetResolution(&GAME_STATE->engine.render, (float)width, (float)height);
|
||||
}
|
||||
|
||||
void glfwOnKey(GLFWwindow *window,
|
||||
int32_t key, int32_t scancode, int32_t action, int32_t mods
|
||||
) {
|
||||
input_t *input = &GAME_STATE->engine.input;
|
||||
input_t *input;
|
||||
ASSERT_NOT_NULL(window);
|
||||
|
||||
input = &GAME_STATE->engine.input;
|
||||
if(action == GLFW_PRESS) {
|
||||
inputStateSet(input, glfwGetInputSourceForKey(key), 1.0f);
|
||||
} else if(action == GLFW_RELEASE) {
|
||||
@ -131,7 +136,10 @@ void glfwOnError(int error, const char* description) {
|
||||
}
|
||||
|
||||
void glfwOnCursor(GLFWwindow *window, double x, double y) {
|
||||
input_t *input = &GAME_STATE->engine.input;
|
||||
input_t *input;
|
||||
ASSERT_NOT_NULL(window);
|
||||
|
||||
input = &GAME_STATE->engine.input;
|
||||
inputStateSet(input, GLFW_PLATFORM_INPUT_MOUSE_X, (float)x);
|
||||
inputStateSet(input, GLFW_PLATFORM_INPUT_MOUSE_Y, (float)y);
|
||||
}
|
||||
@ -145,5 +153,6 @@ inputsource_t glfwGetInputSourceForKey(int32_t key) {
|
||||
}
|
||||
|
||||
void glfwClientSetTitle(char *name) {
|
||||
ASSERT_NOT_NULL(window);
|
||||
glfwSetWindowTitle(window, name);
|
||||
}
|
@ -8,6 +8,7 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <libs.h>
|
||||
#include "display/render.h"
|
||||
#include "assert/assert.h"
|
||||
#include "input/input.h"
|
||||
#include "game/game.h"
|
||||
|
||||
|
@ -9,8 +9,9 @@
|
||||
#include "../libs.h"
|
||||
|
||||
#define ASSERT_TRUE(x) assert(x)
|
||||
#define ASSERT_FALSE(x) ASSERT_TRUE((x) == false)
|
||||
|
||||
#define ASSERT_NOT_NULL(x) ASSERT_TRUE(x != NULL)
|
||||
#define ASSERT_NOT_NULL(x) ASSERT_FALSE(x == NULL)
|
||||
|
||||
#define ASSERT_IF(x, y) (x ? y : false)
|
||||
|
||||
@ -21,4 +22,7 @@
|
||||
#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)
|
||||
#define ASSERT_GREATER_THAN_EQUAL_TO(x, y) ASSERT_TRUE(x >= y)
|
||||
|
||||
#define ASSERT_FLAG_OFF(flags, flag) ASSERT_FALSE(flags & flag)
|
||||
#define ASSERT_FLAG_ON(flags, flag) ASSERT_TRUE(flags & flag)
|
@ -11,6 +11,9 @@ tilesetdiv_t * bitmapFontGetCharacterDivision(tileset_t *tileset,
|
||||
char character
|
||||
) {
|
||||
int32_t i = ((int32_t)character) - BITMAP_FONT_CHAR_START;
|
||||
|
||||
ASSERT_NOT_NULL(tileset);
|
||||
|
||||
return tileset->divisions + i;
|
||||
}
|
||||
|
||||
@ -24,6 +27,10 @@ bitmapfontmeasure_t bitmapFontMeasure(char *string,
|
||||
.height = 0, .lines = 1, .width = 0
|
||||
};
|
||||
|
||||
ASSERT_NOT_NULL(string);
|
||||
ASSERT_GREATER_THAN(charWidth, 0);
|
||||
ASSERT_GREATER_THAN(charHeight, 0);
|
||||
|
||||
i = 0;
|
||||
y = 0;
|
||||
x = 0;
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "../assert/assert.h"
|
||||
#include "spritebatch.h"
|
||||
#include "tileset.h"
|
||||
#include "../util/math.h"
|
||||
|
@ -11,11 +11,13 @@ void cameraLookAt(camera_t *camera,
|
||||
float x, float y, float z,
|
||||
float targetX, float targetY, float targetZ
|
||||
) {
|
||||
ASSERT_NOT_NULL(camera);
|
||||
matrixIdentity(&camera->view);
|
||||
matrixLookAt(&camera->view, x, y, z, targetX, targetY, targetZ, 0, 1, 0);
|
||||
}
|
||||
|
||||
void cameraLookAtStruct(camera_t *camera, cameralookat_t look) {
|
||||
ASSERT_NOT_NULL(camera);
|
||||
cameraLookAt(camera,
|
||||
look.x, look.y, look.z, look.lookX, look.lookY, look.lookZ
|
||||
);
|
||||
@ -25,6 +27,8 @@ void cameraLook(camera_t *camera,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll
|
||||
) {
|
||||
ASSERT_NOT_NULL(camera);
|
||||
|
||||
matrixIdentity(&camera->view);
|
||||
matrixLook(&camera->view, x, y, z, pitch, yaw, roll, 0, 1, 0);
|
||||
}
|
||||
@ -32,6 +36,8 @@ void cameraLook(camera_t *camera,
|
||||
void cameraPerspective(camera_t *camera,
|
||||
float fov, float aspect, float camNear, float camFar
|
||||
) {
|
||||
ASSERT_NOT_NULL(camera);
|
||||
|
||||
matrixIdentity(&camera->projection);
|
||||
matrixPerspective(&camera->projection,mathDeg2Rad(fov),aspect,camNear,camFar);
|
||||
}
|
||||
@ -39,6 +45,8 @@ void cameraPerspective(camera_t *camera,
|
||||
void cameraOrtho(camera_t *camera,
|
||||
float left, float right, float bottom, float top, float camNear, float camFar
|
||||
) {
|
||||
ASSERT_NOT_NULL(camera);
|
||||
|
||||
matrixIdentity(&camera->projection);
|
||||
matrixOrtho(&camera->projection, left, right, bottom, top, camNear, camFar);
|
||||
}
|
||||
@ -47,9 +55,13 @@ void cameraOrbit(camera_t *camera,
|
||||
float distance, float yaw, float pitch,
|
||||
float targetX, float targetY, float targetZ
|
||||
) {
|
||||
|
||||
float cy = cosf(pitch);
|
||||
float x = distance * sinf(yaw) * cy;
|
||||
float y = distance * sinf(pitch);
|
||||
float z = distance * cosf(yaw) * cy;
|
||||
|
||||
ASSERT_NOT_NULL(camera);
|
||||
|
||||
cameraLookAt(camera, x, y, z, targetX, targetY, targetZ);
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "../assert/assert.h"
|
||||
#include "../util/math.h"
|
||||
#include "matrix.h"
|
||||
|
||||
|
@ -32,10 +32,16 @@ void renderDispose() {
|
||||
}
|
||||
|
||||
void renderSetResolution(render_t *render, float width, float height) {
|
||||
ASSERT_NOT_NULL(render);
|
||||
ASSERT_GREATER_THAN(width, 0);
|
||||
ASSERT_GREATER_THAN(height, 0);
|
||||
|
||||
render->width = width;
|
||||
render->height = height;
|
||||
}
|
||||
|
||||
void renderResetFramebuffer(render_t *render) {
|
||||
ASSERT_NOT_NULL(render);
|
||||
|
||||
frameBufferUnbind(render->width, render->height, true);
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "../assert/assert.h"
|
||||
#include "framebuffer.h"
|
||||
|
||||
/**
|
||||
|
@ -6,7 +6,6 @@
|
||||
# Sources
|
||||
target_sources(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
client.c
|
||||
engine.c
|
||||
thread.c
|
||||
event.c
|
||||
|
@ -1,12 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "client.h"
|
||||
|
||||
void clientInit(client_t *client) {
|
||||
client->setTitle = NULL;
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
|
||||
/** Callback to set the window title */
|
||||
typedef void clientsettitle_t(char *string);
|
||||
|
||||
typedef struct {
|
||||
clientsettitle_t *setTitle;
|
||||
} client_t;
|
||||
|
||||
/**
|
||||
* Initialize the client to its default state.
|
||||
*
|
||||
* @param client Client to initialize.
|
||||
*/
|
||||
void clientInit(client_t *client);
|
@ -8,6 +8,8 @@
|
||||
#include "engine.h"
|
||||
|
||||
void engineInit(engine_t *engine) {
|
||||
ASSERT_NOT_NULL(engine);
|
||||
|
||||
randSeed(123);
|
||||
|
||||
#if defined(GAME_NAME)
|
||||
@ -16,7 +18,6 @@ void engineInit(engine_t *engine) {
|
||||
engine->name = "Dawn";
|
||||
#endif
|
||||
|
||||
clientInit(&engine->client);
|
||||
epochInit(&engine->time);
|
||||
saveManagerInit(&engine->save);
|
||||
inputInit(&engine->input);
|
||||
@ -27,6 +28,9 @@ void engineInit(engine_t *engine) {
|
||||
}
|
||||
|
||||
void engineUpdateStart(engine_t *engine, float delta) {
|
||||
ASSERT_NOT_NULL(engine);
|
||||
ASSERT_GREATER_THAN(delta, 0);
|
||||
|
||||
epochUpdate(&engine->time, delta);
|
||||
inputUpdate(&engine->input);
|
||||
assetManagerUpdate(&engine->assetManager);
|
||||
@ -35,6 +39,8 @@ void engineUpdateStart(engine_t *engine, float delta) {
|
||||
}
|
||||
|
||||
bool engineUpdateEnd(engine_t *engine) {
|
||||
ASSERT_NOT_NULL(engine);
|
||||
|
||||
if(inputIsPressed(&engine->input, INPUT_NULL)) {
|
||||
printf("Game exit requested\n");
|
||||
return false;
|
||||
@ -43,6 +49,8 @@ bool engineUpdateEnd(engine_t *engine) {
|
||||
}
|
||||
|
||||
void engineDispose(engine_t *engine) {
|
||||
ASSERT_NOT_NULL(engine);
|
||||
|
||||
assetManagerDispose(&engine->assetManager);
|
||||
inputDispose(&engine->input);
|
||||
renderDispose();
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "client.h"
|
||||
#include "../assert/assert.h"
|
||||
#include "../util/rand.h"
|
||||
#include "../input/input.h"
|
||||
#include "../epoch/epoch.h"
|
||||
@ -33,9 +33,6 @@ typedef struct {
|
||||
|
||||
/** Save Manager for the game */
|
||||
savemanager_t save;
|
||||
|
||||
/** Game client information */
|
||||
client_t client;
|
||||
} engine_t;
|
||||
|
||||
/**
|
||||
|
@ -8,38 +8,55 @@
|
||||
#include "thread.h"
|
||||
|
||||
void threadInit(thread_t *thread, threadfunction_t *call) {
|
||||
ASSERT_NOT_NULL(thread);
|
||||
ASSERT_NOT_NULL(call);
|
||||
|
||||
thread->call = call;
|
||||
thread->user = NULL;
|
||||
thread->state = 0x00;
|
||||
}
|
||||
|
||||
void threadStart(thread_t *thread) {
|
||||
ASSERT_NOT_NULL(thread);
|
||||
ASSERT_FLAG_OFF(thread->state, THREAD_FLAG_RUNNING);
|
||||
ASSERT_FLAG_OFF(thread->state, THREAD_FLAG_QUEUED);
|
||||
|
||||
thread->state |= THREAD_FLAG_QUEUED;
|
||||
sysThreadCreate(&_threadWrappedCallback, thread->pt, thread);
|
||||
}
|
||||
|
||||
void threadJoin(thread_t *thread) {
|
||||
ASSERT_NOT_NULL(thread);
|
||||
ASSERT_FLAG_ON(thread->state, THREAD_FLAG_RUNNING);
|
||||
|
||||
sysThreadJoin(thread->pt);
|
||||
}
|
||||
|
||||
void threadCancel(thread_t *thread) {
|
||||
ASSERT_NOT_NULL(thread);
|
||||
ASSERT_FLAG_ON(thread->state, THREAD_FLAG_RUNNING);
|
||||
|
||||
sysThreadCancel(thread->pt, 1);
|
||||
}
|
||||
|
||||
void threadSleep(float time) {
|
||||
ASSERT_GREATER_THAN(time, 0);
|
||||
|
||||
sleep((unsigned long)(time * 1000.0f));
|
||||
}
|
||||
|
||||
int32_t _threadWrappedCallback(thread_t *thread) {
|
||||
int32_t response;
|
||||
|
||||
flagOff(thread->state, THREAD_FLAG_QUEUED);
|
||||
ASSERT_NOT_NULL(thread);
|
||||
|
||||
thread->state |= THREAD_FLAG_RUNNING;
|
||||
thread->state = flagOff(thread->state, THREAD_FLAG_QUEUED);
|
||||
|
||||
response = thread->call(thread);
|
||||
|
||||
thread->state |= THREAD_FLAG_HAS_RUN;
|
||||
flagOff(thread->state, THREAD_FLAG_RUNNING);
|
||||
thread->state = flagOff(thread->state, THREAD_FLAG_RUNNING);
|
||||
|
||||
return response;
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "../assert/assert.h"
|
||||
#include "../util/flags.h"
|
||||
|
||||
typedef struct _thread_t thread_t;
|
||||
|
@ -17,6 +17,7 @@ bool onEventTriggered(void *user, event_t event, void *args[], int32_t argc) {
|
||||
}
|
||||
|
||||
bool gameInit(game_t *game) {
|
||||
ASSERT_NOT_NULL(game);
|
||||
|
||||
eventManagerInit(&eventManager);
|
||||
eventManagerSubscribe(&eventManager, EVENT_TEST, NULL, onEventTriggered);
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "../../assert/assert.h"
|
||||
#include "../../engine/engine.h"
|
||||
#include "../../engine/event.h"
|
||||
#include "../../display/primitive/primitive.h"
|
||||
|
Reference in New Issue
Block a user