Page 1 of 1

I AM SUCH A NEWBITO

Posted: Thu Oct 09, 2008 6:35 am
by dejai
I have written some crap, half assed framework and I was just wondering what you guys thought of it. Keep in mind its horrible and really spaghetti coded. Its a game state framework and I have no idea if there is a better way to manage my game. Ignore the mesh loading I am going to be making an xml reader sooner or later. In fact pretty much ignore anything that uses the Irrlicht API. This is in response to Lusikka Mage not knowing what I was talking about in my pm.

main.cpp

Code: Select all


#include <iostream>
#include <irrlicht.h> // Include the Irrlicht Library
#include "include/CGame.h"

int main(int argc, char* argv[])
{
    CGame Game;
    Game.Run();
    return 0;

}

GAME LOOP

Code: Select all

#ifndef CGAME_H
#define CGAME_H

#include "../include/CGameManager.h"

class CGame
{
    public:
        CGame();
        virtual ~CGame();

        void Run(); // Game Loop

        CGameManager m_pGameManager; // Object from CGameManager
    protected:
    private:
};

#endif // CGAME_H

Code: Select all

#include "../include/CGame.h"

CGame::CGame()
{
    //ctor
}

CGame::~CGame()
{
    //dtor
}

void CGame::Run() // Game Loop
{


    while(m_pGameManager.getDevice()->run()) // While the Running Function Returns True
    {


        m_pGameManager.HandleEvents(); // Handle Events
        m_pGameManager.Update(); // Update
        m_pGameManager.Draw(); // Draw

    }

  //  m_pGameManager.Cleanup(); // When loop ends, Clean Up
  //  m_pGameManager.File()->LoadGUI("data/configure/gui/gui_exit.gcs", true); // Say goodbye
}
CGameManager:

Code: Select all

#ifndef CGAMEMANAGER_H
#define CGAMEMANAGER_H

#include <iostream>
#include <irrlicht.h>

#include "../src/ATMOsphere.cpp"

#include <stdio.h>
#include <math.h>
#include <wchar.h>

class CGameState;

using namespace std;

using namespace irr;
using namespace core;
using namespace video;
using namespace scene;
using namespace io;
using namespace gui;


class CGameManager
{

public:

    CGameManager();
    virtual ~CGameManager();

    void Init(); // Initialise some things

    void ChangeState(CGameState* state); // Change States


	void HandleEvents(); // Handle Events
	void Update(); // Update information
	void Draw(); // Draw your graphics

	bool Running() { return m_running; } // Returns if the games still running
	void Quit() { m_running = false;  } // Quit sets m_run to false.

	IrrlichtDevice* getDevice();
	IVideoDriver* getDriver();
	ISceneManager* getSmgr();
	IGUIEnvironment* getEnv();

	ATMOsphere* getATMO();


private:

    friend class CGameState;

	CGameState* m_pGameState; // Vector of the states

	IrrlichtDevice* m_pDevice;
	IVideoDriver* m_pDriver;
	ISceneManager* m_pSmgr;
	IGUIEnvironment* m_pEnv;

    ATMOsphere* m_pAtmo;

    void CreateDevice();
	bool m_running; // The variable that determines if the state is running.





protected:

};

#endif // CGAMEMANAGER_H

Code: Select all

#include "../include/CGameManager.h"
#include "../include/CGameState.h"
#include "../include/CGameStateLevel1.h"



CGameManager::CGameManager()
{

    Init();
    CreateDevice();
    ChangeState( CGameStateLevel1::Instance() );

}

CGameManager::~CGameManager()
{

}
void CGameManager::Init()
{
    m_pGameState = 0;
    m_pAtmo = new ATMOsphere;



}

void CGameManager::CreateDevice()
{


    m_pDevice = createDevice( video::EDT_OPENGL, dimension2d<s32>(1280, 960), 32,
    false, true, true, 0);

     m_pDriver = m_pDevice->getVideoDriver();
     m_pSmgr = m_pDevice->getSceneManager();
     m_pEnv = m_pDevice->getGUIEnvironment();

     m_pDevice->setWindowCaption(L"Dejaiin- Development Version");




}

