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.
This commit is contained in:
parent
3ce1418ce5
commit
18717569ac
|
@ -104,6 +104,7 @@ set(components
|
|||
game
|
||||
gfx
|
||||
gui
|
||||
header
|
||||
instance
|
||||
pad
|
||||
player
|
||||
|
|
|
@ -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})
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
set(src "src/header.c")
|
||||
set(inc "inc")
|
||||
|
||||
add_library(header ${src})
|
||||
target_include_directories(header PUBLIC ${inc})
|
|
@ -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 */
|
|
@ -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;
|
||||
}
|
|
@ -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})
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
cmake_minimum_required(VERSION 3.0)
|
||||
project(utils)
|
||||
add_executable(container "container.c")
|
||||
target_compile_options(container PUBLIC -g3)
|
||||
add_executable(add-header "add-header.c")
|
||||
set(cflags -Wall -g3)
|
||||
target_compile_options(container PUBLIC ${cflags})
|
||||
target_compile_options(add-header PUBLIC ${cflags})
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char **const argv)
|
||||
{
|
||||
int ret = EXIT_FAILURE;
|
||||
FILE *in = NULL, *out = NULL;
|
||||
|
||||
if (argc < 3)
|
||||
{
|
||||
fprintf(stderr, "%s [arg ...] <in-file> <out-file>\n", *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
const char *const in_path = argv[argc - 2],
|
||||
*const out_path = argv[argc - 1];
|
||||
|
||||
if (!(out = fopen(out_path, "wb")))
|
||||
{
|
||||
fprintf(stderr, "could not open %s: %s\n", out_path, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
else if (!(in = fopen(in_path, "rb")))
|
||||
{
|
||||
fprintf(stderr, "could not open %s: %s\n", in_path, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc - 2; i++)
|
||||
{
|
||||
if (fprintf(out, "%s", argv[i]) < 0
|
||||
|| putc('\0', out) == EOF
|
||||
|| ferror(out))
|
||||
{
|
||||
fprintf(stderr, "failed writing to %s\n", out_path);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
while (!feof(in))
|
||||
{
|
||||
char c;
|
||||
|
||||
if ((!fread(&c, sizeof c, 1, in)
|
||||
|| !fwrite(&c, sizeof c, 1, out))
|
||||
&& (ferror(in) || ferror(out)))
|
||||
{
|
||||
fprintf(stderr, "ferror(%s)=%d, ferror(%s)=%d\n",
|
||||
in_path, ferror(in), out_path, ferror(out));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
ret = EXIT_SUCCESS;
|
||||
|
||||
end:
|
||||
if (out)
|
||||
fclose(out);
|
||||
|
||||
if (in)
|
||||
fclose(in);
|
||||
|
||||
return ret;
|
||||
}
|
Loading…
Reference in New Issue