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
| #include <string.h>
#include <SDL_image.h>
#include "main.h"
SDL_Surface *InitSDL(int width, int height, const char WindowTitle[], const char IconFileName[])
{
SDL_Surface *screen = NULL;
if(SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr, "%s", SDL_GetError());
exit(-1);
}
if((screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
{
fprintf(stderr, "%s", SDL_GetError());
exit(-1);
}
SDL_WM_SetCaption(WindowTitle, IconFileName);
return screen;
}
void UpdateEvents(Input *in)
{
SDL_Event event;
in->mousebuttons[SDL_BUTTON_WHEELUP] = 0;
in->mousebuttons[SDL_BUTTON_WHEELDOWN] = 0;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
in->quit = 1;
break;
case SDL_KEYDOWN:
in->key[event.key.keysym.sym] = 1;
break;
case SDL_KEYUP:
in->key[event.key.keysym.sym] = 0;
break;
case SDL_MOUSEMOTION:
in->mousex = event.motion.x;
in->mousey = event.motion.y;
in->mousexrel = event.motion.xrel;
in->mouseyrel = event.motion.yrel;
break;
case SDL_MOUSEBUTTONDOWN:
in->mousebuttons[event.button.button] = 0;
break;
case SDL_MOUSEBUTTONUP:
if(event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN)
in->mousebuttons[event.button.button] = 0;
break;
default:
break;
}
}
}
SDL_Surface *LoadAdaptedIMG(const char IMG_FileName[])
{
SDL_Surface *IMG = NULL;
SDL_Surface *IMG_Result = NULL;
if((IMG = IMG_Load(IMG_FileName)) == NULL)
{
fprintf(stderr, "%s", IMG_GetError());
exit(-1);
}
IMG_Result = SDL_DisplayFormat(IMG);
SDL_FreeSurface(IMG);
return IMG_Result;
}
void LoadCharset(Player *P, FILE *F)
{
int i, j, nbFrame;
char charsetName[32] = {0};
fscanf(F, "%s", charsetName);
P->charset = LoadAdaptedIMG(charsetName);
fscanf(F, "%d %d", &P->nbFramesCharsetX, &P->nbFramesCharsetY);
P->Animations.R = malloc(P->nbFramesCharsetX*P->nbFramesCharsetY*sizeof(SDL_Rect));
P->width = P->charset->w / P->nbFramesCharsetX;
P->height = P->charset->h / P->nbFramesCharsetY;
for(j = 0, nbFrame = 0; j < P->nbFramesCharsetY; j++)
{
for(i = 0; i < P->nbFramesCharsetX; i++, nbFrame++)
{
P->Animations.R[nbFrame].x = i*P->width;
P->Animations.R[nbFrame].y = j*P->height;
P->Animations.R[nbFrame].w = P->width;
P->Animations.R[nbFrame].h = P->height;
}
}
}
void GetAnimationInfo(Player *P, FILE *F)
{
int i;
fscanf(F, "%d %d", &P->Animations.nbAnimations, &P->Animations.interval);
P->Animations.List = malloc(P->Animations.nbAnimations*sizeof(AnimationList));
for(i = 0; i <= P->Animations.nbAnimations; i++)
P->Animations.List[i].SequenceTable = malloc(P->Animations.nbAnimations*sizeof(Sequence));
}
void GetAnimationSequences(Player *P, FILE *F)
{
int i, j;
int curentAnim;
for(j = 0; j < P->nbFramesCharsetY; j++)
{
for(i = 0; i < P->nbFramesCharsetX; i++)
{
fscanf(F, "%d", &curentAnim);
if(curentAnim >= P->nbFramesCharsetX*P->nbFramesCharsetY)
{
fprintf(stderr, "Error animation number is out of limit : %d", curentAnim);
exit(-1);
}
P->Animations.List[j].SequenceTable[i] = curentAnim;
fprintf(stderr, "%d ", curentAnim);
}
fprintf(stderr, "\n");
}
}
Player LoadPlayer(const char PlayerFileName[])
{
FILE *F = NULL;
Player P;
char tag[32] = {0};
if((F = fopen(PlayerFileName, "r")) == NULL)
{
fprintf(stderr, "Failled to open %s", PlayerFileName);
exit(-1);
}
do
{
fscanf(F, "%s", tag);
if(strstr(tag, "#charset_info"))
LoadCharset(&P, F);
if(strstr(tag, "#animation_info"))
GetAnimationInfo(&P, F);
if(strstr(tag, "#animation_sequences"))
GetAnimationSequences(&P, F);
}while(strstr(tag, "#end") != NULL);
fclose(F);
return P;
}
int main(int argc, char* argv[])
{
int i;
Input in;
SDL_Surface *screen = InitSDL(640, 480, "Animation", NULL);
Player P;
P = LoadPlayer("anim.txt");
memset(&in, 0, sizeof(in));
SDL_SetColorKey(P.charset, SDL_SRCCOLORKEY, SDL_MapRGB(P.charset->format, 255, 0, 255));
while(!in.quit && !in.key[SDLK_ESCAPE])
{
UpdateEvents(&in);
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
SDL_BlitSurface(P.charset, &P.Animations.R[0], screen, NULL);
SDL_Flip(screen);
SDL_Delay(5);
}
free(P.Animations.R);
for(i = 0; i < P.Animations.nbAnimations; i++)
free(P.Animations.List[i].SequenceTable);
free(P.Animations.List);
SDL_FreeSurface(P.charset);
SDL_Quit();
return 0;
} |
Partager