-
Notifications
You must be signed in to change notification settings - Fork 0
/
spriteengine.c
120 lines (108 loc) · 5.49 KB
/
spriteengine.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
119
120
#include <stdlib.h>
#include <stdbool.h>
#include "spriteengine.h"
#include "libs/SMSlib.h"
entity *entitylist[MAX_ENTITIES];
unsigned char entityIndex = 0;
unsigned int entityVramPosition = 256;
void initEngine() {
int i;
for(i = 0;i < MAX_ENTITIES; i++) {
free(entitylist[i]);
entitylist[i] = 0;
}
entityIndex = 0;
}
entity* addEntity(character* characterData, unsigned char px, unsigned char py) {
entitylist[entityIndex] = malloc(sizeof(entity));
entitylist[entityIndex]->entityIndex = entityIndex;
entitylist[entityIndex]->currentAnimation = 0;
entitylist[entityIndex]->currentFrame = 0;
entitylist[entityIndex]->framecnt = 0;
entitylist[entityIndex]->has2ReloadTiles = true;
entitylist[entityIndex]->frameEnded = true;
entitylist[entityIndex]->animationEnded = true;
entitylist[entityIndex]->characterData = characterData;
entitylist[entityIndex]->px = px;
entitylist[entityIndex]->py = py;
entitylist[entityIndex]->direction = RIGHT_DIRECTION;
entitylist[entityIndex]->vramposition = entityVramPosition;
entityVramPosition = entityVramPosition + characterData->maxsprites;
entityIndex++;
return entitylist[entityIndex - 1];
}
void setAnimation(unsigned char entitynumber, int animationNumber) {
if(entitylist[entitynumber]->currentAnimation != animationNumber) {
entitylist[entitynumber]->has2ReloadTiles = true;
}
entitylist[entitynumber]->currentAnimation = animationNumber;
entitylist[entitynumber]->frameEnded = false;
entitylist[entitynumber]->animationEnded = false;
entitylist[entitynumber]->currentFrame = 0;
entitylist[entitynumber]->framecnt = 0;
}
void forceReload(unsigned char entitynumber) {
entitylist[entitynumber]->has2ReloadTiles = true;
}
bool isFrameEnded(unsigned char entitynumber) {
return entitylist[entitynumber]->frameEnded;
}
bool isAnimationEnded(unsigned char entitynumber) {
return entitylist[entitynumber]->animationEnded;
}
void setDirection(unsigned char entitynumber, unsigned char direction) {
entitylist[entitynumber]->direction = direction;
entitylist[entitynumber]->has2ReloadTiles = true;
}
void updateAnimations() {
int i;
for(i = 0;i < entityIndex; i++) {
entitylist[i]->framecnt++;
if(entitylist[i]->framecnt > entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].framelist[entitylist[i]->currentFrame].frame_time){
entitylist[i]->frameEnded = true;
if(entitylist[i]->currentFrame < entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].numframes - 1) { //tenir en compte els mirrored!
entitylist[i]->currentFrame++;
entitylist[i]->has2ReloadTiles = true;
} else {
entitylist[i]->animationEnded = true;
entitylist[i]->currentFrame = 0;
}
entitylist[i]->framecnt = 0;
}
}
}
void updateTiles() {
int i;
for(i = 0;i < entityIndex; i++) {
if(entitylist[i]->has2ReloadTiles) {
if(entitylist[i]->direction == RIGHT_DIRECTION) {
SMS_mapROMBank(entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].framelist[entitylist[i]->currentFrame].tile_bank);
SMS_loadTiles(entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].framelist[entitylist[i]->currentFrame].tiles, entitylist[i]->vramposition, entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].framelist[entitylist[i]->currentFrame].tile_bin_size);
} else {
SMS_mapROMBank(entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].framelist[entitylist[i]->currentFrame].tile_bank);
SMS_loadTiles(entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].mirroredframelist[entitylist[i]->currentFrame].tiles, entitylist[i]->vramposition, entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].mirroredframelist[entitylist[i]->currentFrame].tile_bin_size);
}
entitylist[i]->has2ReloadTiles = false;
}
}
}
void drawEntities() {
int i,j;
SMS_initSprites();
for(i = 0; i < entityIndex; i++) {
if(entitylist[i]->direction == RIGHT_DIRECTION) {
for(j = 0; j < entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].framelist[entitylist[i]->currentFrame].numsprites; j++){
SMS_addSprite (entitylist[i]->px + entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].framelist[entitylist[i]->currentFrame].tilelist[j].dpx,entitylist[i]->py + entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].framelist[entitylist[i]->currentFrame].tilelist[j].dpy,entitylist[i]->vramposition + entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].framelist[entitylist[i]->currentFrame].tilelist[j].tile);
}
} else {
for(j = 0; j < entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].mirroredframelist[entitylist[i]->currentFrame].numsprites; j++){
SMS_addSprite (entitylist[i]->px + entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].mirroredframelist[entitylist[i]->currentFrame].tilelist[j].dpx,entitylist[i]->py + entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].mirroredframelist[entitylist[i]->currentFrame].tilelist[j].dpy,entitylist[i]->vramposition + entitylist[i]->characterData->animationlist[entitylist[i]->currentAnimation].mirroredframelist[entitylist[i]->currentFrame].tilelist[j].tile);
}
}
}
SMS_finalizeSprites();
}
void move_entity(unsigned char entitynumber, signed char x, signed char y) {
entitylist[entitynumber]->px = entitylist[entitynumber]->px + x;
entitylist[entitynumber]->py = entitylist[entitynumber]->py + y;
}