• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

C++ and SFML game structure help

3 posts in this topic

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


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?




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);

	std::vector<sf::Sprite> displayObjects;

	while (window.isOpen())

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


		//Draw out the selected sprites
		for (int i = 0; i < displayObjects.size(); i++)


	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

'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):




and get rid of the clear().


Obviusly, the draw part will be:



Edited by arka80

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0