RTS Camera

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
EglStrk
Posts: 7
Joined: Mon Apr 09, 2007 6:52 pm

RTS Camera

Post by EglStrk »

I see to be having trouble with some RTS camera code I got from one of the Irrlcht forums. I understand the error, but not why I'm getting it. I'm using Dev C++ with Irrlich 1-3.

Here are my errors:
  • 'pCamera' undeclared (first use this function)
    'pDriver' undeclared (first use this function)
    'pDevice' undeclared (first use this function)
Here is my code:

Code: Select all

#include <irrlicht.h>

using namespace irr;

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

#pragma comment(lib, "Irrlicht.lib")
void updateCamera();
int main(){
    IrrlichtDevice* pDevice = 
                    createDevice(EDT_OPENGL, dimension2d<s32>(800, 600));
    pDevice->setWindowCaption(L"RTS Camera");
    
    IVideoDriver* pDriver = pDevice->getVideoDriver();
    ISceneManager* pSmgr = pDevice->getSceneManager();
    IGUIEnvironment* pGuienv = pDevice->getGUIEnvironment();
    
    pGuienv->addStaticText(L"Basic Custom RTS Camera", rect<int>(10,10,200,22),true);
    
    IAnimatedMesh* pMesh = pSmgr->getMesh("C:/Program Files/Engines/irrlicht-1.3/media/sydney.md2");
    IAnimatedMeshSceneNode* pNode = pSmgr->addAnimatedMeshSceneNode(pMesh);
    
    if(pNode){
             pNode->setMaterialFlag(EMF_LIGHTING, false);
             pNode->setFrameLoop(0, 310);
             pNode->setMaterialTexture(0, pDriver->getTexture("C:/Program Files/Engines/irrlicht-1.3/media/sydney.bmp"));
    }
    
    ICameraSceneNode* pCamera;
    pCamera = pSmgr->addCameraSceneNode();
    
    while (pDevice->run()){
          pDriver->beginScene(true, true, SColor(255, 100, 101, 140));
          
          pSmgr->drawAll();
          pGuienv->drawAll();
          updateCamera();
          pDriver->endScene();
    }
    pDevice->drop();
    return 0;
}

void updateCamera(){
     vector3df cpos = pCamera->getPosition();
     dimension2d<s32> screenSize = pDriver->getScreenSize(); 
     cpos.Z+=2;
     cpos.Y-=2;
     pCamera->setTarget(cpos);
     
     int x = pDevice->getCursorControl()->getPosition().X;
     int y = pDevice->getCursorControl()->getPosition().Y;
     
     int fps = pDriver->getFPS();
     float dist = 200.0f/fps;
     if (x > (screenSize.Width - 50)) 
   {    
      cpos = pCamera->getPosition();       
      cpos.X += dist; 
      pCamera->setPosition(cpos);                         
   } 
   else if (x < 50) 
   {    
      cpos = pCamera->getPosition();       
      cpos.X -= dist; 
      pCamera->setPosition(cpos);                      
   } 

   if (y < 50) 
   { 
      cpos = pCamera->getPosition();       
      cpos.Z += dist; 
      pCamera->setPosition(cpos);                         
   } 
   else if (y > (screenSize.Height - 50)) 
   {    
      cpos = pCamera->getPosition();       
      cpos.Z -= dist; 
      pCamera->setPosition(cpos);                      
   } 
}   
Help is appreciated. Thankyou.
zeno60
Posts: 342
Joined: Sun May 21, 2006 2:48 am
Location: NC, USA
Contact:

Post by zeno60 »

The cameras are undeclared in your updateCamera() function. This is a C++ problem, not an Irrlicht problem:

Code: Select all

int main()
{
     // decleration of x: "x is of type int"
     int x = 0;
     foo();
     return 0;
}

void foo()
{
     // defining x without declaring what x is a type of.
     x = 3;
}
In foo() x is undeclared. You have to pass the x variable in main to foo, declare a new x variable, or create a global x variable.

http://www.cplusplus.com/doc/tutorial/functions.html


Fixed code may contain errors as i'm not at home to double check, but I would strongly suggest you take up a few C++ lessons before continuing on your Irrlicht venture.

Code: Select all

#include <irrlicht.h>

using namespace irr;

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

#pragma comment(lib, "Irrlicht.lib")
void updateCamera();

IrrlichtDevice* pDevice;

int main(){
    pDevice =  createDevice(EDT_OPENGL, dimension2d<s32>(800, 600));
    pDevice->setWindowCaption(L"RTS Camera");
   
    IVideoDriver* pDriver = pDevice->getVideoDriver();
    ISceneManager* pSmgr = pDevice->getSceneManager();
    IGUIEnvironment* pGuienv = pDevice->getGUIEnvironment();
   
    pGuienv->addStaticText(L"Basic Custom RTS Camera", rect<int>(10,10,200,22),true);
   
    IAnimatedMesh* pMesh = pSmgr->getMesh("C:/Program Files/Engines/irrlicht-1.3/media/sydney.md2");
    IAnimatedMeshSceneNode* pNode = pSmgr->addAnimatedMeshSceneNode(pMesh);
   
    if(pNode){
             pNode->setMaterialFlag(EMF_LIGHTING, false);
             pNode->setFrameLoop(0, 310);
             pNode->setMaterialTexture(0, pDriver->getTexture("C:/Program Files/Engines/irrlicht-1.3/media/sydney.bmp"));
    }
   
    ICameraSceneNode* pCamera;
    pCamera = pSmgr->addCameraSceneNode();
   
    while (pDevice->run()){
          pDriver->beginScene(true, true, SColor(255, 100, 101, 140));
         
          pSmgr->drawAll();
          pGuienv->drawAll();
          updateCamera();
          pDriver->endScene();
    }
    pDevice->drop();
    return 0;
}

void updateCamera(){
     ICameraSceneNode* pCamera = pDevice->getActiveCamera();
     IVideoDriver* pDriver = pDevice->getVideoDriver();
     
     vector3df cpos = pCamera->getPosition();
     dimension2d<s32> screenSize = pDriver->getScreenSize();
     cpos.Z+=2;
     cpos.Y-=2;
     pCamera->setTarget(cpos);
     
     int x = pDevice->getCursorControl()->getPosition().X;
     int y = pDevice->getCursorControl()->getPosition().Y;
     
     int fps = pDriver->getFPS();
     float dist = 200.0f/fps;
     if (x > (screenSize.Width - 50))
   {   
      cpos = pCamera->getPosition();       
      cpos.X += dist;
      pCamera->setPosition(cpos);                         
   }
   else if (x < 50)
   {   
      cpos = pCamera->getPosition();       
      cpos.X -= dist;
      pCamera->setPosition(cpos);                     
   }

   if (y < 50)
   {
      cpos = pCamera->getPosition();       
      cpos.Z += dist;
      pCamera->setPosition(cpos);                         
   }
   else if (y > (screenSize.Height - 50))
   {   
      cpos = pCamera->getPosition();       
      cpos.Z -= dist;
      pCamera->setPosition(cpos);                     
   }
}
Post Reply