removed redundant sprite copying in Player and Room, more concise collision detection code
This commit is contained in:
parent
873c57cb13
commit
2cf26a1de9
9 changed files with 92 additions and 145 deletions
5
Engine.c
5
Engine.c
|
@ -111,7 +111,7 @@ Mix_Chunk* loadSFX(char* filename)
|
|||
|
||||
void renderBackground()
|
||||
{
|
||||
applySurface(0, 0, rightHere->bgImage, screen, NULL);
|
||||
applySurface(0, 0, rightHere->spriteSheet, screen, &(rightHere->clip));
|
||||
animate(rightHere);
|
||||
}
|
||||
|
||||
|
@ -481,8 +481,7 @@ void mainmenu()
|
|||
while (!select)
|
||||
{
|
||||
timeStart(&fps);
|
||||
applySurface(0, 0, rightHere->bgImage, screen, NULL);
|
||||
animate(rightHere);
|
||||
renderBackground();
|
||||
|
||||
while(SDL_PollEvent(&event))
|
||||
{
|
||||
|
|
27
Kaos.c
27
Kaos.c
|
@ -179,23 +179,21 @@ Kaos* newLook(Player* t, char d)
|
|||
void runLook(Kaos* self)
|
||||
{
|
||||
Look* kSelf = self->kType;
|
||||
SDL_Rect playerClip = { 0,0,16,16};
|
||||
switch(kSelf->dir)
|
||||
{
|
||||
case 'n':
|
||||
playerClip.x = 96;
|
||||
kSelf->target->clip.x = 96;
|
||||
break;
|
||||
case 's':
|
||||
playerClip.x = 32;
|
||||
kSelf->target->clip.x = 32;
|
||||
break;
|
||||
case 'e':
|
||||
playerClip.x = 64;
|
||||
kSelf->target->clip.x = 64;
|
||||
break;
|
||||
case 'w':
|
||||
default:
|
||||
kSelf->target->clip.x = 0;
|
||||
break;
|
||||
}
|
||||
changeSprite(kSelf->target, &playerClip);
|
||||
}
|
||||
|
||||
void deleteLook(Kaos* target)
|
||||
|
@ -277,29 +275,24 @@ Kaos* newFaceEachother(Player* p1, Player* p2)
|
|||
void runFaceEachother(Kaos* self)
|
||||
{
|
||||
FaceEachother* kSelf = self->kType;
|
||||
SDL_Rect p1Clip = {0,0,16,16};
|
||||
SDL_Rect p2Clip = {0,0,16,16};
|
||||
|
||||
if (kSelf->p1->point.x > kSelf->p2->point.x)
|
||||
p2Clip.x = 64;
|
||||
kSelf->p2->clip.x = 64;
|
||||
|
||||
if (kSelf->p1->point.x < kSelf->p2->point.x)
|
||||
p1Clip.x = 64;
|
||||
kSelf->p1->clip.x = 64;
|
||||
|
||||
if (kSelf->p1->point.y > kSelf->p2->point.y)
|
||||
{
|
||||
p1Clip.x = 96;
|
||||
p2Clip.x = 32;
|
||||
kSelf->p1->clip.x = 96;
|
||||
kSelf->p2->clip.x = 32;
|
||||
}
|
||||
|
||||
if (kSelf->p1->point.y < kSelf->p2->point.y)
|
||||
{
|
||||
p1Clip.x = 32;
|
||||
p2Clip.x = 96;
|
||||
kSelf->p1->clip.x = 32;
|
||||
kSelf->p2->clip.x = 96;
|
||||
}
|
||||
|
||||
changeSprite(kSelf->p1, &p1Clip);
|
||||
changeSprite(kSelf->p2, &p2Clip);
|
||||
}
|
||||
|
||||
void deleteFaceEachother(Kaos* target)
|
||||
|
|
88
Player.c
88
Player.c
|
@ -22,44 +22,33 @@ typedef struct hyperKaos HyperKaos;
|
|||
|
||||
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->clip.x = 32;
|
||||
self->clip.y = 0;
|
||||
self->clip.h = 16;
|
||||
self->clip.w = 16;
|
||||
|
||||
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;
|
||||
|
||||
self->tombStone = 0;
|
||||
|
||||
return self;
|
||||
|
@ -68,7 +57,6 @@ Player* newPlayer(char* filename, int a, int b)
|
|||
void killPlayer(Player* self)
|
||||
{
|
||||
|
||||
SDL_FreeSurface(self->sprite);
|
||||
SDL_FreeSurface(self->spriteSheet);
|
||||
free(self);
|
||||
}
|
||||
|
@ -123,7 +111,7 @@ void movePlayer(Player* self, Room* hereNow)
|
|||
|
||||
int kt = 0;
|
||||
int kf = 0;
|
||||
if (checkKCollision(rightHere, &(self->boundBox), rightHere->eventTriggers, &kf, &kt))
|
||||
if (checkKCollision(rightHere, self, rightHere->eventTriggers, &kf, &kt, 0))
|
||||
{
|
||||
if (!kt)
|
||||
{
|
||||
|
@ -134,7 +122,7 @@ void movePlayer(Player* self, Room* hereNow)
|
|||
kaosFlag = kf;
|
||||
}
|
||||
}
|
||||
if (checkKCollision(rightHere, &(self->frontFaceBox), rightHere->eventTriggers, &kf, &kt))
|
||||
if (checkKCollision(rightHere, self, rightHere->eventTriggers, &kf, &kt, 1))
|
||||
{
|
||||
if (kt)
|
||||
{
|
||||
|
@ -148,7 +136,7 @@ void movePlayer(Player* self, Room* hereNow)
|
|||
int outgoing = 0;
|
||||
if (!captive)
|
||||
{
|
||||
if (checkWCollision(rightHere, &(self->boundBox), rightHere->warps, &outgoing))
|
||||
if (checkWCollision(rightHere, self, rightHere->warps, &outgoing))
|
||||
{
|
||||
nextChunk = rightHere->warps[outgoing]->chunk;
|
||||
|
||||
|
@ -176,76 +164,55 @@ void movePlayer(Player* self, Room* hereNow)
|
|||
|
||||
void drawPlayer(Player* self)
|
||||
{
|
||||
applySurface(self->point.x - 8, self->point.y - 8, self->sprite, screen, NULL);
|
||||
applySurface(self->point.x - 8, self->point.y - 8, self->spriteSheet, screen, &(self->clip));
|
||||
walkAnim(self);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
self->clip.x = 32;
|
||||
}
|
||||
if (self->counter == 4)
|
||||
{
|
||||
playerClip.x = 48;
|
||||
changeSprite(self, &playerClip);
|
||||
self->clip.x = 48;
|
||||
}
|
||||
}
|
||||
if (self->bearing.y < 0)
|
||||
{
|
||||
if (self->counter == 0)
|
||||
{
|
||||
playerClip.x = 112;
|
||||
changeSprite(self, &playerClip);
|
||||
self->clip.x = 112;
|
||||
}
|
||||
if (self->counter == 4)
|
||||
{
|
||||
playerClip.x = 96;
|
||||
changeSprite(self, &playerClip);
|
||||
self->clip.x = 96;
|
||||
}
|
||||
}
|
||||
if (self->bearing.x > 0)
|
||||
{
|
||||
if (self->counter == 0)
|
||||
{
|
||||
playerClip.x = 64;
|
||||
changeSprite(self, &playerClip);
|
||||
self->clip.x = 64;
|
||||
}
|
||||
if (self->counter == 4)
|
||||
{
|
||||
playerClip.x = 80;
|
||||
changeSprite(self, &playerClip);
|
||||
self->clip.x = 80;
|
||||
}
|
||||
}
|
||||
if (self->bearing.x < 0)
|
||||
{
|
||||
if (self->counter == 0)
|
||||
{
|
||||
playerClip.x = 0;
|
||||
changeSprite(self, &playerClip);
|
||||
self->clip.x = 0;
|
||||
}
|
||||
if (self->counter == 4)
|
||||
{
|
||||
playerClip.x = 16;
|
||||
changeSprite(self, &playerClip);
|
||||
self->clip.x = 16;
|
||||
}
|
||||
}
|
||||
self->counter++;
|
||||
|
@ -285,4 +252,15 @@ int playerIsInRect(Player* self, SDL_Rect* rect)
|
|||
&& (playerY >= rect->y && playerY <= rect->y + rect->h))
|
||||
return 1;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
int playerFacesRect(Player* self, SDL_Rect* rect)
|
||||
{
|
||||
SDL_Rect* pBox = &(self->frontFaceBox);
|
||||
int playerX = pBox->x + (pBox->w)/2;
|
||||
int playerY = pBox->y + (pBox->h)/2;
|
||||
if ((playerX >= rect->x && playerX <= rect->x + rect->w)
|
||||
&& (playerY >= rect->y && playerY <= rect->y + rect->h))
|
||||
return 1;
|
||||
else return 0;
|
||||
}
|
14
Player.h
14
Player.h
|
@ -7,24 +7,26 @@ typedef struct
|
|||
|
||||
typedef struct player
|
||||
{
|
||||
int counter;
|
||||
Coord point;
|
||||
Coord bearing;
|
||||
|
||||
SDL_Rect boundBox;
|
||||
SDL_Rect frontFaceBox;
|
||||
SDL_Surface* spriteSheet;
|
||||
SDL_Surface* sprite;
|
||||
int tombStone;
|
||||
|
||||
SDL_Surface* spriteSheet;
|
||||
SDL_Rect clip;
|
||||
int counter;
|
||||
|
||||
int tombStone;
|
||||
} Player;
|
||||
|
||||
Player* newPlayer(char* filename, int a, int b);
|
||||
void killPlayer(Player* target);
|
||||
|
||||
void movePlayer(Player* self, Room* rightHere);
|
||||
void changeSprite(Player* self, SDL_Rect* clip);
|
||||
void drawPlayer(Player* self);
|
||||
void walkAnim(Player* self);
|
||||
|
||||
int playerFaces(Player* self, char dir);
|
||||
int playerIsInRect(Player* self, SDL_Rect* rect);
|
||||
int playerIsInRect(Player* self, SDL_Rect* rect);
|
||||
int playerFacesRect(Player* self, SDL_Rect* rect);
|
72
Room.c
72
Room.c
|
@ -116,14 +116,11 @@ Room* newRoom(char* filename, int a)
|
|||
self->numberOfSigils = 0;
|
||||
self->maxNumberOfSigils = 4;
|
||||
|
||||
SDL_Rect zeroRect;
|
||||
zeroRect.x=0;
|
||||
zeroRect.y=0;
|
||||
zeroRect.w=320;
|
||||
zeroRect.h=180;
|
||||
self->clip.x=0;
|
||||
self->clip.y=0;
|
||||
self->clip.w=320;
|
||||
self->clip.h=180;
|
||||
self->spriteSheet = loadImage(filename);
|
||||
self->bgImage = loadImage("assets/img/backgrounds/loading.gif");
|
||||
SDL_BlitSurface(self->spriteSheet, &zeroRect, self->bgImage, &zeroRect);
|
||||
self->frameNo = 0;
|
||||
|
||||
return self;
|
||||
|
@ -163,9 +160,7 @@ void deleteRoom(Room* target)
|
|||
target->sigils = NULL;
|
||||
|
||||
SDL_FreeSurface(target->spriteSheet);
|
||||
SDL_FreeSurface(target->bgImage);
|
||||
target->spriteSheet = NULL;
|
||||
target->bgImage = NULL;
|
||||
|
||||
free(target);
|
||||
}
|
||||
|
@ -174,50 +169,32 @@ void deleteRoom(Room* target)
|
|||
// room bg graphics
|
||||
//
|
||||
|
||||
void changeRSprite(Room* self, SDL_Rect* clip)
|
||||
{
|
||||
SDL_Rect zeroOffset;
|
||||
zeroOffset.x = 0;
|
||||
zeroOffset.y = 0;
|
||||
SDL_BlitSurface(self->spriteSheet, clip, self->bgImage, &zeroOffset);
|
||||
}
|
||||
|
||||
void animate(Room* self)
|
||||
{
|
||||
SDL_Rect animClip;
|
||||
animClip.w = 320;
|
||||
animClip.h = 180;
|
||||
animClip.x = 0;
|
||||
|
||||
if (self->frameNo == 0)
|
||||
{
|
||||
animClip.y = 180;
|
||||
changeRSprite(self, &animClip);
|
||||
self->clip.y = 180;
|
||||
}
|
||||
if (self->frameNo == 10)
|
||||
{
|
||||
animClip.y = 360;
|
||||
changeRSprite(self, &animClip);
|
||||
self->clip.y = 360;
|
||||
}
|
||||
if (self->frameNo == 20)
|
||||
{
|
||||
animClip.y = 540;
|
||||
changeRSprite(self, &animClip);
|
||||
self->clip.y = 540;
|
||||
}
|
||||
if (self->frameNo == 30)
|
||||
{
|
||||
animClip.y = 360;
|
||||
changeRSprite(self, &animClip);
|
||||
self->clip.y = 360;
|
||||
}
|
||||
if (self->frameNo == 40)
|
||||
{
|
||||
animClip.y = 180;
|
||||
changeRSprite(self, &animClip);
|
||||
self->clip.y = 180;
|
||||
}
|
||||
if (self->frameNo == 50)
|
||||
{
|
||||
animClip.y = 0;
|
||||
changeRSprite(self, &animClip);
|
||||
self->clip.y = 0;
|
||||
}
|
||||
self->frameNo++;
|
||||
if (self->frameNo == 60) self->frameNo = 0;
|
||||
|
@ -234,8 +211,6 @@ int checkCollision(Room* self, Player* player, Obstruction** box)
|
|||
for (i = 1; i <= self->numberOfObstacles; i++)
|
||||
{
|
||||
if (!((*box)->tombStone) && playerIsInRect(player, &(*box)->domain))
|
||||
// if ( (playerX >= box->x && playerX <= box->x + box->w)
|
||||
// && (playerY >= box->y && playerY <= box->y + box->h) )
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
@ -244,18 +219,13 @@ int checkCollision(Room* self, Player* player, Obstruction** box)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int checkWCollision(Room* self, SDL_Rect* player, WarpZone** warpBoxes, int* whichWarp)
|
||||
int checkWCollision(Room* self, Player* player, WarpZone** warpBoxes, int* whichWarp)
|
||||
{
|
||||
int i;
|
||||
int playerX = player->x + (player->w)/2;
|
||||
int playerY = player->y + (player->h)/2;
|
||||
|
||||
for (i = 1; i <= self->numberOfWarps; i++)
|
||||
{
|
||||
if (
|
||||
!((*warpBoxes)->tombStone) &&
|
||||
(playerX >= (*warpBoxes)->location.x && playerX <= ((*warpBoxes)->location.x + (*warpBoxes)->location.w)
|
||||
&& playerY >= (*warpBoxes)->location.y && playerY <= ((*warpBoxes)->location.y + (*warpBoxes)->location.h)) )
|
||||
if (!((*warpBoxes)->tombStone) && playerIsInRect(player, &(*warpBoxes)->location))
|
||||
{
|
||||
*whichWarp = i-1;
|
||||
return 1;
|
||||
|
@ -265,18 +235,22 @@ int checkWCollision(Room* self, SDL_Rect* player, WarpZone** warpBoxes, int* whi
|
|||
return 0;
|
||||
}
|
||||
|
||||
int checkKCollision(Room* self, SDL_Rect* player, HyperKaos** triggers, int* whichTrigger, int* kType)
|
||||
int checkKCollision(Room* self, Player* player, HyperKaos** triggers, int* whichTrigger, int* kType, int iType)
|
||||
{
|
||||
int i;
|
||||
int playerX = player->x + (player->w)/2;
|
||||
int playerY = player->y + (player->h)/2;
|
||||
int (*checkFunc)(Player*, SDL_Rect*);
|
||||
|
||||
switch (iType)
|
||||
{
|
||||
case 0:
|
||||
checkFunc = &playerIsInRect; break;
|
||||
default:
|
||||
checkFunc = &playerFacesRect; break;
|
||||
}
|
||||
|
||||
for (i = 1; i <= self->numberOfTriggers; i++)
|
||||
{
|
||||
|
||||
if (!((*triggers)->tombStone) &&
|
||||
(playerX > ((*triggers)->domain).x && playerX < ((*triggers)->domain).x + ((*triggers)->domain).w)
|
||||
&& (playerY > ((*triggers)->domain).y && playerY < ((*triggers)->domain).y + ((*triggers)->domain).h) )
|
||||
if (!((*triggers)->tombStone) && checkFunc(player, &(*triggers)->domain))
|
||||
{
|
||||
if ((*triggers)->eventType == 0)
|
||||
{
|
||||
|
|
7
Room.h
7
Room.h
|
@ -39,7 +39,7 @@ void deleteWarpZone(WarpZone* target);
|
|||
typedef struct room
|
||||
{
|
||||
SDL_Surface* spriteSheet;
|
||||
SDL_Surface* bgImage;
|
||||
SDL_Rect clip;
|
||||
int frameNo;
|
||||
|
||||
Obstruction** obstacle;
|
||||
|
@ -75,12 +75,11 @@ void warpto(Room* destination);
|
|||
Room* newRoom(char* filename, int a);
|
||||
void deleteRoom(Room* target);
|
||||
|
||||
void changeRSprite(Room* self, SDL_Rect* clip);
|
||||
void animate(Room* self);
|
||||
|
||||
int checkCollision(Room* self, Player* player, Obstruction** box);
|
||||
int checkWCollision(Room* self, SDL_Rect* player, WarpZone** warpBoxes, int* whichWarp);
|
||||
int checkKCollision(Room* self, SDL_Rect* player, HyperKaos** triggers, int* whichTrigger, int* triggerType);
|
||||
int checkWCollision(Room* self, Player* player, WarpZone** warpBoxes, int* whichWarp);
|
||||
int checkKCollision(Room* self, Player* player, HyperKaos** triggers, int* whichTrigger, int* triggerType, int iType);
|
||||
|
||||
void addObstacle(Room* self, int x, int y, int w, int h);
|
||||
void deleteObstacle(Room* self, int i);
|
||||
|
|
10
config.h
10
config.h
|
@ -1,17 +1,21 @@
|
|||
/* see https://sdl.beuc.net/sdl.wiki/SDLKey for keysyms */
|
||||
|
||||
// key configuration
|
||||
// see https://sdl.beuc.net/sdl.wiki/SDLKey for keysyms
|
||||
// for joypad support use a tool like antimicro
|
||||
#define DPAD_UP SDLK_w
|
||||
#define DPAD_DOWN SDLK_s
|
||||
#define DPAD_LEFT SDLK_a
|
||||
#define DPAD_RIGHT SDLK_d
|
||||
|
||||
#define A_BUTTON SDLK_j
|
||||
#define B_BUTTON SDLK_k
|
||||
#define L_BUTTON SDLK_o
|
||||
#define R_BUTTON SDLK_p
|
||||
|
||||
#define FS_BUTTON SDLK_f
|
||||
#define PAUSE_BUTTON SDLK_q
|
||||
|
||||
/* video scaling factor should be a positive integer */
|
||||
// video scaling factor
|
||||
#define SCALE_FACTOR 2
|
||||
|
||||
// sound support
|
||||
//#define SOUND_ON
|
||||
|
|
12
extern.h
12
extern.h
|
@ -18,12 +18,12 @@ extern SDL_Surface* screen;
|
|||
extern SDL_Surface* window;
|
||||
extern Timer fps;
|
||||
extern Room* rightHere;
|
||||
|
||||
extern Player* hero;
|
||||
extern HyperKaos** spellBook;
|
||||
|
||||
extern int kaosFlag;
|
||||
extern int spellFlag;
|
||||
|
||||
extern long long int savestate;
|
||||
extern int spellKnowledge[10];
|
||||
extern int bookMark;
|
||||
|
@ -37,10 +37,6 @@ extern SDL_Surface* selectArrow;
|
|||
extern SDL_Surface* loadingTxt;
|
||||
extern SDL_Surface* spellGlyphs;
|
||||
|
||||
#ifdef SOUND_ON
|
||||
extern Mix_Music* menuBGM;
|
||||
#endif
|
||||
|
||||
extern TTF_Font* font;
|
||||
extern SDL_Color textColor;
|
||||
|
||||
|
@ -51,10 +47,12 @@ extern Room** mapData;
|
|||
extern Room** mapBuffer;
|
||||
extern TextBox** dialogueData;
|
||||
|
||||
extern Kaos** kaosData;
|
||||
extern Scene** theatre;
|
||||
|
||||
#ifdef SOUND_ON
|
||||
extern Mix_Music** bgmData;
|
||||
extern Mix_Chunk** sfxData;
|
||||
extern Mix_Music* menuBGM;
|
||||
#endif
|
||||
|
||||
extern Kaos** kaosData;
|
||||
extern Scene** theatre;
|
||||
|
|
2
main.c
2
main.c
|
@ -93,7 +93,7 @@ int main (int argc, char* args[])
|
|||
renderForeground();
|
||||
renderHUD();
|
||||
frameAdvance();
|
||||
kListen(&kaosFlag);
|
||||
kListen();
|
||||
pager();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue