source: abuse/branches/lol/src/imlib/include.cpp

Last change on this file was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

File size: 1.7 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "common.h"
16
17#include "include.h"
18#include "ctype.h"
19
20void write_include(AImage *im, Palette *pal, char *filename, char *name)
21{
22  char tmp_name[200];
23  strcpy(tmp_name, name);
24  int append = 0;
25  for (unsigned j = 0; j < strlen(name); j++)
26    if (toupper(tmp_name[j])<'A' || toupper(tmp_name[j])>'Z')
27      tmp_name[j]='_';
28
29  FILE *fp=fopen(filename,"rb");  // see if the file already exists
30  if (fp)
31  {
32    fclose(fp);
33    fp=fopen(filename,"ab");  // if so, append to the end and don't write the palette
34    append=1;
35  }
36  else fp=fopen(filename,"wb");
37
38  if (fp)
39  {
40    fprintf(fp,"/* File produced by Satan Paint (c) 1994 Jonathan Clark */\n\n");
41    if (!append)
42    {
43      fprintf(fp, "unsigned char %s_palette[256*3] = {\n    ", tmp_name);
44      for (int i = 0; i < 256; ++i)
45      {
46          u8vec3 c = pal->GetColor(i);
47          fprintf(fp, "%d, %d, %d,%s", c.r, c.g, c.b,
48                  (i % 5 == 4) ? "\n    " : " ");
49        }
50      fprintf(fp, "};\n\n");
51    }
52    ivec2 size = im->Size();
53    fprintf(fp, "unsigned char %s[%d*%d]={\n    ", tmp_name, size.x, size.y);
54    for (int y = 0, i = 0; y < size.y; y++)
55      for (int x = 0; x < size.x; x++, i++)
56      {
57        fprintf(fp,"%d",(int)im->Pixel(ivec2(x,y)));
58          if (i%15==14)
59            fprintf(fp,",\n    ");
60          else fprintf(fp,", ");
61      }
62    fprintf(fp,"};\n\n");
63    fclose(fp);
64  }
65}
66
Note: See TracBrowser for help on using the repository browser.