bflderrstate.c

Go to the documentation of this file.
00001 
00026 #include <SDL_image.h>
00027 #include <string.h>
00028 #include <stdlib.h>
00029 #include "states.h"
00030 #include "color.h"
00031 #include "gameconfig.h"
00032 #include "tanksprite.h"
00033 #include "message.h"
00034 
00035 extern RenderItem background;
00036 
00040 extern Uint32 startTime;
00041 
00045 extern Player *tanks;
00046 
00053 static void BfldErrResize() {
00054     int loop;
00055     // assure good color values
00056     SetupColorVals();   
00057     // put the backgound image in place
00058     background.loc.w = dInfo.pw;
00059     background.loc.h = dInfo.ph;
00060     PlaceRenderItem(&background);
00061     // title at the top
00062     textItems[TEXTITEM_TITLE].loc.y = TOP_MARGIN;
00063     // continue at the bottom
00064     textItems[TEXTITEM_ERRCONT].loc.y = dInfo.ph - (BOTTOM_MARGIN << 1) -
00065     textItems[TEXTITEM_ERRCONT].loc.h;
00066     // place the tank
00067     tanks->tank.loc.w = tanks->tank.loc.h = dInfo.luw << 3;
00068     tanks->tank.loc.x = (dInfo.pw >> 1) - (dInfo.luw << 2);
00069     tanks->tank.loc.y = textItems[TEXTITEM_TITLE].loc.y +
00070     textItems[TEXTITEM_TITLE].loc.h + (TOP_MARGIN << 1);
00071     PlaceRenderItem(&(tanks->tank));
00072     // re-render the error text
00073     TextRenderString(&textItems[TEXTITEM_ERROR], FONT_SCORE, dInfo.pw - 40,
00074     colorFmtInd[COLOR_TEXT], bfErr.msg);
00075     // place the error a little below the tank
00076     textItems[TEXTITEM_ERROR].loc.y = tanks->tank.loc.y + tanks->tank.loc.h +
00077     ((dInfo.ph - tanks->tank.loc.y - tanks->tank.loc.h -
00078     textItems[TEXTITEM_ERROR].loc.h) >> 1);
00079     // center all text horizontally
00080     for (loop = TEXTITEM_TITLE; loop < TEXTITEM_ERRCONT + 1; loop++) {
00081         // compute the centered location
00082         textItems[loop].loc.x = (dInfo.pw >> 1) - (textItems[loop].loc.w >> 1);
00083         // add the text
00084         if (textItems[loop].parent == NULL) {
00085             AddRenderItem(&textItems[loop], LAYER_TEXT);
00086         }
00087         // place the item
00088         PlaceRenderItem(&(textItems[loop]));
00089     }
00090 }
00091 
00092 int BfldErrInit() {
00093     memset(&background, 0, sizeof(background));
00094     // get memory for a tank to use as an icon
00095     if ((tanks = calloc(1, sizeof(Player))) == NULL) {
00096         // out of memory
00097         return STATE_ERROR;
00098     }
00099     // setup position of screen parts
00100     dInfo.luw = dInfo.luh = 8;
00101     dInfo.lw = dInfo.pw / dInfo.luw;
00102     dInfo.lh = dInfo.ph / dInfo.luh;
00103     // put the backgound image in place
00104     background.renderer = SolidFillRenderer;
00105     background.data = &(colorVals[COLOR_REGBACK]);
00106     AddRenderItem(&background, LAYER_BACKGROUND);
00107     // initalize a tank
00108     tanks->tank.renderer = DrawTank;
00109     AddRenderItem(&(tanks->tank), LAYER_TANKS);
00110     // set the text color
00111     colorFmtInd[COLOR_TEXT] = colorFmtInd[COLOR_BLACK];
00112     // attempt to render the title and prompt text off-screen
00113     if (!TextRenderString(&textItems[TEXTITEM_TITLE], FONT_MESSAGE, 0,
00114     colorFmtInd[COLOR_TEXT], strings[STRING_TITLE]) ||
00115     !TextRenderString(&textItems[TEXTITEM_ERRCONT], FONT_SCORE, 0,
00116     colorFmtInd[COLOR_TEXT], strings[STRING_FIRECONT])) {
00117         // couldn't render the text
00118         free(tanks);
00119         return STATE_ERROR;
00120     }
00121     // place all the text
00122     BfldErrResize();
00123     // record the time
00124     startTime = SDL_GetTicks();
00125     return STATE_OK;
00126 }
00127 
00128 int BfldErrUninit() {
00129     // see if a background image, rather than a color, was used
00130     if (background.renderer == GenericSurfaceRenderer) {
00131         // remove the background image
00132         SDL_FreeSurface(background.data);
00133     }
00134     RemoveRenderItem(&background);
00135     RemoveRenderItem(&(tanks->tank));
00136     free(tanks);
00137     TextUnsetState();
00138     return STATE_OK;
00139 }
00140 
00141 int BfldErrRun() {
00142     SDL_Event event;
00143     
00144     // loop through all waiting events
00145     while (SDL_PollEvent(&event)) {
00146         // figure event type
00147         switch (event.type) {
00148             case SDL_QUIT:
00149                 return STATE_QUIT;
00150                 break;
00151             case SDL_KEYDOWN:
00152                 // check for a request to quit the program
00153                 if (event.key.keysym.sym == SDLK_ESCAPE)
00154                     return STATE_QUIT;
00155                 break;
00156             case SDL_KEYUP:
00157             case SDL_JOYBUTTONDOWN:
00158                 // enforce a minimum delay
00159                 if ((SDL_GetTicks() - startTime) > 511)
00160                     return STATE_MENUROOT;
00161                 break;
00162             case SDL_VIDEORESIZE:
00163                 ResizeWindow(event.resize.w, event.resize.h);
00164                 BfldErrResize();
00165             default:
00166                 break;
00167         }
00168     }
00169     // rotate the tank
00170     tanks->rot += 128;
00171     // make the new rotaion visible
00172     PlaceRenderItem(&(tanks->tank));
00173     
00174     // draw it
00175     //Render();
00176     // don't monoplize the processor
00177     //SDL_Delay(16);
00178     // ----- run network code ----
00179     // Without this, if the client quickly connects to a server it will not
00180     // respond until running the menu state, which can be long enough for the
00181     // server to decide that the client is not responding.
00182     ProcessNetworkMessages();
00183     // continue with the error screen
00184     return STATE_BFLDERR;
00185 }
00186 

Generated on Mon May 28 04:41:38 2007 for Retro Tank Super Attack by  doxygen 1.5.2