• (2 Pages)
  • +
  • 1
  • 2

C++ Tile Engine from Scratch -- Part 2

#1 RevTorA  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 76
  • View blog
  • Posts: 246
  • Joined: 22-April 11

Posted 03 May 2011 - 06:15 PM

*
POPULAR

C++ Tile Engine from Scratch -- Part 2
Now we have some base code on which we can build our engine. Since I'm sure you're eager to get something drawing on the screen, let's make that the objective for this tutorial.

By the way, if you haven't read the parts of the tutorial preceding this, I suggest you do that here

SFML Graphics
We already saw a couple of features from the SFML api in the last tutorial, namely sf::Renderwindow. In this tutorial, we'll be looking at the basic graphics api available to us.

The first class we'll need is sf::Image, which is used for loading, manipulating and saving images. We can store images, load images from a file, save images to a file, even manipulate individual pixels in the image all with the Image class. All you need to do is create an instance of an Image, like so: sf::Image myImage;. Once you've created an instance, you can fill it with information in a number of ways, but the method we'll be using first is loading an image from a file. Doing this is a simple command: myImage.LoadFromFile(filename)

Once you've loaded an image, time to draw right? Not yet! Since Images are large, slow objects, SFML wraps them with a smaller and faster class called sf::Sprite. The Sprite defines how to draw the image. You can define where to draw it, what portion of the image to draw, you can scale it, rotate it, flip it, and more. The best part is that it's fast, because Sprites don't actually contain any of the data. Instead, it stores a reference to an Image, and uses its data to figure out what to draw.

There's a catch however. Since Sprites don't actually contain a copy of the Image, if the Image is destroyed (by leaving its scope for instance), the Sprite won't have anything to draw. We could store a copy of each image for every sprite, but that would be inefficient. However, we need to make sure that any Images we use for sprites remain intact for as long as we need it.

The solution is to have an Image manager class. This class will hold a vector of Images, and we can use it to add or retrieve Images to use with our Sprites. So, let's create a new header file for our class, and call it "ImageManager.h".

#include <vector>
#include <SFML\Graphics.hpp>

class ImageManager
{
private:
	vector<sf::Image> imageList;

public:
	ImageManager();
	~ImageManager();

	void AddImage(sf::Image& image);
	sf::Image& GetImage(int index);
}

#endif


Not a lot to this class at the moment, but this will suffice for now. We're using a vector to keep all of our images so we can dynamically allocate them, and the class implements two accessors for adding and retrieving Images. It is important to note that we are manipulating the Images by reference. If we were passing the Images around by value, we would be creating copies of Images, and it would be hard to manage them.

Now let's add in the code for this class in a new code file called "ImageManager.cpp".
#include "ImageManager.h"
#include <vector>
#include <SFML\Graphics.hpp>

ImageManager::ImageManager()
{

}

ImageManager::~ImageManager()
{

}

void ImageManager::AddImage(sf::Image& image)
{
	imageList.push_back(image);
}

sf::Image& ImageManager::GetImage(int index)
{
	return imageList[index];
}


Nothing too complicated.

Setting up the Tile class
Now that we have our ImageManager, we can move on to how we're going to draw them to the screen. Since this is a Top-down 2D tile engine, our "levels" will consist of a 2D grid of tiles. So let's go ahead and create a Tile class in a new header file called "Tile.h".
#ifndef _TILE_H
#define _TILE_H

#include <SFML\Graphics.hpp>

class Tile
{
private:
	sf::Sprite baseSprite;

public:
	Tile(sf::Image& image);
	~Tile();

	void Draw(int x, int y, sf::RenderWindow* rw);
}

#endif


Remember that we're just laying out a basic skeleton, so we don't need to add all the functionality we can think of right away. So for now, our Tile only has a Sprite. We have our constructor/destructor, and then a Draw method. The Draw method will, you guessed it, draw the tile to the RenderWindow at the specified x and y coordinates. Let's write out the code for the Tile class now in a new code file called "Tile.cpp"
#include "Tile.h"
#include <SFML\Graphics.hpp>

Tile::Tile(sf::Image& image)
{
	baseSprite.SetImage(image, true);
}

Tile::~Tile()
{

}

void Tile::Draw(int x, int y, sf::RenderWindow* rw)
{
	baseSprite.SetPosition(x, y);
	rw->Draw(baseSprite);
}


