File: cross.c

package info (click to toggle)
toppler 1.3-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,868 kB
  • sloc: cpp: 8,979; ansic: 2,349; makefile: 402; sh: 24
file content (55 lines) | stat: -rw-r--r-- 1,233 bytes parent folder | download | duplicates (2)
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
#include <SDL.h>
#include <SDL_image.h>

#include <stdio.h>


Uint8 get_color(SDL_Surface *s, int x, int y) {
  return ((Uint8*)s->pixels)[y*s->pitch+x];
}

Uint8 get_alpha(SDL_Surface *s, int x, int y) {
  return s->format->palette->colors[((Uint8*)s->pixels)[y*s->pitch+x]].r;
}

void write_palette(FILE *out, SDL_Surface *s) {
  int i;
  Uint8 c = s->format->palette->ncolors - 1;
  
  fwrite(&c, 1, 1, out);

  for (i = 0; i < s->format->palette->ncolors; i++) {
    fwrite(&s->format->palette->colors[i].r, 1, 1, out);
    fwrite(&s->format->palette->colors[i].g, 1, 1, out);
    fwrite(&s->format->palette->colors[i].b, 1, 1, out);
  }
}

int main() {

  FILE *outf = fopen("cross.dat", "wb");

  SDL_Surface * colors = IMG_LoadPNG_RW(SDL_RWFromFile("cross_colors.png", "rb"));
  SDL_Surface * mask = IMG_LoadPNG_RW(SDL_RWFromFile("cross_mask.png", "rb"));

  printf("writing palette\n");
  write_palette(outf, colors);

  int n, x, y;

  for (n = 0; n < 120; n++) {
    for (y = 0; y < 32; y++) {
      for (x = 0; x < 32; x++) {
        Uint8 c = get_color(colors, x, n*32 + y);
        fwrite(&c, 1, 1, outf);

        c = get_alpha(mask, x, n*32 + y);
        fwrite(&c, 1, 1, outf);
      }
    }
  }

  fclose(outf);
}