source: abuse/trunk/src/imlib/include.cpp @ 682

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

core: rename vec2i to ivec2 and update matrix.h from Lol Engine.

File size: 1.8 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 defined 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(image *im, palette *pal, char *filename, char *name)
21{
22  char tmp_name[200];
23  strcpy(tmp_name,name);
24  unsigned int j;
25  int append=0,i;
26  for (j=0; j<strlen(name); j++)
27    if (toupper(tmp_name[j])<'A' || toupper(tmp_name[j])>'Z')
28      tmp_name[j]='_';
29
30  FILE *fp=fopen(filename,"rb");  // see if the file already exists
31  if (fp)
32  {
33    fclose(fp);
34    fp=fopen(filename,"ab");  // if so, append to the end and don't write the palette
35    append=1;
36  }
37  else fp=fopen(filename,"wb");
38
39  if (fp)
40  {
41    fprintf(fp,"/* File produced by Satan Paint (c) 1994 Jonathan Clark */\n\n");
42    if (!append)
43    {
44      fprintf(fp,"unsigned char %s_palette[256*3] = {\n    ",tmp_name);
45      unsigned char *p=(unsigned char *)pal->addr();
46      for (i=0; i<768; i++,p++)
47      {
48    fprintf(fp,"%d",(int)*p);
49    if (i==767)
50        fprintf(fp,"};\n\n");
51    else
52        if (i%15==14)
53    fprintf(fp,",\n    ");
54        else fprintf(fp,", ");
55      }
56    }
57    ivec2 size = im->Size();
58    fprintf(fp,"unsigned char %s[%d*%d]={\n    ",tmp_name, size.x, size.y);
59    int x,y,max=size.x*size.y-1;
60    for (y=0,i=0; y<size.y; y++)
61      for (x=0; x<size.x; x++,i++)
62      {
63        fprintf(fp,"%d",(int)im->Pixel(ivec2(x,y)));
64        if (i==max)
65          fprintf(fp,"};\n\n");
66        else
67          if (i%15==14)
68            fprintf(fp,",\n    ");
69          else fprintf(fp,", ");
70      }
71    fclose(fp);
72  }
73}
74
Note: See TracBrowser for help on using the repository browser.