Use lovrAssert and lovrThrow for better errors;

This commit is contained in:
bjorn 2017-08-10 01:05:04 -07:00
parent 7f1d441cdf
commit 74f585ca8d
17 changed files with 48 additions and 101 deletions

View File

@ -389,18 +389,12 @@ int l_lovrGraphicsSetWireframe(lua_State* L) {
// Transforms
int l_lovrGraphicsPush(lua_State* L) {
if (lovrGraphicsPush()) {
return luaL_error(L, "Unbalanced matrix stack (more pushes than pops?)");
}
lovrGraphicsPush();
return 0;
}
int l_lovrGraphicsPop(lua_State* L) {
if (lovrGraphicsPop()) {
return luaL_error(L, "Unbalanced matrix stack (more pops than pushes?)");
}
lovrGraphicsPop();
return 0;
}

View File

@ -10,13 +10,11 @@ static AudioState state;
void lovrAudioInit() {
ALCdevice* device = alcOpenDevice(NULL);
if (!device) {
error("Unable to open default audio device");
}
lovrAssert(device, "Unable to open default audio device");
ALCcontext* context = alcCreateContext(device, NULL);
if (!context || !alcMakeContextCurrent(context) || alcGetError(device) != ALC_NO_ERROR) {
error("Unable to create OpenAL context");
lovrThrow("Unable to create OpenAL context");
}
#ifndef EMSCRIPTEN

View File

@ -199,10 +199,7 @@ void lovrSourceSetDirection(Source* source, float x, float y, float z) {
}
void lovrSourceSetFalloff(Source* source, float reference, float max, float rolloff) {
if (lovrSourceGetChannels(source) != 1) {
error("Positional audio is only supported for mono sources.");
}
lovrAssert(lovrSourceGetChannels(source) == 1, "Positional audio is only supported for mono sources");
alSourcef(source->id, AL_REFERENCE_DISTANCE, reference);
alSourcef(source->id, AL_MAX_DISTANCE, max);
alSourcef(source->id, AL_ROLLOFF_FACTOR, rolloff);
@ -217,10 +214,7 @@ void lovrSourceSetPitch(Source* source, float pitch) {
}
void lovrSourceSetPosition(Source* source, float x, float y, float z) {
if (lovrSourceGetChannels(source) != 1) {
error("Positional audio is only supported for mono sources.");
}
lovrAssert(lovrSourceGetChannels(source) == 1, "Positional audio is only supported for mono sources");
alSource3f(source->id, AL_POSITION, x, y, z);
}

View File

@ -21,7 +21,7 @@ static FilesystemState state;
void lovrFilesystemInit(const char* arg0, const char* arg1) {
if (!PHYSFS_init(arg0)) {
error("Could not initialize filesystem: %s", PHYSFS_getLastError());
lovrThrow("Could not initialize filesystem: %s", PHYSFS_getLastError());
}
state.source = malloc(LOVR_PATH_MAX * sizeof(char));
@ -111,7 +111,7 @@ int lovrFilesystemGetExecutablePath(char* dest, unsigned int size) {
#elif __linux__
memset(dest, 0, size);
if (readlink("/proc/self/exe", dest, size) == -1) {
perror("readlink");
return 1;
}
#else
#error "This platform is missing an implementation for lovrFilesystemGetExecutablePath"
@ -230,7 +230,7 @@ int lovrFilesystemSetIdentity(const char* identity) {
strncpy(state.savePathFull, fullPathBuffer, LOVR_PATH_MAX);
PHYSFS_mkdir(state.savePathRelative);
if (!PHYSFS_setWriteDir(state.savePathFull)) {
error("Could not set write directory: %s (%s)", PHYSFS_getLastError(), state.savePathRelative);
lovrThrow("Could not set write directory: %s (%s)", PHYSFS_getLastError(), state.savePathRelative);
}
PHYSFS_mount(state.savePathFull, NULL, 0);

View File

@ -91,9 +91,7 @@ void lovrGraphicsPrepare() {
}
void lovrGraphicsCreateWindow(int w, int h, int fullscreen, int msaa, const char* title, const char* icon) {
if (state.window) {
error("Window is already created");
}
lovrAssert(!state.window, "Window is already created");
#ifdef EMSCRIPTEN
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
@ -120,7 +118,7 @@ void lovrGraphicsCreateWindow(int w, int h, int fullscreen, int msaa, const char
state.window = glfwCreateWindow(w ? w : mode->width, h ? h : mode->height, title, fullscreen ? monitor : NULL, NULL);
if (!state.window) {
glfwTerminate();
error("Could not create window");
lovrThrow("Could not create window");
}
if (icon) {
@ -374,14 +372,18 @@ void lovrGraphicsSetWireframe(int wireframe) {
// Transforms
int lovrGraphicsPush() {
if (++state.transform >= MAX_TRANSFORMS) { return 1; }
void lovrGraphicsPush() {
if (++state.transform >= MAX_TRANSFORMS) {
lovrThrow("Unbalanced matrix stack (more pushes than pops?)");
}
memcpy(state.transforms[state.transform], state.transforms[state.transform - 1], 16 * sizeof(float));
return 0;
}
int lovrGraphicsPop() {
return --state.transform < 0;
void lovrGraphicsPop() {
if (--state.transform < 0) {
lovrThrow("Unbalanced matrix stack (more pops than pushes?)");
}
}
void lovrGraphicsOrigin() {
@ -870,7 +872,7 @@ void lovrGraphicsPrint(const char* str, mat4 transform, float wrap, HorizontalAl
// Internal State
void lovrGraphicsPushCanvas() {
if (++state.canvas >= MAX_CANVASES) {
error("Canvas overflow");
lovrThrow("Canvas overflow");
}
memcpy(&state.canvases[state.canvas], &state.canvases[state.canvas - 1], sizeof(CanvasState));
@ -878,7 +880,7 @@ void lovrGraphicsPushCanvas() {
void lovrGraphicsPopCanvas() {
if (--state.canvas < 0) {
error("Canvas underflow");
lovrThrow("Canvas underflow");
}
int* viewport = state.canvases[state.canvas].viewport;

View File

@ -137,8 +137,8 @@ int lovrGraphicsIsWireframe();
void lovrGraphicsSetWireframe(int wireframe);
// Transforms
int lovrGraphicsPush();
int lovrGraphicsPop();
void lovrGraphicsPush();
void lovrGraphicsPop();
void lovrGraphicsOrigin();
void lovrGraphicsTranslate(float x, float y, float z);
void lovrGraphicsRotate(float angle, float ax, float ay, float az);

View File

@ -99,7 +99,7 @@ static GLuint compileShader(GLenum type, const char* source) {
char* log = (char*) malloc(logLength);
glGetShaderInfoLog(shader, logLength, &logLength, log);
error(log);
lovrThrow("Could not compile shader %s", log);
}
return shader;
@ -130,7 +130,7 @@ static GLuint linkShaders(GLuint vertexShader, GLuint fragmentShader) {
char* log = (char*) malloc(logLength);
glGetProgramInfoLog(shader, logLength, &logLength, log);
error(log);
lovrThrow("Could not link shader %s", log);
}
glDetachShader(shader, vertexShader);
@ -196,7 +196,7 @@ Shader* lovrShaderCreateDefault(DefaultShader type) {
case SHADER_SKYBOX: return lovrShaderCreate(lovrSkyboxVertexShader, lovrSkyboxFragmentShader);
case SHADER_FONT: return lovrShaderCreate(NULL, lovrFontFragmentShader);
case SHADER_FULLSCREEN: return lovrShaderCreate(lovrNoopVertexShader, NULL);
default: error("Unknown default shader type");
default: lovrThrow("Unknown default shader type");
}
}

View File

@ -26,10 +26,7 @@ Skybox* lovrSkyboxCreate(Blob** blobs, SkyboxType type) {
int width, height, channels;
stbi_set_flip_vertically_on_load(0);
unsigned char* image = stbi_load_from_memory(blobs[i]->data, blobs[i]->size, &width, &height, &channels, 3);
if (!image) {
error("Could not load skybox image %d", i);
}
lovrAssert(image, "Could not load skybox image %d", i);
if (type == SKYBOX_CUBE) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);

View File

@ -93,13 +93,9 @@ Texture* lovrTextureCreateWithFramebuffer(TextureData* textureData, TextureProje
glBindFramebuffer(GL_FRAMEBUFFER, texture->framebuffer);
}
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
error("Error creating texture\n");
} else {
lovrGraphicsClear(1, 1);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
lovrAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Error creating texture");
lovrGraphicsClear(1, 1);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return texture;
}
@ -114,9 +110,7 @@ void lovrTextureDestroy(const Ref* ref) {
}
void lovrTextureBindFramebuffer(Texture* texture) {
if (!texture->framebuffer) {
error("Texture cannot be used as a canvas");
}
lovrAssert(texture->framebuffer, "Texture cannot be used as a canvas");
int w = texture->textureData->width;
int h = texture->textureData->height;

View File

@ -461,7 +461,7 @@ float lovrHeadsetControllerGetAxis(Controller* controller, ControllerAxis axis)
case CONTROLLER_AXIS_TRIGGER: return input.rAxis[1].x;
case CONTROLLER_AXIS_TOUCHPAD_X: return input.rAxis[0].x;
case CONTROLLER_AXIS_TOUCHPAD_Y: return input.rAxis[0].y;
default: error("Bad controller axis");
default: lovrThrow("Bad controller axis");
}
return 0.;
@ -487,7 +487,7 @@ int lovrHeadsetControllerIsDown(Controller* controller, ControllerButton button)
return (input.ulButtonPressed >> EVRButtonId_k_EButton_SteamVR_Touchpad) & 1;
default:
error("Bad controller button");
lovrThrow("Bad controller button");
}
return 0;

View File

@ -62,7 +62,7 @@ static int ftCubicTo(const FT_Vector* control1, const FT_Vector* control2, const
FontData* lovrFontDataCreate(Blob* blob, int size) {
if (!ft && FT_Init_FreeType(&ft)) {
error("Error initializing FreeType");
lovrThrow("Error initializing FreeType");
}
FT_Face face = NULL;
@ -75,10 +75,7 @@ FontData* lovrFontDataCreate(Blob* blob, int size) {
}
err = err || FT_Set_Pixel_Sizes(face, 0, size);
if (err) {
error("Problem loading font");
}
lovrAssert(!err, "Problem loading font");
FontData* fontData = malloc(sizeof(FontData));
fontData->rasterizer = face;
@ -117,9 +114,7 @@ void lovrFontDataLoadGlyph(FontData* fontData, uint32_t character, Glyph* glyph)
err = err || FT_Load_Glyph(face, FT_Get_Char_Index(face, character), FT_LOAD_DEFAULT);
err = err || FT_Outline_Decompose(&face->glyph->outline, &callbacks, &context);
if (err) {
error("Error loading glyph");
}
lovrAssert(!err, "Error loading glyph");
metrics = &face->glyph->metrics;

View File

@ -70,9 +70,7 @@ ModelData* lovrModelDataCreate(Blob* blob) {
}
// Normals
if (!assimpMesh->mNormals) {
error("Model must have normals");
}
lovrAssert(assimpMesh->mNormals, "Model must have normals");
modelData->hasNormals = 1;
vec_init(&mesh->normals);

View File

@ -183,7 +183,7 @@ TextureData* lovrTextureDataFromBlob(Blob* blob) {
textureData->blob = NULL;
if (!textureData->data) {
error("Could not load texture data from '%s'", blob->name);
lovrThrow("Could not load texture data from '%s'", blob->name);
free(textureData);
return NULL;
}
@ -193,7 +193,7 @@ TextureData* lovrTextureDataFromBlob(Blob* blob) {
void lovrTextureDataResize(TextureData* textureData, int width, int height, uint8_t value) {
if (textureData->format.compressed || textureData->mipmaps.generated) {
error("Can't resize a compressed texture or a texture with generated mipmaps.");
lovrThrow("Can't resize a compressed texture or a texture with generated mipmaps.");
}
int size = width * height * textureData->format.blockBytes;

View File

@ -11,7 +11,7 @@
static int hasErrored = 0;
static void onGlfwError(int code, const char* description) {
error(description);
lovrThrow(description);
}
static int getStackTrace(lua_State* L) {
@ -31,7 +31,7 @@ static void handleError(lua_State* L, const char* message) {
lua_pushstring(L, message);
lua_pcall(L, 1, 0, 0);
} else {
error(message);
fprintf(stderr, "%s\n", message);
}
lovrDestroy(1);
}
@ -67,7 +67,7 @@ void lovrInit(lua_State* L, int argc, char** argv) {
glfwSetErrorCallback(onGlfwError);
if (!glfwInit()) {
error("Error initializing glfw");
lovrThrow("Error initializing glfw");
}
// arg global

View File

@ -258,10 +258,7 @@ int lovrWorldIsCollisionEnabledBetween(World* world, const char* tag1, const cha
}
Collider* lovrColliderCreate(World* world, float x, float y, float z) {
if (!world) {
error("No world specified");
}
lovrAssert(world, "No world specified");
Collider* collider = lovrAlloc(sizeof(Collider), lovrColliderDestroy);
if (!collider) return NULL;
@ -888,10 +885,7 @@ void lovrJointSetUserData(Joint* joint, void* data) {
}
BallJoint* lovrBallJointCreate(Collider* a, Collider* b, float x, float y, float z) {
if (a->world != b->world) {
error("Joint bodies must exist in same World");
}
lovrAssert(a->world == b->world, "Joint bodies must exist in same World");
BallJoint* joint = lovrAlloc(sizeof(BallJoint), lovrJointDestroy);
if (!joint) return NULL;
@ -921,10 +915,7 @@ void lovrBallJointSetAnchor(BallJoint* joint, float x, float y, float z) {
}
DistanceJoint* lovrDistanceJointCreate(Collider* a, Collider* b, float x1, float y1, float z1, float x2, float y2, float z2) {
if (a->world != b->world) {
error("Joint bodies must exist in same World");
}
lovrAssert(a->world == b->world, "Joint bodies must exist in same World");
DistanceJoint* joint = lovrAlloc(sizeof(DistanceJoint), lovrJointDestroy);
if (!joint) return NULL;
@ -963,10 +954,7 @@ void lovrDistanceJointSetDistance(DistanceJoint* joint, float distance) {
}
HingeJoint* lovrHingeJointCreate(Collider* a, Collider* b, float x, float y, float z, float ax, float ay, float az) {
if (a->world != b->world) {
error("Joint bodies must exist in same World");
}
lovrAssert(a->world == b->world, "Joint bodies must exist in same World");
HingeJoint* joint = lovrAlloc(sizeof(HingeJoint), lovrJointDestroy);
if (!joint) return NULL;
@ -1029,10 +1017,7 @@ void lovrHingeJointSetUpperLimit(HingeJoint* joint, float limit) {
}
SliderJoint* lovrSliderJointCreate(Collider* a, Collider* b, float ax, float ay, float az) {
if (a->world != b->world) {
error("Joint bodies must exist in same World");
}
lovrAssert(a->world == b->world, "Joint bodies must exist in the same world");
SliderJoint* joint = lovrAlloc(sizeof(SliderJoint), lovrJointDestroy);
if (!joint) return NULL;

View File

@ -13,15 +13,6 @@
char lovrErrorMessage[MAX_ERROR_LENGTH];
jmp_buf* lovrCatch;
void error(const char* format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
fputs("\n", stderr);
va_end(args);
exit(EXIT_FAILURE);
}
void lovrThrow(const char* format, ...) {
va_list args;
va_start(args, format);

View File

@ -22,7 +22,6 @@ typedef struct {
extern char lovrErrorMessage[];
extern jmp_buf* lovrCatch;
void error(const char* format, ...);
void lovrThrow(const char* format, ...);
void lovrSleep(double seconds);
void* lovrAlloc(size_t size, void (*destructor)(const Ref* ref));