Revision 216
many changes. implemented collision detection
graph.c | ||
---|---|---|
271 | 271 |
uint16_t w, h; |
272 | 272 |
int16_t u0, v0; |
273 | 273 |
}; |
274 |
|
|
275 | 274 |
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0){ |
276 | 275 |
basic_sprite_t *ret = malloc(sizeof(basic_sprite_t)); |
277 | 276 |
if(ret == NULL) return NULL; |
... | ... | |
279 | 278 |
xpm_image_t img; |
280 | 279 |
ret->map = xpm_load((xpm_map_t)xpm, type, &img); |
281 | 280 |
if(ret->map == NULL){ |
282 |
free(ret);
|
|
281 |
basic_sprite_dtor(ret);
|
|
283 | 282 |
return NULL; |
284 | 283 |
} |
285 | 284 |
ret->w = img.width; |
... | ... | |
293 | 292 |
free(p->map); |
294 | 293 |
free(p); |
295 | 294 |
} |
296 |
|
|
297 | 295 |
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p){ return p->map; } |
298 | 296 |
uint16_t (basic_sprite_get_w) (const basic_sprite_t *p){ return p->w ; } |
299 | 297 |
uint16_t (basic_sprite_get_h) (const basic_sprite_t *p){ return p->h ; } |
300 | 298 |
int16_t (basic_sprite_get_u0) (const basic_sprite_t *p){ return p->u0 ; } |
301 | 299 |
int16_t (basic_sprite_get_v0) (const basic_sprite_t *p){ return p->v0 ; } |
302 | 300 |
|
301 |
/* |
|
302 |
struct basic_sprite_alpha{ |
|
303 |
uint8_t *map; |
|
304 |
uint16_t w, h; |
|
305 |
int16_t u0, v0; |
|
306 |
}; |
|
307 |
basic_sprite_alpha_t* (basic_sprite_alpha_ctor)(const char **xpm, int16_t u0, int16_t v0){ |
|
308 |
basic_sprite_alpha_t *ret = malloc(sizeof(basic_sprite_t)); |
|
309 |
if(ret == NULL) return NULL; |
|
310 |
enum xpm_image_type type = XPM_8_8_8_8; |
|
311 |
xpm_image_t img; |
|
312 |
ret->map = NULL; |
|
313 |
uint8_t *m = xpm_load((xpm_map_t)xpm, type, &img); |
|
314 |
if(m == NULL){ |
|
315 |
basic_sprite_alpha_dtor(ret); |
|
316 |
return NULL; |
|
317 |
} |
|
318 |
ret->map = m; |
|
319 |
if(ret->map == NULL){ |
|
320 |
basic_sprite_alpha_dtor(ret); |
|
321 |
return NULL; |
|
322 |
} |
|
323 |
ret->w = img.width; |
|
324 |
ret->h = img.height; |
|
325 |
ret->u0 = u0; |
|
326 |
ret->v0 = v0; |
|
327 |
return ret; |
|
328 |
} |
|
329 |
void (basic_sprite_alpha_dtor)(basic_sprite_alpha_t *p){ |
|
330 |
if(p == NULL) return; |
|
331 |
free(p->map); |
|
332 |
free(p); |
|
333 |
} |
|
334 |
const uint8_t* (basic_sprite_alpha_get_map)(const basic_sprite_alpha_t *p){ return p->map; } |
|
335 |
uint16_t (basic_sprite_alpha_get_w) (const basic_sprite_alpha_t *p){ return p->w ; } |
|
336 |
uint16_t (basic_sprite_alpha_get_h) (const basic_sprite_alpha_t *p){ return p->h ; } |
|
337 |
int16_t (basic_sprite_alpha_get_u0) (const basic_sprite_alpha_t *p){ return p->u0 ; } |
|
338 |
int16_t (basic_sprite_alpha_get_v0) (const basic_sprite_alpha_t *p){ return p->v0 ; } |
|
339 |
*/ |
|
340 |
|
|
303 | 341 |
struct sprite{ |
304 | 342 |
const basic_sprite_t *bsp; |
305 | 343 |
int16_t x, y; //position in screen |
306 | 344 |
double theta, s, c; |
307 | 345 |
double scale; |
308 | 346 |
}; |
309 |
|
|
310 | 347 |
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp){ |
311 | 348 |
sprite_t *ret = malloc(sizeof(sprite_t)); |
312 | 349 |
if(ret == NULL) return NULL; |
... | ... | |
321 | 358 |
if(p == NULL) return; |
322 | 359 |
free(p); |
323 | 360 |
} |
324 |
|
|
325 | 361 |
void (sprite_set_pos) (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; } |
326 | 362 |
void (sprite_set_angle) (sprite_t *p, double angle ){ p->theta = angle; p->c = fm_cos(p->theta); p->s = fm_sin(p->theta); } |
327 | 363 |
void (sprite_set_scale) (sprite_t *p, double scale ){ p->scale = scale; } |
328 | 364 |
int16_t (sprite_get_x)(const sprite_t *p){ return p->x; } |
329 | 365 |
int16_t (sprite_get_y)(const sprite_t *p){ return p->y; } |
330 |
|
|
366 |
uint16_t (sprite_get_w)(const sprite_t *p){ return basic_sprite_get_w(p->bsp); } |
|
367 |
uint16_t (sprite_get_h)(const sprite_t *p){ return basic_sprite_get_h(p->bsp); } |
|
331 | 368 |
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){ |
332 | 369 |
double dx = (x - p->x)/p->scale; |
333 | 370 |
double dy = (y - p->y)/p->scale; |
... | ... | |
336 | 373 |
*u = du + basic_sprite_get_u0(p->bsp); |
337 | 374 |
*v = dv + basic_sprite_get_v0(p->bsp); |
338 | 375 |
} |
339 |
|
|
340 | 376 |
void (sprite_pic2src)(const sprite_t *p, int16_t u, int16_t v, int16_t *x, int16_t *y){ |
341 | 377 |
int16_t du = u - basic_sprite_get_u0(p->bsp); |
342 | 378 |
int16_t dv = v - basic_sprite_get_v0(p->bsp); |
... | ... | |
345 | 381 |
*x = dx*p->scale + 0.5 + p->x; |
346 | 382 |
*y = dy*p->scale + 0.5 + p->y; |
347 | 383 |
} |
348 |
|
|
349 | 384 |
void (sprite_draw)(const sprite_t *p){ |
350 | 385 |
const uint16_t w = basic_sprite_get_w(p->bsp); |
351 | 386 |
const uint16_t h = basic_sprite_get_h(p->bsp); |
... | ... | |
370 | 405 |
sprite_src2pic(p, x, y, &u, &v); |
371 | 406 |
if(0 <= u && u < w && 0 <= v && v < h){ |
372 | 407 |
const uint8_t *c_p = map+(v*w+u)*4; |
373 |
if(*(c_p+3) < 0x7F) //alpha
|
|
408 |
if(*(c_p+3) < ALPHA_THRESHOLD) //alpha
|
|
374 | 409 |
memcpy(place, c_p, bytes_pixel); |
375 | 410 |
} |
376 | 411 |
} |
Also available in: Unified diff