summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-01-11 11:34:19 -0700
committercflip <cflip@cflip.net>2023-01-11 11:34:19 -0700
commit54d0e32bced43dc3d85b41e30bcb1fa430ae90dc (patch)
treee7d961b9dcac64e4ff06f7caa410fbeb3241672c
parent0324dda2cc779bcd6d3eb425216e29b37a9a95ed (diff)
Use the new error function with the new window and font code
-rw-r--r--error.c8
-rw-r--r--error.h2
-rw-r--r--font.c11
-rw-r--r--window.c20
4 files changed, 19 insertions, 22 deletions
diff --git a/error.c b/error.c
index 4944ff4..e42bae1 100644
--- a/error.c
+++ b/error.c
@@ -1,11 +1,15 @@
#include "error.h"
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
-void fatal_error(const char *msg)
+void fatal_error(const char *msgfmt, ...)
{
- perror(msg);
+ va_list args;
+ va_start(args, msgfmt);
+ vfprintf(stderr, msgfmt, args);
+ va_end(args);
exit(-1);
}
diff --git a/error.h b/error.h
index f30fd24..1a8f350 100644
--- a/error.h
+++ b/error.h
@@ -1,7 +1,7 @@
#ifndef _ERROR_H
#define ERROR_H
-void fatal_error(const char *msg);
+void fatal_error(const char *msgfmt, ...);
void warning(const char *msg);
#endif
diff --git a/font.c b/font.c
index e2c1a74..c1d1e7a 100644
--- a/font.c
+++ b/font.c
@@ -31,9 +31,9 @@ SDL_Texture *font_create_texture(SDL_Renderer *renderer, PSFFont *font)
}
result = SDL_CreateTextureFromSurface(renderer, surface);
- if (result == NULL) {
- fprintf(stderr, "Failed to create texture: %s\n", SDL_GetError());
- }
+ if (result == NULL)
+ fatal_error("Failed to create texture: %s\n", SDL_GetError());
+
SDL_FreeSurface(surface);
printf("Created font texture atlas of size %dx%d\n", surface->w, surface->h);
@@ -51,9 +51,8 @@ PSFFont font_load(const char *filename)
fseek(fp, 0, SEEK_SET);
fread(&font.magic, 4, 1, fp);
- if (font.magic != PSF_MAGIC_NUMBER) {
- printf("%x: Font '%s' header does not contain expected value.\n", font.magic, filename);
- }
+ if (font.magic != PSF_MAGIC_NUMBER)
+ fatal_error("Font header mismatch! '%s' has magic value %x.\n", filename, font.magic);
fread(&font.version, 4, 1, fp);
fread(&font.header_size, 4, 1, fp);
diff --git a/window.c b/window.c
index 219c0ca..b87188d 100644
--- a/window.c
+++ b/window.c
@@ -4,6 +4,7 @@
#include "buffer.h"
#include "editor.h"
+#include "error.h"
#include "font.h"
#include "input.h"
@@ -15,27 +16,20 @@ static PSFFont font;
void window_init(const char *title, int rows, int cols)
{
- /* TODO: There should be a 'panic' method of sorts to be reused here. */
- if (SDL_Init(SDL_INIT_VIDEO) != 0) {
- fprintf(stderr, "Failed to initalize SDL: %s\n", SDL_GetError());
- return;
- }
+ if (SDL_Init(SDL_INIT_VIDEO) != 0)
+ fatal_error("Failed to init SDL: %s\n", SDL_GetError());
font = font_load("terminus/ter-u12n.psf");
int window_width = cols * font.width;
int window_height = rows * font.height;
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, window_width, window_height, 0);
- if (window == NULL) {
- fprintf(stderr, "Failed to create a window: %s\n", SDL_GetError());
- return;
- }
+ if (window == NULL)
+ fatal_error("Failed to create window: %s\n", SDL_GetError());
renderer = SDL_CreateRenderer(window, -1, 0);
- if (renderer == NULL) {
- fprintf(stderr, "Failed to create a renderer: %s\n", SDL_GetError());
- return;
- }
+ if (renderer == NULL)
+ fatal_error("Failed to create renderer: %s\n", SDL_GetError());
font_texture = font_create_texture(renderer, &font);