Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //you need a chessimage.bmp 1920*1920 image
- #include <SDL/SDL.h>
- #include <iostream>
- bool collision(SDL_Rect* rect1,SDL_Rect* rect2)
- {
- if(rect1->y >= rect2->y + rect2->h)
- return 0;
- if(rect1->x >= rect2->x + rect2->w)
- return 0;
- if(rect1->y + rect1->h <= rect2->y)
- return 0;
- if(rect1->x + rect1->w <= rect2->x)
- return 0;
- return 1;
- }
- int main(int argc,char** argv)
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
- bool running=true;
- Uint32 start;
- SDL_Event event;
- bool b[4]={0,0,0,0};
- SDL_Surface* chessImage=SDL_LoadBMP("chessimage.bmp");
- if(!chessImage)
- std::cout << "You don't ave chessimage.bmp" << std::endl;
- SDL_Rect camera={0,0,640,480};
- SDL_Rect absCoord={0,0,640,480};
- SDL_Rect playerpos={320,240,20,20};
- SDL_Rect rec={100,100,20,20};
- while(running)
- {
- start=SDL_GetTicks();
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- running=false;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- running=false;
- break;
- case SDLK_UP:
- b[0]=true;
- break;
- case SDLK_RIGHT:
- b[1]=true;
- break;
- case SDLK_DOWN:
- b[2]=true;
- break;
- case SDLK_LEFT:
- b[3]=true;
- break;
- }
- break;
- case SDL_KEYUP:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- running=false;
- break;
- case SDLK_UP:
- b[0]=false;
- break;
- case SDLK_RIGHT:
- b[1]=false;
- break;
- case SDLK_DOWN:
- b[2]=false;
- break;
- case SDLK_LEFT:
- b[3]=false;
- break;
- }
- break;
- }
- }
- if(b[0])
- {
- if(playerpos.y>=30)
- playerpos.y-=3;
- else
- {
- absCoord.y-=3;
- camera.y-=3;
- }
- }
- if(b[1])
- {
- if(playerpos.x<=610)
- playerpos.x+=3;
- else
- {
- absCoord.x+=3;
- camera.x+=3;
- }
- }
- if(b[2])
- {
- if(playerpos.y<=450)
- playerpos.y+=3;
- else
- {
- absCoord.y+=3;
- camera.y+=3;
- }
- }
- if(b[3])
- {
- if(playerpos.x>=30)
- playerpos.x-=3;
- else
- {
- absCoord.x-=3;
- camera.x-=3;
- }
- }
- if(camera.x>1920-640) //image width: 1920, screen-width: 640
- camera.x=0;
- if(camera.y>1920-480)
- camera.y=0;
- if(camera.x<0)
- camera.x=1920-640;
- if(camera.y<0)
- camera.y=1920-640;
- SDL_Rect tmpRec={rec.x-absCoord.x,rec.y-absCoord.y,rec.w,rec.h};
- if(collision(&tmpRec,&playerpos))
- std::cout << "collision" << std::endl;
- SDL_BlitSurface(chessImage,&camera,screen,NULL);
- SDL_FillRect(screen,&playerpos,SDL_MapRGB(screen->format,255,0,0));
- SDL_FillRect(screen,&tmpRec,600);
- SDL_Flip(screen);
- if(1000/30>(SDL_GetTicks()-start))
- SDL_Delay(1000/30-(SDL_GetTicks()-start));
- }
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement