diff --git a/Scene.c b/Scene.c new file mode 100644 index 0000000..0265521 --- /dev/null +++ b/Scene.c @@ -0,0 +1,125 @@ +#include + +#include +#include +#include +#include + +#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); +} \ No newline at end of file diff --git a/Scene.h b/Scene.h new file mode 100644 index 0000000..d1ce9a9 --- /dev/null +++ b/Scene.h @@ -0,0 +1,30 @@ +typedef struct sLayer +{ + SDL_Surface* sprite; + int x,y,h,v; + int oX, oY; +} SLayer; + +typedef struct transition +{ + int in; + SDL_Color incolor; + int out; + SDL_Color outcolor; +} Transition; + +typedef struct scene +{ + SLayer** sprites; + int nSprites; + int time; + Transition fade; +} Scene; + +SLayer* newSLayer(char* filename, int x, int y, int h, int v); +void deleteSLayer(SLayer* target); + +Scene* newScene(int in, int out, int time, SDL_Color incolor, SDL_Color outcolor); +void buildScene(Scene* self, SLayer* sprite); +void playScene(Scene* self); +void deleteScene(Scene* target); \ No newline at end of file diff --git a/assets/img/backgrounds/presents.png b/assets/img/backgrounds/presents.png new file mode 100644 index 0000000..390763c Binary files /dev/null and b/assets/img/backgrounds/presents.png differ diff --git a/main.c b/main.c index cea0ddb..593bf86 100644 --- a/main.c +++ b/main.c @@ -12,6 +12,7 @@ #include "Kaos.h" #include "HyperKaos.h" #include "WorldData.h" +#include "Scene.h" /*const int SCREEN_WIDTH = 320; const int SCREEN_HEIGHT = 180; @@ -68,6 +69,17 @@ int main (int argc, char* args[]) return 1; } + SDL_Color black = {0,0,0}; + SLayer* presents = newSLayer("assets/img/backgrounds/presents.png", 0,0,0,0); + SLayer* menuTransition = newSLayer("assets/img/backgrounds/mainmenu.png",0,0,0,0); + Scene* intro = newScene(30,30, 90, black, black); + Scene* transition = newScene(30,0,30, black, black); + buildScene(intro, presents); + buildScene(transition, menuTransition); + playScene(intro); + playScene(transition); + deleteScene(intro); + deleteScene(transition); // main game loop while (!quit)