0 Replies - 201 Views - Last Post: 31 August 2007 - 04:45 AM

#1 Tomas   User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 12-June 07

SDL and C++ Tiling System

Posted 31 August 2007 - 04:45 AM

Description: Firstly you must download the SDL library from the SDL website, which follows:
http://www.libsdl.org

next copy the code into the appropriate files: tile.h and tile.cpp

Make sure to include SDL.lib and SDLmain.lib in your project settings
and then compile.

It may not work at first due to the fact that this was just taken from withiin my own program, however if you play with it a bit until it is implemented correctly it should work just fine.a simple but very effective method of tiling and image in C++ and SDL
//tile.h
#ifndef TILE_H
#define TILE_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>

#include <SDL/SDL.h>

struct tile{ //a struct to declare the positions of each tile
SDL_Surface* tilesurface; //new SDL surface pointer
int topleftX;
int topleftY;
int toprightX;
int toprightY;
int botleftX;
int botleftY;
int botrightX;
int botrightY;
int id;
};

class tiles{ //a new class tiles
public:
tiles();
~tiles();
void tilegraphic();
void blitgraphic();
protected:
tile screentiles[32][24]; //an array refering to the struct tile
SDL_Surface* grass; //a new SDL Surface pointer
SDL_Surface* screen; //a new SDL Surface pointer
SDL_Rect destrect;
int prevX;
int prevY;
};

#endif



//tile.cpp
#ifndef TILE_CPP
#define TILE_CPP

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>

#include <SDL/SDL.h>

#include "tile.h"

using namespace std;

tiles::tiles(){
    grass = SDL_LoadBMP("grass.bmp"); //this makes the SDL Surface grass equal to the image grass.bmp
    prevX = 0;
    prevY = 0;  
}

tiles::~tiles(){}

void tiles::tilegraphic(){
     int x = 0;
     int y = 0;
     int id = 0; 

     while(y < 24){ //a while loop which applies the image grass to each tile and gives it its own unique ID and then moves to the next tile
             x = 0;
             while(x < 32){
                                  screentiles[x][y].id = id;
                                  screentiles[x][y].tilesurface = grass;
                                  if(screentiles[x][y].tilesurface != grass){
                                      cout << "Tiling failed!!" << endl;
                                  }
                                  screentiles[x][y].passable = true;
                                  
                                  screentiles[x][y].topleftX = prevX;
                                  screentiles[x][y].topleftY = prevY;
                                  
                                  screentiles[x][y].toprightX = prevX + 32;
                                  screentiles[x][y].toprightY = prevY;
                                  
                                  screentiles[x][y].botleftX = prevX;
                                  screentiles[x][y].botleftY = prevY + 32;
                                  
                                  screentiles[x][y].botrightX = prevX + 32;
                                  screentiles[x][y].botrightY = prevY + 32;
                                  
                                  prevX = prevX + 32;
                                  id++;
                                  x++;                        
                      }
                      prevX = 0;
                      prevY = prevY + 32;
                      y++;
     }
}

void tiles::blitgraphic(){ //this function actually blits the image grass to each tiles and then blits the entire surface to SDL_Surface screen
     int x;
     int y = 0;
     cout << "Tiling started" << endl;
     while(y < 24){
             x = 0;
             while(x < 32){
                             destrect.x = screentiles[x][y].topleftX;
                             destrect.y = screentiles[x][y].topleftY;
                             SDL_BlitSurface(screentiles[x][y].tilesurface, NULL, screen, &destrect);
                             x++;
             }
             y++;
             
     }
     cout << "Tiling ended" << endl;
}

#endif




Is This A Good Question/Topic? 0
  • +

Page 1 of 1