aboutsummaryrefslogtreecommitdiff
path: root/src/sprite-packer/main.c
blob: 945fa51bca3590903bbdc4ef7ea48e62dd508ce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
}