all repos — hyperkaos @ ed6e55207cbb590f8e3cb231699f09089951fe17

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

Player.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdio.h>

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"

#include "enum.h"
#include "Engine.h"
#include "Player.h"
#include "Room.h"
#include "WorldData.h"

typedef struct TTF_Font TTF_Font;
typedef struct timer Timer;
typedef struct textBox TextBox;
typedef struct kaos Kaos;
#include "extern.h"

Player* newPlayer(char* filename, int a, int b)
{
  SDL_Rect originClip;
  originClip.x = 32;
  originClip.y = 0;
  originClip.h = 16;
  originClip.w = 16;
  Uint32 rmask, gmask, bmask, amask;

  #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
  #else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
  #endif

  Player* self = malloc(sizeof(Player));

  self->counter = 0;
  self->spriteSheet = NULL;
  self->spriteSheet = loadImage(filename);
  self->sprite = SDL_CreateRGBSurface(0,16,16,32,rmask,gmask,bmask,amask);
  applySurface(0, 0, self->spriteSheet, self->sprite, &originClip);
  self->point.x = a;
  self->point.y = b;
  self->bearing.x = 0;
  self->bearing.y = 0;
  self->boundBox.x = self->point.x - 8;
  self->boundBox.y = self->point.y - 8;
  self->boundBox.h = 16;
  self->boundBox.w = 16;
  self->frontFaceBox.x = self->boundBox.x;
  self->frontFaceBox.y = self->boundBox.y + 16;
  self->frontFaceBox.w = 16;
  self->frontFaceBox.h = 16;

  return self;
}

void killPlayer(Player* self)
{

  SDL_FreeSurface(self->sprite);
  SDL_FreeSurface(self->spriteSheet);
  free(self);
}

void movePlayer(Player* self, Room* hereNow)
{
  self->point.x += self->bearing.x;
  self->boundBox.x = self->point.x - 8;

 // obstacle collision

  if (checkCollision(rightHere, &(self->boundBox), rightHere->obstacle))
  {
    self->point.x -= self->bearing.x;
    self->boundBox.x = self->point.x - 8;
  }

  self->point.y += self->bearing.y;
  self->boundBox.y = self->point.y - 8;


  if (checkCollision(rightHere, &(self->boundBox), rightHere->obstacle))
  {
    self->point.y -= self->bearing.y;
    self->boundBox.y = self->point.y - 8;
  }

  // event trigger collision

  if (self->bearing.y > 0)
  {
    self->frontFaceBox.x = self->boundBox.x;
    self->frontFaceBox.y = self->boundBox.y + 16;
  }

  if (self->bearing.y < 0)
  {
    self->frontFaceBox.x = self->boundBox.x;
    self->frontFaceBox.y = self->boundBox.y - 16;
  }

  if (self->bearing.x > 0)
  {
    self->frontFaceBox.x = self->boundBox.x + 16;
    self->frontFaceBox.y = self->boundBox.y;
  }
  if (self->bearing.x < 0)
  {
    self->frontFaceBox.x = self->boundBox.x - 16;
    self->frontFaceBox.y = self->boundBox.y;
  }

  int kt = 0;
  int kf = 0;
  if (checkKCollision(rightHere, &(self->boundBox), rightHere->eventTriggers, &kf, &kt))
  {
    if (!kt)
    {
      kaosFlag = kf;
    }
    else if (actionbutton)
    {
      kaosFlag = kf;
    }
  }
  if (checkKCollision(rightHere, &(self->frontFaceBox), rightHere->eventTriggers, &kf, &kt))
  {
    if (kt)
    {
      if (actionbutton)
        kaosFlag = kf;
    }
  }

  // warp collision

  int outgoing = 0;
  if (!captive)
  {
  if (checkWCollision(rightHere, &(self->boundBox), rightHere->warps, &outgoing))
  {
    nextChunk = ((rightHere->warps) + outgoing)->chunk;

    if (nextChunk != thisChunk)
    {
      bufferData(((hereNow->warps) + outgoing)->chunk);
      warpto(mapBuffer[((rightHere->warps) + outgoing)->destination]);
    }
    else warpto(mapData[((rightHere->warps) + outgoing)->destination]);
    self->point.x = ((hereNow->warps) + outgoing)->x;
    self->point.y = ((hereNow->warps) + outgoing)->y;
  }
  }
  //stay onscreen

  if (self->point.x < 1) self->point.x = 1;
  if (self->point.x > 320 - 1) self->point.x = 320 - 1;
  if (self->point.y < 1) self->point.y = 1;
  if (self->point.y > 180) self->point.y = 180 - 1;
}

//
// graphics
//

void drawPlayer(Player* self)
{
  applySurface(self->point.x - 8, self->point.y - 8, self->sprite, screen, NULL);
}


void changeSprite(Player* self, SDL_Rect* clip)
{
  SDL_Rect zeroOffset;
  zeroOffset.x = 0;
  zeroOffset.y = 0;
  SDL_FillRect(self->sprite, NULL, 0x000000);
  SDL_BlitSurface(self->spriteSheet, clip, self->sprite, &zeroOffset);
}

void walkAnim(Player* self)
{
  SDL_Rect playerClip;
  playerClip.w = 16;
  playerClip.h = 16;
  playerClip.y = 0;
  if (self->bearing.y > 0)
  {
    if (self->counter == 0)
    {
      playerClip.x = 32;
      changeSprite(self, &playerClip);
    }
    if (self->counter == 4)
    {
      playerClip.x = 48;
      changeSprite(self, &playerClip);
    }
  }
  if (self->bearing.y < 0)
  {
    if (self->counter == 0)
    {
      playerClip.x = 112;
      changeSprite(self, &playerClip);
    }
    if (self->counter == 4)
    {
      playerClip.x = 96;
      changeSprite(self, &playerClip);
    }
  }
  if (self->bearing.x > 0)
  {
    if (self->counter == 0)
    {
      playerClip.x = 64;
      changeSprite(self, &playerClip);
    }
    if (self->counter == 4)
    {
      playerClip.x = 80;
      changeSprite(self, &playerClip);
    }
  }
  if (self->bearing.x < 0)
  {
    if (self->counter == 0)
    {
      playerClip.x = 0;
      changeSprite(self, &playerClip);
    }
    if (self->counter == 4)
    {
      playerClip.x = 16;
      changeSprite(self, &playerClip);
    }
  }
  self->counter++;
  if (self->counter == 8) self->counter = 0;
}