Simple right? In our draw method we simply set the position of the Sprite, and draw it with the Renderwindow. We'll be adding a lot more to this eventually, but this will do for now.

Test out the code
Alright, we're ready to draw some sprites! Let's modify our Engine class by adding the following to "Engine.h"
#include "ImageManager.h"

private:
	ImageManager imageManager;
	void LoadImages();
	
	Tile* testTile;



We include our new ImageManager class, and add an instance of it to our Engine. We've also added a temporary LoadImages method and a test tile to draw. Now let's add to our "Engine.cpp" file:
void Engine::LoadImages()
{
	sf::Image sprite;
	sprite.LoadFromFile("sprite1.png");

	imageManager.AddImage(sprite);

	testTile = new Tile(imageManager.GetImage(0));
}

void Engine::RenderFrame()
{
	window->Clear();
	testTile->Draw(0,0, window);
	window->Display();
}



And don't forget to add a call to LoadImages(); in the Engine::Init() method.

Conclusion
Compile and run. Eureka! We've finally drawn something to the screen. This may seem like a lot of work just to draw a single tile, but we have laid the groundwork for the next tutorial which is going to be big, so hurry up and head over to the next tutorial.

I've also attached the source for this part so you can have something to add to in the next tutorial.

Attached File(s)


This post has been edited by RevTorA: 26 May 2011 - 07:30 PM


Is This A Good Question/Topic? 5
  • +

Replies To: C++ Tile Engine from Scratch -- Part 2

#2 yukinari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-May 11

Posted 08 May 2011 - 12:52 AM

Can't believe, no replies, I was searching for the implementation of tiling in SFML, thanks.

Too bad you didn't create part 3.
Was This Post Helpful? 0
  • +
  • -

#3 RevTorA  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 76
  • View blog
  • Posts: 246
  • Joined: 22-April 11

Posted 08 May 2011 - 01:06 PM

View Postyukinari, on 08 May 2011 - 12:52 AM, said:

Can't believe, no replies, I was searching for the implementation of tiling in SFML, thanks.

Too bad you didn't create part 3.


Part 3 is actually submitted, just hasn't been posted yet, and I'm working on part 4. Unfortunately, part 3's source will be missing (I explain why at the end hehe).
Was This Post Helpful? 0
  • +
  • -

#4 yukinari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-May 11

Posted 11 May 2011 - 10:11 AM

View PostRevTorA, on 08 May 2011 - 01:06 PM, said:

View Postyukinari, on 08 May 2011 - 12:52 AM, said:

Can't believe, no replies, I was searching for the implementation of tiling in SFML, thanks.

Too bad you didn't create part 3.


Part 3 is actually submitted, just hasn't been posted yet, and I'm working on part 4. Unfortunately, part 3's source will be missing (I explain why at the end hehe).

Please warn me, when part 3 will be available to read.
Was This Post Helpful? 0
  • +
  • -

#5 Adell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-May 11

Posted 24 May 2011 - 02:40 PM

Thanks for tutorial. I was searching for SFML one for long time. Will be waiting for 3rd part.

Also, wanted to note that in ImageManager.cpp it's not

imageList.Push(image);


but

imageList.push_back(image);

Was This Post Helpful? 0
  • +
  • -

#6 LiquidPirate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-June 11

Posted 21 June 2011 - 04:28 AM

Hey, I have been having problems with this tutorial from the start, and have finally come across something that I can't figure out how to fix,

