This repository was archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsprite.c
118 lines (104 loc) · 2.9 KB
/
sprite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdarg.h> // va_* macros are defined here
#include <stdint.h>
#include <stdio.h>
#include <minix/driver.h>
#include <lcom/lcf.h>
#include <sys/mman.h>
#include <stdio.h>
#include "sprite.h"
#include "graphic.h"
/** Creates a new sprite with pixmap "pic", with specified
* position (within the screen limits) and speed;
* Does not draw the sprite on the screen
* Returns NULL on invalid pixmap.
*/
Sprite *create_sprite(xpm_map_t xpm, int x, int y,int xspeed, int yspeed) {
//allocate space for the "object"
Sprite *sp = (Sprite *) malloc ( sizeof(Sprite));
if( sp == NULL )
return NULL;
// read the sprite pixmap
xpm_image_t img;
sp->map = xpm_load(xpm, XPM_INDEXED, &img);
sp->x = x;
sp->y = y;
sp->xspeed= xspeed;
sp->yspeed= yspeed;
sp->width = img.width;
sp->height= img.height;
if( sp->map == NULL ) {
free(sp);
return NULL;
}
return sp;
}
void destroy_sprite(Sprite *sp) {
if( sp == NULL )
return;
if( sp ->map )
free(sp->map);
free(sp);
sp = NULL; // XXX: pointer is passed by value
// should do this @ the caller
}
Sprite* SpriteInit(xpm_map_t xpm,uint16_t *xi,uint16_t *xf,uint16_t *yi,uint16_t *yf,int16_t speed,uint16_t *required){
Sprite * res;
int movementX;
int movementY;
if((*xi == *xf) && *yi == *yf){
res = create_sprite(xpm,*xi,*yi,0,0);
return res;
}
if (speed > 0){
if (*yi == *yf){
movementX = absO(speed)/(abs(*xf -*xi)/(*xf-*xi));
movementY = 0;
}
else{
movementY = absO(speed)/(abs((int)*yf -*yi)/(*yf-*yi));
movementX = 0;
}
*required = 0;
}
else{
if (*yi == *yf){
movementX = (abs(*xf -*xi)/(*xf-*xi));
movementY = 0;
}
else{
movementY = (abs(*yf -*yi)/(*yf-*yi));
movementX = 0;
}
*required = absO(speed);
}
res = create_sprite(xpm,*xi,*yi,movementX,movementY);
return res;
}
int animate(xpm_map_t xpm,Sprite *sp,uint16_t xf,uint16_t yf,uint16_t required,uint16_t *frame,vbe_mode_info_t *mode_info){
int speedX = sp->xspeed;
int speedY = sp->yspeed;
if(speedX < 0 || speedY < 0){
if(*frame < required) return 1;
}
//if((sp->x + speedX + sp->width < xBound) && (sp->y + speedY + sp->height < yBound))
sp->x+=speedX;
sp->y+=speedY;
if(speedX > 0 && (sp->x >= xf)){
sp->x = xf;
sp->y = yf;
}
else if(speedY > 0 && (sp->y >= yf )){
sp->x = xf;
sp->y = yf;
}
else if(speedX < 0 && (sp->x <= xf)) {
sp->x = xf;
sp->y = yf;
}
else if(speedY < 0 && (sp->y <= yf)) {
sp->x = xf;
sp->y = yf;
}
drawXpm(xpm,sp->x,sp->y);
return 0;
}