aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/gfx/CMakeLists.txt1
-rw-r--r--src/gfx/sdl-1.2/src/sprite.c54
-rw-r--r--src/header/CMakeLists.txt5
-rw-r--r--src/header/inc/header.h18
-rw-r--r--src/header/src/header.c65
-rw-r--r--src/sfx/CMakeLists.txt2
-rw-r--r--src/sfx/sdl-1.2/inc/sfx/port.h11
-rw-r--r--src/sfx/sdl-1.2/src/sound.c21
-rw-r--r--tools/CMakeLists.txt5
-rw-r--r--tools/add-header.c67
11 files changed, 244 insertions, 6 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 758d0a0..eb39a79 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -104,6 +104,7 @@ set(components
game
gfx
gui
+ header
instance
pad
player
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();
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 946eed7..843301d 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -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})
diff --git a/tools/add-header.c b/tools/add-header.c
new file mode 100644
index 0000000..75f7458
--- /dev/null
+++ b/tools/add-header.c
@@ -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;
+}