1>------ Build started: Project: Dead Pixel, Configuration: Debug Win32 ------
1>  Tile.cpp
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\tile.cpp(5): error C2533: 'Tile::{ctor}' : constructors not allowed a return type
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\tile.cpp(16): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\tile.cpp(16): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>  Main.cpp
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2143: syntax error : missing ';' before '<'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2238: unexpected token(s) preceding ';'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2236: unexpected 'class' 'Engine'. Did you forget a ';'?
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2143: syntax error : missing ';' before '{'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2447: '{' : missing function header (old-style formal list?)
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\main.cpp(7): error C2065: 'Engine' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\main.cpp(7): error C2065: 'engine' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\main.cpp(7): error C2061: syntax error : identifier 'Engine'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\main.cpp(11): error C2065: 'engine' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\main.cpp(11): error C2227: left of '->Go' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>  ImageManager.cpp
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2143: syntax error : missing ';' before '<'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2238: unexpected token(s) preceding ';'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(6): error C2533: 'ImageManager::{ctor}' : constructors not allowed a return type
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(17): error C2065: 'imageList' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(17): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(22): error C2065: 'imageList' : undeclared identifier
1>  Engine.cpp
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2143: syntax error : missing ';' before '<'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2238: unexpected token(s) preceding ';'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2236: unexpected 'class' 'Engine'. Did you forget a ';'?
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2143: syntax error : missing ';' before '{'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2447: '{' : missing function header (old-style formal list?)
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(17): error C2065: 'imageList' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(17): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(22): error C2065: 'imageList' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.cpp(6): error C2653: 'Engine' : is not a class or namespace name
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.cpp(9): warning C4508: 'Engine' : function should return a value; 'void' return type assumed
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.cpp(11): error C2653: 'Engine' : is not a class or namespace name
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.cpp(11): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Sorry I'm not sure if you are suppost to post your error logs here or not, but I am really unsure weather I have to fix each probablem individally or it is one thing that could be creating loads of errors? Maybe something to do with the SFML not linking correctly? again sorry for asking but if you do have some time to point me in the right direction it would help a awfull lot, thanks
Was This Post Helpful? 0
  • +
  • -

#7 DarkGlitch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 24-July 11

Posted 24 July 2011 - 12:22 PM

View PostLiquidPirate, on 21 June 2011 - 04:28 AM, said:

Hey, I have been having problems with this tutorial from the start, and have finally come across something that I can't figure out how to fix,

1>------ Build started: Project: Dead Pixel, Configuration: Debug Win32 ------
1>  Tile.cpp
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\tile.cpp(5): error C2533: 'Tile::{ctor}' : constructors not allowed a return type
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\tile.cpp(16): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\tile.cpp(16): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>  Main.cpp
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2143: syntax error : missing ';' before '<'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2238: unexpected token(s) preceding ';'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2236: unexpected 'class' 'Engine'. Did you forget a ';'?
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2143: syntax error : missing ';' before '{'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2447: '{' : missing function header (old-style formal list?)
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\main.cpp(7): error C2065: 'Engine' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\main.cpp(7): error C2065: 'engine' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\main.cpp(7): error C2061: syntax error : identifier 'Engine'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\main.cpp(11): error C2065: 'engine' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\main.cpp(11): error C2227: left of '->Go' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>  ImageManager.cpp
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2143: syntax error : missing ';' before '<'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2238: unexpected token(s) preceding ';'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(6): error C2533: 'ImageManager::{ctor}' : constructors not allowed a return type
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(17): error C2065: 'imageList' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(17): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(22): error C2065: 'imageList' : undeclared identifier
1>  Engine.cpp
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2143: syntax error : missing ';' before '<'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.h(10): error C2238: unexpected token(s) preceding ';'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2236: unexpected 'class' 'Engine'. Did you forget a ';'?
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2143: syntax error : missing ';' before '{'
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.h(9): error C2447: '{' : missing function header (old-style formal list?)
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(17): error C2065: 'imageList' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(17): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\imagemanager.cpp(22): error C2065: 'imageList' : undeclared identifier
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.cpp(6): error C2653: 'Engine' : is not a class or namespace name
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.cpp(9): warning C4508: 'Engine' : function should return a value; 'void' return type assumed
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.cpp(11): error C2653: 'Engine' : is not a class or namespace name
1>c:\dead weight\2d tile engine\dead pixel\dead pixel\engine.cpp(11): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Sorry I'm not sure if you are suppost to post your error logs here or not, but I am really unsure weather I have to fix each probablem individally or it is one thing that could be creating loads of errors? Maybe something to do with the SFML not linking correctly? again sorry for asking but if you do have some time to point me in the right direction it would help a awfull lot, thanks


Not sure without seeing your code, but make sure you have a ; after your class definitions in the header files. Also, read the errors, they usually tell you what your are missing. If it says "missing" something then go to the line indicated and see if you are missing whatever it says. You must place a ; at the end of each declaration or command, meaning:

// Incorrect
int x

// or

if(!x)
{
   x = z
}

// incorrect class definition

class dood
{
public:
   dood()
}



Will not work but:

