diff options
author | kdx <kikoodx@paranoici.org> | 2022-12-18 06:50:29 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2022-12-18 06:50:29 +0100 |
commit | ecaf8b65537214b738e08a10c0ffb4055672fbd1 (patch) | |
tree | 9ca7cd9164669b4ae34a324f7fb9c1108270ab35 | |
parent | ac08e18ac1b53d59629d672ca422c27aa94ec801 (diff) | |
download | lzr-ecaf8b65537214b738e08a10c0ffb4055672fbd1.tar.gz |
toggle to disable features w/ depends
-rw-r--r-- | lzr.c | 78 | ||||
-rw-r--r-- | lzr.h | 9 |
2 files changed, 75 insertions, 12 deletions
@@ -1,9 +1,15 @@ /* Licensing information can be found at the end of the file. */ #include "lzr.h" #include <SDL2/SDL.h> +#ifdef LZR_ENABLE_GFX #include <SDL2/SDL2_gfxPrimitives.h> +#endif +#ifdef LZR_ENABLE_IMAGE #include <SDL2/SDL_image.h> +#endif +#ifdef LZR_ENABLE_MIXER #include <SDL2/SDL_mixer.h> +#endif #include <stdbool.h> #include <stdint.h> #include <string.h> @@ -32,14 +38,17 @@ static unsigned int map[LZR_BUTTON_COUNT] = { static bool input[LZR_BUTTON_COUNT] = {false}; static SDL_Point *points = NULL; static uint64_t tick = 0; -static Mix_Chunk *sounds[LZR_MAX_SOUNDS] = {0}; -static Mix_Music *music = NULL; static int off_x = 0; static int off_y = 0; static float scale = 1.0; static int mouse_x = 0; static int mouse_y = 0; +#ifdef LZR_ENABLE_MIXER +static Mix_Chunk *sounds[LZR_MAX_SOUNDS] = {0}; +static Mix_Music *music = NULL; +#endif + static char *_strdup(const char *str) { char *const cpy = malloc(strlen(str) + 1); @@ -60,6 +69,7 @@ static int _scode_to_button(unsigned int scode) static void _draw_btn(SDL_Renderer *ren, int btn, int x, int y, unsigned int size) { +#ifdef LZR_ENABLE_GFX const unsigned int size_2thirds = size * 2 / 3; switch (btn) { case LZR_BUTTON_LEFT: @@ -94,6 +104,9 @@ static void _draw_btn(SDL_Renderer *ren, int btn, int x, int y, default: break; } +#else + (void)ren, (void)btn, (void)x, (void)y, (void)size; +#endif } static void _bind_menu(void) @@ -161,10 +174,13 @@ int LZR_Init(LZR_Config cfg) SDL_Log("%s", SDL_GetError()); return -1; } +#ifdef LZR_ENABLE_IMAGE if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) { SDL_Log("%s", IMG_GetError()); return -1; } +#endif +#ifdef LZR_ENABLE_MIXER if (Mix_Init(MIX_INIT_OGG) != MIX_INIT_OGG) { SDL_Log("%s", Mix_GetError()); return -1; @@ -173,6 +189,7 @@ int LZR_Init(LZR_Config cfg) SDL_Log("%s", Mix_GetError()); return -1; } +#endif basepath = SDL_GetBasePath(); if (basepath == NULL) { SDL_Log("%s", SDL_GetError()); @@ -215,12 +232,16 @@ int LZR_Init(LZR_Config cfg) void LZR_Quit(void) { LZR_StopMusic(); +#ifdef LZR_ENABLE_MIXER for (int i = 0; i < LZR_MAX_SOUNDS; i++) if (sounds[i] != NULL) { Mix_FreeChunk(sounds[i]); sounds[i] = NULL; SDL_Log("destroyed sound %d", i); } + Mix_CloseAudio(); + Mix_Quit(); +#endif for (int i = 0; i < LZR_MAX_IMAGES; i++) if (images[i].tex != NULL) { SDL_DestroyTexture(images[i].tex); @@ -247,9 +268,9 @@ void LZR_Quit(void) SDL_free(basepath); basepath = NULL; } - Mix_CloseAudio(); - Mix_Quit(); +#ifdef LZR_ENABLE_IMAGE IMG_Quit(); +#endif SDL_Quit(); } @@ -293,14 +314,18 @@ int LZR_ImageLoad(const char *path) SDL_Log("LZR_PathPrefix failed"); return -1; } - SDL_Surface *const bmp = IMG_Load(apath); +#ifdef LZR_ENABLE_IMAGE + SDL_Surface *const surf = IMG_Load(apath); +#else + SDL_Surface *const surf = SDL_LoadBMP(apath); +#endif free(apath); - if (bmp == NULL) { + if (surf == NULL) { SDL_Log("%s: %s", path, SDL_GetError()); return -1; } - SDL_Texture *const tex = SDL_CreateTextureFromSurface(renderer, bmp); - SDL_FreeSurface(bmp); + SDL_Texture *const tex = SDL_CreateTextureFromSurface(renderer, surf); + SDL_FreeSurface(surf); if (tex == NULL) { SDL_Log("%s", SDL_GetError()); return -1; @@ -320,6 +345,7 @@ int LZR_ImageLoad(const char *path) int LZR_SoundLoad(const char *path, float volume) { +#ifdef LZR_ENABLE_MIXER int i; for (i = 0; i < LZR_MAX_SOUNDS; i++) if (sounds[i] == NULL) @@ -342,6 +368,10 @@ int LZR_SoundLoad(const char *path, float volume) Mix_VolumeChunk(chunk, volume * MIX_MAX_VOLUME); sounds[i] = chunk; return i; +#else + (void)path, (void)volume; + return -1; +#endif } bool LZR_PollEvent(LZR_Event *e) @@ -564,16 +594,23 @@ int LZR_DrawRectangle(bool fill, int x, int y, int w, int h) int LZR_DrawCircle(bool fill, int x, int y, int radius) { +#ifdef LZR_ENABLE_GFX if ((fill ? filledCircleRGBA : circleRGBA)(renderer, x, y, radius, UNPACKED_COLOR, 255) < 0) { SDL_Log("%s", SDL_GetError()); return -1; } return 0; +#else + (void)fill, (void)x, (void)y, (void)radius; + SDL_Log("LZR GFX module is disabled"); + return -1; +#endif } int LZR_DrawPolygon(bool fill, int *vx, int *vy, int n) { +#ifdef LZR_ENABLE_GFX if (n > 32) { SDL_Log("%d > 32", n); return -1; @@ -587,6 +624,11 @@ int LZR_DrawPolygon(bool fill, int *vx, int *vy, int n) return -1; } return 0; +#else + (void)fill, (void)vx, (void)vy, (void)n; + SDL_Log("LZR GFX module is disabled"); + return -1; +#endif } int LZR_DrawImage(int id, int x, int y) @@ -701,6 +743,7 @@ int LZR_DrawTile(int id, int tile, int x, int y, double rot, int flip) int LZR_PlaySound(int id) { +#ifdef LZR_ENABLE_MIXER if (id < 0) { SDL_Log("id is negative"); return -1; @@ -714,19 +757,31 @@ int LZR_PlaySound(int id) return -1; } return 0; +#else + (void)id; + SDL_Log("LZR MIXER module is disabled"); + return -1; +#endif } int LZR_SetMusicVolume(float volume) { +#ifdef LZR_ENABLE_MIXER if (Mix_VolumeMusic(volume * MIX_MAX_VOLUME) < 0) { SDL_Log("%s", Mix_GetError()); return -1; } return 0; +#else + (void)volume; + SDL_Log("LZR MIXER module is disabled"); + return -1; +#endif } int LZR_PlayMusic(const char *path, int loops) { +#ifdef LZR_ENABLE_MIXER LZR_StopMusic(); char *const apath = LZR_PathPrefix(path); if (apath == NULL) { @@ -745,16 +800,23 @@ int LZR_PlayMusic(const char *path, int loops) } Mix_RewindMusic(); return 0; +#else + (void)path, (void)loops; + SDL_Log("LZR MIXER module is disabled"); + return -1; +#endif } void LZR_StopMusic(void) { +#ifdef LZR_ENABLE_MIXER if (Mix_PlayingMusic()) Mix_HaltMusic(); if (music != NULL) { Mix_FreeMusic(music); music = NULL; } +#endif } void LZR_ToggleFullscreen(void) @@ -1,9 +1,12 @@ /* Licensing informations can be found at the end of the file. */ -#ifndef LZR_H_ -#define LZR_H_ +#pragma once #include <stdbool.h> #include <stdint.h> +#define LZR_ENABLE_IMAGE +#define LZR_ENABLE_MIXER +#define LZR_ENABLE_GFX + #define LZR_MAX_IMAGES 32 #define LZR_MAX_SOUNDS 64 #define LZR_BUTTON(btn) LZR_ButtonDown(LZR_BUTTON_##btn) @@ -82,8 +85,6 @@ uint64_t LZR_GetTick(void); void LZR_ScreenTransform(int *x, int *y); void LZR_MousePosition(int *x, int *y); -#endif - /* ** Copyright (c) 2022 kdx ** |