00001 00024 #include <assert.h> 00025 #include <string.h> 00026 #include <stdlib.h> 00027 #include "obstacle.h" 00028 #include "game.h" 00029 #include "gameconfig.h" 00030 #include "color.h" 00031 #include "tanksprite.h" 00032 00033 Obstacle *obstacles = NULL; 00034 00035 void ObstacleInit() { 00036 int loop; 00037 Obstacle *obst; 00038 // the obstacle array should already be initialized 00039 assert(obstacles != NULL); 00040 // loop through the obstacles 00041 for (obst = obstacles, loop = gameOpts.obstacles; loop > 0; 00042 loop--, obst++) { 00043 // compute screen coordinates 00044 obst->ri.loc.h = obst->loc.h * dInfo.luw; 00045 obst->ri.loc.w = obst->loc.w * dInfo.luh; 00046 obst->ri.loc.x = obst->loc.x * dInfo.luw; 00047 obst->ri.loc.y = obst->loc.y * dInfo.luh; 00048 // draw the obstacles as solid rectangles 00049 obst->ri.renderer = SolidFillRenderer; 00050 // clear out a few values 00051 obst->ri.parent = NULL; 00052 obst->ri.next = obst->ri.prev = NULL; 00053 // use the already set obstacle color 00054 obst->ri.data = &(colorVals[COLOR_OBSTACLE]); 00055 // add the item to the render system 00056 AddRenderItem(&(obst->ri), LAYER_OBSTACLES); 00057 // make it visible in the next frame 00058 PlaceRenderItem(&(obst->ri)); 00059 } 00060 } 00061 00062 void ObstacleUninit() { 00063 int loop; 00064 Obstacle *obst; 00065 // if there are no obstacles . . . 00066 if ((loop = gameOpts.obstacles) == 0) { 00067 // . . . there is nothing to do 00068 return; 00069 } 00070 // loop through the obstacles 00071 for (obst = obstacles; loop > 0; loop--, obst++) { 00072 // remove the obstacle's render item 00073 RemoveRenderItem(&(obst->ri)); 00074 } 00075 // deallocate the obstacles 00076 free(obstacles); 00077 // clear the count 00078 gameOpts.obstacles = 0; 00079 } 00080 00081 Bool IsCollidingObstacle(Player *p, Obstacle *o) { 00082 SDL_Rect rect; 00083 int delta; 00084 Uint8 *pSprite; 00085 00086 // Compute overlap region. 00087 if ((delta = o->loc.y - (p->y >> 8)) > 0) { 00088 // check for no vertical overlap 00089 if (delta >= 8) 00090 return FALSE; 00091 // set y starting position 00092 rect.y = delta; 00093 // set the height of the overlap 00094 rect.h = 8 - delta; 00095 } 00096 else { 00097 // check for no vertical overlap 00098 if (-delta >= o->loc.h) 00099 return FALSE; 00100 // set the overlap height 00101 if ((Sint16)(rect.h = o->loc.h + delta) > 8) 00102 rect.h = 8; 00103 // set y starting position 00104 rect.y = 0; 00105 } 00106 // see which is further left 00107 if ((delta = o->loc.x - (p->x >> 8)) > 0) { 00108 // check for no horizontal overlap 00109 if (delta >= 8) 00110 return FALSE; 00111 // set x starting position 00112 rect.x = delta; 00113 // set the width shift 00114 rect.w = delta; 00115 } 00116 else { 00117 // check for no horizontal overlap 00118 if (-delta >= o->loc.w) 00119 return FALSE; 00120 // set the overlap width shift 00121 if ((Sint16)(rect.w = 8 - (o->loc.w + delta)) < 0) 00122 rect.w = 0; 00123 // set x starting position 00124 rect.x = 0; 00125 } 00126 00127 // compute the location of each player's sprite 00128 pSprite = &(sprites[(p->rot >> 8) & 0xF][rect.y]); 00129 00130 // loop through the overlapping height 00131 for (; rect.h > 0; rect.h--, pSprite++) { 00132 // check for a collision 00133 if (((*pSprite) >> rect.x) & (0xFF >> rect.w)) return TRUE; 00134 } 00135 // no collision 00136 return FALSE; 00137 } 00138