// Correct
int x;

// or

if(!x)
{
   x = z;
}

// or for class definition

class dood
{
public:
   dood();
};



Hope that was clear.

Now, my problem is everything compiles correctly but the image doesn't show. Just a dot where the image is supposed to be. What am I doing wrong?

(Edited cause I didn't want to double post)

This post has been edited by DarkGlitch: 24 July 2011 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#8 phoric  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-January 12

Posted 27 January 2012 - 02:55 PM

Would be nice to get a update to this. The tutorial is broken since sf::Sprite has been changed in the current versions of SFML, and things have been moved around to different classes and renamed. For example, sf::Sprite no longer has the member function SetImage.
Was This Post Helpful? 0
  • +
  • -

#9 TheNexus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 13-May 12

Posted 13 May 2012 - 01:27 AM

View PostRevTorA, on 03 May 2011 - 06:15 PM, said:

There's a catch however. Since Sprites don't actually contain a copy of the Image, if the Image is destroyed (by leaving its scope for instance), the Sprite won't have anything to draw. We could store a copy of each image for every sprite, but that would be inefficient. However, we need to make sure that any Images we use for sprites remain intact for as long as we need it.

The solution is to have an Image manager class. This class will hold a vector of Images, and we can use it to add or retrieve Images to use with our Sprites.
Here is the problem: push_back() on a std::vector may trigger a reallocation of the whole sequence, leading to many copies and invalidations of all references. Consider pre-allocation or a different STL container.


Tile(sf::Image& image);
Since a Tile doesn't modify the sf::Image, you should pass a reference to const.

void Draw(int x, int y, sf::RenderWindow* rw);
Why do you use pointers here? rw may not be NULL, so I'd suggest to use a reference. Or pointers everywhere, but stay consistent.

	sf::Image sprite;
	sprite.LoadFromFile("sprite1.png");
Maybe you should check if the loading succeeds.

And the other points mentioned in part 1 apply also here: Avoid header guards like _TILE_H, get rid of the memory leaks by replacing dynamic allocations (new) with automatic objects or smart pointers, and don't define empty constructors or destructors.
Was This Post Helpful? 0
  • +
  • -

#10 St0n3y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-May 12

Posted 15 May 2012 - 07:36 AM

Two things I want to say:

1) These tutorials are a fantastic introduction into SFML, and its uses. Now there is only one problem, it isn't up to date. As others have said many things have been changed in SFML 2.0. But as long as you just do a little research you can change some of the code and it works perfectly.

I.E. The majority of sf::Image is now in sf::Sprite (such as: SetImage is now SetTexture, ect.)
(try looking at: http://www.sfml-dev....b69a3a75feae46)

and for those having issues with <vector> try placing: using namespace std; in your code.

2) Basically just to reiterate to the author, great tutorials :) And guys, don't forget to research the new changes in code on the :2guns: SFML website!
Was This Post Helpful? 0
  • +
  • -

#11 Haakon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-July 12

Posted 02 August 2012 - 04:44 AM

Man, I can't for the life of me figure out how to get this source to compile. I'm using SFML 2 and since SFML has changed since this tutorial was written, I need to change some stuff around. I've been trying various things, but I always end up with more errors when trying to compile.

I figured out all the stuff that needed to be done in part 1 to get it to run, but I'm stuck now.

Could someone please post what they changed to get it to compile with SFML 2? :)

Thank you :D

P.s. This is my first post. Hey everyone! :lol:
Was This Post Helpful? 0
  • +
  • -

#12 St0n3y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-May 12

Posted 04 August 2012 - 09:53 PM

View PostHaakon, on 02 August 2012 - 04:44 AM, said:

Man, I can't for the life of me figure out how to get this source to compile. I'm using SFML 2 and since SFML has changed since this tutorial was written, I need to change some stuff around. I've been trying various things, but I always end up with more errors when trying to compile.

I figured out all the stuff that needed to be done in part 1 to get it to run, but I'm stuck now.

Could someone please post what they changed to get it to compile with SFML 2? :)

Thank you :D

P.s. This is my first post. Hey everyone! :lol:


Hey :D I know exactly what you mean... I ended up shivering in the corner with a broken keyboard around my feet... :P I ended up quitting these tutorials and going to something else because they were never finished past part 4... sadly... Anyway! To answer your question, here's the same code for sfml 2.0 users:

