aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2022-12-18 06:35:08 +0100
committerkdx <kikoodx@paranoici.org>2022-12-18 06:35:08 +0100
commitac08e18ac1b53d59629d672ca422c27aa94ec801 (patch)
tree3b62e66b938ef3cd257ae2c60555919f4ddef247
parent8644b576b5abaec9c668503f33038dee63d5fdb7 (diff)
downloadlzr-ac08e18ac1b53d59629d672ca422c27aa94ec801.tar.gz
get rid of libdx dependency
-rw-r--r--Makefile2
-rw-r--r--lzr.c178
2 files changed, 93 insertions, 87 deletions
diff --git a/Makefile b/Makefile
index 2fabd11..dbdefd0 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
CC := gcc
LD := $(CC)
CFLAGS := -Wall -Wextra -std=c99 -pedantic $(shell sdl2-config --cflags)
-LDFLAGS := -ldx $(shell sdl2-config --libs) -lSDL2_gfx -lSDL2_image -lSDL2_mixer
+LDFLAGS := $(shell sdl2-config --libs) -lSDL2_gfx -lSDL2_image -lSDL2_mixer
SRC := $(wildcard *.c)
OBJ := $(patsubst %.c,%.o,$(SRC))
NAME := lzr
diff --git a/lzr.c b/lzr.c
index 2feec31..62216ad 100644
--- a/lzr.c
+++ b/lzr.c
@@ -4,9 +4,6 @@
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
-#include <dx/log.h>
-#include <dx/mem.h>
-#include <dx/str.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
@@ -43,6 +40,15 @@ static float scale = 1.0;
static int mouse_x = 0;
static int mouse_y = 0;
+static char *_strdup(const char *str)
+{
+ char *const cpy = malloc(strlen(str) + 1);
+ if (cpy == NULL)
+ return NULL;
+ strcpy(cpy, str);
+ return cpy;
+}
+
static int _scode_to_button(unsigned int scode)
{
for (int i = 0; i < LZR_BUTTON_MOUSE_L; i++)
@@ -92,11 +98,11 @@ static void _draw_btn(SDL_Renderer *ren, int btn, int x, int y,
static void _bind_menu(void)
{
- dx_log_trace("entering bind menu");
+ SDL_Log("entering bind menu");
SDL_Window *win = NULL;
SDL_Renderer *ren = NULL;
if (SDL_CreateWindowAndRenderer(256, 256, 0, &win, &ren) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return;
}
int btn = 0;
@@ -123,22 +129,22 @@ static void _bind_menu(void)
exit_bind_menu:
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
- dx_log_trace("leaving bind menu");
+ SDL_Log("leaving bind menu");
}
int LZR_Init(LZR_Config cfg)
{
memcpy(&config, &cfg, sizeof(config));
if (config.display_width == 0) {
- dx_log_error("display_width can't be 0");
+ SDL_Log("display_width can't be 0");
return -1;
}
if (config.display_height == 0) {
- dx_log_error("display_height can't be 0");
+ SDL_Log("display_height can't be 0");
return -1;
}
if (config.title == NULL) {
- dx_log_warn("title is NULL, defaulting to 'LZR'");
+ SDL_Log("title is NULL, defaulting to 'LZR'");
config.title = "LZR";
}
if (config.tile_size == 0)
@@ -146,30 +152,30 @@ int LZR_Init(LZR_Config cfg)
if (config.ratio <= 0.0)
config.ratio = 1.0;
else {
- const double ratio = (float)config.display_width /
- (float)config.display_height;
+ const double ratio =
+ (float)config.display_width / (float)config.display_height;
config.ratio /= ratio;
}
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) {
- dx_log_error("%s", IMG_GetError());
+ SDL_Log("%s", IMG_GetError());
return -1;
}
if (Mix_Init(MIX_INIT_OGG) != MIX_INIT_OGG) {
- dx_log_error("%s", Mix_GetError());
+ SDL_Log("%s", Mix_GetError());
return -1;
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 8, 1024) < 0) {
- dx_log_error("%s", Mix_GetError());
+ SDL_Log("%s", Mix_GetError());
return -1;
}
basepath = SDL_GetBasePath();
if (basepath == NULL) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
const int dwidth = config.display_width * config.ratio;
@@ -177,25 +183,25 @@ int LZR_Init(LZR_Config cfg)
SDL_WINDOWPOS_UNDEFINED, dwidth,
config.display_height, SDL_WINDOW_RESIZABLE);
if (window == NULL) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888,
SDL_TEXTUREACCESS_TARGET,
config.display_width, config.display_height);
if (target == NULL) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
- points = dx_alloc(sizeof(SDL_Point) * cfg.display_width *
- cfg.display_height);
+ points =
+ calloc(cfg.display_width * cfg.display_height, sizeof(SDL_Point));
if (points == NULL) {
- dx_log_error("dx_alloc failed");
+ SDL_Log("calloc failed");
return -1;
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0);
@@ -213,16 +219,16 @@ void LZR_Quit(void)
if (sounds[i] != NULL) {
Mix_FreeChunk(sounds[i]);
sounds[i] = NULL;
- dx_log_trace("destroyed sound %d", i);
+ SDL_Log("destroyed sound %d", i);
}
for (int i = 0; i < LZR_MAX_IMAGES; i++)
if (images[i].tex != NULL) {
SDL_DestroyTexture(images[i].tex);
images[i].tex = NULL;
- dx_log_trace("destroyed image %d", i);
+ SDL_Log("destroyed image %d", i);
}
if (points != NULL) {
- dx_free(points);
+ free(points);
points = NULL;
}
if (target != NULL) {
@@ -255,20 +261,20 @@ bool LZR_ShouldQuit(void)
char *LZR_PathPrefix(const char *path)
{
if (path == NULL) {
- dx_log_error("path is NULL");
+ SDL_Log("path is NULL");
return NULL;
}
if (basepath == NULL) {
- dx_log_warn("basepath is NULL");
- return dx_strdup(path);
+ SDL_Log("basepath is NULL");
+ return _strdup(path);
}
- char *const buf = dx_alloc(strlen(basepath) + strlen(path) + 1);
+ char *const buf = malloc(strlen(basepath) + strlen(path) + 1);
if (buf == NULL) {
- dx_log_error("dx_alloc failed");
+ SDL_Log("malloc failed");
return NULL;
}
- dx_strcpy(buf, basepath);
- dx_strcat(buf, path);
+ strcpy(buf, basepath);
+ strcat(buf, path);
return buf;
}
@@ -279,34 +285,34 @@ int LZR_ImageLoad(const char *path)
if (images[i].tex == NULL)
break;
if (i >= LZR_MAX_IMAGES) {
- dx_log_error("reached image limit (%d)", LZR_MAX_IMAGES);
+ SDL_Log("reached image limit (%d)", LZR_MAX_IMAGES);
return -1;
}
char *const apath = LZR_PathPrefix(path);
if (apath == NULL) {
- dx_log_error("LZR_PathPrefix failed");
+ SDL_Log("LZR_PathPrefix failed");
return -1;
}
SDL_Surface *const bmp = IMG_Load(apath);
- dx_free(apath);
+ free(apath);
if (bmp == NULL) {
- dx_log_error("%s: %s", path, SDL_GetError());
+ SDL_Log("%s: %s", path, SDL_GetError());
return -1;
}
SDL_Texture *const tex = SDL_CreateTextureFromSurface(renderer, bmp);
SDL_FreeSurface(bmp);
if (tex == NULL) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
images[i].tex = tex;
if (SDL_SetTextureBlendMode(images[i].tex, SDL_BLENDMODE_BLEND) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
if (SDL_QueryTexture(tex, NULL, NULL, &images[i].width,
&images[i].height)) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return i;
@@ -319,18 +325,18 @@ int LZR_SoundLoad(const char *path, float volume)
if (sounds[i] == NULL)
break;
if (i >= LZR_MAX_SOUNDS) {
- dx_log_error("reached sounds limit (%d)", LZR_MAX_SOUNDS);
+ SDL_Log("reached sounds limit (%d)", LZR_MAX_SOUNDS);
return -1;
}
char *const apath = LZR_PathPrefix(path);
if (apath == NULL) {
- dx_log_error("LZR_PathPrefix failed");
+ SDL_Log("LZR_PathPrefix failed");
return -1;
}
Mix_Chunk *const chunk = Mix_LoadWAV(apath);
- dx_free(apath);
+ free(apath);
if (chunk == NULL) {
- dx_log_error("%s: %s", path, Mix_GetError());
+ SDL_Log("%s: %s", path, Mix_GetError());
return -1;
}
Mix_VolumeChunk(chunk, volume * MIX_MAX_VOLUME);
@@ -341,7 +347,7 @@ int LZR_SoundLoad(const char *path, float volume)
bool LZR_PollEvent(LZR_Event *e)
{
if (e == NULL) {
- dx_log_error("e is NULL");
+ SDL_Log("e is NULL");
return false;
}
SDL_Event se;
@@ -422,7 +428,7 @@ bool LZR_ButtonDown(LZR_Button btn)
if (btn >= 0 && btn < LZR_BUTTON_COUNT)
return input[btn];
else
- dx_log_warn("%d button doesn't exist", btn);
+ SDL_Log("%d button doesn't exist", btn);
return false;
}
@@ -430,11 +436,11 @@ void LZR_ButtonBind(LZR_Button btn, unsigned int code)
{
if (btn < LZR_BUTTON_MOUSE_L && code != SCODE_BIND_MENU) {
map[btn] = code;
- dx_log_trace("bound key %s to button %u",
- SDL_GetScancodeName(map[btn]), btn);
+ SDL_Log("bound key %s to button %u",
+ SDL_GetScancodeName(map[btn]), btn);
} else
- dx_log_warn("button %u can't be remapped to key %s", btn,
- SDL_GetScancodeName(code));
+ SDL_Log("button %u can't be remapped to key %s", btn,
+ SDL_GetScancodeName(code));
}
int LZR_DrawBegin(void)
@@ -442,7 +448,7 @@ int LZR_DrawBegin(void)
if (config.target_fps > 0)
next_time += min_dt;
if (SDL_SetRenderTarget(renderer, target) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
@@ -453,12 +459,12 @@ int LZR_DrawBegin(void)
int LZR_DrawEnd(void)
{
if (SDL_SetRenderTarget(renderer, NULL)) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
LZR_DrawSetColor(0.0f, 0.0f, 0.0f);
if (LZR_DrawClear()) {
- dx_log_error("LZY_DrawClear failed");
+ SDL_Log("LZY_DrawClear failed");
return -1;
}
if (config.target_fps) {
@@ -479,7 +485,7 @@ int LZR_DrawEnd(void)
off_y = (win_h - height * scale) / 2;
const SDL_Rect dest = {off_x, off_y, width * scale, height * scale};
if (SDL_RenderCopyEx(renderer, target, NULL, &dest, 0.0, NULL, 0) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
SDL_RenderPresent(renderer);
@@ -493,7 +499,7 @@ int LZR_DrawSetColor(float r, float g, float b)
const unsigned int ug = (unsigned int)(g * 255) & 255;
const unsigned int ub = (unsigned int)(b * 255) & 255;
if (SDL_SetRenderDrawColor(renderer, ur, ug, ub, 255) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
color[0] = ur, color[1] = ug, color[2] = ub;
@@ -503,7 +509,7 @@ int LZR_DrawSetColor(float r, float g, float b)
int LZR_DrawClear(void)
{
if (SDL_RenderClear(renderer) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return 0;
@@ -512,7 +518,7 @@ int LZR_DrawClear(void)
int LZR_DrawPoint(int x, int y)
{
if (SDL_RenderDrawPoint(renderer, x, y) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return 0;
@@ -522,8 +528,8 @@ int LZR_DrawPoints(int *x, int *y, int n)
{
if (n > (int)(config.display_width * config.display_height)) {
- dx_log_error("%d > %u", n,
- config.display_width * config.display_height);
+ SDL_Log("%d > %u", n,
+ config.display_width * config.display_height);
return -1;
}
for (int i = 0; i < n; i++) {
@@ -531,7 +537,7 @@ int LZR_DrawPoints(int *x, int *y, int n)
points[i].y = y[i];
}
if (SDL_RenderDrawPoints(renderer, points, n) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return 0;
@@ -540,7 +546,7 @@ int LZR_DrawPoints(int *x, int *y, int n)
int LZR_DrawLine(int x0, int y0, int x1, int y1)
{
if (SDL_RenderDrawLine(renderer, x0, y0, x1, y1) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return 0;
@@ -550,7 +556,7 @@ int LZR_DrawRectangle(bool fill, int x, int y, int w, int h)
{
SDL_Rect rect = {x, y, w, h};
if ((fill ? SDL_RenderFillRect : SDL_RenderDrawRect)(renderer, &rect)) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return 0;
@@ -560,7 +566,7 @@ int LZR_DrawCircle(bool fill, int x, int y, int radius)
{
if ((fill ? filledCircleRGBA : circleRGBA)(renderer, x, y, radius,
UNPACKED_COLOR, 255) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return 0;
@@ -569,7 +575,7 @@ int LZR_DrawCircle(bool fill, int x, int y, int radius)
int LZR_DrawPolygon(bool fill, int *vx, int *vy, int n)
{
if (n > 32) {
- dx_log_error("%d > 32", n);
+ SDL_Log("%d > 32", n);
return -1;
}
Sint16 x[32], y[32];
@@ -577,7 +583,7 @@ int LZR_DrawPolygon(bool fill, int *vx, int *vy, int n)
x[i] = vx[i], y[i] = vy[i];
if ((fill ? filledPolygonRGBA : polygonRGBA)(renderer, x, y, n,
UNPACKED_COLOR, 255) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return 0;
@@ -586,20 +592,20 @@ int LZR_DrawPolygon(bool fill, int *vx, int *vy, int n)
int LZR_DrawImage(int id, int x, int y)
{
if (id < 0) {
- dx_log_error("id is negative");
+ SDL_Log("id is negative");
return -1;
}
if (id >= LZR_MAX_IMAGES || images[id].tex == NULL) {
- dx_log_error("no image with id %d", id);
+ SDL_Log("no image with id %d", id);
return -1;
}
if (SDL_SetTextureColorMod(images[id].tex, UNPACKED_COLOR) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
const SDL_Rect dest = {x, y, images[id].width, images[id].height};
if (SDL_RenderCopy(renderer, images[id].tex, NULL, &dest) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return 0;
@@ -608,11 +614,11 @@ int LZR_DrawImage(int id, int x, int y)
int LZR_DrawImageEx(int id, int x, int y, LZR_ImageDrawSettings stg)
{
if (id < 0) {
- dx_log_error("id is negative");
+ SDL_Log("id is negative");
return -1;
}
if (id >= LZR_MAX_IMAGES || images[id].tex == NULL) {
- dx_log_error("no image with id %d", id);
+ SDL_Log("no image with id %d", id);
return -1;
}
const int width = (stg.width > 0) ? stg.width : images[id].width;
@@ -643,14 +649,14 @@ int LZR_DrawImageEx(int id, int x, int y, LZR_ImageDrawSettings stg)
dst.h = images[id].height - stg.iy;
}
if (SDL_SetTextureColorMod(images[id].tex, UNPACKED_COLOR) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
const int flip = (stg.flip_v ? SDL_FLIP_VERTICAL : 0) |
(stg.flip_h ? SDL_FLIP_HORIZONTAL : 0);
if (SDL_RenderCopyEx(renderer, images[id].tex, &src, &dst, stg.angle,
NULL, flip)) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return 0;
@@ -659,25 +665,25 @@ int LZR_DrawImageEx(int id, int x, int y, LZR_ImageDrawSettings stg)
int LZR_DrawTile(int id, int tile, int x, int y, double rot, int flip)
{
if (id < 0) {
- dx_log_error("id is negative");
+ SDL_Log("id is negative");
return -1;
}
if (id >= LZR_MAX_IMAGES || images[id].tex == NULL) {
- dx_log_error("no image with id %d", id);
+ SDL_Log("no image with id %d", id);
return -1;
}
if (tile < 0) {
- dx_log_error("tile is negative");
+ SDL_Log("tile is negative");
return -1;
}
const int img_width = images[id].width / config.tile_size;
const int img_height = images[id].height / config.tile_size;
if (tile >= img_width * img_height) {
- dx_log_error("tile exceeds boundaries");
+ SDL_Log("tile exceeds boundaries");
return -1;
}
if (SDL_SetTextureColorMod(images[id].tex, UNPACKED_COLOR) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
SDL_Rect src;
@@ -687,7 +693,7 @@ int LZR_DrawTile(int id, int tile, int x, int y, double rot, int flip)
const SDL_Rect dst = {x, y, config.tile_size, config.tile_size};
if (SDL_RenderCopyEx(renderer, images[id].tex, &src, &dst, rot, NULL,
flip) < 0) {
- dx_log_error("%s", SDL_GetError());
+ SDL_Log("%s", SDL_GetError());
return -1;
}
return 0;
@@ -696,15 +702,15 @@ int LZR_DrawTile(int id, int tile, int x, int y, double rot, int flip)
int LZR_PlaySound(int id)
{
if (id < 0) {
- dx_log_error("id is negative");
+ SDL_Log("id is negative");
return -1;
}
if (id >= LZR_MAX_SOUNDS || sounds[id] == NULL) {
- dx_log_error("no sound with id %d", id);
+ SDL_Log("no sound with id %d", id);
return -1;
}
if (Mix_PlayChannel(-1, sounds[id], 0) < 0) {
- dx_log_error("%s", Mix_GetError());
+ SDL_Log("%s", Mix_GetError());
return -1;
}
return 0;
@@ -713,7 +719,7 @@ int LZR_PlaySound(int id)
int LZR_SetMusicVolume(float volume)
{
if (Mix_VolumeMusic(volume * MIX_MAX_VOLUME) < 0) {
- dx_log_warn("%s", Mix_GetError());
+ SDL_Log("%s", Mix_GetError());
return -1;
}
return 0;
@@ -724,17 +730,17 @@ int LZR_PlayMusic(const char *path, int loops)
LZR_StopMusic();
char *const apath = LZR_PathPrefix(path);
if (apath == NULL) {
- dx_log_error("LZR_PathPrefix failed");
+ SDL_Log("LZR_PathPrefix failed");
return -1;
}
music = Mix_LoadMUS(apath);
- dx_free(apath);
+ free(apath);
if (music == NULL) {
- dx_log_error("%s: %s", path, Mix_GetError());
+ SDL_Log("%s: %s", path, Mix_GetError());
return -1;
}
if (Mix_PlayMusic(music, loops) < 0) {
- dx_log_error("%s", Mix_GetError());
+ SDL_Log("%s", Mix_GetError());
return -1;
}
Mix_RewindMusic();