• 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
Jossos

Header logic help

8 posts in this topic

So I have a class Gamestate:

class GameState
{
public:
	virtual void HandleEvents() = 0;
	virtual void logic() = 0;
	virtual void render() = 0;
	virtual ~GameState(){};

	void SetNextState(GameStates newState);	// State status manager
	void ChangeState();		// State changer

private:
};

 

With the functions:

void GameState::ChangeState()
{
	if (nextState != STATE_NULL)
	{
		if (nextState != STATE_EXIT)
			delete currentState;

		switch (nextState)
		{
		case STATE_INTRO: break;
		case STATE_MENU: currentState = new MainMenu(); break;
		case STATE_GAME: currentState = new Game(32, 32); break;
		}

		nextState = STATE_NULL;
	}
}

void GameState::SetNextState(GameStates newState)
{
	nextState = newState;
}

 

Things to note here are MainMenu, and Game - each in their own header file. This is a problem because MainMenu and Game inherit from GameState:

class Game : public GameState
{
   //Game stuff...
};

class MainMenu : public GameState
{
   //MainMenu stuff...
};

 

Gamestate needs to be able to create a new instance of MainMenu, Game, or watever other states I add, while these objects need to inherit from GameState.

 

See, these header files need to reference each other. Of course, this causes an infinite loop, so how do i fix this?

 

Help is greatly appreciated!

Edited by Jossos
0

Share this post


Link to post
Share on other sites

To solve the header problem, independently of this is good or bad coding style, you just need to forward declare the class instead of including its header. In this case you can use it in the header files, as long as you don't access it there (e.g. in inline code):

 

class MainMenu;
class Game;

    class GameState
    {
    public:
    virtual void HandleEvents() = 0;
    virtual void logic() = 0;
    virtual void render() = 0;
    virtual ~GameState(){};
// new=>
     MainMenu myMainMenu;     


    void SetNextState(GameStates newState); // State status manager
    void ChangeState(); // State changer
     
    private:
    };

 

But this is not the problem here, you can just include all headers in your cpp like this:

#include "GameState"
#include "Game"
#include "MainMenu"


    void GameState::ChangeState()
    {
    if (nextState != STATE_NULL)
    {
    if (nextState != STATE_EXIT)
    delete currentState;
     
    switch (nextState)
    {
    case STATE_INTRO: break;
    case STATE_MENU: currentState = new MainMenu(); break;
    case STATE_GAME: currentState = new Game(32, 32); break;
    }
     
    nextState = STATE_NULL;
    }
    }
     
    void GameState::SetNextState(GameStates newState)
    {
    nextState = newState;
    }

 

Summary  of headers/cpps/includes:

Game.h
---------
include "GameState.h"

Game.cpp
------------
include "Game.h"

MainMenu.h
---------
include "GameState.h"

MainMenu.cpp
------------
include "MainMenu.h"

GameState.h
---------
no include

GameState.cpp
------------
include "GameState.h"
include "MainMenu.h"
include "Game.h"

Edited by Ashaman73
0

Share this post


Link to post
Share on other sites
I don't see where the GameState header needs to reference MainMenu and Game. The cpp file does, but that's no problem.

That said, I wouldn't derive MainMenu and Game from GameState. It clearly does things that aren't relevant to the other classes. Perhaps all three should derive from a common base class.
1

Share this post


Link to post
Share on other sites

Although it is possible to forward include like Ashaman said, wouldn't be better to have a state manager class that handles all these state changes, rather then changing the state in game state and main menu state? That way you would eliminate forward including, and IMHO it would be much easier to manage.

0

Share this post


Link to post
Share on other sites

to Ashaman, thanks a heap! that fixed my problems, and I will use this technique for the rest of my life.

 

to EWClay, my main game loop looks like this:

 

while (stateID != STATE_EXIT)
	{
		// Events
		currentState->HandleEvents();

		currentState->ChangeState();

		// Logic
		currentState->logic();

		// Rendering
		currentState->render();

		drawCursor();

		SDL_GL_SwapBuffers();
	}

 

If they all derive from gamestate, I can just change the state, and the loop will continue normally. (A state machine I think they call this?)

 

to Nusakan, I have NO IDEA how to make a class like that. The idea of "Manager" classes are too abstract for me, but if you want to show me how you would write it, I would really appreciate it, and implement it for sure.

0

Share this post


Link to post
Share on other sites

[quote name='Jossos' timestamp='1357815763' post='5019832']

to Nusakan, I have NO IDEA how to make a class like that. The idea of "Manager" classes are too abstract for me, but if you want to show me how you would write it, I would really appreciate it, and implement it for sure.
[/quote]

 

I would suggest this website. it explains clearly how to do this in a simple manner. http://gamedevgeek.com/tutorials/managing-game-states-in-c/

0

Share this post


Link to post
Share on other sites
When teaching students header management, I always go through a few rules.

Always forward declare rather than including a header to a class unless...

1) The class inherits from the other class.
2) The class contains the other class (Not a pointer!)
3) The class contains (including a pointer) something from the standard library. (i.e std::string, std::string*)

This should get round the looping issue. Especially if you remember that two classes can never contain one another. One will have to only contain a pointer to the other (rule 2).

Also... beware of smart pointers. The deleter function in it will need more than just a forward declare. In this case make sure the class containing the smart pointer contains a destructor and the full header is included in the .cpp file. (A bit messy, but thats life...) Edited by Karsten_
1

Share this post


Link to post
Share on other sites

to Ashaman, thanks a heap! that fixed my problems, and I will use this technique for the rest of my life.

 

to EWClay, my main game loop looks like this:

 

while (stateID != STATE_EXIT)
	{
		// Events
		currentState->HandleEvents();

		currentState->ChangeState();

		// Logic
		currentState->logic();

		// Rendering
		currentState->render();

		drawCursor();

		SDL_GL_SwapBuffers();
	}

 

Sorry if I'm completely misunderstanding, but is currentState not a member of GameState? If not, why is ChangeState() a member of GameState? Game and MainMenu inherit ChangeState(), but they don't seem to need it. Something doesn't look right.

 

The link from Nusakan shows a good way to implement this.

0

Share this post


Link to post
Share on other sites
3) The class contains (including a pointer) something from the standard library. (i.e std::string, std::string*)
Note that some standard library objects have a header that just has forward declarations: iosfwd, which forward declares the various stream classes and related classes.
Also... beware of smart pointers. The deleter function in it will need more than just a forward declare.
It depends on the smart pointer. Some smart pointers will need the full definition to declare as a class member, such as std::auto_ptr since it automatically calls delete. However some smart pointers will only need a forward declaration, such as std::shared_ptr since the deleter isn't created until an object is assigned to the pointer.
0

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