removed redundant sprite copying in Player and Room, more concise collision detection code

This commit is contained in:
Iris Lightshard 2019-01-13 12:19:57 -08:00
parent 873c57cb13
commit 2cf26a1de9
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
9 changed files with 92 additions and 145 deletions

View file

@ -111,7 +111,7 @@ Mix_Chunk* loadSFX(char* filename)
void renderBackground() void renderBackground()
{ {
applySurface(0, 0, rightHere->bgImage, screen, NULL); applySurface(0, 0, rightHere->spriteSheet, screen, &(rightHere->clip));
animate(rightHere); animate(rightHere);
} }
@ -481,8 +481,7 @@ void mainmenu()
while (!select) while (!select)
{ {
timeStart(&fps); timeStart(&fps);
applySurface(0, 0, rightHere->bgImage, screen, NULL); renderBackground();
animate(rightHere);
while(SDL_PollEvent(&event)) while(SDL_PollEvent(&event))
{ {

27
Kaos.c
View file

@ -179,23 +179,21 @@ Kaos* newLook(Player* t, char d)
void runLook(Kaos* self) void runLook(Kaos* self)
{ {
Look* kSelf = self->kType; Look* kSelf = self->kType;
SDL_Rect playerClip = { 0,0,16,16};
switch(kSelf->dir) switch(kSelf->dir)
{ {
case 'n': case 'n':
playerClip.x = 96; kSelf->target->clip.x = 96;
break; break;
case 's': case 's':
playerClip.x = 32; kSelf->target->clip.x = 32;
break; break;
case 'e': case 'e':
playerClip.x = 64; kSelf->target->clip.x = 64;
break; break;
case 'w': case 'w':
default: kSelf->target->clip.x = 0;
break; break;
} }
changeSprite(kSelf->target, &playerClip);
} }
void deleteLook(Kaos* target) void deleteLook(Kaos* target)
@ -277,29 +275,24 @@ Kaos* newFaceEachother(Player* p1, Player* p2)
void runFaceEachother(Kaos* self) void runFaceEachother(Kaos* self)
{ {
FaceEachother* kSelf = self->kType; 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) 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) 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) if (kSelf->p1->point.y > kSelf->p2->point.y)
{ {
p1Clip.x = 96; kSelf->p1->clip.x = 96;
p2Clip.x = 32; kSelf->p2->clip.x = 32;
} }
if (kSelf->p1->point.y < kSelf->p2->point.y) if (kSelf->p1->point.y < kSelf->p2->point.y)
{ {
p1Clip.x = 32; kSelf->p1->clip.x = 32;
p2Clip.x = 96; kSelf->p2->clip.x = 96;
} }
changeSprite(kSelf->p1, &p1Clip);
changeSprite(kSelf->p2, &p2Clip);
} }
void deleteFaceEachother(Kaos* target) void deleteFaceEachother(Kaos* target)

View file

@ -22,44 +22,33 @@ typedef struct hyperKaos HyperKaos;
Player* newPlayer(char* filename, int a, int b) 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)); Player* self = malloc(sizeof(Player));
self->counter = 0; self->counter = 0;
self->spriteSheet = NULL; self->spriteSheet = NULL;
self->spriteSheet = loadImage(filename); 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.x = a;
self->point.y = b; self->point.y = b;
self->bearing.x = 0; self->bearing.x = 0;
self->bearing.y = 0; self->bearing.y = 0;
self->boundBox.x = self->point.x - 8; self->boundBox.x = self->point.x - 8;
self->boundBox.y = self->point.y - 8; self->boundBox.y = self->point.y - 8;
self->boundBox.h = 16; self->boundBox.h = 16;
self->boundBox.w = 16; self->boundBox.w = 16;
self->frontFaceBox.x = self->boundBox.x; self->frontFaceBox.x = self->boundBox.x;
self->frontFaceBox.y = self->boundBox.y + 16; self->frontFaceBox.y = self->boundBox.y + 16;
self->frontFaceBox.w = 16; self->frontFaceBox.w = 16;
self->frontFaceBox.h = 16; self->frontFaceBox.h = 16;
self->tombStone = 0; self->tombStone = 0;
return self; return self;
@ -68,7 +57,6 @@ Player* newPlayer(char* filename, int a, int b)
void killPlayer(Player* self) void killPlayer(Player* self)
{ {
SDL_FreeSurface(self->sprite);
SDL_FreeSurface(self->spriteSheet); SDL_FreeSurface(self->spriteSheet);
free(self); free(self);
} }
@ -123,7 +111,7 @@ void movePlayer(Player* self, Room* hereNow)
int kt = 0; int kt = 0;
int kf = 0; int kf = 0;
if (checkKCollision(rightHere, &(self->boundBox), rightHere->eventTriggers, &kf, &kt)) if (checkKCollision(rightHere, self, rightHere->eventTriggers, &kf, &kt, 0))
{ {
if (!kt) if (!kt)
{ {
@ -134,7 +122,7 @@ void movePlayer(Player* self, Room* hereNow)
kaosFlag = kf; kaosFlag = kf;
} }
} }
if (checkKCollision(rightHere, &(self->frontFaceBox), rightHere->eventTriggers, &kf, &kt)) if (checkKCollision(rightHere, self, rightHere->eventTriggers, &kf, &kt, 1))
{ {
if (kt) if (kt)
{ {
@ -148,7 +136,7 @@ void movePlayer(Player* self, Room* hereNow)
int outgoing = 0; int outgoing = 0;
if (!captive) if (!captive)
{ {
if (checkWCollision(rightHere, &(self->boundBox), rightHere->warps, &outgoing)) if (checkWCollision(rightHere, self, rightHere->warps, &outgoing))
{ {
nextChunk = rightHere->warps[outgoing]->chunk; nextChunk = rightHere->warps[outgoing]->chunk;
@ -176,76 +164,55 @@ void movePlayer(Player* self, Room* hereNow)
void drawPlayer(Player* self) 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); 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) void walkAnim(Player* self)
{ {
SDL_Rect playerClip;
playerClip.w = 16;
playerClip.h = 16;
playerClip.y = 0;
if (self->bearing.y > 0) if (self->bearing.y > 0)
{ {
if (self->counter == 0) if (self->counter == 0)
{ {
playerClip.x = 32; self->clip.x = 32;
changeSprite(self, &playerClip);
} }
if (self->counter == 4) if (self->counter == 4)
{ {
playerClip.x = 48; self->clip.x = 48;
changeSprite(self, &playerClip);
} }
} }
if (self->bearing.y < 0) if (self->bearing.y < 0)
{ {
if (self->counter == 0) if (self->counter == 0)
{ {
playerClip.x = 112; self->clip.x = 112;
changeSprite(self, &playerClip);
} }
if (self->counter == 4) if (self->counter == 4)
{ {
playerClip.x = 96; self->clip.x = 96;
changeSprite(self, &playerClip);
} }
} }
if (self->bearing.x > 0) if (self->bearing.x > 0)
{ {
if (self->counter == 0) if (self->counter == 0)
{ {
playerClip.x = 64; self->clip.x = 64;
changeSprite(self, &playerClip);
} }
if (self->counter == 4) if (self->counter == 4)
{ {
playerClip.x = 80; self->clip.x = 80;
changeSprite(self, &playerClip);
} }
} }
if (self->bearing.x < 0) if (self->bearing.x < 0)
{ {
if (self->counter == 0) if (self->counter == 0)
{ {
playerClip.x = 0; self->clip.x = 0;
changeSprite(self, &playerClip);
} }
if (self->counter == 4) if (self->counter == 4)
{ {
playerClip.x = 16; self->clip.x = 16;
changeSprite(self, &playerClip);
} }
} }
self->counter++; self->counter++;
@ -286,3 +253,14 @@ int playerIsInRect(Player* self, SDL_Rect* rect)
return 1; return 1;
else return 0; 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;
}

View file

@ -7,24 +7,26 @@ typedef struct
typedef struct player typedef struct player
{ {
int counter;
Coord point; Coord point;
Coord bearing; Coord bearing;
SDL_Rect boundBox; SDL_Rect boundBox;
SDL_Rect frontFaceBox; SDL_Rect frontFaceBox;
SDL_Surface* spriteSheet;
SDL_Surface* sprite;
int tombStone;
SDL_Surface* spriteSheet;
SDL_Rect clip;
int counter;
int tombStone;
} Player; } Player;
Player* newPlayer(char* filename, int a, int b); Player* newPlayer(char* filename, int a, int b);
void killPlayer(Player* target); void killPlayer(Player* target);
void movePlayer(Player* self, Room* rightHere); void movePlayer(Player* self, Room* rightHere);
void changeSprite(Player* self, SDL_Rect* clip);
void drawPlayer(Player* self); void drawPlayer(Player* self);
void walkAnim(Player* self); void walkAnim(Player* self);
int playerFaces(Player* self, char dir); 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
View file

@ -116,14 +116,11 @@ Room* newRoom(char* filename, int a)
self->numberOfSigils = 0; self->numberOfSigils = 0;
self->maxNumberOfSigils = 4; self->maxNumberOfSigils = 4;
SDL_Rect zeroRect; self->clip.x=0;
zeroRect.x=0; self->clip.y=0;
zeroRect.y=0; self->clip.w=320;
zeroRect.w=320; self->clip.h=180;
zeroRect.h=180;
self->spriteSheet = loadImage(filename); self->spriteSheet = loadImage(filename);
self->bgImage = loadImage("assets/img/backgrounds/loading.gif");
SDL_BlitSurface(self->spriteSheet, &zeroRect, self->bgImage, &zeroRect);
self->frameNo = 0; self->frameNo = 0;
return self; return self;
@ -163,9 +160,7 @@ void deleteRoom(Room* target)
target->sigils = NULL; target->sigils = NULL;
SDL_FreeSurface(target->spriteSheet); SDL_FreeSurface(target->spriteSheet);
SDL_FreeSurface(target->bgImage);
target->spriteSheet = NULL; target->spriteSheet = NULL;
target->bgImage = NULL;
free(target); free(target);
} }
@ -174,50 +169,32 @@ void deleteRoom(Room* target)
// room bg graphics // 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) void animate(Room* self)
{ {
SDL_Rect animClip;
animClip.w = 320;
animClip.h = 180;
animClip.x = 0;
if (self->frameNo == 0) if (self->frameNo == 0)
{ {
animClip.y = 180; self->clip.y = 180;
changeRSprite(self, &animClip);
} }
if (self->frameNo == 10) if (self->frameNo == 10)
{ {
animClip.y = 360; self->clip.y = 360;
changeRSprite(self, &animClip);
} }
if (self->frameNo == 20) if (self->frameNo == 20)
{ {
animClip.y = 540; self->clip.y = 540;
changeRSprite(self, &animClip);
} }
if (self->frameNo == 30) if (self->frameNo == 30)
{ {
animClip.y = 360; self->clip.y = 360;
changeRSprite(self, &animClip);
} }
if (self->frameNo == 40) if (self->frameNo == 40)
{ {
animClip.y = 180; self->clip.y = 180;
changeRSprite(self, &animClip);
} }
if (self->frameNo == 50) if (self->frameNo == 50)
{ {
animClip.y = 0; self->clip.y = 0;
changeRSprite(self, &animClip);
} }
self->frameNo++; self->frameNo++;
if (self->frameNo == 60) self->frameNo = 0; 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++) for (i = 1; i <= self->numberOfObstacles; i++)
{ {
if (!((*box)->tombStone) && playerIsInRect(player, &(*box)->domain)) 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; return 1;
} }
@ -244,18 +219,13 @@ int checkCollision(Room* self, Player* player, Obstruction** box)
return 0; 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 i;
int playerX = player->x + (player->w)/2;
int playerY = player->y + (player->h)/2;
for (i = 1; i <= self->numberOfWarps; i++) for (i = 1; i <= self->numberOfWarps; i++)
{ {
if ( if (!((*warpBoxes)->tombStone) && playerIsInRect(player, &(*warpBoxes)->location))
!((*warpBoxes)->tombStone) &&
(playerX >= (*warpBoxes)->location.x && playerX <= ((*warpBoxes)->location.x + (*warpBoxes)->location.w)
&& playerY >= (*warpBoxes)->location.y && playerY <= ((*warpBoxes)->location.y + (*warpBoxes)->location.h)) )
{ {
*whichWarp = i-1; *whichWarp = i-1;
return 1; return 1;
@ -265,18 +235,22 @@ int checkWCollision(Room* self, SDL_Rect* player, WarpZone** warpBoxes, int* whi
return 0; 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 i;
int playerX = player->x + (player->w)/2; int (*checkFunc)(Player*, SDL_Rect*);
int playerY = player->y + (player->h)/2;
switch (iType)
{
case 0:
checkFunc = &playerIsInRect; break;
default:
checkFunc = &playerFacesRect; break;
}
for (i = 1; i <= self->numberOfTriggers; i++) for (i = 1; i <= self->numberOfTriggers; i++)
{ {
if (!((*triggers)->tombStone) && checkFunc(player, &(*triggers)->domain))
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)->eventType == 0) if ((*triggers)->eventType == 0)
{ {

7
Room.h
View file

@ -39,7 +39,7 @@ void deleteWarpZone(WarpZone* target);
typedef struct room typedef struct room
{ {
SDL_Surface* spriteSheet; SDL_Surface* spriteSheet;
SDL_Surface* bgImage; SDL_Rect clip;
int frameNo; int frameNo;
Obstruction** obstacle; Obstruction** obstacle;
@ -75,12 +75,11 @@ void warpto(Room* destination);
Room* newRoom(char* filename, int a); Room* newRoom(char* filename, int a);
void deleteRoom(Room* target); void deleteRoom(Room* target);
void changeRSprite(Room* self, SDL_Rect* clip);
void animate(Room* self); void animate(Room* self);
int checkCollision(Room* self, Player* player, Obstruction** box); int checkCollision(Room* self, Player* player, Obstruction** box);
int checkWCollision(Room* self, SDL_Rect* player, WarpZone** warpBoxes, int* whichWarp); int checkWCollision(Room* self, Player* player, WarpZone** warpBoxes, int* whichWarp);
int checkKCollision(Room* self, SDL_Rect* player, HyperKaos** triggers, int* whichTrigger, int* triggerType); 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 addObstacle(Room* self, int x, int y, int w, int h);
void deleteObstacle(Room* self, int i); void deleteObstacle(Room* self, int i);

View file

@ -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_UP SDLK_w
#define DPAD_DOWN SDLK_s #define DPAD_DOWN SDLK_s
#define DPAD_LEFT SDLK_a #define DPAD_LEFT SDLK_a
#define DPAD_RIGHT SDLK_d #define DPAD_RIGHT SDLK_d
#define A_BUTTON SDLK_j #define A_BUTTON SDLK_j
#define B_BUTTON SDLK_k #define B_BUTTON SDLK_k
#define L_BUTTON SDLK_o #define L_BUTTON SDLK_o
#define R_BUTTON SDLK_p #define R_BUTTON SDLK_p
#define FS_BUTTON SDLK_f #define FS_BUTTON SDLK_f
#define PAUSE_BUTTON SDLK_q #define PAUSE_BUTTON SDLK_q
/* video scaling factor should be a positive integer */ // video scaling factor
#define SCALE_FACTOR 2 #define SCALE_FACTOR 2
// sound support
//#define SOUND_ON //#define SOUND_ON

View file

@ -18,12 +18,12 @@ extern SDL_Surface* screen;
extern SDL_Surface* window; extern SDL_Surface* window;
extern Timer fps; extern Timer fps;
extern Room* rightHere; extern Room* rightHere;
extern Player* hero; extern Player* hero;
extern HyperKaos** spellBook; extern HyperKaos** spellBook;
extern int kaosFlag; extern int kaosFlag;
extern int spellFlag; extern int spellFlag;
extern long long int savestate; extern long long int savestate;
extern int spellKnowledge[10]; extern int spellKnowledge[10];
extern int bookMark; extern int bookMark;
@ -37,10 +37,6 @@ extern SDL_Surface* selectArrow;
extern SDL_Surface* loadingTxt; extern SDL_Surface* loadingTxt;
extern SDL_Surface* spellGlyphs; extern SDL_Surface* spellGlyphs;
#ifdef SOUND_ON
extern Mix_Music* menuBGM;
#endif
extern TTF_Font* font; extern TTF_Font* font;
extern SDL_Color textColor; extern SDL_Color textColor;
@ -51,10 +47,12 @@ extern Room** mapData;
extern Room** mapBuffer; extern Room** mapBuffer;
extern TextBox** dialogueData; extern TextBox** dialogueData;
extern Kaos** kaosData;
extern Scene** theatre;
#ifdef SOUND_ON #ifdef SOUND_ON
extern Mix_Music** bgmData; extern Mix_Music** bgmData;
extern Mix_Chunk** sfxData; extern Mix_Chunk** sfxData;
extern Mix_Music* menuBGM;
#endif #endif
extern Kaos** kaosData;
extern Scene** theatre;

2
main.c
View file

@ -93,7 +93,7 @@ int main (int argc, char* args[])
renderForeground(); renderForeground();
renderHUD(); renderHUD();
frameAdvance(); frameAdvance();
kListen(&kaosFlag); kListen();
pager(); pager();
} }
} }