diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-02-20 19:04:21 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-03-30 08:20:21 +0200 |
| commit | 4765653cb3af905a9b20f1e5f2277e50d801c43b (patch) | |
| tree | 9c9f914e5ea9b19f2312cd43221df37d19dec2f3 /src | |
| parent | c9754ac4300abad20d942ad18bc5bf611a0b3a2e (diff) | |
| download | jancity-4765653cb3af905a9b20f1e5f2277e50d801c43b.tar.gz | |
Add metadata header to media files
The following properties are supported:
- Sound: "loop". Must be either 0 or 1
- Images: "transparent". Must be either 0 or 1
These headers are only used for non-PS1 builds, since .TIM and .VAG
files do already implement such information.
Diffstat (limited to 'src')
| -rw-r--r-- | src/gfx/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/gfx/sdl-1.2/src/sprite.c | 54 | ||||
| -rw-r--r-- | src/header/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | src/header/inc/header.h | 18 | ||||
| -rw-r--r-- | src/header/src/header.c | 65 | ||||
| -rw-r--r-- | src/sfx/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/sfx/sdl-1.2/inc/sfx/port.h | 11 | ||||
| -rw-r--r-- | src/sfx/sdl-1.2/src/sound.c | 21 |
8 files changed, 172 insertions, 5 deletions
diff --git a/src/gfx/CMakeLists.txt b/src/gfx/CMakeLists.txt index 6685608..9cc1b0e 100644 --- a/src/gfx/CMakeLists.txt +++ b/src/gfx/CMakeLists.txt @@ -25,6 +25,7 @@ elseif(SDL1_2_BUILD) "sdl-1.2/src/sprite.c" "sdl-1.2/src/quad.c") set(deps ${deps} SDL) + set(privdeps ${privdeps} header) endif() add_library(gfx ${src}) diff --git a/src/gfx/sdl-1.2/src/sprite.c b/src/gfx/sdl-1.2/src/sprite.c index 9d65c0d..e20f1c8 100644 --- a/src/gfx/sdl-1.2/src/sprite.c +++ b/src/gfx/sdl-1.2/src/sprite.c @@ -1,8 +1,11 @@ #include <gfx.h> #include <gfx/port.h> +#include <header.h> #include <SDL/SDL.h> +#include <errno.h> #include <stddef.h> #include <stdlib.h> +#include <string.h> void sprite_free(struct sprite *const s) { @@ -16,7 +19,41 @@ int sprite_clone(const struct sprite *const src, struct sprite *const dst) return 0; } -int sprite_from_fp(struct sprite *const s, FILE *const f) +int sprite_screen_resize_ev(struct sprite *const s) +{ + int ret = -1; + SDL_Surface *const old = s->s; + + /* Magenta as transparent. */ + if (s->transparent + && SDL_SetColorKey(old, SDL_SRCCOLORKEY | SDL_RLEACCEL, + SDL_MapRGB(old->format, 255, 0, 255))) + { + fprintf(stderr, "SDL_SetColorKey: %s\n", SDL_GetError()); + goto end; + } + else if (!(s->s = SDL_DisplayFormat(old))) + { + fprintf(stderr, "SDL_DisplayFormat: %s\n", SDL_GetError()); + goto end; + } + + ret = 0; + +end: + SDL_FreeSurface(old); + return ret; +} + +static int load_header(struct sprite *const s, FILE *const f) +{ + if (header_load_bool(f, "transparent", &s->transparent)) + return -1; + + return 0; +} + +static int load_bitmap(struct sprite *const s, FILE *const f) { int ret = -1; SDL_RWops *ops = NULL; @@ -33,7 +70,9 @@ int sprite_from_fp(struct sprite *const s, FILE *const f) goto end; } /* Magenta as transparent. */ - else if (SDL_SetColorKey(ts, SDL_SRCCOLORKEY, SDL_MapRGB(ts->format, 255, 0, 255))) + else if (s->transparent + && SDL_SetColorKey(ts, SDL_SRCCOLORKEY | SDL_RLEACCEL, + SDL_MapRGB(ts->format, 255, 0, 255))) { fprintf(stderr, "SDL_SetColorKey: %s\n", SDL_GetError()); goto end; @@ -44,6 +83,7 @@ int sprite_from_fp(struct sprite *const s, FILE *const f) goto end; } + gfx_register_sprite(s); s->w = ts->w; s->h = ts->h; ret = 0; @@ -54,6 +94,16 @@ end: return ret; } +int sprite_from_fp(struct sprite *const s, FILE *const f) +{ + memset(s, 0, sizeof *s); + + if (load_header(s, f) || load_bitmap(s, f)) + return -1; + + return 0; +} + void sprite_sort(struct sprite *const s) { SDL_Rect r = diff --git a/src/header/CMakeLists.txt b/src/header/CMakeLists.txt new file mode 100644 index 0000000..e23d4fd --- /dev/null +++ b/src/header/CMakeLists.txt @@ -0,0 +1,5 @@ +set(src "src/header.c") +set(inc "inc") + +add_library(header ${src}) +target_include_directories(header PUBLIC ${inc}) diff --git a/src/header/inc/header.h b/src/header/inc/header.h new file mode 100644 index 0000000..7514132 --- /dev/null +++ b/src/header/inc/header.h @@ -0,0 +1,18 @@ +#ifndef HEADER_H +#define HEADER_H + +#include <stdbool.h> +#include <stdio.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +int header_load_bool(FILE *f, const char *key, bool *value); + +#ifdef __cplusplus +} +#endif + +#endif /* HEADER_H */ diff --git a/src/header/src/header.c b/src/header/src/header.c new file mode 100644 index 0000000..bfc6534 --- /dev/null +++ b/src/header/src/header.c @@ -0,0 +1,65 @@ +#include <header.h> +#include <errno.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int header_load_bool(FILE *const f, const char *const key, bool *const value) +{ + for (size_t i = 0; i < strlen(key); i++) + { + const int ci = fgetc(f); + + if (ferror(f) || ci != key[i]) + { + fprintf(stderr, "%s: failed to read property %s\n", + __func__, key); + return -1; + } + } + + { + const int ci = fgetc(f); + + if (ferror(f) || ci != '=') + { + fprintf(stderr, "%s: expected '=' on property %s\n", + __func__, key); + return -1; + } + } + + { + static const char exp[] = "1"; + char str[sizeof exp]; + + for (char *c = str; c - str < sizeof str; c++) + { + const int ci = fgetc(f); + + if (ferror(f) || ci == EOF) + { + fprintf(stderr, "%s: could not read value for property %s\n", + __func__, key); + return -1; + } + + *c = ci; + } + + errno = 0; + const unsigned long out = strtoul(str, NULL, 10); + + if (errno || (out != 0 && out != 1)) + { + fprintf(stderr, "%s: unexpected value for property %s: %lu\n", + __func__, key, out); + return -1; + } + + *value = out; + } + + return 0; +} diff --git a/src/sfx/CMakeLists.txt b/src/sfx/CMakeLists.txt index 3453d2e..6b774fd 100644 --- a/src/sfx/CMakeLists.txt +++ b/src/sfx/CMakeLists.txt @@ -10,7 +10,7 @@ elseif(SDL1_2_BUILD) "sdl-1.2/src/sound.c") set(inc ${inc} "sdl-1.2/inc") set(deps ${deps} SDL_mixer) - set(privdeps ${privdeps} SDL) + set(privdeps ${privdeps} SDL header) endif() add_library(sfx ${src}) diff --git a/src/sfx/sdl-1.2/inc/sfx/port.h b/src/sfx/sdl-1.2/inc/sfx/port.h index d8418f7..2279229 100644 --- a/src/sfx/sdl-1.2/inc/sfx/port.h +++ b/src/sfx/sdl-1.2/inc/sfx/port.h @@ -2,10 +2,21 @@ #define SFX_SDL1_2_H #include <SDL/SDL_mixer.h> +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" +{ +#endif struct sound { Mix_Chunk *chunk; + bool loop; }; +#ifdef __cplusplus +} +#endif + #endif /* SFX_SDL1_2_H */ diff --git a/src/sfx/sdl-1.2/src/sound.c b/src/sfx/sdl-1.2/src/sound.c index e843923..e3e9a8b 100644 --- a/src/sfx/sdl-1.2/src/sound.c +++ b/src/sfx/sdl-1.2/src/sound.c @@ -1,5 +1,6 @@ #include <sfx.h> #include <sfx/port.h> +#include <header.h> #include <SDL/SDL.h> #include <SDL/SDL_mixer.h> #include <stdio.h> @@ -12,13 +13,13 @@ void sfx_free(struct sound *const s) int sfx_play(const struct sound *const s) { - if (Mix_PlayChannel(-1, s->chunk, 0) < 0) + if (Mix_PlayChannel(-1, s->chunk, s->loop ? -1 : 0) < 0) return -1; return 0; } -int sfx_sound_from_fp(struct sound *const s, FILE *const f) +static int load_data(struct sound *const s, FILE *const f) { int ret = -1; SDL_RWops *const ops = SDL_RWFromFP(f, 0); @@ -41,6 +42,22 @@ end: return ret; } +static int load_header(struct sound *const s, FILE *const f) +{ + if (header_load_bool(f, "loop", &s->loop)) + return -1; + + return 0; +} + +int sfx_sound_from_fp(struct sound *const s, FILE *const f) +{ + if (load_header(s, f) || load_data(s, f)) + return -1; + + return 0; +} + void sfx_deinit(void) { Mix_CloseAudio(); |
