Jump to content
  • Advertisement
Sign in to follow this  
Canvas

C++ and SFML game structure help

This topic is 1600 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm in the early stages of a game I would love to create and i'm trying to work out how the game program should be structured (classes, headers etc). I'm using C++ and SFML for this project.

Let me first by showing you my basic image of the structure. Don't pay attention to MainMenuState.cpp and NewGameState.cpp

dymw5.png

Game.cpp will have an instance of all those services, it will also have the MainMenuState, NewGameState and LevelState. Now here is my problem.

Game.cpp will have a method that will call the RenderService.RenderWindow, I'm trying to work out exactly how I should code this method? Should I pass in an arrayList of sf::sprites? If I do this, the LevelState.cpp will need an ArrayList of sf::Sprites. which will be updated on each game step. But lets say the Hero.cpp has 1 sf::Sprite. the Map.cpp has 20 sf::sprites and Monster.cpp has 1 sf::Sprite. this would mean I have to pass 22 sf::Sprites back to the RenderService every game step (60fps). so that is 1320 sf::Sprites per second back to the RenderService. I understand game structure is a very hard thing to do and hard to implement correctly, because there isn't a "set" way. I have created a Vector which holds sf::Sprites and every time I call my Render function I clear the vector before so everything is removed for memory. This works fine. Would that be the answer? But as I stated before is passing a lot of sf::Sprites to be rendered ok?

 

P.S.

 

Here is my simple SFML program to test this

#include <SFML/Graphics.hpp>
#include "ImageService.h"
#include "DisplayObject.h"
#include "Enums.h"

ImageService is;
DisplayObject displayObject;

int main()
{
	sf::RenderWindow window(sf::VideoMode(1024, 768), "SFML works!");
	sf::CircleShape shape(100.f);
	shape.setFillColor(sf::Color::Green);

	is.Images[image1].loadFromFile("ship1.tga");
	displayObject.sprite.setTexture(is.Images[displayObject.spriteType]);
	std::vector<sf::Sprite> displayObjects;

	while (window.isOpen())
	{
		displayObjects.clear();
		displayObjects.push_back(displayObject.sprite);

		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
		}

		window.clear();

		window.draw(shape);
		//Draw out the selected sprites
		for (int i = 0; i < displayObjects.size(); i++)
		{
			window.draw(displayObjects.at(i));
		}

		window.display();
	}

	return 0;
}

Let me know if you would like me to explain any part of my program

Edited by Canvas

Share this post


Link to post
Share on other sites
Advertisement

'State' should really just be something that Game.cpp keeps track of as an enum rather than a whole class. I'd maybe change from MainMenuState etc to MainMenuScreen. Have all these screens inherit from a BaseScreen class and then have Game.cpp hold a pointer to a BaseScreen. Then you can switch between them as you wish. Very clean and easy transitioning with the write functions to compliment said change!

 

I wouldn't clear and re-add to the vector EVERY frame. Vectors aren't particularly quick (someone'll reply to this no doubt :D) so doing that operation so often could be quite heavy when the game gets larger. Consider having vectors of type GameObject (your own class) and as you create these objects add them to this vector. Then rendering them all is simply a job of iterating over them and calling render. That way you only call push_back once.

 

For my 2nd year project (in C++ and SFML oddly enough!) I had a LevelLoader class that simply populated vectors stored in Game.cpp. Pass a pointer to game and let it do what it needs to do to get your level sorted.

 

You're right about there being no 'set' way of doing things though; that's the beautiful thing about programming. People can offer more efficient solutions, yes, but it's never the ONLY way to do it! If you can justify your methodology, follow it :)

Share this post


Link to post
Share on other sites

Dezachu that all sounds good to me, but one question I do need to ask. You have stated that if the game was to get large ( or larger). clearing the vector and re-populating it would be quite heavy on process. This makes sense to me, but where you have said create a vector of GameObject and then iterate over that vector just render them. Could you try and explain this for my Level.cpp class. My level has a Hero object which will have a sprite, the Monster object will have a sprite and the Map will have a numerous amount of sprites. if each one of these have a GameObject which just holds the sprite. If I pass them back as the object, the vector will just take a copy, and when I call the Hero.Update method that update which changes the position will not even the GameObject copy in the vector. Would I have to change my vector to take pointers of GameObject? I would prefer to stay away from pointers as this will most probably cause a problem with memory management

Share this post


Link to post
Share on other sites

Pay attention you are populating your displayObjects vector with Sprite, not Sprite*... this way you are creating and destructing objects every loop, which is bad. The more, you probably think you are working with references to your original sprites, but it's not, you are working with copies of your sprites: this means that if you change a sprite in an update cycle (you have not yet, but you will) this change will not reflect on the drawed sprite.

 

EDIT: just read your last post, you get the point. Better use pointers. Anyway, if you choosed to use C++ it's better you have no fear of use them ;) They're not that bad if you just remember the simple rule: every new needs a delete. 

In your example you have no need of news, anyway: just populate your vector this way (out of the loop):

 

displayObjects.push_back(&displayObject.sprite);

 

and get rid of the clear().

 

Obviusly, the draw part will be:

 

window.draw(*displayObjects.at(i));

Edited by arka80

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!