IntelliSense is not a reliable source for the syntactical correctness of your code. It's just a tool to assist you, but you should really compile the code to get more certainity. Even then you can't be 100% sure, in case of doubt you have to lookup the C++ standard.
By the way, use forward slashes, they are portable.
texture.loadFromFile("data/rpg.jpg");
And here
Game *game = new Game();
it's not necessary to use dynamic memory,
Game game;
would work as well. See also the article (http://www.bromeon.ch/articles/raii.html) I recently wrote about why you should avoid new and delete.
I've now set VSync(true) and removed the couts.
Now I have a constant framerate and it works without laggs.
I still have another question:
I want to build my RPG out of many rectangles and I will save them in a 2D Array.
How should I structure the data in the file? And which file format would you recommend me to use?
...
Good to hear that it works! :)
Assuming you're going to have pre-built maps you should have a look at the Tiled map editor (http://mapeditor.org). It uses XML as a format.
If you're going to have generated maps though and you want to save them to a file... you would probably still be best off using XML. Although it's not going to be much use to you for just tiles, you're going to love it later on when you're saving enemies, sprites etc.
Another problem is that I want to expand and update the Map sometimes.
So if I save the users last position, how can I place his character on the same position after the map update?
I don't quite understand what you mean. Do you mean that you're going to update the map at runtime? (e.g. secret passageway opens up by removing a tile). Or do you mean you're going to update the map via a map editor?
For both cases you shouldn't need to worry. However do keep in mind that moving objects shouldn't be tiles.
You're going to have some trouble with transparency and many other things otherwise.
The best solution would be to just use tilemaps of maybe 255x255 rectangles and a 4D Array.
The first two dimension would specify the position of the tilemap and the second two the content of the tilemap.
A 4D array? You should only need 2 dimensions. If you wanted to you could even make a 1D array.
Here's an example for a 1D array which uses numbers to represent different types of tiles:
const int map[16] =
{
2, 2, 2, 2,
2, 1, 1, 2,
2, 2, 1, 2,
2, 2, 2, 2
};
To get a tile you would do this:map[x * mapWidth + y];
An example for this case would be: map[3 * 4 + 2]; (coordinates start at 0)
which would return '2' because the tile at [3, 2] has the ID 2.
Also each rectangle should be filled with a sprite for the surface.
So should I just use an Array of Sprites and use a switch case to load the textures into each rectangle(sprite)?
Kind of. Use an std::vector<sf::Texture>.
Here's an example:
std::vector<sf::Texture> textureMap;
textureMap.push_back(...); // Texture for Tile ID 1
textureMap.push_back(...); // Texture for Tile ID 2
// and so on...
Here's an example for drawing with the above setup:
sf::Sprite tileSprite; // This should preferrably be a class member.
for (unsigned int x = 0; x < mapWidth; ++x)
{
for (unsigned int y = 0, y < mapHeight; ++y)
{
int id = map[x * mapWidth + y]; // Get the ID at [x, y] from the map.
tileSprite.setTexture(textureMap[id - 1]); // - 1 since id #1 is #0 in the vector.
window.draw(tileSprite); // Draw it.
}
}
There also must be a value for each rectangle whether clipping is enables for it or not.
I also need to save the height because I have at least 2 terrain levels.
The ground and objects that shoud be drawn over it, like houses, trees etc.
You could use a seperate array just for the collision map. But that's pretty inefficient, I advise against it.
So instead what you should do is make a vector like above that stores bools rather than textures.
For the layers, you would add another dimension to the array.
I've removed the pointer..
After these changes the programm still need constantly 33% of my CPU?!
And sometimes, instead of making big jumps like before, it still scrolls faster?!
Have you tried a release build? From my experience a debug build with a framelimit of 60fps will take up to 10-40% CPU. While a release build produces a mere 03-10%.
I wanted to expand my programm to implement your code, but an error appeared..
Error(translation):
1>------ Constructing started: Project: 2D RPG, Configuration: Debug Win32 ------
1>The building process was started 25.01.2014 23:07:11.
1>InitializeBuildStatus:
1> Updating the timestamp of "Debug\2D RPG.unsuccessfulbuild".
1>ClCompile:
1> menu.cpp
1>c:\sfml\include\sfml\window\window.hpp(477): error C2248: "sf::NonCopyable::operator =": No access to private members, which declaration is done in sf::NonCopyable-Class.
1> c:\sfml\include\sfml\system\noncopyable.hpp(79): See declaration of 'sf::NonCopyable::operator ='
1> c:\sfml\include\sfml\system\noncopyable.hpp(42): See declaration of 'sf::NonCopyable'
1> This diagnostic occurred in the compiler generated function "sf::Window &sf::Window::operator =(const sf::Window &)".
1>c:\sfml\include\sfml\graphics\rendertarget.hpp(419): error C2248: "sf::NonCopyable::operator =": No access to private members, which declaration is done in sf::NonCopyable-Class.
1> c:\sfml\include\sfml\system\noncopyable.hpp(79): See declaration of 'sf::NonCopyable::operator ='
1> c:\sfml\include\sfml\system\noncopyable.hpp(42): See declaration of 'sf::NonCopyable'
1> This diagnostic occurred in the compiler generated function "sf::RenderTarget &sf::RenderTarget::operator =(const sf::RenderTarget &)".
1> gui.cpp
1>c:\sfml\include\sfml\window\window.hpp(477): error C2248: "sf::NonCopyable::operator =": No access to private members, which declaration is done in sf::NonCopyable-Class.
1> c:\sfml\include\sfml\system\noncopyable.hpp(79): See declaration of 'sf::NonCopyable::operator ='
1> c:\sfml\include\sfml\system\noncopyable.hpp(42): See declaration of 'sf::NonCopyable'
1> This diagnostic occurred in the compiler generated function "sf::Window &sf::Window::operator =(const sf::Window &)".
1>c:\sfml\include\sfml\graphics\rendertarget.hpp(419): error C2248: "sf::NonCopyable::operator =": No access to private members, which declaration is done in sf::NonCopyable-Class.
1> c:\sfml\include\sfml\system\noncopyable.hpp(79): See declaration of 'sf::NonCopyable::operator ='
1> c:\sfml\include\sfml\system\noncopyable.hpp(42): See declaration of 'sf::NonCopyable'
1> This diagnostic occurred in the compiler generated function "sf::RenderTarget &sf::RenderTarget::operator =(const sf::RenderTarget &)".
1> button.cpp
1> Code is generated...
1>
1>Failed to create
1>
1>Elapsed time 00:00:01.93
========== Create: 0 successful, Skipped error at 1, 0 current, 0 skipped ==========
gui.h
//==========================================================//
// CLASS: GUI //
// DESC: Manages GUI //
//==========================================================//
#ifndef __GUI_H__
#define __GUI_H__
//=================================
// included dependencies
#include "menu.h"
#include <SFML/Graphics.hpp>
//=================================
// class
class GUI
{
public:
GUI(); // Constructor
~GUI(); // Destructor
sf::RenderWindow window; // Needed to display graphics
enum menus { main, game, options, credits }; // Enum of menus
void Initialize(sf::RenderWindow _window); // Initialize
void LoadContent(); // Loading the needed content
void UnloadContent(); // Unload all content
void Update(sf::Time elapsedTime, sf::Event event); // Check Events
void Draw(sf::Time elapsedTime); // Redraw screen
};
#endif // __GUI_H__
gui.cpp
#include "gui.h"
GUI::GUI()
{
}
GUI::~GUI()
{
}
void GUI::Initialize(sf::RenderWindow _window)
{
window = _window;
}
void GUI::LoadContent()
{
}
void GUI::UnloadContent()
{
}
void GUI::Update(sf::Time elapsedTime, sf::Event event)
{
}
void GUI::Draw(sf::Time elapsedTime)
{
window.clear();
window.display();
}
menu.h
//==========================================================//
// CLASS: MENU //
// DESC: Menu prototype //
//==========================================================//
#ifndef __MENU_H__
#define __MENU_H__
//=================================
// included dependencies
#include <SFML/Graphics.hpp>
//=================================
// class
class Menu
{
public:
Menu(); // Constructor
~Menu(); // Destructor
sf::RenderWindow window; // Needed to display graphics
void Initialize(sf::RenderWindow _window); // Initialize
void LoadContent(); // Loading the needed content
void UnloadContent(); // Unload all content
void Update(sf::Time elapsedTime, sf::Event event); // Check Events
void Draw(sf::Time elapsedTime); // Redraw screen
};
#endif // __MENU_H__
menu.cpp
#include "menu.h"
Menu::Menu()
{
}
Menu::~Menu()
{
}
void Menu::Initialize(sf::RenderWindow _window)
{
window = _window;
}
void Menu::LoadContent()
{
}
void Menu::UnloadContent()
{
}
void Menu::Update(sf::Time elapsedTime, sf::Event event)
{
}
void Menu::Draw(sf::Time elapsedTime)
{
}
element.h
//==========================================================//
// CLASS: ELEMENT //
// DESC: Base class for all UI Elements (e.g. Button) //
//==========================================================//
#ifndef __ELEMENT_H__
#define __ELEMENT_H__
//=================================
// included dependencies
#include <SFML/Graphics.hpp>
//=================================
// class
class Element
{
public:
sf::RenderWindow window; // Needed to display graphics
virtual void Initialize(sf::RenderWindow _window); // Initialize
virtual void LoadContent(); // Loading the needed content
virtual void UnloadContent(); // Unload all content
virtual void Update(sf::Time elapsedTime, sf::Event event); // Check Events
virtual void Draw(sf::Time elapsedTime); // Redraw screen
};
#endif // __ELEMENT_H__
button.h
//==========================================================//
// CLASS: GUI //
// DESC: Manages GUI //
//==========================================================//
#ifndef __BUTTON_H__
#define __BUTTON_H__
//=================================
// included dependencies
#include "element.h"
//=================================
// class
class Button : public Element
{
public:
Button(); // Constructor
~Button(); // Destructor
void Initialize(); // Initialize
void LoadContent(); // Loading the needed content
void UnloadContent(); // Unload all content
void Update(sf::Time elapsedTime, sf::Event event); // Check Events
void Draw(sf::Time elapsedTime); // Redraw screen
private:
};
#endif // __BUTTON_H__
button.cpp
#include "button.h"
Button::Button()
{
}
Button::~Button()
{
}
void Button::Initialize()
{
}
void Button::LoadContent()
{
}
void Button::UnloadContent()
{
}
void Button::Update(sf::Time elapsedTime, sf::Event event)
{
}
void Button::Draw(sf::Time elapsedTime)
{
}