Kaktusen

PickNsquare

Oct 27th, 2011
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include "SDL/SDL.h"
  2. #include <ctime>
  3. #include <stdlib.h>
  4.  
  5. #include "surfaces.h"
  6. #include "classes.h"
  7.  
  8. void load_sprite(int x, int y, int bx, int by, int width, int height, SDL_Surface *source, SDL_Surface *destination) {
  9.     SDL_Rect rect;
  10.     SDL_Rect offset;
  11.  
  12.     rect.x = bx;
  13.     rect.y = by;
  14.     rect.w = width;
  15.     rect.h = height;
  16.  
  17.     offset.x = x;
  18.     offset.y = y;
  19.  
  20.     SDL_BlitSurface(source, &rect, destination, &offset);
  21. }
  22.  
  23. void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination) {
  24.     SDL_Rect rect;
  25.  
  26.     rect. x = x;
  27.     rect.y = y;
  28.  
  29.     SDL_BlitSurface(source, NULL, destination, &rect);
  30. }
  31.  
  32. int main(int argc, char *args[]) {
  33.     SDL_Init(SDL_INIT_EVERYTHING);
  34.  
  35.     srand(time(NULL));
  36.  
  37.     Uint8 *key;
  38.     bool gameLoop = true;
  39.     SDL_Event event;
  40.  
  41.     //Load classes.h var's
  42.     cPlayer.x = 640/2;
  43.     cPlayer.y = 480/2;
  44.  
  45.     cPoint.x = rand() % (640 - 16) + 0;
  46.     cPoint.y = rand() % (480 - 16) + 0;
  47.  
  48.     apply_surface(0, 0, background, screen);
  49.     SDL_Flip(screen);
  50.  
  51.     //Gameloop
  52.     while(gameLoop == true) {
  53.  
  54.         SDL_PumpEvents();
  55.         key = SDL_GetKeyState(NULL);
  56.  
  57.         if(key[SDLK_DOWN] || key[SDLK_s]) {
  58.             cPlayer.y++;
  59.         }
  60.  
  61.         if(key[SDLK_UP] || key[SDLK_w]) {
  62.             cPlayer.y--;
  63.         }
  64.  
  65.         if(key[SDLK_RIGHT] || key[SDLK_d]) {
  66.             cPlayer.x++;
  67.         }
  68.  
  69.         if(key[SDLK_LEFT] || key[SDLK_a]) {
  70.             cPlayer.x--;
  71.         }
  72.  
  73.         if(key[SDLK_ESCAPE]) {
  74.             gameLoop = false;
  75.         }
  76.  
  77.         if(event.type == SDL_QUIT) {
  78.             gameLoop = false;
  79.         }
  80.  
  81.         apply_surface(0, 0, background, screen);
  82.         load_sprite(cPlayer.x, cPlayer.y, 0, 0, 16, 16, tileset, screen);
  83.         load_sprite(cPoint.x, cPoint.y, 16, 0, 16, 16, tileset, screen);
  84.  
  85.         if(cPlayer.y > 480) {
  86.             cPlayer.y = 0;
  87.         } else if(cPlayer.y < 0) {
  88.             cPlayer.y = 480;
  89.         }
  90.  
  91.         if(cPlayer.x > 640) {
  92.             cPlayer.x = 0;
  93.         } else if(cPlayer.x < 0) {
  94.             cPlayer.x = 640;
  95.         }
  96.  
  97.         if(cPoint.y>cPlayer.y && cPoint.y<cPlayer.y+16) {
  98.             if(cPoint.x>cPlayer.x && cPoint.x<cPlayer.x+16) {
  99.                 cPoint.x = rand() % (640 - 16) + 0;
  100.                 cPoint.y = rand() % (480 - 16) + 0;
  101.             }
  102.         }
  103.  
  104.         SDL_Delay(30/1000);
  105.  
  106.         SDL_Flip(screen);
  107.  
  108.     }
  109.  
  110.     SDL_FreeSurface(background);
  111.     SDL_FreeSurface(tileset);
  112.  
  113.     SDL_Quit();
  114. }
Advertisement
Add Comment
Please, Sign In to add comment