source: abuse/branches/lol/src/items.cpp @ 732

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

build: SDL2 compilation fixes.

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