Revision 210
reverted
graph.c | ||
---|---|---|
260 | 260 |
int (graph_draw)(void){ memcpy(video_mem, video_buf, graph_get_vram_size()); return SUCCESS; } |
261 | 261 |
|
262 | 262 |
/// SPRITE |
263 |
|
|
264 |
struct basic_sprite{ |
|
265 |
uint8_t *map; |
|
266 |
uint16_t w, h; |
|
267 |
int16_t u0, v0; |
|
268 |
}; |
|
269 |
|
|
270 |
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0){ |
|
271 |
basic_sprite_t *ret = malloc(sizeof(basic_sprite_t)); |
|
272 |
if(ret == NULL) return NULL; |
|
273 |
enum xpm_image_type type = XPM_8_8_8_8; |
|
274 |
xpm_image_t img; |
|
275 |
ret->map = xpm_load((xpm_map_t)xpm, type, &img); |
|
276 |
if(ret->map == NULL){ |
|
277 |
free(ret); |
|
278 |
return NULL; |
|
279 |
} |
|
280 |
ret->w = img.width; |
|
281 |
ret->h = img.height; |
|
282 |
ret->u0 = u0; |
|
283 |
ret->v0 = v0; |
|
284 |
return ret; |
|
285 |
} |
|
286 |
void (basic_sprite_dtor)(basic_sprite_t *p){ |
|
287 |
if(p == NULL) return; |
|
288 |
free(p->map); |
|
289 |
free(p); |
|
290 |
} |
|
291 |
|
|
292 |
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p){ return p->map; } |
|
293 |
uint16_t (basic_sprite_get_w) (const basic_sprite_t *p){ return p->w ; } |
|
294 |
uint16_t (basic_sprite_get_h) (const basic_sprite_t *p){ return p->h ; } |
|
295 |
int16_t (basic_sprite_get_u0) (const basic_sprite_t *p){ return p->u0 ; } |
|
296 |
int16_t (basic_sprite_get_v0) (const basic_sprite_t *p){ return p->v0 ; } |
|
297 |
|
|
298 |
struct sprite{ |
|
299 |
const basic_sprite_t *bsp; |
|
300 |
int16_t x, y; //position in screen |
|
301 |
double theta, s, c; |
|
302 |
double scale; |
|
303 |
}; |
|
304 |
|
|
305 |
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp){ |
|
306 |
sprite_t *ret = malloc(sizeof(sprite_t)); |
|
307 |
if(ret == NULL) return NULL; |
|
308 |
ret->bsp = bsp; |
|
309 |
ret->x = 0; |
|
310 |
ret->y = 0; |
|
311 |
sprite_set_angle(ret, 0.0); |
|
312 |
ret->scale = 1.0; |
|
313 |
return ret; |
|
314 |
} |
|
315 |
void (sprite_dtor)(sprite_t *p){ |
|
316 |
if(p == NULL) return; |
|
317 |
free(p); |
|
318 |
} |
|
319 |
|
|
320 |
void (sprite_set_pos) (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; } |
|
321 |
void (sprite_set_angle) (sprite_t *p, double angle ){ p->theta = angle; p->c = fm_cos(p->theta); p->s = fm_sin(p->theta); } |
|
322 |
void (sprite_set_scale) (sprite_t *p, double scale ){ p->scale = scale; } |
|
323 |
int16_t (sprite_get_x)(const sprite_t *p){ return p->x; } |
|
324 |
int16_t (sprite_get_y)(const sprite_t *p){ return p->y; } |
|
325 |
|
|
326 |
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){ |
|
327 |
double dx = (x - p->x)/p->scale; |
|
328 |
double dy = (y - p->y)/p->scale; |
|
329 |
int16_t du = dx*p->c - dy*p->s + 0.5; |
|
330 |
int16_t dv = dx*p->s + dy*p->c + 0.5; |
|
331 |
*u = du + basic_sprite_get_u0(p->bsp); |
|
332 |
*v = dv + basic_sprite_get_v0(p->bsp); |
|
333 |
} |
|
334 |
|
|
335 |
void (sprite_pic2src)(const sprite_t *p, int16_t u, int16_t v, int16_t *x, int16_t *y){ |
|
336 |
int16_t du = u - basic_sprite_get_u0(p->bsp); |
|
337 |
int16_t dv = v - basic_sprite_get_v0(p->bsp); |
|
338 |
double dx = du*p->c + dv*p->s; |
|
339 |
double dy = -du*p->s + dv*p->c; |
|
340 |
*x = dx*p->scale + 0.5 + p->x; |
|
341 |
*y = dy*p->scale + 0.5 + p->y; |
|
342 |
} |
|
343 |
|
|
344 |
void (sprite_draw)(const sprite_t *p){ |
|
345 |
const uint16_t w = basic_sprite_get_w(p->bsp); |
|
346 |
const uint16_t h = basic_sprite_get_h(p->bsp); |
|
347 |
int16_t xmin, xmax, ymin, ymax; { |
|
348 |
int16_t x, y; |
|
349 |
sprite_pic2src(p, 0, 0, &x, &y); |
|
350 |
xmin = x; xmax = x; ymin = y; ymax = y; |
|
351 |
sprite_pic2src(p, w, 0, &x, &y); |
|
352 |
xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax); |
|
353 |
sprite_pic2src(p, 0, h, &x, &y); |
|
354 |
xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax); |
|
355 |
sprite_pic2src(p, w, h, &x, &y); |
|
356 |
xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax); |
|
357 |
xmin = max(xmin-2, 0); xmax = min(xmax+2, graph_get_XRes()); |
|
358 |
ymin = max(ymin-2, 0); ymax = min(ymax+2, graph_get_YRes()); |
|
359 |
} |
|
360 |
const uint8_t *map = basic_sprite_get_map(p->bsp); |
|
361 |
const uint16_t bytes_pixel = graph_get_bytes_pixel(); |
|
362 |
for(int16_t u, v, y = ymin; y < ymax; ++y){ |
|
363 |
unsigned pos = (xmin + y*graph_get_XRes())*bytes_pixel; |
|
364 |
for(int16_t x = xmin; x < xmax; ++x, pos += bytes_pixel){ |
|
365 |
sprite_src2pic(p, x, y, &u, &v); |
|
366 |
if(0 <= u && u < w && 0 <= v && v < h){ |
|
367 |
uint32_t c = *(uint32_t*)(map + (v*w + u)*4); |
|
368 |
if(GET_ALP(c) < 0x7F) |
|
369 |
graph_set_pixel_pos(pos, c); |
|
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
} |
Also available in: Unified diff