Project

General

Profile

Revision 125

forgot files

View differences:

lab5/sprite.c
1
#include <lcom/lcf.h>
2

  
3
#include "sprite.h"
4

  
5
#include "graphics.h"
6

  
7
struct sprite{
8
    int x, y;
9
    int w, h;
10
    uint8_t *map;
11
};
12

  
13
sprite_t* sprite_ctor(const xpm_map_t xpm){
14
    sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t));
15
    if(ret == NULL) return NULL;
16
    enum xpm_image_type type = XPM_INDEXED;
17
    xpm_image_t img;
18
    ret->map = xpm_load(xpm, type, &img);
19
    if(ret->map == NULL){
20
        free(ret);
21
        return NULL;
22
    }
23
    ret->w = img.width;
24
    ret->h = img.height;
25
    return ret;
26
}
27

  
28
void sprite_dtor(sprite_t *p){
29
    if(p == NULL) return;
30
    if(p->map) free(p->map);
31
    free(p);
32
}
33

  
34
void sprite_set_x(sprite_t *p, int x){ p->x = x; }
35
void sprite_set_y(sprite_t *p, int y){ p->y = y; }
36
void sprite_set_pos(sprite_t *p, int x, int y){
37
    sprite_set_x(p, x);
38
    sprite_set_y(p, y);
39
}
40

  
41
void sprite_draw(const sprite_t *p){
42
    for (int i = 0; i < p->w; i++) {
43
        for (int j = 0; j < p->h; j++) {
44
            set_pixel(p->x + i, p->y + j, p->map[i + j * p->w]);
45
        }
46
    }
47
}
0 48

  
lab5/sprite.h
1
#ifndef SPRITE_H_INCLUDED
2
#define SPRITE_H_INCLUDED
3

  
4
struct sprite;
5
typedef struct sprite sprite_t;
6

  
7
sprite_t* sprite_ctor(const xpm_map_t xpm);
8
void      sprite_dtor(sprite_t *p);
9

  
10
void sprite_set_x(sprite_t *p, int x);
11
void sprite_set_y(sprite_t *p, int y);
12
void sprite_set_pos(sprite_t *p, int x, int y);
13

  
14
void sprite_draw(const sprite_t *p);
15

  
16
#endif //SPRITE_H_INCLUDED
0 17

  

Also available in: Unified diff