source: abuse/trunk/src/items.cpp @ 541

Last change on this file since 541 was 541, checked in by Sam Hocevar, 12 years ago

imlib: rename TImage to TransImage? for readability.

File size: 7.1 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 or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include "common.h"
14
15#include "items.h"
16#include "lisp.h"
17#include "dev.h"
18
19
20extern palette *pal;
21
22boundary::boundary(bFILE *fp, char const *er_name) : point_list(fp)
23{
24  int x1,y1,x2,y2,checkx,checky,i;
25  if (tot)
26  {
27    if (!(data[0]==data[(tot-1)*2] &&
28      data[1]==data[tot*2-1]))
29    {
30      printf("%s : Endpoints of foretile do not match start points\n",er_name);
31      exit(0);
32    }
33
34    inside=(uint8_t *)malloc(tot);
35  }
36
37  uint8_t *point_on;
38
39  for (i=0,point_on=data; i<tot-1; i++)
40  {
41    x1=*(point_on++);
42    y1=*(point_on++);
43    x2=point_on[0];
44    y2=point_on[1];
45
46    checkx=(x1+x2)/2;
47    checky=(y1+y2)/2;
48
49    int j,xp1,yp1,xp2,yp2,maxx,maxy,minx,miny;
50    uint8_t *point2,segs_left=0,segs_right=0,segs_down=0;
51    int skip_next=0;
52    int check_left=0,check_right=0,check_down=0;
53
54
55    if (y1==y2) check_down=1;
56    else if (x1==x2) check_left=1;
57    else
58    {
59      check_down=1;
60      if (x1<x2)
61        if (y1<y2) check_left=1;
62        else check_right=1;
63      else if (y1<y2) check_right=1;
64      else check_left=1;
65    }
66
67    maxx=Max(x1,x2);
68    maxy=Max(y1,y2);
69    minx=Min(x1,x2);
70    miny=Min(y1,y2);
71
72    if (skip_next)
73      skip_next=0;
74    else
75    {
76      for (j=0,point2=data; j<tot-1; j++,point2+=2)
77      {
78    if (skip_next)
79      skip_next=0;
80    else
81    {
82      if (j!=i)           // make sure we are not looking at ourself
83      {
84        xp1=point2[0];
85        yp1=point2[1];
86        xp2=point2[2];
87        yp2=point2[3];
88
89        if ((checkx>=xp1 && checkx<=xp2) || (checkx>=xp2 &&  checkx<=xp1))
90            {
91            if (check_down && (yp1>miny && yp2>miny))
92        segs_down++;
93          if (checkx==xp2) skip_next=1;
94        } else if ((checky>=yp1 && checky<=yp2) || (checky>=yp2 &&  checky<=yp1))
95        {
96          if (check_left && xp1<maxx && xp2<maxx)
97        segs_left++;
98          if (check_right && xp1>minx && xp2>minx)
99            segs_right++;
100          if (checky==yp2) skip_next=1;
101        }
102      }
103    }
104      }
105    }
106    if (!check_down) segs_down=1;
107    if (!check_right) segs_right=1;
108    if (!check_left) segs_left=1;
109
110    inside[i]=!(((segs_left&1)&&(segs_right&1)&&(segs_down&1)));
111  }
112}
113
114boundary::boundary(boundary *p) : point_list(p->tot,p->data)
115{
116  int x1,y1,x2,y2,checkx,checky,i;
117  uint8_t *point_on;
118  if (tot)
119  {
120    inside=(uint8_t *)malloc(tot);
121  } else inside=NULL;
122  for (i=0,point_on=data; i<tot-1; i++)
123  {
124    x1=*(point_on++);
125    y1=*(point_on++);
126    x2=point_on[0];
127    y2=point_on[1];
128
129    checkx=(x1+x2)/2;
130    checky=(y1+y2)/2;
131
132    int j,xp1,yp1,xp2,yp2,maxx,maxy,minx,miny;
133    uint8_t *point2,segs_left=0,segs_right=0,segs_down=0;
134    int skip_next=0;
135    int check_left=0,check_right=0,check_down=0;
136
137
138    if (y1==y2) check_down=1;
139    else if (x1==x2) check_right=1;
140    else
141    {
142      check_down=1;
143      if (x1<x2)
144        if (y1<y2) check_left=1;
145        else check_right=1;
146      else if (y1<y2) check_right=1;
147      else check_left=1;
148    }
149
150
151
152    maxx=Max(x1,x2);
153    maxy=Max(y1,y2);
154    minx=Min(x1,x2);
155    miny=Min(y1,y2);
156
157    if (skip_next)
158      skip_next=0;
159    else
160    {
161      for (j=0,point2=data; j<tot-1; j++,point2+=2)
162      {
163    if (skip_next)
164      skip_next=0;
165    else
166    {
167      if (j!=i)           // make sure we are not looking at ourself
168      {
169        xp1=point2[0];
170        yp1=point2[1];
171        xp2=point2[2];
172        yp2=point2[3];
173
174        if ((checkx>=xp1 && checkx<=xp2) || (checkx>=xp2 &&  checkx<=xp1))
175            {
176            if (check_down && (yp1>miny && yp2>miny))
177        segs_down++;
178          if (checkx==xp2) skip_next=1;
179        } else if ((checky>=yp1 && checky<=yp2) || (checky>=yp2 &&  checky<=yp1))
180        {
181          if (check_left && xp1<maxx && xp2<maxx)
182        segs_left++;
183          if (check_right && xp1>minx && xp2>minx)
184            segs_right++;
185          if (checky==yp2) skip_next=1;
186        }
187      }
188    }
189      }
190    }
191    if (!check_down) segs_down=1;
192    if (!check_right) segs_right=1;
193    if (!check_left) segs_left=1;
194
195    inside[i]=!(((segs_left&1)&&(segs_right&1)&&(segs_down&1)));
196  }
197}
198
199backtile::backtile(bFILE *fp)
200{
201  im=load_image(fp);
202  next=fp->read_uint16();
203}
204
205backtile::backtile(spec_entry *e, bFILE *fp)
206{
207  im=load_image(e,fp);
208  next=fp->read_uint16();
209}
210
211foretile::foretile(bFILE *fp)
212{
213    uint8_t *sl;
214    image *img = load_image(fp);
215
216    // create the micro image of the fore tile by averaging the color values
217    // in 2×2 space and storing the closest match
218
219    //uint8_t *buffer=(uint8_t *)&micro_image;
220    int x, y, w = img->Size().x, h = img->Size().y, l;
221    int r[AUTOTILE_WIDTH * AUTOTILE_HEIGHT],
222        g[AUTOTILE_WIDTH * AUTOTILE_HEIGHT],
223        b[AUTOTILE_WIDTH * AUTOTILE_HEIGHT],
224        t[AUTOTILE_WIDTH * AUTOTILE_HEIGHT];
225    memset(t, 0, AUTOTILE_WIDTH * AUTOTILE_HEIGHT * sizeof(int));
226    memset(r, 0, AUTOTILE_WIDTH * AUTOTILE_HEIGHT * sizeof(int));
227    memset(g, 0, AUTOTILE_WIDTH * AUTOTILE_HEIGHT * sizeof(int));
228    memset(b, 0, AUTOTILE_WIDTH * AUTOTILE_HEIGHT * sizeof(int));
229
230  if (!pal)
231  {
232    lbreak("Palette has no been defined\nuse load_palette before load_tiles");
233    exit(0);
234  }
235
236  if (!color_table)
237  {
238    lbreak("color filter has no been defined\nuse load_color_filter before load_tiles");
239    exit(0);
240  }
241
242  for (y=0; y<h; y++)
243  {
244    sl=img->scan_line(y);
245    for (x=0; x<w; x++,sl++)
246    {
247      l=(y*AUTOTILE_HEIGHT/h)*AUTOTILE_WIDTH +  x*AUTOTILE_WIDTH/w;
248      r[l]+=pal->red(*sl);
249      g[l]+=pal->green(*sl);
250      b[l]+=pal->blue(*sl);
251      t[l]++;
252    }
253  }
254  micro_image = new image(vec2i(AUTOTILE_WIDTH, AUTOTILE_HEIGHT));
255
256  for (l=0; l<AUTOTILE_WIDTH*AUTOTILE_HEIGHT; l++)
257    micro_image->PutPixel(vec2i(l % AUTOTILE_WIDTH, l / AUTOTILE_WIDTH),
258       color_table->lookup_color((r[l]/(t[l]*4/5))>>3,
259                 (g[l]/(t[l]*4/5))>>3,
260                 (b[l]/(t[l]*4/5))>>3));
261
262
263  im=new TransImage(img,"foretile");
264  delete img;
265
266  next=fp->read_uint16();
267  fp->read(&damage,1);
268
269
270  points=new boundary(fp,"foretile boundry");
271
272
273}
274
275size_t figure::MemUsage()
276{
277    return forward->DiskUsage() + backward->DiskUsage() + hit->size()
278            + f_damage->size() + b_damage->size() + sizeof(figure);
279}
280
281
282figure::figure(bFILE *fp, int type)
283{
284  image *im=load_image(fp);
285  forward=new TransImage(im,"figure data");
286  im->FlipX();
287  backward=new TransImage(im,"figure backward data");
288  delete im;
289
290  fp->read(&hit_damage,1);
291
292  fp->read(&xcfg,1);
293  xcfg=xcfg*scale_mult/scale_div;
294
295  if (type==SPEC_CHARACTER)
296  {
297    point_list p(fp);
298    advance=0;
299  } else advance=fp->read_uint8();
300
301  f_damage=new boundary(fp,"fig bound");
302  b_damage=new boundary(f_damage);
303  hit=new point_list(fp);
304}
305
306
307char_tint::char_tint(bFILE *fp)  // se should be a palette entry
308{
309  palette *p=new palette(fp);
310  uint8_t *t=data,*p_addr=(uint8_t *)p->addr();
311  for (int i=0; i<256; i++,t++,p_addr+=3)
312    *t=pal->find_closest(*p_addr,p_addr[1],p_addr[2]);
313
314  delete p;
315}
316
317
318
319
Note: See TracBrowser for help on using the repository browser.