aboutsummaryrefslogtreecommitdiff
path: root/src/sprite-packer/main.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-01-13 17:41:24 -0500
committerPaul Duncan <pabs@pablotron.org>2019-01-13 17:41:24 -0500
commiteb30ab240acea4f1e338bb4fcc1ddadb5ccc5aee (patch)
tree4b8ed34c26598fe807c8af3206c17be4989a8c49 /src/sprite-packer/main.c
parente4f70b11a2f91b47f474fa9e5e4e89978a57472a (diff)
downloadsok-eb30ab240acea4f1e338bb4fcc1ddadb5ccc5aee.tar.bz2
sok-eb30ab240acea4f1e338bb4fcc1ddadb5ccc5aee.zip
add assets, sprite-packer, sprite rendering (disabled, currently busted)
Diffstat (limited to 'src/sprite-packer/main.c')
-rw-r--r--src/sprite-packer/main.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/sprite-packer/main.c b/src/sprite-packer/main.c
new file mode 100644
index 0000000..945fa51
--- /dev/null
+++ b/src/sprite-packer/main.c
@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <SDL.h>
+#include "../sdl/util.h"
+#define STB_IMAGE_IMPLEMENTATION
+#define STB_ASSERT(x)
+#define STB_ONLY_PNG
+#include "../sdl/stb_image.h"
+
+typedef struct {
+ const Uint32 r,
+ g,
+ b,
+ a;
+} mask_t;
+
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+static const mask_t
+MASK = { 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff };
+#else
+static const mask_t
+MASK = { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 };
+#endif /* SDL_BYTEORDER */
+
+static SDL_Surface *
+load_image(
+ const char * const png_path
+) {
+ // load image
+ int x, y, n;
+ unsigned char * const data = stbi_load(png_path, &x, &y, &n, 4);
+ if (!data) {
+ die("stbi_load()");
+ }
+
+ // create surface
+ SDL_Surface *src = SDL_CreateRGBSurfaceFrom(data, x, y, 32, 4 * x, MASK.r, MASK.g, MASK.b, MASK.a);
+ if (!src) {
+ die("SDLCreateRGBSurfaceFrom(): %s", SDL_GetError());
+ }
+
+ // free image data
+ free(data);
+
+ // return surface
+ return src;
+}
+
+int main(int argc, char *argv[]) {
+ // check cli
+ if (argc < 3) {
+ printf("Usage: %s out.bmp <src_images...>", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ // create output surface
+ SDL_Surface *dst = SDL_CreateRGBSurfaceWithFormat(0, 256, 256, 32, SDL_PIXELFORMAT_RGBA32);
+ if (!dst) {
+ die("SDl_CreateRGBSurfaceWithFormat(): %s", SDL_GetError());
+ }
+
+ // walk input images
+ for (size_t i = 2; i < argc; i++) {
+ // load image
+ SDL_Surface * const src = load_image(argv[i]);
+
+ // copy/scale image
+ SDL_Rect dst_rect = { (i - 2) * 32, 0, 32, 32 };
+ if (SDL_BlitScaled(src, NULL, dst, &dst_rect)) {
+ die("SDL_BlitScaled(): %s", SDL_GetError());
+ }
+
+ // free surface
+ SDL_FreeSurface(src);
+ }
+
+ // save to output path
+ if (SDL_SaveBMP(dst, argv[1])) {
+ die("SDL_SaveBMP(): %s", SDL_GetError());
+ }
+
+ // free dst surface
+ SDL_FreeSurface(dst);
+
+ // return success
+ return EXIT_SUCCESS;
+}