summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-06-23 08:44:21 +0200
committerkdx <kikoodx@paranoici.org>2023-06-23 08:44:21 +0200
commitdfc0ced3ad485a19aab7ce492fca0408b0d06c49 (patch)
tree43ce4bb17f383967c06fb0e020c3526e8f50a244
downloadhmle-dfc0ced3ad485a19aab7ce492fca0408b0d06c49.tar.gz
initial commit
-rw-r--r--.gitignore1
-rwxr-xr-xbuild.sh3
-rw-r--r--compile_flags.txt3
-rwxr-xr-xrun.sh5
-rw-r--r--samples/tset.pngbin0 -> 173 bytes
-rw-r--r--src/log.c168
-rw-r--r--src/log.h49
-rw-r--r--src/main.c51
-rw-r--r--src/sdl.c20
-rw-r--r--src/sdl.h7
-rw-r--r--src/texture.c59
-rw-r--r--src/texture.h25
-rw-r--r--src/tileset.c32
-rw-r--r--src/tileset.h17
-rw-r--r--src/window.c42
-rw-r--r--src/window.h13
16 files changed, 495 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..84c048a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/build/
diff --git a/build.sh b/build.sh
new file mode 100755
index 0000000..ca2f20c
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+mkdir -p build
+gcc -std=c2x -Wall -Wextra -DLOG_USE_COLOR= -o build/hmle src/*.c -lSDL2 -lSDL2_image
diff --git a/compile_flags.txt b/compile_flags.txt
new file mode 100644
index 0000000..f5cab1b
--- /dev/null
+++ b/compile_flags.txt
@@ -0,0 +1,3 @@
+-Wall
+-Wextra
+-std=c2x
diff --git a/run.sh b/run.sh
new file mode 100755
index 0000000..25325fb
--- /dev/null
+++ b/run.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+printf './build.sh\n'
+./build.sh || exit 1
+printf './build/hmle\n'
+./build/hmle
diff --git a/samples/tset.png b/samples/tset.png
new file mode 100644
index 0000000..f6a015b
--- /dev/null
+++ b/samples/tset.png
Binary files differ
diff --git a/src/log.c b/src/log.c
new file mode 100644
index 0000000..1a7626e
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2020 rxi
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "log.h"
+
+#define MAX_CALLBACKS 32
+
+typedef struct {
+ log_LogFn fn;
+ void *udata;
+ int level;
+} Callback;
+
+static struct {
+ void *udata;
+ log_LockFn lock;
+ int level;
+ bool quiet;
+ Callback callbacks[MAX_CALLBACKS];
+} L;
+
+
+static const char *level_strings[] = {
+ "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
+};
+
+#ifdef LOG_USE_COLOR
+static const char *level_colors[] = {
+ "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
+};
+#endif
+
+
+static void stdout_callback(log_Event *ev) {
+ char buf[16];
+ buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
+#ifdef LOG_USE_COLOR
+ fprintf(
+ ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
+ buf, level_colors[ev->level], level_strings[ev->level],
+ ev->file, ev->line);
+#else
+ fprintf(
+ ev->udata, "%s %-5s %s:%d: ",
+ buf, level_strings[ev->level], ev->file, ev->line);
+#endif
+ vfprintf(ev->udata, ev->fmt, ev->ap);
+ fprintf(ev->udata, "\n");
+ fflush(ev->udata);
+}
+
+
+static void file_callback(log_Event *ev) {
+ char buf[64];
+ buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
+ fprintf(
+ ev->udata, "%s %-5s %s:%d: ",
+ buf, level_strings[ev->level], ev->file, ev->line);
+ vfprintf(ev->udata, ev->fmt, ev->ap);
+ fprintf(ev->udata, "\n");
+ fflush(ev->udata);
+}
+
+
+static void lock(void) {
+ if (L.lock) { L.lock(true, L.udata); }
+}
+
+
+static void unlock(void) {
+ if (L.lock) { L.lock(false, L.udata); }
+}
+
+
+const char* log_level_string(int level) {
+ return level_strings[level];
+}
+
+
+void log_set_lock(log_LockFn fn, void *udata) {
+ L.lock = fn;
+ L.udata = udata;
+}
+
+
+void log_set_level(int level) {
+ L.level = level;
+}
+
+
+void log_set_quiet(bool enable) {
+ L.quiet = enable;
+}
+
+
+int log_add_callback(log_LogFn fn, void *udata, int level) {
+ for (int i = 0; i < MAX_CALLBACKS; i++) {
+ if (!L.callbacks[i].fn) {
+ L.callbacks[i] = (Callback) { fn, udata, level };
+ return 0;
+ }
+ }
+ return -1;
+}
+
+
+int log_add_fp(FILE *fp, int level) {
+ return log_add_callback(file_callback, fp, level);
+}
+
+
+static void init_event(log_Event *ev, void *udata) {
+ if (!ev->time) {
+ time_t t = time(NULL);
+ ev->time = localtime(&t);
+ }
+ ev->udata = udata;
+}
+
+
+void log_log(int level, const char *file, int line, const char *fmt, ...) {
+ log_Event ev = {
+ .fmt = fmt,
+ .file = file,
+ .line = line,
+ .level = level,
+ };
+
+ lock();
+
+ if (!L.quiet && level >= L.level) {
+ init_event(&ev, stderr);
+ va_start(ev.ap, fmt);
+ stdout_callback(&ev);
+ va_end(ev.ap);
+ }
+
+ for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) {
+ Callback *cb = &L.callbacks[i];
+ if (level >= cb->level) {
+ init_event(&ev, cb->udata);
+ va_start(ev.ap, fmt);
+ cb->fn(&ev);
+ va_end(ev.ap);
+ }
+ }
+
+ unlock();
+}
diff --git a/src/log.h b/src/log.h
new file mode 100644
index 0000000..b1fae24
--- /dev/null
+++ b/src/log.h
@@ -0,0 +1,49 @@
+/**
+ * Copyright (c) 2020 rxi
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the MIT license. See `log.c` for details.
+ */
+
+#ifndef LOG_H
+#define LOG_H
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <time.h>
+
+#define LOG_VERSION "0.1.0"
+
+typedef struct {
+ va_list ap;
+ const char *fmt;
+ const char *file;
+ struct tm *time;
+ void *udata;
+ int line;
+ int level;
+} log_Event;
+
+typedef void (*log_LogFn)(log_Event *ev);
+typedef void (*log_LockFn)(bool lock, void *udata);
+
+enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
+
+#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
+#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
+#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
+#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
+#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
+#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
+
+const char* log_level_string(int level);
+void log_set_lock(log_LockFn fn, void *udata);
+void log_set_level(int level);
+void log_set_quiet(bool enable);
+int log_add_callback(log_LogFn fn, void *udata, int level);
+int log_add_fp(FILE *fp, int level);
+
+void log_log(int level, const char *file, int line, const char *fmt, ...);
+
+#endif
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..4ca065b
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,51 @@
+#include "log.h"
+#include "sdl.h"
+#include "window.h"
+#include "tileset.h"
+#include <errno.h>
+#include <SDL2/SDL.h>
+
+Window window = {};
+Tileset tileset = {};
+
+[[nodiscard]] static int init(const char *tset_path);
+static void deinit(void);
+
+int
+main(void)
+{
+ if (init("samples/tset.png"))
+ return 1;
+
+ deinit();
+ return 0;
+}
+
+static
+int init(const char *tset_path)
+{
+ if (sdl_init()) {
+ log_fatal("sdl_init failed");
+ return deinit(), -1;
+ }
+
+ if (window_init(&window)) {
+ log_fatal("window_init failed");
+ return deinit(), -1;
+ }
+
+ if (tileset_init(&tileset, &window, tset_path, 16, 16)) {
+ log_fatal("tileset_init failed");
+ return deinit(), -1;
+ }
+
+ return 0;
+}
+
+static void
+deinit(void)
+{
+ tileset_deinit(&tileset);
+ window_deinit(&window);
+ sdl_deinit();
+}
diff --git a/src/sdl.c b/src/sdl.c
new file mode 100644
index 0000000..e269aba
--- /dev/null
+++ b/src/sdl.c
@@ -0,0 +1,20 @@
+#include "sdl.h"
+#include "log.h"
+#include <SDL2/SDL.h>
+
+int
+sdl_init(void)
+{
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) {
+ log_error("%s", SDL_GetError());
+ return -1;
+ }
+
+ return 0;
+}
+
+void
+sdl_deinit(void)
+{
+ SDL_Quit();
+}
diff --git a/src/sdl.h b/src/sdl.h
new file mode 100644
index 0000000..24ba72e
--- /dev/null
+++ b/src/sdl.h
@@ -0,0 +1,7 @@
+#pragma once
+
+/* return -1 on error */
+[[nodiscard]]
+int sdl_init(void);
+
+void sdl_deinit(void);
diff --git a/src/texture.c b/src/texture.c
new file mode 100644
index 0000000..4748624
--- /dev/null
+++ b/src/texture.c
@@ -0,0 +1,59 @@
+#include "texture.h"
+#include "log.h"
+#include <SDL2/SDL_image.h>
+
+int
+texture_init(Texture *this, Window *win, int access, int w, int h)
+{
+ memset(this, 0, sizeof(*this));
+
+ this->ptr = SDL_CreateTexture(win->ren, SDL_PIXELFORMAT_ARGB8888,
+ access, w, h);
+ if (this->ptr == NULL) {
+ log_error("%s", SDL_GetError());
+ return -1;
+ }
+ this->win = win;
+ this->access = access;
+ this->width = w;
+ this->height = h;
+
+ return 0;
+}
+
+int
+texture_init_target(Texture *this, Window *win, int w, int h)
+{
+ return texture_init(this, win, SDL_TEXTUREACCESS_TARGET, w, h);
+}
+
+int
+texture_load(Texture *this, Window *win, const char *path)
+{
+ memset(this, 0, sizeof(*this));
+
+ this->ptr = IMG_LoadTexture(win->ren, path);
+ if (this->ptr == NULL) {
+ log_error("%s", SDL_GetError());
+ return texture_deinit(this), -1;
+ }
+ this->win = win;
+
+ if (SDL_QueryTexture(this->ptr, NULL, &this->access,
+ &this->width, &this->height))
+ {
+ log_error("%s", SDL_GetError());
+ return texture_deinit(this), -1;
+ }
+
+ return 0;
+}
+
+void
+texture_deinit(Texture *this)
+{
+ if (this->ptr) {
+ SDL_DestroyTexture(this->ptr);
+ this->ptr = NULL;
+ }
+}
diff --git a/src/texture.h b/src/texture.h
new file mode 100644
index 0000000..1fcdd69
--- /dev/null
+++ b/src/texture.h
@@ -0,0 +1,25 @@
+#pragma once
+#include "window.h"
+#include <SDL2/SDL_render.h>
+
+typedef struct Texture {
+ SDL_Texture *ptr;
+ Window *win;
+ int access;
+ int width;
+ int height;
+} Texture;
+
+/* return -1 on error */
+[[nodiscard]]
+int texture_init(Texture *this, Window *win, int access, int w, int h);
+
+/* return -1 on error */
+[[nodiscard]]
+int texture_init_target(Texture *this, Window *win, int w, int h);
+
+/* return -1 on error */
+[[nodiscard]]
+int texture_load(Texture *this, Window *win, const char *path);
+
+void texture_deinit(Texture *this);
diff --git a/src/tileset.c b/src/tileset.c
new file mode 100644
index 0000000..32f46a4
--- /dev/null
+++ b/src/tileset.c
@@ -0,0 +1,32 @@
+#include "tileset.h"
+#include "log.h"
+
+int
+tileset_init(Tileset *this, Window *win,
+ const char *path, int tile_width, int tile_height)
+{
+ memset(this, 0, sizeof(*this));
+
+ if (strlen(path) >= sizeof(this->name)) {
+ log_error("path length (%d) too long (max %d) '%s'",
+ strlen(path), sizeof(this->name) - 1, path);
+ return -1;
+ }
+
+ if (texture_load(&this->tex, win, path)) {
+ log_error("texture_load failed");
+ return -1;
+ }
+
+ strncpy(this->name, path, sizeof(this->name) - 1);
+ this->tile_width = tile_width;
+ this->tile_height = tile_height;
+
+ return 0;
+}
+
+void
+tileset_deinit(Tileset *this)
+{
+ texture_deinit(&this->tex);
+}
diff --git a/src/tileset.h b/src/tileset.h
new file mode 100644
index 0000000..7ebdc37
--- /dev/null
+++ b/src/tileset.h
@@ -0,0 +1,17 @@
+#pragma once
+#include "texture.h"
+
+typedef struct Tileset {
+ Texture tex;
+ Window *win;
+ char name[256];
+ int tile_width;
+ int tile_height;
+} Tileset;
+
+/* return -1 on error */
+[[nodiscard]]
+int tileset_init(Tileset *this, Window *win,
+ const char *path, int tile_width, int tile_height);
+
+void tileset_deinit(Tileset *this);
diff --git a/src/window.c b/src/window.c
new file mode 100644
index 0000000..e625a05
--- /dev/null
+++ b/src/window.c
@@ -0,0 +1,42 @@
+#include "window.h"
+#include "log.h"
+
+int
+window_init(Window *this)
+{
+ memset(this, 0, sizeof(*this));
+
+ const Uint32 window_flags = SDL_WINDOW_RESIZABLE
+ | SDL_WINDOW_MAXIMIZED;
+ this->ptr = SDL_CreateWindow("HMLE",
+ SDL_WINDOWPOS_UNDEFINED,
+ SDL_WINDOWPOS_UNDEFINED,
+ 640, 480, window_flags);
+ if (this->ptr == NULL) {
+ log_error("%s", SDL_GetError());
+ return window_deinit(this), -1;
+ }
+
+ const Uint32 renderer_flags = SDL_RENDERER_PRESENTVSYNC
+ | SDL_RENDERER_TARGETTEXTURE;
+ this->ren = SDL_CreateRenderer(this->ptr, -1, renderer_flags);
+ if (this->ren == NULL) {
+ log_error("%s", SDL_GetError());
+ return window_deinit(this), -1;
+ }
+
+ return 0;
+}
+
+void
+window_deinit(Window *this)
+{
+ if (this->ren) {
+ SDL_DestroyRenderer(this->ren);
+ this->ren = NULL;
+ }
+ if (this->ptr) {
+ SDL_DestroyWindow(this->ptr);
+ this->ptr = NULL;
+ }
+}
diff --git a/src/window.h b/src/window.h
new file mode 100644
index 0000000..9a587bb
--- /dev/null
+++ b/src/window.h
@@ -0,0 +1,13 @@
+#pragma once
+#include <SDL2/SDL_render.h>
+
+typedef struct Window {
+ SDL_Window *ptr;
+ SDL_Renderer *ren;
+} Window;
+
+/* return -1 on error */
+[[nodiscard]]
+int window_init(Window *this);
+
+void window_deinit(Window *this);