Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


game state management not working?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Adam West   Members   -  Reputation: 219

Like
0Likes
Like

Posted 04 June 2012 - 04:52 AM

hey there again,

I am trying to create a game state manager using an input manager where a gui will appear when the user presses escape in game, for some reason its not working :(, any ideas??
main.h
#ifndef MAIN_H_
#define MAIN_H_
#include <irrlicht.h>
#include <iostream>
#include <string.h>

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


//globals
class redNovember
{
public:
// always initialize class-members in the constructor - especially set pointers to 0!
redNovember() : device(0), driver(0), smgr(0),guienv(0)
{}

IrrlichtDevice *device;
IVideoDriver* driver;
ISceneManager* smgr;
IGUIEnvironment* guienv;
ICameraSceneNode * camera;
IMetaTriangleSelector * meta;
IGPUProgrammingServices* gpu;
scene::IParticleSystemSceneNode* ps;
gui::IGUIInOutFader* fader;

void initIrrlicht();
void initFPSCamera();
void renderScene();
int shutdown();
void inputHandle();
//for the game scenes
void LoadScene(); // const chars for maps
void initSkybox(const char* up,const char* down,const char* left, const char* right, const char* front, const char* back);
void initSkydome(const char* skydomefile);
//shader stuff
void initShader();
void VertexShader(const char* VertexShaderFilename, const char* VertexShaderFunction);
void PixelShader(const char* PixelShaderFilename, const char* PixelShaderFunction);
void GeoShader(const char* GeoShaderFilename, const char* GeoShaderFunction);
void initRain();
void initFog();
void initBloom();
void initHDR();
void initSSAO();
void initBokehDOF();
//user interface and effects
bool showGameMenu;
void fadeInTransition();
void fadeOutTransition();
void showInGameMenu();
void hideInGameMenu();
//material and shader ID's

s32 bloomMaterial;
s32 bokehDOFMaterial;
//s32 CgExampleMaterialType;
//for shaders

// You can put any other variables you want in here.
// For example driver, scenemanger although you can also always get them from device
// Note that the clean solution is making variables private and use public
// get-functions to access them from other classes as you can see when looking at Irrlicht classes.
// You can for example also create a function like that:
//irr::scene::ISceneManager* getSceneManager() const { return device ? device->getSceneManager() : 0; }

};


// every other global you would need can instead of being global just be put in here.
class MyEventReceiver : public IEventReceiver
{
public:
// This is the one method that we have to implement
virtual bool OnEvent(const SEvent& event)
{
  // Remember whether each key is down or up
  if (event.EventType == irr::EET_KEY_INPUT_EVENT)
   KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
  return false;
}
// This is used to check whether a key is being held down
virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
  return KeyIsDown[keyCode];
}
MyEventReceiver()
{
  for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
   KeyIsDown[i] = false;
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
#endif
main.cpp
#include "main.h"
#include "luamanager.h"
#include "physicsmanager.h"
LuaManager * LUA = new LuaManager;
//newtonManager* PHYS = new newtonManager;
redNovember* SYS = new redNovember;
MyEventReceiver receiver;

int redNovember::shutdown()
{
LUA->DropLua();
delete LUA;
//PHYS->drop();
device->drop();

//delete PHYS;
delete SYS;
return 0;
}
int main()
{
SYS->initIrrlicht();

//PHYS->init();
SYS->LoadScene();
SYS->initShader();

SYS->initFPSCamera();



//SYS->initRain();
SYS->initFog();

LUA->loadScript("assets/scripts/test.lua");

///put main shit in here
//put gui loop here

SYS->initSkydome("assets/skydome/storm1.jpg");

/*
SYS->initSkybox("assets/skybox/citystorm/up.jpg",
   "assets/skybox/citystorm/down.jpg",
   "assets/skybox/citystorm/left.jpg",
   "assets/skybox/citystorm/right.jpg",
   "assets/skybox/citystorm/back.jpg",
   "assets/skybox/citystorm/front.jpg");*/


SYS->renderScene();

SYS->shutdown();
}
void redNovember::initIrrlicht()
{
device = createDevice(EDT_OPENGL, dimension2d<u32>(800, 600), 32,
  false, false, true, 0);
device->setWindowCaption(L"Project: Red November");
driver = device->getVideoDriver();
smgr = device->getSceneManager();
guienv = device->getGUIEnvironment();
device->getCursorControl()->setVisible(false); //hide cursor
fader = device->getGUIEnvironment()->addInOutFader();
// Then create the event receiver, giving it that context structure.
// And tell the device to use our custom event receiver.
 

}

void redNovember::renderScene()
{
int lastFPS = -1;
while(device->run())
{
 
  inputHandle(); //input handler

  if(!device->isWindowActive())
  {
   //add pause functions here

  }
  if (device->isWindowActive())
  {
   driver->beginScene(true, true, video::SColor(255,200,200,200));

   smgr->drawAll();
   driver->endScene();
   int fps = driver->getFPS();
   if (lastFPS != fps)
   {
    core::stringw str = L"[";
    str += driver->getName();
    str += "] FPS:";
    str += fps;
    device->setWindowCaption(str.c_str());
    lastFPS = fps;
   }
  }
  else
   device->yield();
}
}
void redNovember::inputHandle()
{
//menu key handler
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
  showGameMenu = true;


//menu shit
  if (showGameMenu = true)
  {
   SYS->showInGameMenu();
  }
  else
   SYS->hideInGameMenu();

};
gui.cpp
#include "main.h"

void redNovember::fadeInTransition()
{
fader->setColor(video::SColor(0,0,0,0));
fader->fadeOut(4000);
}
void redNovember::fadeOutTransition()
{
fader->setColor(video::SColor(0,0,0,0));
fader->fadeIn(4000);
}
void redNovember::showInGameMenu()
{
std::cout << "on" << std::endl;
}
void redNovember::hideInGameMenu()
{
std::cout << "off" << std::endl;
}


at the moment it should spam off on the output window and then spam on when the escape key is pressed... the output only spams on though

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8685

Like
2Likes
Like

Posted 04 June 2012 - 05:22 AM

Define "its not working". Compiler error? Runtime crash? Unexpected behaviour? Gremlins escape from your computer?

In redNovember::inputHandle(), you are using an assignment in a conditional expression:
if (showGameMenu = true)
{
   SYS->showInGameMenu();
}
else
   SYS->hideInGameMenu();
The first line sets showGameMenu to true, then tests if it is true (which it must be), and then executes shwoInGameMenu(). This is assuming you have no memory related errors, such as calling inputHandle() on an invalid object or uninitialised pointer.

Consider increasing your warning level until the compiler outputs a message about this. Also in future, try to fully describe the problematic behaviour. If you are getting errors or warnings, post them. If you are getting strange behaviour, post both what you expect to happen and what actually happened. Should gremlins emerge from your computer, give an estimate as to how many, what size they were and what colour.

#3 Adam West   Members   -  Reputation: 219

Like
0Likes
Like

Posted 04 June 2012 - 05:55 AM

Define "its not working". Compiler error? Runtime crash? Unexpected behaviour? Gremlins escape from your computer?


if you read the code and the statement, the problem is it only spams the function which causes it to show, it should be off then on when escape is pressed.

#4 Matthew Wood   Members   -  Reputation: 114

Like
1Likes
Like

Posted 04 June 2012 - 06:10 AM

if( showGameMenu == true )

'showGameMenu = true' will always return true

#5 rip-off   Moderators   -  Reputation: 8685

Like
1Likes
Like

Posted 04 June 2012 - 06:20 AM

if you read the code and the statement...

There is a lot of code, and I am not familiar with Irrlicht. I don't have a large amount of time, so I skimmed the code.

... the problem is it only spams the function which causes it to show, it should be off then on when escape is pressed.

Sorry, I did miss the final sentence in your original post. Still, I cannot parse that sentence. I don't know what "to spam" a function means.

Are you trying to say that the escape key is supposed to toggle the menu? Presumably, if you are saying that then the actual behaviour is different. This is what I want to see - a clear description of what the problem is.

As I mentioned, the code as you posted will always show the menu, because you unconditionally assign showGameMenu to "true". As Matthew Wood states, you can fix it by using the double equals comparison operator. Alternatively, you can just write:
if(showGameMenu) {
    // Show the menu
} else {
    // Hide the menu
}

Edited by rip-off, 04 June 2012 - 06:24 AM.


#6 Adam West   Members   -  Reputation: 219

Like
0Likes
Like

Posted 04 June 2012 - 03:37 PM

Thanks guys, both answers are super helpful.
Posted Image




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS