Guest User

Untitled

a guest
May 22nd, 2012
21
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "SDL/SDL.h"
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. #define SCREEN_WIDTH  400
  6. #define SCREEN_HEIGHT 200
  7. #define SPRITE_SIZE    10
  8.  
  9. using namespace std;
  10.  
  11. vector<vector<int> > generateMap() {
  12.   vector<vector<int> > vMap;
  13.   int horizPos;
  14.   int vertPos=rand()%(SCREEN_HEIGHT/SPRITE_SIZE);
  15.   for(int i=0;i<(SCREEN_WIDTH/SPRITE_SIZE);i++) {
  16.     vector<int> vSubmap;
  17.     horizPos=i;
  18.     for(int j=0;j<(SCREEN_HEIGHT/SPRITE_SIZE)-1;j++) {
  19.       if(!vSubmap.empty()) {
  20.         vSubmap.clear();
  21.       }
  22.       if(j<vertPos) {
  23.         vSubmap.push_back(1);
  24.       }
  25.       else if(j==vertPos) {
  26.         vSubmap.push_back(2);
  27.       }
  28.       else if(j>vertPos) {
  29.         vSubmap.push_back(0);
  30.       }
  31.     }
  32.     vMap.push_back(vSubmap);
  33.   }
  34. }
  35.  
  36. int main (int argc,char *argv[]) {
  37.   SDL_Surface *screen, *temp;
  38.   SDL_Rect rcTile;
  39.   SDL_Event event;
  40.   Uint8 *keystate;
  41.   int gameRunning=1;
  42.  
  43.   SDL_Init(SDL_INIT_VIDEO);
  44.   SDL_WM_SetCaption("SDL TILE TEST","");
  45.   screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
  46.  
  47.   vector<vector<int> > tileMap;
  48.   tileMap=generateMap();
  49.   /*
  50.   TileA     Sky
  51.   TileB     Dirt
  52.   TileC     Grass
  53.   */
  54.  
  55.   temp=SDL_LoadBMP("sky.bmp");
  56.   SDL_Surface *TileA =SDL_DisplayFormat(temp);
  57.   SDL_FreeSurface(temp);
  58.  
  59.   temp=SDL_LoadBMP("dirt.bmp");
  60.   SDL_Surface *TileB =SDL_DisplayFormat(temp);
  61.   SDL_FreeSurface(temp);
  62.  
  63.   temp=SDL_LoadBMP("grass.bmp");
  64.   SDL_Surface *TileC =SDL_DisplayFormat(temp);
  65.   SDL_FreeSurface(temp);
  66.  
  67.   while (gameRunning) {
  68.     if (SDL_PollEvent(&event)) {
  69.       switch (event.type) {
  70.         case SDL_QUIT:
  71.           gameRunning=0;
  72.           break;
  73.         case SDL_KEYDOWN:
  74.           switch (event.key.keysym.sym) {
  75.             case SDLK_ESCAPE:
  76.             case SDLK_q:
  77.               gameRunning=0;
  78.               break;
  79.           }
  80.           break;
  81.       }
  82.     }
  83.  
  84.     /*
  85.     keystate = SDL_GetKeyState(NULL);
  86.     if (keystate[SDLK_LEFT] ) {
  87.       //doshit
  88.     }*/
  89.    
  90.        
  91.     for (int x = 0; x < SCREEN_WIDTH / SPRITE_SIZE; x++) {
  92.       for (int y = 0; y < SCREEN_HEIGHT / SPRITE_SIZE; y++) {
  93.         rcTile.x = x * SPRITE_SIZE;
  94.         rcTile.y = y * SPRITE_SIZE;
  95.         if(tileMap[x][y]==0) {
  96.           SDL_BlitSurface(TileA,NULL,screen,&rcTile);
  97.         }
  98.         else if(tileMap[x][y]==1) {
  99.           SDL_BlitSurface(TileB,NULL,screen,&rcTile);
  100.         }
  101.         else if(tileMap[x][y]==2) {
  102.           SDL_BlitSurface(TileC, NULL, screen, &rcTile);
  103.         }
  104.       }
  105.     }
  106.     /* draw the sprite */
  107.     // SDL_BlitSurface(TileA, NULL, screen, &rcTileA);
  108.  
  109.     /* update the screen */
  110.     SDL_UpdateRect(screen, 0, 0, 0, 0);
  111.   }
  112.  
  113.   /* clean up */
  114.   SDL_FreeSurface(TileA);
  115.   SDL_FreeSurface(TileB);
  116.   SDL_FreeSurface(TileC);
  117.   SDL_Quit();
  118.   return 0;
  119. }
RAW Paste Data