hyperkaos/Scene.c

125 lines
No EOL
2.9 KiB
C

#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_mixer.h>
#include "Scene.h"
#include "Timer.h"
#include "Engine.h"
typedef struct kaos Kaos;
typedef struct textBox TextBox;
typedef struct room Room;
typedef struct player Player;
#include "extern.h"
SLayer* newSLayer(char* filename, int x, int y, int h, int v)
{
SLayer* self = malloc(sizeof(SLayer));
self->sprite = loadImage(filename);
self->x = x;
self->y = y;
self->h = h;
self->v = v;
self->oX = x;
self->oY = y;
return self;
}
void deleteSLayer(SLayer* target)
{
SDL_FreeSurface(target->sprite);
free(target);
}
Scene* newScene(int in, int out, int time, SDL_Color incolor, SDL_Color outcolor)
{
Scene* self = malloc(sizeof(Scene));
self->sprites = NULL;
self->time = time;
self->nSprites = 0;
self->fade = (Transition){in, incolor, out, outcolor};
return self;
}
void buildScene(Scene* self, SLayer* sprite)
{
int i;
SLayer** temp;
if (self->nSprites)
temp = self->sprites;
self->sprites = malloc((self->nSprites+1)*sizeof(SLayer));
if (self->nSprites)
{
for (i = 0; i < self->nSprites; i++)
self->sprites[i] = temp[i];
}
self->sprites[self->nSprites++] = sprite;
}
void playScene(Scene* self)
{
int i, j;
int alphamod;
Uint32 icolor, ocolor;
SDL_Surface* fadeIn, *fadeOut;
fadeIn = loadImage("assets/img/backgrounds/blueroom.png");
fadeOut = fadeIn;
icolor = SDL_MapRGB(fadeIn->format, self->fade.incolor.r, self->fade.incolor.g, self->fade.incolor.b);
ocolor = SDL_MapRGB(fadeIn->format, self->fade.outcolor.r, self->fade.outcolor.g, self->fade.outcolor.b);
SDL_FillRect(fadeIn, NULL, icolor);
SDL_FillRect(fadeOut, NULL, ocolor);
for (i = 0; i < self->time; i++)
{
timeStart(fps);
for (j = 0; j < self->nSprites; j++)
{
applySurface(self->sprites[j]->x, self->sprites[j]->y, self->sprites[j]->sprite, screen, NULL);
self->sprites[j]->x += self->sprites[j]->h;
self->sprites[j]->y += self->sprites[j]->v;
}
if (i < self->fade.in)
{
alphamod = 255 - (int)(255*((float)i/self->fade.in));
SDL_SetAlpha(fadeIn, SDL_SRCALPHA|SDL_RLEACCEL, alphamod);
applySurface(0,0, fadeIn, screen, NULL);
}
if (self->time - i < self->fade.out)
{
alphamod = 255 - (int)(255*((float)(self->time - i)/self->fade.out));
SDL_SetAlpha(fadeOut, SDL_SRCALPHA|SDL_RLEACCEL, alphamod);
applySurface(0,0, fadeOut, screen, NULL);
}
SDL_Flip(screen);
timeDilation();
}
for (j = 0; j < self->nSprites; j++)
{
self->sprites[j]->x = self->sprites[j]->oX;
self->sprites[j]->y = self->sprites[j]->oY;
}
}
void deleteScene(Scene* target)
{
int i;
for (i = 0; i < target->nSprites; i++)
{
SDL_FreeSurface(target->sprites[i]->sprite);
free(target->sprites[i]);
}
free(target->sprites);
free(target);
}