all repos — hyperkaos @ 8b1e4cafdf8b83c958d3b5c2dd5a9543a35f318b

lightweight modular puzzle/adventure game engine in C with SDL 1.2

Scene.c (raw)

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
#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(SDL_Surface* img, int x, int y, int h, int v, int alpha)
{
  SLayer* self = malloc(sizeof(SLayer));

  self->sprite = img;
  self->x = x;
  self->y = y;
  self->h = h;
  self->v = v;

  self->oX = x;
  self->oY = y;

  if (alpha != 255)
    SDL_SetAlpha(self->sprite, SDL_SRCALPHA|SDL_RLEACCEL, alpha);

  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;
  self->nSprites++;
}

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);
}