forked from lahwaacz/libSGE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sge_collision.cpp
388 lines (331 loc) · 8.19 KB
/
sge_collision.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* SDL Graphics Extension
* Collision routines
*
* Started 000625
*
* License: LGPL v2+ (see the file LICENSE)
* (c)1999-2003 Anders Lindström
*/
/*********************************************************************
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
*********************************************************************/
#include "SDL.h"
#include <stdio.h>
#include <string.h>
#include <new>
#include "sge_collision.h"
#include "sge_surface.h"
#include "sge_shape.h"
using namespace std;
Uint8 sge_mask[8]={SGE_FLAG1,SGE_FLAG2,SGE_FLAG3,SGE_FLAG4,SGE_FLAG5,SGE_FLAG6,SGE_FLAG7,SGE_FLAG8};
SDL_Rect _ua;
Sint16 _cx=0,_cy=0;
int memand(Uint8 *s1, Uint8 *s2, int shift1, int shift2, int N);
//==================================================================================
// Makes a new collision map from img. Set colorkey first!
//==================================================================================
sge_cdata *sge_make_cmap(SDL_Surface *img)
{
sge_cdata *cdata;
Uint8 *map;
Sint16 x,y;
Sint32 offs;
int i;
cdata=new(nothrow) sge_cdata;
if(!cdata){SDL_SetError("SGE - Out of memory");return NULL;}
cdata->w=img->w; cdata->h=img->h;
offs=(img->w*img->h)/8;
cdata->map=new(nothrow) Uint8[offs+2];
if(!cdata->map){SDL_SetError("SGE - Out of memory");return NULL;}
memset(cdata->map,0x00,offs+2);
map=cdata->map;
i=0;
for(y=0; y < img->h; y++){
for(x=0; x < img->w; x++){
if(i>7){i=0;map++;}
if(sge_GetPixel(img, Sint16(x),Sint16(y))!=img->format->colorkey){
*map=*map|sge_mask[i];
}
i++;
}
}
return cdata;
}
//==================================================================================
// Checks bounding boxes for collision: 0-no collision 1-collision
//==================================================================================
int sge_bbcheck(sge_cdata *cd1,Sint16 x1,Sint16 y1, sge_cdata *cd2,Sint16 x2,Sint16 y2)
{
Sint16 w1=cd1->w;
Sint16 h1=cd1->h;
Sint16 w2=cd2->w;
Sint16 h2=cd2->h;
if(x1 < x2){
if(x1+w1 > x2){
if(y1 < y2){
if(y1+h1 > y2){
_ua.x=x2;
_ua.y=y2;
return 1;
}
}
else{
if(y2+h2 > y1){
_ua.x=x2;
_ua.y=y1;
return 1;
}
}
}
}
else{
if(x2+w2 > x1){
if(y2 < y1){
if(y2+h2 > y1){
_ua.x=x1;
_ua.y=y1;
return 1;
}
}
else{
if(y1+h1 > y2){
_ua.x=x1;
_ua.y=y2;
return 1;
}
}
}
}
return 0;
}
//==================================================================================
// Checks bounding boxes for collision: 0-no collision 1-collision
//==================================================================================
int _sge_bbcheck(Sint16 x1,Sint16 y1,Sint16 w1,Sint16 h1, Sint16 x2,Sint16 y2,Sint16 w2,Sint16 h2)
{
if(x1 < x2){
if(x1+w1 > x2){
if(y1 < y2){
if(y1+h1 > y2){
_ua.x=x2;
_ua.y=y2;
return 1;
}
}
else{
if(y2+h2 > y1){
_ua.x=x2;
_ua.y=y1;
return 1;
}
}
}
}
else{
if(x2+w2 > x1){
if(y2 < y1){
if(y2+h2 > y1){
_ua.x=x1;
_ua.y=y1;
return 1;
}
}
else{
if(y1+h1 > y2){
_ua.x=x1;
_ua.y=y2;
return 1;
}
}
}
}
return 0;
}
//==================================================================================
// AND N bits of s1 and s2
// Returns the number of the bit (or zero)
//==================================================================================
int memand(Uint8 *s1, Uint8 *s2, int shift1, int shift2, int N)
{
int b,i1=shift1,i2=shift2;
for(b=0; b<N; b++){
if(i1>7){i1=0;s1++;}
if(i2>7){i2=0;s2++;}
if( (*s1&sge_mask[i1]) && (*s2&sge_mask[i2]) ) return b+1;
i1++; i2++;
}
return 0;
}
//==================================================================================
// Checks for pixel perfect collision: 0-no collision 1-collision
// sge_bbcheck MUST be called first!!!
//==================================================================================
int _sge_cmcheck(sge_cdata *cd1,Sint16 x1,Sint16 y1, sge_cdata *cd2,Sint16 x2,Sint16 y2)
{
if(cd1->map==NULL || cd2->map==NULL)
return 0;
Sint16 w1=cd1->w;
Sint16 h1=cd1->h;
Sint16 w2=cd2->w;
Sint16 h2=cd2->h;
//masks
Sint32 x1o=0,x2o=0,y1o=0,y2o=0,offs; //offsets
int i1=0,i2=0;
Uint8 *map1=cd1->map;
Uint8 *map2=cd2->map;
//Calculate correct starting point
if(_ua.x==x2 && _ua.y==y2){
x1o=x2-x1;
y1o=y2-y1;
offs=w1*y1o+x1o;
map1+=offs/8;
i1=offs%8;
}
else if(_ua.x==x2 && _ua.y==y1){
x1o=x2-x1;
y2o=y1-y2;
map1+=x1o/8;
i1=x1o%8;
offs=w2*y2o;
map2+=offs/8;
i2=offs%8;
}
else if(_ua.x==x1 && _ua.y==y1){
x2o=x1-x2;
y2o=y1-y2;
offs=w2*y2o+x2o;
map2+=offs/8;
i2=offs%8;
}
else if(_ua.x==x1 && _ua.y==y2){
x2o=x1-x2;
y1o=y2-y1;
offs=w1*y1o;
map1+=offs/8;
i1=offs%8;
map2+=x2o/8;
i2=x2o%8;
}
else
return 0;
Sint16 y;
Sint16 lenght;
if(x1+w1 < x2+w2)
lenght=w1-x1o;
else
lenght=w2-x2o;
//AND(map1,map2)
for(y=_ua.y; y<=y1+h1 && y<=y2+h2; y++){
offs=memand(map1,map2,i1,i2,lenght);
if(offs){
_cx=_ua.x+offs-1; _cy=y;
return 1;
}
//goto the new line
offs=(y-y1)*w1+x1o;
map1=cd1->map; //reset pointer
map1+=offs/8;
i1=offs%8;
offs=(y-y2)*w2+x2o;
map2=cd2->map; //reset pointer
map2+=offs/8;
i2=offs%8;
}
return 0;
}
//==================================================================================
// Checks pixel perfect collision: 0-no collision 1-collision
// calls sge_bbcheck automaticly
//==================================================================================
int sge_cmcheck(sge_cdata *cd1,Sint16 x1,Sint16 y1, sge_cdata *cd2,Sint16 x2,Sint16 y2)
{
if(!sge_bbcheck(cd1,x1,y1, cd2,x2,y2))
return 0;
if(cd1->map==NULL || cd2->map==NULL)
return 1;
return _sge_cmcheck(cd1,x1,y1, cd2,x2,y2);
}
//==================================================================================
// Get the position of the last collision
//==================================================================================
Sint16 sge_get_cx(void)
{
return _cx;
}
Sint16 sge_get_cy(void)
{
return _cy;
}
//==================================================================================
// Removes collision map from memory
//==================================================================================
void sge_destroy_cmap(sge_cdata *cd)
{
if(cd->map!=NULL)
delete [] cd->map;
delete cd;
}
//==================================================================================
// Checks bounding boxes for collision: 0-no collision 1-collision
// (sprites)
//==================================================================================
#ifndef _SGE_NO_CLASSES
int sge_bbcheck_shape(sge_shape *shape1, sge_shape *shape2)
{
return _sge_bbcheck(shape1->get_xpos(), shape1->get_ypos(), shape1->get_w(), shape1->get_h(), shape2->get_xpos(), shape2->get_ypos(), shape2->get_w(), shape2->get_h());
}
#endif
//==================================================================================
// Clears an area in a cmap
//==================================================================================
void sge_unset_cdata(sge_cdata *cd, Sint16 x, Sint16 y, Sint16 w, Sint16 h)
{
Uint8 *map=cd->map;
Sint16 offs,len;
int i,n=0;
offs=y*cd->w + x;
map+=offs/8;
i=offs%8;
while(h--){
len=w;
while(len--){
if(i>7){i=0;map++;}
*map&=~sge_mask[i];
i++;
}
n++;
map=cd->map;
offs=(y+n)*cd->w + x;
map+=offs/8;
i=offs%8;
}
}
//==================================================================================
// Fills an area in a cmap
//==================================================================================
void sge_set_cdata(sge_cdata *cd, Sint16 x, Sint16 y, Sint16 w, Sint16 h)
{
Uint8 *map=cd->map;
Sint16 offs,len;
int i,n=0;
offs=y*cd->w + x;
map+=offs/8;
i=offs%8;
while(h--){
len=w;
while(len--){
if(i>7){i=0;map++;}
*map|=sge_mask[i];
i++;
}
n++;
map=cd->map;
offs=(y+n)*cd->w + x;
map+=offs/8;
i=offs%8;
}
}