-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanim.c
227 lines (189 loc) · 6.63 KB
/
anim.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <stdio.h>
#include <windows.h>
#include "anim.h"
#include "shader.h"
#include "casc.h"
#include "grpsizes.h"
void drawAnim(anim* a, u32 x, u32 y, u32 frame, u32 playercolor, u32 multcolor, bool halluc){
u32 texx,texy,texw,texh;
float l,t,r,b;
if(frame >= a->frames) frame = 0;
// get coords
texx = a->framedata[frame].x;
texy = a->framedata[frame].y;
texw = a->framedata[frame].width;
texh = a->framedata[frame].height;
l = -(a->width/2.0) + a->framedata[frame].xoffs;
t = -(a->height/2.0) + a->framedata[frame].yoffs;
r = l + texw;
b = t + texh;
// shader params
glUseProgram(r_sprite.program);
glUniform1i(r_sprite.uniforms[U_SPRITE_SPRITETEX], 0);
glUniform1i(r_sprite.uniforms[U_SPRITE_TEAMCOLORTEX], 1);
glUniform1f(r_sprite.uniforms[U_SPRITE_HALLUCINATE], (float)halluc);
glUniform4f(r_sprite.uniforms[U_SPRITE_MULTIPLYCOLOR], GETRF(multcolor), GETGF(multcolor), GETBF(multcolor), GETAF(multcolor));
glUniform4f(r_sprite.uniforms[U_SPRITE_TEAMCOLOR], GETRF(playercolor), GETGF(playercolor), GETBF(playercolor), GETAF(playercolor));
glPushMatrix();
// bind & prep textures
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, a->tex.diffuse);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScalef(a->scWidth, a->scHeight, 1.0);
glTranslatef(0.5, -0.5, 0.0); // fix half-pixel offset
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, a->tex.teamcolor);
// translate & draw
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef((float)x, (float)y, 0.0);
glBegin(GL_QUADS);
glTexCoord2i(texx, texy);
glVertex2f(l, t);
glTexCoord2i(texx, texy+texh);
glVertex2f(l, b);
glTexCoord2i(texx+texw, texy+texh);
glVertex2f(r, b);
glTexCoord2i(texx+texw, texy);
glVertex2f(r, t);
glEnd();
glPopMatrix();
}
anim* loadAnim(const u8* path){
u32 i;
anim_header header;
anim_entry entry;
anim* a = NULL;
u32 texw,texh;
HANDLE cascfile = NULL;
u8* data = NULL;
u32 size = 0;
u32 sizeread = 0;
cascfile = openCascFile(path);
if(cascfile == NULL){
puts("Error: Could not load casc file");
return NULL;
}
readCascData(cascfile, &header, -1, sizeof(anim_header), &sizeread);
if(sizeread != sizeof(anim_header)) puts("anim_header");
readCascData(cascfile, &entry, -1, sizeof(anim_entry), &sizeread);
if(sizeread != sizeof(anim_entry)) puts("anim_entry");
if(header.filetype != INTSTR('ANIM')){
puts("Error: Invalid header");
printf("%08x == %08x?\n", header.filetype, INTSTR('ANIM'));
closeCascFile(cascfile);
return NULL;
}
if(header.version == 0x0101){
puts("Cannot load SD anim with this function.");
closeCascFile(cascfile);
return NULL;
}
if(header.entries != 1){
printf("WARNING: Some entries value other than 1? ??? %d\n", header.entries);
}
if(entry.frames == 0){
puts("Error: Frames == 0 ?!?!?!");
closeCascFile(cascfile);
return NULL;
}
a = malloc(sizeof(anim) + (entry.frames - 1)*sizeof(frame));
if(a == NULL){
puts("Error: Memory");
closeCascFile(cascfile);
return NULL;
}
a->frames = entry.frames;
a->width = entry.grpWidth;
a->height = entry.grpHeight;
texw = entry.imgs[0].texWidth;
texh = entry.imgs[0].texHeight;
a->scWidth = 1.0/(float)texw;
a->scHeight = 1.0/(float)texh;
memset(&a->tex, 0, sizeof(a->tex));
for(i = 0; i < header.layers; i++){
//printf("Layer: %s\n", header.layernames[i]);
if(entry.imgs[i].ptr != 0){
if(entry.imgs[i].texWidth != texw || entry.imgs[i].texHeight != texh){
printf("Warning: Texture size mismatch %s\n", header.layernames[i]);
}
if(strcmp(header.layernames[i], "diffuse") == 0){
data = readCascData(cascfile, NULL, entry.imgs[i].ptr, entry.imgs[i].size, &sizeread);
if(sizeread != entry.imgs[i].size) puts("diffuse");
a->tex.diffuse = loadDDS(data, entry.imgs[i].size);
if(a->tex.diffuse == 0) puts("Error loading diffuse texture");
}
if(strcmp(header.layernames[i], "teamcolor") == 0){
// TODO: 'BMP ' format
data = readCascData(cascfile, NULL, entry.imgs[i].ptr, entry.imgs[i].size, &sizeread);
if(sizeread != entry.imgs[i].size) puts("teamcolor");
a->tex.teamcolor = loadDDS(data, entry.imgs[i].size);
if(a->tex.teamcolor == 0) puts("Error loading teamcolor texture");
}
}
}
size = sizeof(frame) * entry.frames;
readCascData(cascfile, a->framedata, entry.frameptr, size, &sizeread);
if(sizeread != size) puts("framedata");
closeCascFile(cascfile);
return a;
}
anim* loadAnimSD(u8* data, u32 file_size, u32 index){
u32 i;
anim_header* header = (anim_header*)data;
u32 entryPtr = *(u32*)(data + sizeof(anim_header) + index*sizeof(u32));
anim_entry* entry = (anim_entry*)(data + entryPtr);
anim* a = NULL;
u32 texw,texh;
if(entry->frames == 0){
puts("Error: Frames == 0 ?!?!?!");
return NULL;
}
a = malloc(sizeof(anim) + (entry->frames - 1)*sizeof(frame));
if(a == NULL){
puts("Error: Memory");
return NULL;
}
a->frames = entry->frames;
a->width = grpsizes[index].w;
a->height = grpsizes[index].h;
texw = entry->imgs[0].texWidth;
texh = entry->imgs[0].texHeight;
a->scWidth = 1.0/(float)texw;
a->scHeight = 1.0/(float)texh;
memset(&a->tex, 0, sizeof(a->tex));
for(i = 0; i < header->layers; i++){
//printf("Layer: %s\n", header->layernames[i]);
if(entry->imgs[i].ptr != 0){
if(entry->imgs[i].texWidth != texw || entry->imgs[i].texHeight != texh){
printf("Warning: Texture size mismatch %s\n", header->layernames[i]);
}
if(strcmp(header->layernames[i], "diffuse") == 0){
a->tex.diffuse = loadDDS(data + entry->imgs[i].ptr, entry->imgs[i].size);
if(a->tex.diffuse == 0) puts("Error loading diffuse texture");
}
if(strcmp(header->layernames[i], "teamcolor") == 0){
switch(*(u32*)(data + entry->imgs[i].ptr)){
case INTSTR('DDS '):
a->tex.teamcolor = loadDDS(data + entry->imgs[i].ptr, entry->imgs[i].size);
break;
case INTSTR('BMP '):
a->tex.teamcolor = loadBMPLum(data + entry->imgs[i].ptr, entry->imgs[i].texWidth, entry->imgs[i].texHeight);
break;
default:
printf("Unknown format: %.4s\n", data + entry->imgs[i].ptr);
break;
}
if(a->tex.teamcolor == 0) puts("Error loading teamcolor texture");
}
}
}
memcpy(a->framedata, data + entry->frameptr, sizeof(frame) * entry->frames);
return a;
}
void unloadAnim(anim* a){
if(a == NULL) return;
glDeleteTextures(ANIM_TEX_COUNT, &(a->tex.diffuse));
free(a);
}