C:/Documents and Settings/jegan/Desktop/projectX/colorMain.cpp

Go to the documentation of this file.
00001 /*
00002  * Small SDL example to demonstrate dynamically loading 
00003  * OpenGL lib and functions
00004  *
00005  * (FYI it was supposed to look like snow in the wind or something...)
00006  *
00007  * Compile with :
00008  * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
00009  *
00010  * You can specify a different OpenGL lib on the command line, i.e. :
00011  * ./testdyngl  /usr/X11R6/lib/libGL.so.1.2
00012  * or
00013  * ./testdyngl  /usr/lib/libGL.so.1.0.4496
00014  *
00015  */
00016 
00017 
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string>
00021 #include <io.h>
00022 #include <iostream>
00023 #include <sstream>
00024 #include <cassert>
00025 #include <map>
00026 #include <algorithm>
00027 #include <hash_map>
00028 
00029 #include "SDL.h"
00030 
00031 #include "SDL_image.h"
00032 
00033 #include "glfuncs.h"
00034 #include "colorScreen.h"
00035 #include "Sprite.h"
00036 
00037 using namespace std;
00038 
00039 static Uint32 WALKBEHIND;
00040 static Uint32 WALKINTO;
00041 static Uint32 WALKINFRONT;
00042 static std::map<Uint32,Sprite *> TEXTURES;
00043 static std::map<Uint32,std::string> OBJECTS;
00044 static std::map<Uint32,Sprite *> PELS;
00045 static vector<Uint32> COLORS;
00046 
00047 
00048 void loadObjects();
00049 Uint32 loadPixelFile(std::string filename);
00050 void readObjects(std::string dir);
00051 void readTextures(std::string extension);
00052 
00053 void showAll(int type, int page);
00054 void showObject(Uint32 pixel, int y);
00055 void showTexture(Uint32 pixel, int y);
00056 void showColl(int y);
00057 
00058 
00059 
00060 int main(int argc,char *argv[])
00061 {               
00062         //string levelfile = "levels/level";
00063         //string levelfile = "levels/pipes2";
00064         //string levelfile = "levels/pipes3";
00065         string levelfile = "levels/factory";
00066         
00067         /*
00068         List *list = Parser::parse("textures/jesus.txt");
00069     //cout << newList->getString();
00070 
00071         List *newList = list;
00072     while(!newList->empty()) {
00073         cout << newList->getString() << endl;
00074         newList = newList->rest();
00075     }
00076     delete list;
00077 
00078         return 0;
00079         */
00080 
00081         SDL_Event event;
00082         
00083         int ticks = SDL_GetTicks();
00084 
00085         Screen* screen = Screen::instance();
00086 
00087         glfuncs* f = glfuncs::instance();
00088 
00089         loadObjects();
00090         
00091         GLfloat whiteCoords[4];
00092         SDL_Surface *whiteSurface = f->load_image("whiteBG.png");
00093         int whiteBG = f->SDL_GL_LoadTexture(whiteSurface, whiteCoords);
00094 
00095         GLfloat scramCoords[4];
00096         SDL_Surface *scramSurface = f->load_image("scramble.png");
00097         int scram = f->SDL_GL_LoadTexture(scramSurface, scramCoords);
00098 
00099         int mode = 0;
00100         int page = 0;
00101         bool leave = false;
00102 
00103                 do
00104                 {
00105                         // Clear OpenGL screen
00106                         f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00107 
00108                         SDL_Rect screenRect = screen->getScreenRect();
00109                         screenRect.x = 0;
00110                         screenRect.y = 0;
00111                         screen->drawScreen(whiteBG,whiteCoords,screenRect);
00112 
00113                         /*SDL_Rect quad;
00114                         quad.h = 128;
00115                         quad.w = 128;
00116                         quad.x = 240;
00117                         quad.y = 240;
00118                         for(int i = 0; i < 4; i++) {
00119                                 screen->drawLevel(texture,texCoords,quad);
00120                                 quad.x += 128;
00121                         }*/
00122 
00123                         //This runs if we are trying to run frames in too rapid succession
00124                         
00125                         while((SDL_GetTicks() - ticks) < 1000/Screen::FRAMERATE)
00126                         {
00127                                 //Just wait until we can do a frame again
00128                                 Sleep(0);
00129                         }
00130                         //reset for the next time we want to update
00131                         ticks = SDL_GetTicks();
00132 
00133                         showAll(mode,page);
00134 
00135                         SDL_Rect scramRect;
00136                         scramRect.w = 51;
00137                         scramRect.h = 81;
00138                         scramRect.x = 5;
00139                         scramRect.y = screen->getScreenRect().h - 80;
00140                         screen->drawScreen(scram,scramCoords,scramRect);
00141 
00142 
00143                         f->glFinish();
00144                         SDL_GL_SwapBuffers();
00145 
00146                         //person.control();
00147                         //screen->control();
00148 
00149 
00150                         while(SDL_PollEvent(&event))
00151                         {
00152                                 if(event.type & SDL_KEYDOWN) {
00153                                         switch( event.key.keysym.sym ) 
00154                                         {
00155                                         case SDLK_ESCAPE:
00156                                                 leave = true;
00157                                                 break;
00158                                         case SDLK_o:
00159                                                 mode = 1;
00160                                                 break;
00161                                         case SDLK_t:
00162                                                 mode = 0;
00163                                                 break;
00164                                         case SDLK_y:
00165                                                 mode = 3;
00166                                                 break;
00167                                         case SDLK_1:
00168                                                 page = 0;
00169                                                 break;
00170                                         case SDLK_2:
00171                                                 page = 1;
00172                                                 break;
00173                                         case SDLK_3:
00174                                                 page = 2;
00175                                                 break;
00176                                         case SDLK_4:
00177                                                 page = 3;
00178                                                 break;
00179                                         case SDLK_5:
00180                                                 page = 4;
00181                                                 break;
00182                                         case SDLK_6:
00183                                                 page = 5;
00184                                                 break;
00185                                         case SDLK_7:
00186                                                 page = 6;
00187                                                 break;
00188                                         case SDLK_8:
00189                                                 page = 7;
00190                                                 break;
00191                                         case SDLK_9:
00192                                                 page = 8;
00193                                                 break;
00194                                         case SDLK_0:
00195                                                 page = 9;
00196                                                 break;
00197                                         case SDLK_q:
00198                                                 page = 10;
00199                                                 break;
00200                                         case SDLK_w:
00201                                                 page = 11;
00202                                                 break;
00203                                         case SDLK_e:
00204                                                 page = 12;
00205                                                 break;
00206                                         case SDLK_r:
00207                                                 page = 13;
00208                                                 break;
00209                                         case SDLK_a:
00210                                                 page = 14;
00211                                                 break;
00212                                         case SDLK_s:
00213                                                 page = 15;
00214                                                 break;
00215                                         case SDLK_d:
00216                                                 page = 16;
00217                                                 break;
00218                                         case SDLK_f:
00219                                                 page = 17;
00220                                                 break;
00221                                         case SDLK_z:
00222                                                 page = 18;
00223                                                 break;
00224                                         case SDLK_x:
00225                                                 page = 19;
00226                                                 break;
00227                                         case SDLK_c:
00228                                                 page = 20;
00229                                                 break;
00230                                         case SDLK_v:
00231                                                 page = 21;
00232                                                 break;
00233                                         default:
00234                                                 break;
00235                                         }
00236                                 }
00237                         }
00238 
00239                         //SDL_Delay(20);
00240                 } while(!leave);
00241         
00242         
00243 
00244 
00245 
00246         //f->KillFont(base);
00247 
00248         SDL_Quit();
00249         return 0;
00250 }
00251 
00252 void loadObjects()
00253 {
00254         readTextures("png");
00255         readTextures("txt");
00256         
00257         WALKBEHIND = loadPixelFile("levels/walkbehind_p.png");
00258         WALKINFRONT = loadPixelFile("levels/walkinfront_p.png");
00259         WALKINTO = loadPixelFile("levels/walkinto_p.png");
00260 
00261         readObjects("characters");
00262         readObjects("events");
00263         readObjects("cutscenes");
00264         readObjects("entities");
00265         //readTextures("bmp");
00266 }
00267 
00268 Uint32 loadPixelFile(std::string filename)
00269 {
00270         SDL_Surface *surface = glfuncs::instance()->load_image(filename);
00271         SDL_LockSurface(surface);
00272         Uint32 pixel=*((Uint32*)surface->pixels);
00273         SDL_UnlockSurface(surface);
00274         SDL_FreeSurface(surface);
00275 
00276         /*Sprite * sprite = Sprite::loadSprite(filename);
00277         sprite->setHeight(8);
00278         sprite->setWidth(8);
00279 
00280         PELS[pixel] = sprite;
00281         COLORS.push_back(pixel);*/
00282 
00283         return pixel;
00284 }
00285 
00286 void readObjects(std::string dir)
00287 {
00288         struct _finddata_t file;
00289         intptr_t hFile;
00290         string extension = "txt";
00291 
00292         if( (hFile = _findfirst( (dir + "/*." + extension).c_str(), &file )) != -1L ) {
00293                 do {
00294                         string filename(file.name);
00295                         
00296                         string end = filename.substr(filename.size() - 6,2);
00297                         if(end != "_p") {
00298 
00299                                 filename = dir + "/" + filename;
00300                                 //Sprite * sprite = new Sprite(filename, tileWidth, tileHeight);
00301                                 string objFile = filename;
00302 
00303                                 //filename.insert(filename.find("."),"_p");
00304                                 filename.erase(filename.find("."),4);
00305                                 filename += "_p.png";
00306 
00307                                 SDL_Surface *surface = glfuncs::instance()->load_image(filename);
00308                                 SDL_LockSurface(surface);
00309                                 Uint32 pixel=*((Uint32*)surface->pixels);
00310                                 
00311                                 //cerr << (int) pixel << endl;
00312 
00313                                 SDL_UnlockSurface(surface);
00314                                 SDL_FreeSurface(surface);
00315 
00316                                 OBJECTS[pixel] = objFile;
00317                                 Sprite * sprite = Sprite::loadSprite(filename);
00318                                 sprite->setHeight(8);
00319                                 sprite->setWidth(8);
00320                                 PELS[pixel] = sprite;
00321                                 COLORS.push_back(pixel);
00322                         }
00323                         else
00324                                 continue;
00325                 }
00326                 while( _findnext( hFile, &file ) == 0 );
00327 
00328                 _findclose( hFile );
00329         }
00330 }
00331 
00332 void readTextures(std::string extension)
00333 {
00334         struct _finddata_t file;
00335         intptr_t hFile;
00336 
00337         if( (hFile = _findfirst( ("textures/*." + extension).c_str(), &file )) != -1L ) {
00338                 do {
00339                         string filename(file.name);
00340                         
00341                         string end = filename.substr(filename.size() - 6,2);
00342                         if(end != "_p") {
00343 
00344                                 filename = "textures/" + filename;
00345                                 //Sprite * sprite = new Sprite(filename, tileWidth, tileHeight);
00346                                 Sprite * sprite = Sprite::loadSprite(filename);
00347 
00348                                 //filename.insert(filename.find("."),"_p");
00349                                 filename.erase(filename.find("."),4);
00350                                 filename += "_p.png";
00351 
00352                                 SDL_Surface *surface = glfuncs::instance()->load_image(filename);
00353                                 SDL_LockSurface(surface);
00354                                 Uint32 pixel=*((Uint32*)surface->pixels);
00355                                 
00356                                 //cerr << (int) pixel << endl;
00357 
00358                                 SDL_UnlockSurface(surface);
00359                                 SDL_FreeSurface(surface);
00360 
00361                                 TEXTURES[pixel] = sprite;
00362 
00363                                 sprite = Sprite::loadSprite(filename);
00364                                 sprite->setHeight(8);
00365                                 sprite->setWidth(8);
00366                                 PELS[pixel] = sprite;
00367                                 COLORS.push_back(pixel);
00368                         }
00369                         else
00370                                 continue;
00371                 }
00372                 while( _findnext( hFile, &file ) == 0 );
00373 
00374                 _findclose( hFile );
00375         }
00376 }
00377 
00378 void showAll(int type, int page)
00379 {
00380         int y=13;
00381         
00382         int pagelength = 0;
00383 
00384         int i = 0;
00385         if(type == 0)
00386         {
00387                 Screen::instance() ->drawText("TEXTURES:",0,y);
00388                 y+=13;
00389                 pagelength = 15;
00390         }
00391         else if(type == 1)
00392         {
00393                 Screen::instance() ->drawText("OBJECTS:",0,y);
00394                 y+=13;
00395                 pagelength = 40;
00396         }
00397         else if(type == 3)
00398         {
00399                 Screen::instance() ->drawText("COLLISION COLORS:",0,y);
00400                 y+=13;
00401                 showColl(y);
00402                 return;
00403         }
00404 
00405         int numdrawn = 0;
00406         for(int i = pagelength*page; (i < COLORS.size())&&(numdrawn <= pagelength); i++)
00407         {
00408                 //we're showing textures
00409                 if(type == 0 && TEXTURES[COLORS[i]] != NULL)
00410                 {
00411                         showTexture(COLORS[i],y);
00412                         y+= 32;
00413                         numdrawn ++;
00414                 }
00415                 else if(type == 1 && OBJECTS[COLORS[i]]!="")
00416                 {
00417                         showObject(COLORS[i],y);
00418                         y+= 13;
00419                         numdrawn ++;
00420                 }
00421         }
00422 }
00423 
00424 void showObject(Uint32 pixel, int y)
00425 {
00426         int x=0;
00427         PELS[pixel]->draw(x,y,Sprite::SCREEN);
00428         x+= 10;
00429         y+=10;
00430         Screen::instance() ->drawText(OBJECTS[pixel],x,y);
00431         y-=10;
00432 }
00433 
00434 void showTexture(Uint32 pixel, int y)
00435 {
00436         int x=0;
00437         PELS[pixel]->draw(x,y,Sprite::SCREEN);
00438         x+= 10;
00439         TEXTURES[pixel]->draw(x,y,Sprite::SCREEN);
00440         TEXTURES[pixel]->update();
00441 }
00442 
00443 void showColl(int y)
00444 {
00445         int x=0;
00446         PELS[WALKBEHIND]->draw(x,y,Sprite::SCREEN);
00447         x+= 10;
00448         y+=10;
00449         Screen::instance() ->drawText("Walk Behind",x,y);
00450         y+=3;
00451 
00452         x=0;
00453         PELS[WALKINTO]->draw(x,y,Sprite::SCREEN);
00454         x+= 10;
00455         y+=10;
00456         Screen::instance() ->drawText("Walk Into",x,y);
00457         y+=3;
00458 }

Generated on Fri May 5 00:20:18 2006 for ProjectX by  doxygen 1.4.6-NO