From ff46718f04710fcfa7171237ba4be4bc094b7861 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Thu, 20 Dec 2018 19:14:26 -0800 Subject: [PATCH] added tombstones to everything -- lazy deletion of objects allows more robust behavior --- HyperKaos.c | 1 + HyperKaos.h | 2 + Player.c | 13 +-- Player.h | 1 + Room.c | 251 ++++++++++++++++++++++++++-------------------------- Room.h | 26 ++++-- Synergy.h | 1 + TODO | 10 +-- 8 files changed, 162 insertions(+), 143 deletions(-) diff --git a/HyperKaos.c b/HyperKaos.c index 9c46851..1de95f2 100644 --- a/HyperKaos.c +++ b/HyperKaos.c @@ -25,6 +25,7 @@ HyperKaos* newHyperKaos(int id, int type, int x, int y, int w, int h) self->domain.h = h; self->kaosID = id; self->eventType = type; + self->tombStone = 0; self->head = NULL; return self; diff --git a/HyperKaos.h b/HyperKaos.h index af3c3ec..7e35b6b 100644 --- a/HyperKaos.h +++ b/HyperKaos.h @@ -4,6 +4,8 @@ typedef struct hyperKaos { int kaosID; int eventType; + int tombStone; + SDL_Rect domain; Kaos* head; } HyperKaos; diff --git a/Player.c b/Player.c index 6eadc6b..4e438aa 100644 --- a/Player.c +++ b/Player.c @@ -60,6 +60,7 @@ Player* newPlayer(char* filename, int a, int b) self->frontFaceBox.y = self->boundBox.y + 16; self->frontFaceBox.w = 16; self->frontFaceBox.h = 16; + self->tombStone = 0; return self; } @@ -149,16 +150,16 @@ void movePlayer(Player* self, Room* hereNow) { if (checkWCollision(rightHere, &(self->boundBox), rightHere->warps, &outgoing)) { - nextChunk = ((rightHere->warps) + outgoing)->chunk; + nextChunk = rightHere->warps[outgoing]->chunk; if (nextChunk != thisChunk) { - bufferData(((hereNow->warps) + outgoing)->chunk); - warpto(mapBuffer[((rightHere->warps) + outgoing)->destination]); + 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; + else warpto(mapData[rightHere->warps[outgoing]->destination]); + self->point.x = hereNow->warps[outgoing]->x; + self->point.y = hereNow->warps[outgoing]->y; } } //stay onscreen diff --git a/Player.h b/Player.h index 93b2997..433faf2 100644 --- a/Player.h +++ b/Player.h @@ -14,6 +14,7 @@ typedef struct player SDL_Rect frontFaceBox; SDL_Surface* spriteSheet; SDL_Surface* sprite; + int tombStone; } Player; diff --git a/Room.c b/Room.c index 93b7369..900e262 100644 --- a/Room.c +++ b/Room.c @@ -19,21 +19,88 @@ typedef struct textBox TextBox; typedef struct scene Scene; #include "extern.h" +Obstruction* newObstruction(int x, int y, int w, int h) +{ + Obstruction* self = malloc(sizeof(Obstruction)); + self->domain.x = x; + self->domain.y = y; + self->domain.w = w; + self->domain.h = h; + self->tombStone = 0; + + return self; +} + +void deleteObstruction(Obstruction* target) +{ + free(target); +} + +FGImage* newFGImage(int x, int y, int w, int h, int f, int dual, char* filename, int alpha) +{ + FGImage* self = malloc(sizeof(FGImage)); + + self->location.x = x; + self->location.y = y; + self->location.w = w; + self->location.h = h; + + self->frames = f; + self->frameNow = 0; + self->dualLayer = dual; + self->tombStone = 0; + + self->spriteSheet = loadImage(filename); + if (alpha != 255) + SDL_SetAlpha(self->spriteSheet, SDL_SRCALPHA|SDL_RLEACCEL, alpha); + + return self; +} + +void deleteFGImage(FGImage* target) +{ + SDL_FreeSurface(target->spriteSheet); + free(target); +} + +WarpZone* newWarpZone(int x, int y, int w, int h, enum dataChunks chunk, int dest, int dX, int dY) +{ + WarpZone* self = malloc(sizeof(WarpZone)); + + self->location.x = x; + self->location.y = y; + self->location.w = w; + self->location.h = h; + + self->chunk = chunk; + self->destination = dest; + self->x = dX; + self->y = dY; + self->tombStone = 0; + + return self; +} + +void deleteWarpZone(WarpZone* target) +{ + free(target); +} + Room* newRoom(char* filename, int a) { Room* self = (Room*)malloc(sizeof(Room)); - self->obstacle = malloc(4*sizeof(SDL_Rect)); + self->obstacle = malloc(4*sizeof(Obstruction*)); self->numberOfObstacles = 0; self->maxNumberOfObstacles = 4; - self->fgObject = malloc(4*sizeof(FGImage)); + self->fgObject = malloc(4*sizeof(FGImage*)); self->numberOfObj = 0; self->maxNumberOfObj = 4; self->objSpeed = a; self->objIterator = 0; - self->warps = malloc(4*sizeof(WarpZone)); + self->warps = malloc(4*sizeof(WarpZone*)); self->numberOfWarps = 0; self->maxNumberOfWarps = 4; @@ -65,33 +132,35 @@ Room* newRoom(char* filename, int a) void deleteRoom(Room* target) { int i; + for (i = 0; i < target->numberOfObstacles; i++) + deleteObstruction(target->obstacle[i]); free(target->obstacle); target->obstacle = NULL; for (i = 0; i < target->numberOfObj; i++) - { - SDL_FreeSurface(target->fgObject[i].spriteSheet); - target->fgObject[i].spriteSheet = NULL; - } + deleteFGImage(target->fgObject[i]); free(target->fgObject); - target->fgObject = NULL; + target->fgObject=NULL; + for (i = 0; i < target->numberOfWarps; i++) + deleteWarpZone(target->warps[i]); free(target->warps); target->warps = NULL; for (i = 0; i < target->numberOfTriggers; i++) - { deleteHyperKaos(target->eventTriggers[i]); - } free(target->eventTriggers); target->eventTriggers = NULL; for (i = 0; i < target->numberOfPeople; i++) - { killPlayer(target->people[i]); - } free(target->people); + target->people=NULL; + for (i = 0; i < target->numberOfSigils; i++) + deleteSynergy(target->sigils[i]); + free(target->sigils); + target->sigils = NULL; SDL_FreeSurface(target->spriteSheet); SDL_FreeSurface(target->bgImage); @@ -158,13 +227,13 @@ void animate(Room* self) // collision detection // -int checkCollision(Room* self, Player* player, SDL_Rect* box) +int checkCollision(Room* self, Player* player, Obstruction** box) { int i; for (i = 1; i <= self->numberOfObstacles; i++) { - if (playerIsInRect(player, box)) + if (!((*box)->tombStone) && playerIsInRect(player, &(*box)->domain)) // if ( (playerX >= box->x && playerX <= box->x + box->w) // && (playerY >= box->y && playerY <= box->y + box->h) ) { @@ -175,7 +244,7 @@ int checkCollision(Room* self, Player* player, SDL_Rect* box) return 0; } -int checkWCollision(Room* self, SDL_Rect* player, WarpZone* warpBoxes, int* whichWarp) +int checkWCollision(Room* self, SDL_Rect* player, WarpZone** warpBoxes, int* whichWarp) { int i; int playerX = player->x + (player->w)/2; @@ -183,8 +252,10 @@ int checkWCollision(Room* self, SDL_Rect* player, WarpZone* warpBoxes, int* whic for (i = 1; i <= self->numberOfWarps; i++) { - if ( (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) && + (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; return 1; @@ -203,7 +274,8 @@ int checkKCollision(Room* self, SDL_Rect* player, HyperKaos** triggers, int* whi for (i = 1; i <= self->numberOfTriggers; i++) { - if ( (playerX > ((*triggers)->domain).x && playerX < ((*triggers)->domain).x + ((*triggers)->domain).w) + 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) @@ -234,7 +306,7 @@ void addObstacle(Room* self, int x, int y, int w, int h) if (self->numberOfObstacles+1 > self->maxNumberOfObstacles) { self->maxNumberOfObstacles *= 2; - SDL_Rect* temp = malloc(self->maxNumberOfObstacles*sizeof(SDL_Rect)); + Obstruction** temp = malloc(self->maxNumberOfObstacles*sizeof(Obstruction*)); for (i = 0; i < self->numberOfObstacles; i++) temp[i] = self->obstacle[i]; free(self->obstacle); @@ -242,26 +314,15 @@ void addObstacle(Room* self, int x, int y, int w, int h) temp = NULL; } - self->obstacle[self->numberOfObstacles].x = x; - self->obstacle[self->numberOfObstacles].y = y; - self->obstacle[self->numberOfObstacles].w = w; - self->obstacle[self->numberOfObstacles].h = h; + + self->obstacle[self->numberOfObstacles] = newObstruction(x, y, w, h); self->numberOfObstacles++; } void deleteObstacle(Room* self, int i) { - int j; - SDL_Rect* temp = malloc(self->maxNumberOfObstacles*sizeof(SDL_Rect)); - for (j = 0; j < i; j++) - temp[j] = self->obstacle[j]; - for (j = i + 1; j < self->numberOfObstacles; j++) - temp[j-1] = self->obstacle[j]; - free(self->obstacle); - self->obstacle = temp; - temp = NULL; - self->numberOfObstacles--; + self->obstacle[i]->tombStone = 1; } // @@ -274,7 +335,7 @@ void addFgObj(Room* self, int x, int y, int w, int h, char* filename, int f, int if (self->numberOfObj+1 > self->maxNumberOfObj) { self->maxNumberOfObj *= 2; - FGImage* temp = malloc(self->maxNumberOfObj*sizeof(FGImage));; + FGImage** temp = malloc(self->maxNumberOfObj*sizeof(FGImage*));; for (i = 0; i < self->numberOfObj; i++) temp[i] = self->fgObject[i]; free(self->fgObject); @@ -282,35 +343,15 @@ void addFgObj(Room* self, int x, int y, int w, int h, char* filename, int f, int temp = NULL; } - self->fgObject[self->numberOfObj].location.x = x; - self->fgObject[self->numberOfObj].location.y = y; - self->fgObject[self->numberOfObj].location.w = w; - self->fgObject[self->numberOfObj].location.h = h; + self->fgObject[self->numberOfObj] = newFGImage(x, y, w, h, f, dual, filename, alpha); - self->fgObject[self->numberOfObj].frames = f; - self->fgObject[self->numberOfObj].frameNow = 0; - self->fgObject[self->numberOfObj].dualLayer = dual; - - self->fgObject[self->numberOfObj].spriteSheet = loadImage(filename); - if (alpha != 255) - SDL_SetAlpha(self->fgObject[self->numberOfObj].spriteSheet, SDL_SRCALPHA|SDL_RLEACCEL, alpha); self->numberOfObj++; } void deleteFgObj(Room* self, int i) { - int j; - FGImage* temp = malloc(self->maxNumberOfObj*sizeof(FGImage)); - for (j = 0; j < i; j++) - temp[j] = self->fgObject[j]; - for (j = i + 1; j < self->numberOfObj; j++) - temp[j-1] = self->fgObject[j]; - SDL_FreeSurface(self->fgObject[i].spriteSheet); - free(self->fgObject); - self->fgObject = temp; - temp = NULL; - self->numberOfObj--; + self->fgObject[i]->tombStone = 1; } void drawFgObjects1(Room* self) @@ -325,14 +366,17 @@ void drawFgObjects1(Room* self) for (i = 0; i < self->numberOfObj; i++) { - clip.w = self->fgObject[i].location.w; - clip.x = self->fgObject[i].frameNow*clip.w; - clip.h = self->fgObject[i].location.h; - applySurface(self->fgObject[i].location.x, self->fgObject[i].location.y, self->fgObject[i].spriteSheet, screen, &clip ); - if (self->objIterator == self->objSpeed) - self->fgObject[i].frameNow++; - if (self->fgObject[i].frameNow == self->fgObject[i].frames) - self->fgObject[i].frameNow = 0; + if (!(self->fgObject[i]->tombStone)) + { + clip.w = self->fgObject[i]->location.w; + clip.x = self->fgObject[i]->frameNow*clip.w; + clip.h = self->fgObject[i]->location.h; + applySurface(self->fgObject[i]->location.x, self->fgObject[i]->location.y, self->fgObject[i]->spriteSheet, screen, &clip ); + if (self->objIterator == self->objSpeed) + self->fgObject[i]->frameNow++; + if (self->fgObject[i]->frameNow == self->fgObject[i]->frames) + self->fgObject[i]->frameNow = 0; + } } if (self->objIterator == self->objSpeed) self->objIterator = 0; @@ -351,17 +395,17 @@ void drawFgObjects2(Room* self) for (i = 0; i < self->numberOfObj; i++) { - if (self->fgObject[i].dualLayer) + if (!(self->fgObject[i]->tombStone) && self->fgObject[i]->dualLayer) { - clip.w = self->fgObject[i].location.w; - clip.x = self->fgObject[i].frameNow*clip.w; - clip.h = self->fgObject[i].location.h; + clip.w = self->fgObject[i]->location.w; + clip.x = self->fgObject[i]->frameNow*clip.w; + clip.h = self->fgObject[i]->location.h; clip.y = clip.h; - applySurface(self->fgObject[i].location.x, self->fgObject[i].location.y, self->fgObject[i].spriteSheet, screen, &clip ); + applySurface(self->fgObject[i]->location.x, self->fgObject[i]->location.y, self->fgObject[i]->spriteSheet, screen, &clip ); if (self->objIterator == self->objSpeed) - self->fgObject[i].frameNow++; - if (self->fgObject[i].frameNow == self->fgObject[i].frames) - self->fgObject[i].frameNow = 0; + self->fgObject[i]->frameNow++; + if (self->fgObject[i]->frameNow == self->fgObject[i]->frames) + self->fgObject[i]->frameNow = 0; } } if (self->objIterator == self->objSpeed) @@ -379,7 +423,7 @@ void addWarp(Room* self, int x, int y, int w, int h, enum dataChunks toChunk, in if (self->numberOfWarps+1 > self->maxNumberOfWarps) { self->maxNumberOfWarps *= 2; - WarpZone* temp = malloc(self->maxNumberOfWarps*sizeof(WarpZone)); + WarpZone** temp = malloc(self->maxNumberOfWarps*sizeof(WarpZone*)); for (i = 0; i < self->numberOfWarps; i++) temp[i] = self->warps[i]; free(self->warps); @@ -387,31 +431,13 @@ void addWarp(Room* self, int x, int y, int w, int h, enum dataChunks toChunk, in temp = NULL; } - self->warps[self->numberOfWarps].x = goesX; - self->warps[self->numberOfWarps].y = goesY; - - self->warps[self->numberOfWarps].location.x = x; - self->warps[self->numberOfWarps].location.y = y; - self->warps[self->numberOfWarps].location.w = w; - self->warps[self->numberOfWarps].location.h = h; - - self->warps[self->numberOfWarps].chunk = toChunk; - self->warps[self->numberOfWarps++].destination = goesTo; - + self->warps[self->numberOfWarps] = newWarpZone(x, y, w, h, toChunk, goesTo, goesX, goesY); + self->numberOfWarps++; } void deleteWarp(Room* self, int i) { - int j; - WarpZone* temp = malloc(self->maxNumberOfWarps*sizeof(WarpZone)); - for (j = 0; j < i; j++) - temp[j] = self->warps[j]; - for (j = i + 1; j < self->numberOfWarps; j++) - temp[j-1] = self->warps[j]; - free(self->warps); - self->warps = temp; - temp = NULL; - self->numberOfWarps--; + self->warps[i]->tombStone = 1; } // @@ -437,17 +463,7 @@ void addTrigger(Room* self, HyperKaos* newTrigger) void deleteTrigger(Room* self, int i) { - int j; - HyperKaos** temp = malloc(self->maxNumberOfTriggers*sizeof(HyperKaos*)); - for (j = 0; j < i; j++) - temp[j] = self->eventTriggers[j]; - for (j = i + 1; j < self->numberOfTriggers; j++) - temp[j-1] = self->eventTriggers[j]; - deleteHyperKaos(self->eventTriggers[i]); - free(self->eventTriggers); - self->eventTriggers = temp; - temp = NULL; - self->numberOfTriggers--; + self->eventTriggers[i]->tombStone = 1; } void addSigil(Room* self, Synergy* newSigil) @@ -469,17 +485,7 @@ void addSigil(Room* self, Synergy* newSigil) void deleteSigil(Room* self, int i) { - int j; - Synergy** temp = malloc(self->maxNumberOfSigils*sizeof(Synergy*)); - for (j = 0; j < i; j++) - temp[j] = self->sigils[j]; - for (j = i + 1; j < self->numberOfSigils; j++) - temp[j-1] = self->sigils[j]; - deleteSynergy(self->sigils[i]); - free(self->sigils); - self->sigils = temp; - temp = NULL; - self->numberOfSigils--; + self->sigils[i]->tombStone = 1; } void addPerson(Room* self, Player* newPlayer) @@ -501,17 +507,7 @@ void addPerson(Room* self, Player* newPlayer) void deletePerson(Room* self, int i) { - int j; - Player** temp = malloc(self->maxNumberOfPeople*sizeof(Player*)); - for (j = 0; j < i; j++) - temp[j] = self->people[j]; - for (j = i + 1; j < self->numberOfPeople; j++) - temp[j-1] = self->people[j]; - killPlayer(self->people[i]); - free(self->people); - self->people = temp; - temp = NULL; - self->numberOfPeople--; + self->people[i]->tombStone = 1; } void drawPeople(Room* self) @@ -519,6 +515,7 @@ void drawPeople(Room* self) int i; for (i = 0; i < self->numberOfPeople; i++) { + if(!(self->people[i]->tombStone)) drawPlayer(self->people[i]); } } diff --git a/Room.h b/Room.h index 6c28944..9222660 100644 --- a/Room.h +++ b/Room.h @@ -1,16 +1,28 @@ typedef struct hyperKaos HyperKaos; typedef struct synergy Synergy; +typedef struct obstruction +{ + SDL_Rect domain; + int tombStone; +} Obstruction; + +Obstruction* newObstruction(int x, int y, int w, int h); +void deleteObstruction(Obstruction* target); + typedef struct fgImage { int frames; int frameNow; int dualLayer; + int tombStone; SDL_Rect location; SDL_Surface* spriteSheet; } FGImage; +FGImage* newFGImage(int x, int y, int w, int h, int f, int dual, char* filename, int alpha); +void deleteFGImage(FGImage* target); typedef struct warpZone { @@ -18,25 +30,29 @@ typedef struct warpZone enum dataChunks chunk; int destination; int x, y; + int tombStone; } WarpZone; +WarpZone* newWarpZone(int x, int y, int w, int h, enum dataChunks chunk, int dest, int dX, int dY); +void deleteWarpZone(WarpZone* target); + typedef struct room { SDL_Surface* spriteSheet; SDL_Surface* bgImage; int frameNo; - SDL_Rect* obstacle; + Obstruction** obstacle; int numberOfObstacles; int maxNumberOfObstacles; - struct fgImage* fgObject; + FGImage** fgObject; int numberOfObj; int maxNumberOfObj; int objSpeed; int objIterator; - WarpZone* warps; + WarpZone** warps; int numberOfWarps; int maxNumberOfWarps; @@ -62,8 +78,8 @@ void deleteRoom(Room* target); void changeRSprite(Room* self, SDL_Rect* clip); void animate(Room* self); -int checkCollision(Room* self, Player* player, SDL_Rect* box); -int checkWCollision(Room* self, SDL_Rect* player, WarpZone* warpBoxes, int* whichWarp); +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); void addObstacle(Room* self, int x, int y, int w, int h); diff --git a/Synergy.h b/Synergy.h index b61c061..796a49f 100644 --- a/Synergy.h +++ b/Synergy.h @@ -4,6 +4,7 @@ typedef struct synergy char dir; SDL_Rect zone; HyperKaos* trigger; + int tombStone; } Synergy; Synergy* newSynergy(int s, char d, int x, int y, int w, int h, HyperKaos* t); diff --git a/TODO b/TODO index 411cb2b..08d3751 100644 --- a/TODO +++ b/TODO @@ -1,8 +1,8 @@ --add tombstones to everything - -encase obstacles in their own struct - -adjust collision detection - -adjust kListen - -adjust deleteThing +xadd tombstones to everything + xencase obstacles in their own struct + xadjust collision detection + xadjust kListen + xadjust deleteThing -anything else that might be affected by tombstones -spell stuff -HUD