(btw, I'm writing this, rather than copy and paste. Seeing as my code is completely different. So make sure you get the caps ect. right)
(this is in the same order as in the tutorial):

#ifndef TEXTURE_MANAGER_H
#define TEXTURE_MANAGER_H

#include <SFML/Graphics.hpp>
#include <vector>
using namespace std;

class TextureManager{
private:
vector<sf::Texture> textureList;

public:
TextureManager();
~TextureManager();

void AddTexture(sf::Texture& texture);
sf::Texture& GetTexture(int index);

};
#endif



#include "TextureManager.h"

TextureManager::TextureManager(){
}

TextureManager::~TextureManager(){
}

void TextureManager::AddTexture(sf::Texture& texture){
textureList.push_back(texture);
}

sf::Texture& TextureManager::GetTexture(int index){
return textureList[index];
}



(I don't use a tile class.. So here's what I'm guessing is this bit):
#ifndef TILE_H
#define TILE_H

#include <SFML/Graphics.hpp>

class Tile{
private:
sf::Sprite baseSprite;

public:
Tile(sf::Texture& texture);
~Tile();

void Draw(int x, int y, sf::RenderWindow* rw);
};
#endif



(By the way - if you've included "graphics.hpp" in your headers - you don't need to put them in your corresponding code - unless you're not including those headers into that particular file)

#include "Tile.h"
#include "SFML/Graphics.hpp"

Tile::Tile(sf::Texture& texture){
baseSprite.SetTexture(texture);
}

Tile::~Tile(){
}

void Tile::Draw(int x, int y, sf::RenderWindow* rw){
baseSprite.setPosition(x, y);
rw->Draw(baseSprite);
}



next:

#include "TextureManager.h"

private:
TextureManager* texturemanager;

void LoadTextures();

Tile* testTile;



Oh, and one thing before the next bit. Make sure to make a new TextureManager the same way you made the render window. Example:
In the "Initialize game" function in your engine, which is the very first thing that runs - code:
{
texturemanager = new TextureManager();
}

and in the Engine::~Engine()
Add:

{
delete window;
//this is so that the 'window = new sf::RenderWindow' is removed from memory when the engine stops)
delete texturemanager;
//this is so that the texturemanager is also deleted once the engine is stopped
}

void Engine::LoadTextures(){
sf::Texture texture;
texture.LoadFromFile("sprite1.png");

texturemanager.AddTexture(texture);

testTile = new Tile(textureManager.GetTexture(0));
}

void Engine::RenderFrame(){
window->Clear();
testTile->Draw(0, 0, window);
window->Display();
}




That *SHOULD* work :) PM me or something if it doesn't and I'll take another look :) But hopefully you'll be fine! :)
Was This Post Helpful? 1
  • +
  • -

#13 Haakon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-July 12

Posted 05 August 2012 - 02:17 AM

Thank you very much! :)

I'll try this :D
Was This Post Helpful? 0
  • +
  • -

#14 rishan1996  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-September 12

Posted 09 September 2012 - 02:21 AM

Help?


1>------ Build started: Project: My First Game, Configuration: Debug Win32 ------
1>  Tile.cpp
1>c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\tile.cpp(15): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\tile.cpp(15): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>  Main.cpp
1>c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\main.cpp(228): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\main.cpp(229): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>  Engine.cpp
1>c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\engine.cpp(76): error C2143: syntax error : missing ';' before '.'
1>c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\engine.cpp(76): error C2143: syntax error : missing ';' before '.'
1>c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\engine.cpp(80): warning C4832: token '.' is illegal after UDT 'TextureManager'
1>          c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\texturemanager.h(8) : see declaration of 'TextureManager'
1>c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\engine.cpp(80): error C2275: 'TextureManager' : illegal use of this type as an expression
1>          c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\texturemanager.h(8) : see declaration of 'TextureManager'
1>c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\engine.cpp(80): error C2228: left of '.GetTexture' must have class/struct/union
1>c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\engine.cpp(83): error C2084: function 'void Engine::RenderFrame(void)' already has a body
1>          c:\users\rishans beast\documents\visual studio 2010\projects\my first game\my first game\engine.h(20) : see previous definition of 'RenderFrame'
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



