All pastes #450627 Raw Edit

GameDev: Tile Loader

public text v1 · immutable
·published 2007-04-20 23:51 UTC
rendered paste body
#define TILE_GRASS 400
#define TILE_DIRT 401
#define TILE_GRAVEL 402
#define TILE_TABLE 403

#define NUM_TILES 5

#define TILE_WIDTH 25
#define TILE_HEIGHT 25
#define LEVEL_WIDTH 800
#define LEVEL_HEIGHT 600

class CTile { 

 private: 

  SDL_Rect *r;
  int type;  
  SDL_Surface *image; 

 public: 

 CTile( int x, int y, int TYPE, SDL_Surface *img ) {
  r = new SDL_Rect();
  r->x = x; 
  r->y = y;
  type = TYPE;
  image = img;
 };

 void draw() {
  apply_surface(r->x,r->y,screen,image);
 };

 void getType() {
  return type;
 };

 SDL_Rect *getRect() {
  return r;
 }; 
};

bool set_tiles( Tile *tiles[] ) { 

 SDL_Surface *grass = load_image("map_grass.bmp");
 SDL_Surface *dirt = load_image("map_dirt.bmp");
 SDL_Surface *gravel = load_image("map_gravel.bmp");
 SDL_Surface *table = load_image("map_table.bmp");

 int x = 0, y = 0; 

 std::ifstream map( "gamemap.lvl" ); 

 if( map == NULL ) { return false; }

 for( int t = 0; t < NUM_TILES; t++ ) { 

  int ttype = -1; 

  map >> tile; // this loads 1 int, eg in a file "12 13 928 922 123", it would load "12"  

  if( map.fail() == true ) { return false; }

  if(ttype = TILE_GRASS) 
   tiles[t] = new CTile(x, y, ttype, grass);
  if(ttype = TILE_DIRT)
   tiles[t] = new CTile(x, y, ttype, dirt);
  if(ttype = TILE_GRAVEL)
   tiles[t] = new CTile(x, y, ttype, gravel);
  if(ttype = TILE_TABLE)
   tiles[t] = new CTile(x, y, ttype, gravel);

  x += TILE_WIDTH; 

  // If we've gone too far 
  if( x >= LEVEL_WIDTH ) { 

   // Move back 
   x = 0; 

   // Move to the next row
   y += TILE_HEIGHT; 
  }
 }

}