Revision 160
implemented sprite rotation
proj/DR.mk | ||
---|---|---|
2 | 2 |
|
3 | 3 |
SRCS= proj.c graphics.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c |
4 | 4 |
|
5 |
CPPFLAGS += -pedantic -D DIOGO # -D __LCOM_OPTIMIZED_ |
|
5 |
CPPFLAGS += -pedantic -I./bmp -I./xpm -D DIOGO # -D __LCOM_OPTIMIZED_
|
|
6 | 6 |
|
7 | 7 |
DPADD += ${LIBLCF} |
8 | 8 |
LDADD += -llcf |
proj/Makefile | ||
---|---|---|
2 | 2 |
|
3 | 3 |
SRCS= proj.c graphics.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c |
4 | 4 |
|
5 |
CPPFLAGS += -pedantic -D __LCOM_OPTIMIZED_ |
|
5 |
CPPFLAGS += -pedantic -I./bmp -I./xpm -D __LCOM_OPTIMIZED_
|
|
6 | 6 |
|
7 | 7 |
DPADD += ${LIBLCF} |
8 | 8 |
LDADD += -llcf |
proj/TB.mk | ||
---|---|---|
2 | 2 |
|
3 | 3 |
SRCS= proj.c graphics.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c |
4 | 4 |
|
5 |
CPPFLAGS += -pedantic -D __LCOM_OPTIMIZED_ -D TELMO |
|
5 |
CPPFLAGS += -pedantic -I./bmp -I./xpm -D __LCOM_OPTIMIZED_ -D TELMO
|
|
6 | 6 |
|
7 | 7 |
DPADD += ${LIBLCF} |
8 | 8 |
LDADD += -llcf |
proj/graphics.c | ||
---|---|---|
198 | 198 |
return SUCCESS; |
199 | 199 |
} |
200 | 200 |
|
201 |
|
|
202 | 201 |
int (set_pixel)(uint16_t x, uint16_t y, uint32_t color) { |
203 | 202 |
if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) { |
204 | 203 |
printf("%s: invalid pixel.\n", __func__); |
... | ... | |
208 | 207 |
memcpy((void*)((unsigned int)video_mem + pos), &color, get_bytes_pixel()); |
209 | 208 |
return SUCCESS; |
210 | 209 |
} |
210 |
int (set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha){ |
|
211 |
unsigned int pos = (x + y * vbe_mem_info.XResolution) * get_bytes_pixel(); |
|
212 |
uint32_t color_; |
|
213 |
memcpy(&color_, (void*)((unsigned int)video_mem + pos), get_bytes_pixel()); |
|
214 |
float a = 1.0-(alpha&0xFF)/(float)0xFF; |
|
215 |
uint8_t r = GET_RED(color)*a + GET_RED(color_)*(1.0-a); |
|
216 |
uint8_t g = GET_GRE(color)*a + GET_GRE(color_)*(1.0-a); |
|
217 |
uint8_t b = GET_BLU(color)*a + GET_BLU(color_)*(1.0-a); |
|
218 |
return set_pixel(x,y,SET_RGB(r,g,b)); |
|
219 |
//return set_pixel(x,y,color); |
|
220 |
} |
|
211 | 221 |
|
212 | 222 |
int (draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){ |
213 | 223 |
int r; |
proj/graphics.h | ||
---|---|---|
4 | 4 |
#include <lcom/lcf.h> |
5 | 5 |
#include <stdint.h> |
6 | 6 |
|
7 |
#define GET_ALP(n) (0xFF & ((n) >> 24)) |
|
7 | 8 |
#define GET_RED(n) (0xFF & ((n) >> 16)) |
8 | 9 |
#define GET_GRE(n) (0xFF & ((n) >> 8)) |
9 | 10 |
#define GET_BLU(n) (0xFF & (n )) |
11 |
#define SET_ALP(n) (((n)&0xFF) << 24) |
|
10 | 12 |
#define SET_RED(n) (((n)&0xFF) << 16) |
11 | 13 |
#define SET_GRE(n) (((n)&0xFF) << 8) |
12 | 14 |
#define SET_BLU(n) (((n)&0xFF) ) |
13 |
#define SET_COLOR(r,g,b) (SET_RED(r) | SET_GRE(g) | SET_BLU(b)) |
|
15 |
#define SET_RGB(r,g,b) ( SET_RED(r) | SET_GRE(g) | SET_BLU(b)) |
|
16 |
#define SET_ARGB(a,r,g,b) (SET_ALP(a) | SET_RED(r) | SET_GRE(g) | SET_BLU(b)) |
|
14 | 17 |
#define FAR2PHYS(n) ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF)) |
15 | 18 |
|
16 | 19 |
typedef struct __attribute__((packed)) { |
... | ... | |
61 | 64 |
|
62 | 65 |
int (free_memory_map)(void); |
63 | 66 |
|
64 |
int (set_pixel)(uint16_t row, uint16_t col, uint32_t color); |
|
67 |
int (set_pixel) (uint16_t x, uint16_t y, uint32_t color); |
|
68 |
int (set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha); |
|
65 | 69 |
|
66 | 70 |
/** |
67 | 71 |
* @brief |
proj/proj.c | ||
---|---|---|
23 | 23 |
#include "proj_func.h" |
24 | 24 |
|
25 | 25 |
#ifdef DIOGO |
26 |
#include "plus.xpm" |
|
26 |
#include "shooter.xpm" |
|
27 |
#include "pistol.xpm" |
|
27 | 28 |
#endif |
28 | 29 |
|
29 | 30 |
int main(int argc, char* argv[]) { |
... | ... | |
60 | 61 |
}; |
61 | 62 |
|
62 | 63 |
#ifdef DIOGO |
63 |
sprite_t *sp = sprite_ctor((xpm_map_t)plus_xpm); |
|
64 |
sprite_draw(sp); |
|
64 |
paint_screen(0x777777); |
|
65 |
sprite_t *shooter = sprite_ctor((xpm_map_t)shooter_xpm); sprite_set_pos(shooter, 100, 100); sprite_set_center(shooter, 25, 25); |
|
66 |
for(double angle = 0; angle < 6.29; angle += 0.01){ |
|
67 |
sprite_set_angle(shooter, angle); |
|
68 |
//paint_screen(0x777777); |
|
69 |
sprite_draw(shooter); |
|
70 |
tickdelay(micros_to_ticks(10000)); |
|
71 |
} |
|
72 |
sprite_draw(shooter); |
|
65 | 73 |
#endif |
66 | 74 |
|
67 | 75 |
/// loop stuff |
proj/proj_macros.h | ||
---|---|---|
19 | 19 |
#define REFRESH_RATE 60 /** @brief Screen refresh rate */ |
20 | 20 |
|
21 | 21 |
//Graphics mode |
22 |
#define GRAPH_MODE DIRECT_800_600 /** @brief Graphic mode used */
|
|
22 |
#define GRAPH_MODE DIRECT_1280_1024_888 /** @brief Graphic mode used */
|
|
23 | 23 |
|
24 | 24 |
#endif /* end of include guard: PROJ_MACROS_H_INCLUDED */ |
proj/sprite.c | ||
---|---|---|
3 | 3 |
#include "sprite.h" |
4 | 4 |
|
5 | 5 |
#include "graphics.h" |
6 |
#include <math.h> |
|
6 | 7 |
|
7 | 8 |
struct sprite{ |
8 | 9 |
int x, y; |
9 | 10 |
int w, h; |
11 |
int u0, v0; |
|
12 |
double theta; |
|
10 | 13 |
uint8_t *map; |
11 | 14 |
}; |
12 | 15 |
|
13 | 16 |
sprite_t* sprite_ctor(const xpm_map_t xpm){ |
14 | 17 |
sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t)); |
15 | 18 |
if(ret == NULL) return NULL; |
16 |
enum xpm_image_type type = XPM_8_8_8; |
|
19 |
enum xpm_image_type type = XPM_8_8_8_8;
|
|
17 | 20 |
xpm_image_t img; |
18 | 21 |
ret->map = xpm_load(xpm, type, &img); |
19 | 22 |
if(ret->map == NULL){ |
... | ... | |
24 | 27 |
ret->y = 0; |
25 | 28 |
ret->w = img.width; |
26 | 29 |
ret->h = img.height; |
30 |
ret->u0 = 0; |
|
31 |
ret->v0 = 0; |
|
32 |
ret->theta = 0; |
|
27 | 33 |
return ret; |
28 | 34 |
} |
29 | 35 |
|
... | ... | |
39 | 45 |
sprite_set_x(p, x); |
40 | 46 |
sprite_set_y(p, y); |
41 | 47 |
} |
48 |
void sprite_set_angle(sprite_t *p, double angle){ p->theta = angle; } |
|
49 |
void sprite_set_center(sprite_t *p, int u0, int v0){ |
|
50 |
p->u0 = u0; |
|
51 |
p->v0 = v0; |
|
52 |
} |
|
42 | 53 |
|
43 | 54 |
int sprite_get_w(const sprite_t *p){ return p->w; } |
44 | 55 |
int sprite_get_h(const sprite_t *p){ return p->h; } |
45 | 56 |
|
57 |
void sprite_src2pic(const sprite_t *p, int x, int y, int *u, int *v){ |
|
58 |
int dx = x - p->x; |
|
59 |
int dy = y - p->y; |
|
60 |
int du = dx*cos(p->theta) - dy*sin(p->theta); |
|
61 |
int dv = dx*sin(p->theta) + dy*cos(p->theta); |
|
62 |
*u = du + p->u0; |
|
63 |
*v = dv + p->v0; |
|
64 |
} |
|
65 |
|
|
46 | 66 |
void sprite_draw(const sprite_t *p){ |
47 |
uint8_t *m = p->map; |
|
48 |
for (int i = 0; i < p->w; i++) { |
|
49 |
for (int j = 0; j < p->h; j++, m += 3) { |
|
50 |
if (p->x + i < get_XRes() && p->y + j < get_YRes()){ |
|
51 |
uint32_t color = SET_COLOR(*m,*(m+1),*(m+2)); |
|
52 |
set_pixel(p->x + i, p->y + j, color); |
|
67 |
const int diag = sqrt(p->w*p->w + p->h*p->h)+2; |
|
68 |
int u, v; |
|
69 |
for(int y = p->y - diag; y < p->y + diag; ++y){ |
|
70 |
for(int x = p->x - diag; x < p->x + diag; ++x){ |
|
71 |
if (p->x + x < get_XRes() && p->y + y < get_YRes()){ |
|
72 |
sprite_src2pic(p, x, y, &u, &v); |
|
73 |
if(0 <= u && u < p->w && 0 <= v && v < p->h){ |
|
74 |
uint8_t *m = p->map + (v*p->w + u)*4; |
|
75 |
uint32_t color = SET_RGB(*(m+2), *(m+1), *(m)); |
|
76 |
set_pixel_alpha(p->x + x, p->y + y, color, *(m+3)); |
|
77 |
} |
|
53 | 78 |
} |
54 | 79 |
} |
55 | 80 |
} |
proj/sprite.h | ||
---|---|---|
7 | 7 |
sprite_t* sprite_ctor(const xpm_map_t xpm); |
8 | 8 |
void sprite_dtor(sprite_t *p); |
9 | 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); |
|
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 |
void sprite_set_angle (sprite_t *p, double angle); |
|
14 |
void sprite_set_center(sprite_t *p, int u0, int v0); |
|
13 | 15 |
|
14 | 16 |
int sprite_get_h(const sprite_t *p); |
15 | 17 |
int sprite_get_w(const sprite_t *p); |
Also available in: Unified diff