if you want the code just ask this is confusing me.
Was This Post Helpful? 0
  • +
  • -

#15 SethGandy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 24-January 13

Posted 24 January 2013 - 02:32 AM

Spinning off of what St0n3y provided, I made some fixes to get this code to work for me in SFML2.0
I'm using Visual Studios 2012 Express too, if that makes any difference.
The only thing that was busted after some fine tuning was the texturemanager.AddTexture(texture) call.
I had to use a pointer like texturemanager->AddTexture(texture) instead.
Hope this helps anyone who's a noob like me! :batman:/>

TextureManager.h
#ifndef TEXTURE_MANAGER_H
#define TEXTURE_MANAGER_H

#include <SFML/Graphics.hpp>
#include <vector>
using namespace std;

class TextureManager{
private:
vector<sf::Texture> textureList;

public:
TextureManager();
~TextureManager();

void AddTexture(sf::Texture& texture);
sf::Texture& GetTexture(int index);

};
#endif



TextureManager.cpp
#include "TextureManager.h"

TextureManager::TextureManager(){
}

TextureManager::~TextureManager(){
}

void TextureManager::AddTexture(sf::Texture& texture){
textureList.push_back(texture);
}

sf::Texture& TextureManager::GetTexture(int index){
return textureList[index];
}



Tile.h
#ifndef TILE_H
#define TILE_H

#include <SFML/Graphics.hpp>

class Tile{
private:
sf::Sprite baseSprite;

public:
Tile(sf::Texture& texture);
~Tile();

void Draw(int x, int y, sf::RenderWindow* rw);
};
#endif



Tile.cpp
#include "Tile.h"
#include "SFML/Graphics.hpp"

Tile::Tile(sf::Texture& texture){
baseSprite.setTexture(texture);
}

Tile::~Tile(){
}

void Tile::Draw(int x, int y, sf::RenderWindow* rw){
baseSprite.setPosition(x, y);
rw->draw(baseSprite);
}



Engine.h
#ifndef _ENGINE_H
#define _ENGINE_H

#include <SFML\Graphics.hpp>

#include "TextureManager.h"
#include "Tile.h"

class Engine
{
private:
	//SFML Render Window
	sf::RenderWindow* window;
	TextureManager* texturemanager;

	//Initializes the engine
	bool Init();				
	//Main Game Loop
	void MainLoop();			
	//Renders one frame
	void RenderFrame();
	//Processes user input
	void ProcessInput();
	//Updates all Engine internals
	void Update();

	//Temporary
	
	void LoadTextures();
	Tile* testTile;

public:
	
	Engine();
	~Engine();
	void Go();					//Starts the engine
};

#endif



Engine.cpp
#include "Engine.h"
#include <SFML\Graphics.hpp>

Engine::Engine()
{
	
}

Engine::~Engine()
{
	delete window; 
	//this is so that the 'window = new sf::RenderWindow' is removed from memory when the engine stops)
	delete texturemanager;
	//this is so that the texturemanager is also deleted once the engine is stopped
}

bool Engine::Init()
{
	texturemanager = new TextureManager();
	window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "RPG");
	
	if(!window)
		return false;

	LoadTextures();

	return true;
}

void Engine::LoadTextures()
{

	sf::Texture texture;
	texture.loadFromFile("sprite1.png");
	
	texturemanager->AddTexture(texture);
	testTile = new Tile(texturemanager->GetTexture(0));
}

void Engine::RenderFrame()
{
	window->clear();
	testTile->Draw(0,0, window);
	window->display();
}

void Engine::ProcessInput()
{
	sf::Event evt;
	//Loop through all window events
	while(window->pollEvent(evt))
	{
		if(evt.type == sf::Event::Closed)
			window->close();
	}
}

void Engine::Update()
{

}

void Engine::MainLoop()
{
	//Loop until our window is closed
	while(window->isOpen())
	{
		ProcessInput();
		Update();
		RenderFrame();
	}
}

void Engine::Go()
{
	if(!Init())
		throw "Could not initialize Engine";
	MainLoop();
}



Main.cpp
#include <Windows.h>

#include "Engine.h"

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
	Engine* engine = new Engine();

	try
	{
		engine->Go();
	}
	catch(char* e)
	{
		MessageBoxA(NULL, e, "Exception Occured", MB_OK | MB_IConerror);
	}
}


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2