void CGameManager::ChangeState(CGameState* state)
{

    if(m_pGameState)
    {
        m_pGameState->Cleanup(this);
    }

    if(state != m_pGameState)
    {
        m_pGameState = state;
        m_pGameState->Init(this);
    }

}

///////////////////////////////////////////////////////
IrrlichtDevice* CGameManager::getDevice()
{
    return m_pDevice;
}

IVideoDriver* CGameManager::getDriver()
{
    return m_pDriver;
}

ISceneManager* CGameManager::getSmgr()
{
    return m_pSmgr;
}

IGUIEnvironment* CGameManager::getEnv()
{
    return m_pEnv;
}

ATMOsphere* CGameManager::getATMO()
{
    return m_pAtmo;
}

////////////////////////////////////////////////////////////
void CGameManager::HandleEvents()
{
	m_pGameState->HandleEvents(this);

}

void CGameManager::Update()
{
	m_pGameState->Update(this);


}

void CGameManager::Draw()
{
    m_pDriver->beginScene(true, true, SColor(255,100,101,140));

	m_pGameState->Draw(this);

    m_pSmgr->drawAll();
    m_pEnv->drawAll();
    m_pDriver->endScene();
}


CGameState:

Code: Select all

#ifndef CGAMESTATE_H
#define CGAMESTATE_H


#include "../include/CGameManager.h"

class CGameState
{
    public:

    virtual void Init(CGameManager* game) = 0; // Initialisation
	virtual void Cleanup(CGameManager* game) = 0; // Clean up before moving to the next class

	virtual void Pause(CGameManager* game) = 0; // Pause the state
	virtual void Resume(CGameManager* game) = 0; // Resume the state

	virtual void HandleEvents(CGameManager* game) = 0; // Handle Events
	virtual void Update(CGameManager* game) = 0; // Update
	virtual void Draw(CGameManager* game) = 0; // Draw

	void ChangeState(CGameManager* game, CGameState* state) {
		game->ChangeState(state); // Change the state.
	}

protected:

	CGameState() { } // Default Constructor

private:
};


#endif // CGAMESTATE_H


And of course its a mish mash of other game state frameworks. I give credit where credit is due.

Re: I AM SUCH A NEWBITO

Posted: Thu Oct 23, 2008 9:03 am
by MarauderIIC
It occurred to me a while ago, but I guess I didn't post it -- why not put your heartbeat in main()? That's sort of what it's there for -- your program is the game, CGame is redundant.

Also, "../include" is probably a bad idea. I'd make the include dir a subdir of the main project dir, or you might even consider adding that particular directory to the directories that your compiler searches for includes at.

I think you should make ~CGameManager as virtual ~CGameManager() = 0; but leave it defined. As a corollary, you might consider, for CGameState, defining a pure virtual destructor, even if it's empty. I'm not totally sure on why this is, unfortunately. Not yet.

As a general rule, you don't want to include .cpp files. I noticed you did so in CGameManager.h. This means if you make a change in ATMOsphere.cpp you'll have to recompile your project, not just the one file, which defeats one of the purposes of having .h and .cpp files. Why didn't you include the ATMOsphere.h (if that's what you needed?)

If you're using C++, use <cstdio>, <cmath> and <cwchar>, not <stdio.h>, <math.h> and <wchar.h>. <c....> are the C++ versions of stdio.h and such. (Also they are defined in the std namespace.)

Re: I AM SUCH A NEWBITO

Posted: Fri Oct 24, 2008 2:44 am
by dejai
Thanks mate, yes I was wondering why I had that .cpp file included its how the person that made the ATMOsphere system wanted it implemented. I have CGame really for no reason but I thought it would keep it sort of neat. Anyway thanks heaps for the advice.

Re: I AM SUCH A NEWBITO

Posted: Fri Oct 24, 2008 10:18 am
by MarauderIIC
dejai wrote:its how the person that made the ATMOsphere system wanted it implemented.
I'm going to be mean here and say that that person is a moron and shouldn't be publishing anything for general use, because people who don't know better will adopt horrible practices that dramatically increase development time (I mean, compile time is already slow enough) and will get their butts kicked or fired in a professional environment.

:)