Project

General

Profile

Revision 211

not working yet

View differences:

graph.c
259 259
int (graph_clear_screen)(void){ memset(video_buf, 0, graph_get_vram_size()); return SUCCESS; }
260 260
int (graph_draw)(void){ memcpy(video_mem, video_buf, graph_get_vram_size()); return SUCCESS; }
261 261

  
262
/// SPRITE
262
///SPRITE
263
#include "utils.h"
264
#include "fast_math.h"
265
#include <math.h>
266

  
267
struct basic_sprite{
268
    uint8_t *map;
269
    uint16_t w, h;
270
    int16_t u0, v0;
271
};
272

  
273
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0){
274
    basic_sprite_t *ret = malloc(sizeof(basic_sprite_t));
275
    if(ret == NULL) return NULL;
276
    enum xpm_image_type type = XPM_8_8_8_8;
277
    xpm_image_t img;
278
    ret->map = xpm_load((xpm_map_t)xpm, type, &img);
279
    if(ret->map == NULL){
280
        free(ret);
281
        return NULL;
282
    }
283
    ret->w = img.width;
284
    ret->h = img.height;
285
    ret->u0 = u0;
286
    ret->v0 = v0;
287
    return ret;
288
}
289
void (basic_sprite_dtor)(basic_sprite_t *p){
290
    if(p == NULL) return;
291
    free(p->map);
292
    free(p);
293
}
294

  
295
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p){ return p->map; }
296
uint16_t       (basic_sprite_get_w)  (const basic_sprite_t *p){ return p->w  ; }
297
uint16_t       (basic_sprite_get_h)  (const basic_sprite_t *p){ return p->h  ; }
298
int16_t        (basic_sprite_get_u0) (const basic_sprite_t *p){ return p->u0 ; }
299
int16_t        (basic_sprite_get_v0) (const basic_sprite_t *p){ return p->v0 ; }
300

  
301
struct sprite{
302
    const basic_sprite_t *bsp;
303
    int16_t x, y; //position in screen
304
    double theta, s, c;
305
    double scale;
306
};
307

  
308
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp){
309
    sprite_t *ret = malloc(sizeof(sprite_t));
310
    if(ret == NULL) return NULL;
311
    ret->bsp = bsp;
312
    ret->x = 0;
313
    ret->y = 0;
314
    sprite_set_angle(ret, 0.0);
315
    ret->scale = 1.0;
316
    return ret;
317
}
318
void (sprite_dtor)(sprite_t *p){
319
    if(p == NULL) return;
320
    free(p);
321
}
322

  
323
void (sprite_set_pos)   (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; }
324
void (sprite_set_angle) (sprite_t *p, double angle          ){ p->theta = angle; p->c = fm_cos(p->theta); p->s = fm_sin(p->theta); }
325
void (sprite_set_scale) (sprite_t *p, double scale          ){ p->scale = scale; }
326
int16_t  (sprite_get_x)(const sprite_t *p){ return p->x; }
327
int16_t  (sprite_get_y)(const sprite_t *p){ return p->y; }
328

  
329
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){
330
    double dx = (x - p->x)/p->scale;
331
    double dy = (y - p->y)/p->scale;
332
    int16_t du = dx*p->c - dy*p->s + 0.5;
333
    int16_t dv = dx*p->s + dy*p->c + 0.5;
334
    *u = du + basic_sprite_get_u0(p->bsp);
335
    *v = dv + basic_sprite_get_v0(p->bsp);
336
}
337

  
338
void (sprite_pic2src)(const sprite_t *p, int16_t u, int16_t v, int16_t *x, int16_t *y){
339
    int16_t du = u - basic_sprite_get_u0(p->bsp);
340
    int16_t dv = v - basic_sprite_get_v0(p->bsp);
341
    double dx =  du*p->c + dv*p->s;
342
    double dy = -du*p->s + dv*p->c;
343
    *x = dx*p->scale + 0.5 + p->x;
344
    *y = dy*p->scale + 0.5 + p->y;
345
}
346

  
347
void (sprite_draw)(const sprite_t *p){
348
    const uint16_t w = basic_sprite_get_w(p->bsp);
349
    const uint16_t h = basic_sprite_get_h(p->bsp);
350
    int16_t xmin, xmax, ymin, ymax; {
351
        int16_t x, y;
352
        sprite_pic2src(p, 0, 0, &x, &y);
353
        xmin = x; xmax = x; ymin = y; ymax = y;
354
        sprite_pic2src(p, w, 0, &x, &y);
355
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
356
        sprite_pic2src(p, 0, h, &x, &y);
357
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
358
        sprite_pic2src(p, w, h, &x, &y);
359
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
360
        xmin = max(xmin-2, 0); xmax = min(xmax+2, graph_get_XRes());
361
        ymin = max(ymin-2, 0); ymax = min(ymax+2, graph_get_YRes());
362
    }
363
    const uint8_t *map = basic_sprite_get_map(p->bsp);
364
    const uint16_t bytes_pixel = graph_get_bytes_pixel();
365
    for(int16_t u, v, y = ymin; y < ymax; ++y){
366
        unsigned pos = (xmin + y*graph_get_XRes())*bytes_pixel;
367
        for(int16_t x = xmin; x < xmax; ++x, pos += bytes_pixel){
368
            sprite_src2pic(p, x, y, &u, &v);
369
            if(0 <= u && u < w && 0 <= v && v < h){
370
                uint32_t c = *(uint32_t*)(map + (v*w + u)*4);
371
                if(GET_ALP(c) < 0x7F)
372
                    graph_set_pixel_pos(pos, c);
373
            }
374
        }
375
    }
376
}

Also available in: Unified diff