Kaktusen

unnamed game

May 26th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include "SDL/SDL.h"
  2. #include <ctime>
  3. #include <stdlib.h>
  4.  
  5. #include "player.cpp"
  6. #include "platform.h"
  7.  
  8. SDL_Surface *tileset = SDL_LoadBMP("images/tileset.bmp");
  9. SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  10.  
  11. void load_sprite(int x, int y, SDL_Surface *source, SDL_Surface *destination, int bx, int by, int width, int height) {
  12.     SDL_Rect rect;
  13.     SDL_Rect offset;
  14.  
  15.     offset.x = x;
  16.     offset.y = y;
  17.  
  18.     rect.x = bx;
  19.     rect.y = by;
  20.     rect.w = width;
  21.     rect.h = height;
  22.  
  23.     SDL_BlitSurface(source, &rect, destination, &offset);
  24. }
  25.  
  26. void game() {
  27.   srand(time(NULL));
  28.   Uint8 *key;
  29.   bool gameLoop = true;
  30.  
  31.   SDL_Surface *background = SDL_LoadBMP("images/background.bmp");
  32.  
  33.   Uint32 colorkey = SDL_MapRGB(tileset -> format, 255, 0, 255);
  34.   SDL_SetColorKey(tileset, SDL_SRCCOLORKEY, colorkey);
  35.  
  36.   cPlayer.x = 640/2-16;
  37.   cPlayer.y = 480-32;
  38.   cPlayer.xspeed = 10;
  39.   cPlayer.yspeed = 50;
  40.   cPlayer.gravity = 5;
  41.   cPlayer.grav = true;
  42.  
  43.   cPlatform.x[1] = 10;
  44.   cPlatform.y[1] = 480-32;
  45.  
  46.   cPlatform.x[2] = 60;
  47.   cPlatform.y[2] = 480-32;
  48.  
  49.   cPlatform.x[3] = 320;
  50.   cPlatform.y[3] = 480-32;
  51.  
  52.   int mouse_x = 55;
  53.   int mouse_y = 55;
  54.  
  55.   int currentTick = SDL_GetTicks();
  56.   int maxFPS = 30;
  57.  
  58.   while(gameLoop) {
  59.     SDL_PumpEvents();
  60.  
  61.     key = SDL_GetKeyState(NULL);
  62.     SDL_GetMouseState(&mouse_x, &mouse_y);
  63.  
  64.     currentTick = SDL_GetTicks();
  65.  
  66.     load_sprite(0, 0, background, screen, 0, 0, 640, 480);
  67.    
  68.     /*
  69.     for(int i=0; i<=3; i++) {
  70.       load_sprite(cPlatform.x[i], cPlatform.y[i], tileset, screen, 32+2, 1, 32, 32);
  71.     }
  72.     */
  73.  
  74.     load_sprite(cPlayer.x, cPlayer.y, tileset, screen, 1, 1, 32, 32);
  75.  
  76.     playerMovment();
  77.  
  78.     if(key[SDLK_ESCAPE]) {
  79.       gameLoop = false;
  80.     }
  81.  
  82.     if(SDL_GetTicks() - currentTick < 1000 / maxFPS) {
  83.       SDL_Delay(((1000/maxFPS)-(SDL_GetTicks() - currentTick)));
  84.     }
  85.  
  86.     SDL_Flip(screen);
  87.  
  88.  
  89.   }
  90.  
  91.   SDL_FreeSurface(tileset);
  92.   SDL_FreeSurface(background);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment