Help with SFML

Started by
2 comments, last by Red Ghost 15 years, 2 months ago
I have been coding a 2d tile based puzzle game using SFML and I am having problems getting my code to work. It compiles and runs but upon launching the app nothing will display [depressed]. Code follows
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

const int TILE_WIDTH = 64;
const int TILE_HEIGHT = 64;

const int TILE_CYAN = 0;
const int TILE_MAGENTA = 1;
const int TILE_YELLOW = 2;
const int TILE_CENTRE = 3;
const int TILE_TOP = 4;
const int TILE_TOPRIGHT = 5;
const int TILE_RIGHT = 6;
const int TILE_BOTTOMRIGHT = 7;
const int TILE_BOTTOM = 8;
const int TILE_BOTTOMLEFT = 9;
const int TILE_LEFT = 10;
const int TILE_TOPLEFT = 11;


class Tile
{

public:
	Tile(const int col, const int row, int TileType);
	int GetType();
	sf::Sprite Sprite;
	void SetPos(int row, int col);
	int offsetX;
	int offsetY;
private:
	int Type;
};

Tile::Tile(const int col, const int row, int TileType)
{
	sf::Image TileMap;
	TileMap.LoadFromFile("tileset.bmp");
	Sprite.SetCenter(0, 0);
	Sprite.SetImage(TileMap);
	Sprite.SetPosition(static_cast<float>(col * TILE_WIDTH), static_cast<float>(row * TILE_HEIGHT)); 
	Type = TileType;
	switch(Type)
	{
	case TILE_CYAN:
		offsetX = 0;
		offsetY = 0;
		break;
	case TILE_MAGENTA:
		offsetX = 0;
		offsetY = 1;
		break;
	case TILE_YELLOW:
		offsetX = 0;
		offsetY = 2;
		break;
	case TILE_TOPRIGHT:
		offsetX = 3;
		offsetY = 0;
		break;
	case TILE_RIGHT:
		offsetX = 3;
		offsetY = 1;
		break;
	case TILE_BOTTOMRIGHT:
		offsetX = 3;
		offsetY = 2;
		break;
	case TILE_BOTTOM:
		offsetX = 2;
		offsetY = 2;
		break;
	case TILE_BOTTOMLEFT:
		offsetX = 1;
		offsetY = 2;
		break;
	case TILE_LEFT:
		offsetX = 1;
		offsetY = 1;
		break;
	case TILE_TOPLEFT:
		offsetX = 1;
		offsetY = 0;
		break;
	case TILE_TOP:
		offsetX = 2;
		offsetY = 0;
		break;
	case TILE_CENTRE:
		offsetX = 2;
		offsetY = 1;
		break;
	}

	Sprite.SetSubRect(sf::IntRect(offsetX * TILE_WIDTH, offsetY * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT));
}

int Tile::GetType()
{
	return Type;
}

void Tile::SetPos(int row, int col)
{
	Sprite.SetPosition(static_cast<float>(col * TILE_WIDTH), static_cast<float>(row * TILE_HEIGHT));
}

int main()
{
	sf::RenderWindow App(sf::VideoMode(1024, 640, 32), "Cool McAwesome's Adventure");

	Tile Tile1(1,1,1);

	while (App.IsOpened())
	{
		sf::Event Event;
		while (App.GetEvent(Event))
		{
			if (Event.Type = Event.Closed)
			{
				App.Close();
			}
		}

		App.Clear();

		App.Draw(Tile1.Sprite);

		App.Display();
	}
	return 0;
}

Advertisement
Hi,

Looking through your code, here are a few questions that will help you find out your problems:

- what is the constructor of sf::IntRect ? Do you pass it correct values ?
- what will be the behavior of your program when you close the window ?

Hope that helps.
Ghostly yours,
Red.
Ghostly yours,Red.
Thanks for those tips, got it working
Also, how would I go about making an array of pointers to tile objects?
Hi,

Making a tile array is a no brainer:

- either you make a one dimension pointer array (knowing the map width in tiles)
Tile _Map[MAP_WIDTH*MAP_HEIGHT];//returns NULL pointer when the coordinates are beyond the mapTile* GetTile(int pCol, int pRow){   if (pRow > MAP_HEIGHT)      return 0;   if (pCol > MAP_WIDTH)      return 0;   return &_Map[pCol + (pRow * MAP_WIDTH)];}


- or you make a two dimension pointer array.
Tile _Map[MAP_WIDTH][MAP_HEIGHT];//returns NULL pointer when the coordinates are beyond the mapTile* GetTile(int pCol, int pRow){   if (pRow > MAP_HEIGHT)      return 0;   if (pCol > MAP_WIDTH)      return 0;   return &_Map[pCol][pRow];}


What is more interesting is adding features to that tile array like:

- tile coloring to show zones (like firing, sight or movement areas)
- path-finding

As a side note, you can improve your map system. I propose a tile structure and a map class:
typedef struct{  int _Type; //TILE_CYAN, ... You could also declare types in a enumeration} TILE;class Map{   public:      //Map constructor      //initiates all the TILE array with a basic tile type      Map(sf::RenderWindow& pWindow)      //Map destructor      ~Map(void);      //use your tile constructor to populate the STL map of sprites      bool InitSprites(std::string& pFilename);      //sets a TILE to a type      //also controls that pType is an allowed value      void SetType(int pCol, int pRow, int pType);      //returns a TILE type      const int GetType(int pCol, in pRow);      //Draws the Map      void Render(void)      {         sf::Sprite* tSprite = 0;         for (int i = 0; i < MAP_WIDTH*MAP_HEIGHT; i++)            {            tSprite = &_Sprites[_TileMap];            tSprite->SetPosition((i % MAP_WIDTH)*TILE_WIDTH, (i / MAP_WIDTH)*TILE_HEIGHT);            _Window->Draw(*tSprite);            }      };    private:      sf::RenderWindow* _Window;      std::map<int, sf::Sprite> _Sprites;      TILE _TileMap[MAP_WIDTH*MAP_HEIGHT];};


Hope that helps.

Ghostly yours,
Red.
Ghostly yours,Red.

This topic is closed to new replies.

Advertisement