#include #include #include #include #include #include #include #include #include #include #include #define TILE_SIZE 32 #define DEFAULT_X 160 #define DEFAULT_Y 104 using namespace std; struct tile { SDL_Rect rect; int type; }; struct scene { int pos_x; int pos_y; }; struct t_level { int width; int height; int * data; int px; int py; int delta_x; } level; struct t_message { string text; SDL_Surface * image; unsigned char wait; }; mapimages; SDL_Surface * Palettize(SDL_Surface *surface) { if(surface->format->BitsPerPixel==8) { //cout << "Doing something..." << endl; SDL_Color colors[32]; int i; int r,g,b; /* Fill colors with color information */ for(i=0;i<32;i++) { r=surface->format->palette->colors[i].r+62; if(r>224) r=224; g=surface->format->palette->colors[i].g+73; if(g>235) g=235; b=surface->format->palette->colors[i].b+13; if(b>175) b=175; colors[i].r=r; colors[i].g=g; colors[i].b=b; } SDL_SetPalette(surface, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 32); } return surface; } void load_map() { cout << "Opening map file..." << endl; ifstream mapf("map"); string line; int value; vectortmp; while(!mapf.eof()) { mapf>>value; tmp.push_back(value); //cout<<"data:"< messages; int current_message; SDL_Surface* screen; scene game_scene; //set working directory chdir("/usr/share/llx-credits/nemo/"); if (SDL_Init(SDL_INIT_VIDEO)!= 0) { cout << "Unable to initialize SDL: " << SDL_GetError() << endl; return 1; } Mix_OpenAudio(22050,AUDIO_S16SYS,2,4096); SDL_WM_SetCaption("Lliurex Nemo 12.06",NULL); screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE | SDL_DOUBLEBUF); TTF_Init(); if (screen==NULL) { cout << "Unable to set video mode: " << SDL_GetError() << endl; return 1; } game_scene.pos_x=0; game_scene.pos_y=0; // LOADING RESOURCES cout << "Loading resources..." << endl; load_map(); TTF_Font * font = TTF_OpenFont("./rsrc/PixAntiqua.ttf",16); SDL_Surface *game_frame= LoadImage("rsrc/gameboy.png",false); SDL_Surface *valentin=LoadImage("rsrc/valentin_gb.png",true); SDL_Surface *valentin_walk1=LoadImage("rsrc/walk1.png",true); SDL_Surface *valentin_walk2=LoadImage("rsrc/walk2.png",true); SDL_Surface *valentin_walk3=LoadImage("rsrc/walk3.png",true); SDL_Surface *valentin_walk4=LoadImage("rsrc/walk4.png",true); SDL_Surface *valentin_walk5=LoadImage("rsrc/walk5.png",true); SDL_Surface *valentin_walk6=LoadImage("rsrc/walk6.png",true); map tiles; tiles[0]= IMG_Load("rsrc/white.png"); tiles[1]=IMG_Load("rsrc/ground1.png"); tiles[2]=IMG_Load("rsrc/ground2.png"); tiles[3]=IMG_Load("rsrc/ground3.png"); tiles[4]=IMG_Load("rsrc/ground4.png"); tiles[10]=IMG_Load("rsrc/pipe1.png"); tiles[11]=IMG_Load("rsrc/pipe2.png"); tiles[12]=IMG_Load("rsrc/pipe3.png"); tiles[30]=IMG_Load("rsrc/joint.png"); tiles[40]=IMG_Load("rsrc/wall1.png"); tiles[41]=IMG_Load("rsrc/wall2.png"); tiles[43]=IMG_Load("rsrc/wall3.png"); tiles[44]=IMG_Load("rsrc/wall4.png"); tiles[45]=IMG_Load("rsrc/wall5.png"); tiles[46]=IMG_Load("rsrc/wall6.png"); tiles[47]=IMG_Load("rsrc/wall7.png"); tiles[48]=IMG_Load("rsrc/box.png"); tiles[49]=IMG_Load("rsrc/door1.png"); tiles[50]=IMG_Load("rsrc/door2.png"); for(map::iterator it=tiles.begin();it!=tiles.end();it++) { Palettize(it->second); } SDL_Rect gf_rect, valentin_rect; gf_rect.x=0; gf_rect.y=0; valentin_rect.x=180; valentin_rect.y=300; //yeah, loadwav also loads ogg, i'm thinking the same Mix_Chunk * sample = Mix_LoadWAV("rsrc/audio.ogg"); LoadImage("rsrc/lliurex.png",true); LoadImage("rsrc/face0.png",true); LoadImage("rsrc/face1.png",true); LoadImage("rsrc/face2.png",true); LoadImage("rsrc/face3.png",true); LoadImage("rsrc/face4.png",true); LoadImage("rsrc/face5.png",true); LoadImage("rsrc/face6.png",true); LoadImage("rsrc/face7.png",true); LoadImage("rsrc/face8.png",true); LoadImage("rsrc/face9.png",true); LoadImage("rsrc/face10.png",true); LoadImage("rsrc/face11.png",true); LoadImage("rsrc/face12.png",true); LoadImage("rsrc/face13.png",true); LoadImage("rsrc/captain.png",true); messages.push_back({"L'equip LliureX ...",NULL,2}); messages.push_back({"esta orgullós de presentar",NULL,2}); messages.push_back({"un any més",NULL,2}); messages.push_back({"LliureX 12.06 Nemo",images["rsrc/lliurex.png"],4}); messages.push_back({"I aquest és l'equip ...",NULL,2}); messages.push_back({"que l'ha fet posible:",NULL,2}); messages.push_back({"Enrique M.G. (Lt_Henry)...",images["rsrc/face0.png"],2}); messages.push_back({"programador i dissenyador",images["rsrc/face0.png"],2}); messages.push_back({"Héctor G.H. (Cless)...",images["rsrc/face1.png"],2}); messages.push_back({"programador i dissenyador",images["rsrc/face1.png"],2}); messages.push_back({"Alfons L.S. (Turix)... ",images["rsrc/face6.png"],2}); messages.push_back({"sysadmin i técnic de só",images["rsrc/face6.png"],2}); messages.push_back({"Raul R.S. (Kbut)...",images["rsrc/face3.png"],2}); messages.push_back({"programador",images["rsrc/face3.png"],2}); messages.push_back({"Angel B.V. (X4ng3l)...",images["rsrc/face5.png"],2}); messages.push_back({"programador i pooladmin",images["rsrc/face5.png"],2}); messages.push_back({"Pilar E.G. ...",images["rsrc/face2.png"],2}); messages.push_back({"traductora",images["rsrc/face2.png"],2}); messages.push_back({"Miguel G.G. ...",images["rsrc/face11.png"],2}); messages.push_back({"programador",images["rsrc/face11.png"],2}); messages.push_back({"Jaime N.F. ...",images["rsrc/face9.png"],2}); messages.push_back({"programador",images["rsrc/face9.png"],2}); messages.push_back({"David M.F. ...",images["rsrc/face7.png"],2}); messages.push_back({"sysadmin",images["rsrc/face7.png"],2}); messages.push_back({"Nacho V.H. ...",images["rsrc/face12.png"],2}); messages.push_back({"sysadmin",images["rsrc/face12.png"],2}); messages.push_back({"Vicent B.D. ...",images["rsrc/face4.png"],2}); messages.push_back({"asistencia técnica",images["rsrc/face4.png"],2}); messages.push_back({"Soledad G.G. ...",images["rsrc/face10.png"],2}); messages.push_back({"documentació",images["rsrc/face10.png"],2}); messages.push_back({"Jose Carlos G.M. ...",images["rsrc/face8.png"],2}); messages.push_back({"coordinació",images["rsrc/face8.png"],2}); messages.push_back({"Salvador A.B. ...",images["rsrc/face13.png"],2}); messages.push_back({"direcció",images["rsrc/face13.png"],2}); messages.push_back({"Especial agraïments a:",NULL,4}); messages.push_back({"Xavi Grau",NULL,1}); messages.push_back({"Samuel Soriano (EDUTICTAC)",NULL,1}); messages.push_back({"Daniel Turienzo (EDUTICTAC)",NULL,1}); messages.push_back({"Alfons Rovira (EDUTICTAC)",NULL,1}); messages.push_back({"Manuel Martinez (El teu mestre)",NULL,1}); messages.push_back({"L'equip de Guadalinex",NULL,1}); messages.push_back({"I a tots el usuaris de LliureX",NULL,3}); messages.push_back({". . .",NULL,2}); messages.push_back({"Ens veiem a LliureX 13 Pandora !!!",NULL,3}); messages.push_back({"\"No necessitem continents nous...",NULL,3}); messages.push_back({"...sinó persones noves\"",NULL,3}); messages.push_back({"Capità Nemo",images["rsrc/captain.png"],3}); messages.push_back({"20.000 llegues de viatge submari",images["rsrc/captain.png"],4}); messages.push_back({"EOF",NULL,0}); current_message=0; // ******* END LOADING RESOURCES ********** // int world_movement_x=0; bool run=true; bool walking; int walk_count=0; SDL_Event event; int fps; int frames=0; int ticks = SDL_GetTicks(); int t1; int frame_ms; int next_message=0; cout << "Entering main loop..." << endl; Mix_PlayChannel(-1,sample,0); while(run) { t1 = SDL_GetTicks(); if( (t1-ticks)>1000 ) { fps=frames; frames=0; ticks=t1; //cout<<"fps:"<=messages[current_message].wait) { current_message++; next_message=0; } } frames++; while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_RIGHT: world_movement_x=2; walking=true; break; case SDLK_LEFT: world_movement_x=-4; break; case SDLK_ESCAPE: run=false; break; default: break; } break; case SDL_KEYUP: switch(event.key.keysym.sym) { case SDLK_RIGHT: walking=false; walk_count=0; world_movement_x=0; break; case SDLK_LEFT: walking=false; walk_count=0; world_movement_x=0; break; case SDLK_ESCAPE: run=false; break; default: break; } break; case SDL_QUIT: run=false; break; default: break; } } //forced walking animation walking=true; world_movement_x=2; if(walking) { if(walk_count<29) walk_count++; else walk_count=0; } // CALCULATING MOVEMENT game_scene.pos_x+=world_movement_x; if (game_scene.pos_x<0) game_scene.pos_x=0; // DRAW SCENE level.delta_x++; if(level.delta_x==32) { level.px++; //if(level.px>=level.width-10)level.px=0; level.delta_x=0; } for(int i=0;i<11;i++) { for(int j=0;j<8;j++) { SDL_Rect tmp_rect; tmp_rect.x=DEFAULT_X+(32*i)-level.delta_x; tmp_rect.y=DEFAULT_Y+(32*j); int dx = (level.px+i) % level.width; SDL_BlitSurface(tiles[level.data[dx+j*level.width]],NULL,screen,&tmp_rect); } } SDL_BlitSurface(game_frame,0,screen,&gf_rect); if(!walking) SDL_BlitSurface(valentin,0,screen,&valentin_rect); else { if (walk_count < 5 && walk_count >=0 ) SDL_BlitSurface(valentin_walk1,0,screen,&valentin_rect); if (walk_count < 10 && walk_count >=5 ) SDL_BlitSurface(valentin_walk2,0,screen,&valentin_rect); if (walk_count < 15 && walk_count >=10 ) SDL_BlitSurface(valentin_walk3,0,screen,&valentin_rect); if (walk_count < 20 && walk_count >=15 ) SDL_BlitSurface(valentin_walk4,0,screen,&valentin_rect); if (walk_count < 25 && walk_count >=20 ) SDL_BlitSurface(valentin_walk5,0,screen,&valentin_rect); if (walk_count < 30 && walk_count >=25 ) SDL_BlitSurface(valentin_walk6,0,screen,&valentin_rect); } SDL_Color fg={62,73,13,0xff}; SDL_Rect tr; tr.x=DEFAULT_X+160; tr.y=DEFAULT_Y+100; if(messages[current_message].text!="EOF") { text_surface = TTF_RenderUTF8_Solid(font,messages[current_message].text.c_str(),fg); tr.x=tr.x-(text_surface->w/2); SDL_BlitSurface(text_surface,NULL,screen,&tr); SDL_FreeSurface(text_surface); if(messages[current_message].image!=NULL) { tr.y=DEFAULT_Y+120; tr.x=DEFAULT_X+160; SDL_BlitSurface(messages[current_message].image,NULL,screen,&tr); } } else { run=false; } SDL_Flip(screen); frame_ms = SDL_GetTicks() - t1; frame_ms=abs(17 - frame_ms); SDL_Delay(frame_ms); } cout << "Game over!" << endl; //loading resources should be here Mix_FreeChunk(sample); for(map::iterator it=tiles.begin();it!=tiles.end();it++) { SDL_FreeSurface(it->second); } for(map::iterator it=images.begin();it!=images.end();it++) { SDL_FreeSurface(it->second); } SDL_Quit(); return 0; }