spaghetti code and how to avoid it?

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
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

spaghetti code and how to avoid it?

Post by humbrol »

since im new to programming, any tips on what you can toss in sep files, what should be made into classes, functions and the best way to organize this?

im thinking i should put all the cockpit functions into a class, but when i moved all the functions over to a seperate file started getting errors left and right


Code: Select all

#include <irrlicht.h>

using namespace irr;

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


//--- set camera to behave as cockpit camera of ship ---
void makeCockpit(irr::scene::ICameraSceneNode *camera, //camera
                irr::scene::ISceneNode *node, //scene node (ship)
                irr::core::vector3df offset) //relative position of camera to node (ship)
{
    // get transformation matrix of node
    irr::core::matrix4 m;
    m.setRotationDegrees(node->getRotation());

    // transform forward vector of camera
    irr::core::vector3df frv = irr::core::vector3df (0.0f, 0.0f, 1.0f);
    m.transformVect(frv);

    // transform upvector of camera
    irr::core::vector3df upv = irr::core::vector3df (0.0f, 1.0f, 0.0f);
    m.transformVect(upv);

    // transform camera offset (thanks to Zeuss for finding it was missing)
    m.transformVect(offset);

    // set camera
    camera->setPosition(node->getPosition() + offset); //position camera in front of the ship
    camera->setUpVector(upv); //set up vector of camera
    camera->setTarget(node->getPosition() + frv); //set target of camera (look at point) (thx Zeuss for correcting it)

    // update absolute position
    camera->updateAbsolutePosition();
}
void move(irr::scene::ISceneNode *node, irr::core::vector3df vel)
{
    irr::core::matrix4 m;
    m.setRotationDegrees(node->getRotation());
    m.transformVect(vel);
    node->setPosition(node->getPosition() + vel);
    node->updateAbsolutePosition();
}

void rotate(irr::scene::ISceneNode *node, irr::core::vector3df rot)
{
    irr::core::matrix4 m;
    m.setRotationDegrees(node->getRotation());
    irr::core::matrix4 n;
    n.setRotationDegrees(rot);
    m *= n;
    node->setRotation( m.getRotationDegrees() );
    node->updateAbsolutePosition();
}

void turn(irr::scene::ISceneNode *node, irr::f32 rot)
{
    rotate(node, irr::core::vector3df(0.0f, rot, 0.0f) );
}

void pitch(irr::scene::ISceneNode *node, irr::f32 rot)
{
    rotate(node, irr::core::vector3df(rot, 0.0f, 0.0f) );
}

void roll(irr::scene::ISceneNode *node, irr::f32 rot)
{
    rotate(node, irr::core::vector3df(0.0f, 0.0f, rot) );
}



// event reciever
bool keys[KEY_KEY_CODES_COUNT];

class MyEventReceiver : public IEventReceiver
{

public:

    virtual bool OnEvent(const SEvent &event)
    {
        if(event.EventType == EET_KEY_INPUT_EVENT)
        {
            keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
            return true;
        }
        return false;
    }
};



int main()
{
    for(s32 i=0; i<KEY_KEY_CODES_COUNT; i++) keys[i] = false;
    MyEventReceiver receiver;
    IrrlichtDevice *device =
      createDevice(video::EDT_OPENGL, core::dimension2d<u32>(800, 600), 32, false, false, false, &receiver);



   video::IVideoDriver* driver = device->getVideoDriver();
   scene::ISceneManager* smgr = device->getSceneManager();

   smgr->addSkyDomeSceneNode(driver->getTexture("../../media/skydome.jpg"),16,16,1.0f,2.0f);


   scene::ISceneNode* node1 = smgr->addCubeSceneNode(); //replace with what ever you want
   if (node1)
      {
      node1->setMaterialFlag(video::EMF_LIGHTING, false);
      node1->setMaterialTexture(0, driver->getTexture("../../media/skydome.jpg"));
      }
    double nodeRot = 90;

    scene::ISceneNode* node2 = smgr->addSphereSceneNode(); //just for reference
   if (node2)
     {
      node2->setMaterialFlag(video::EMF_LIGHTING, false);
      node2->setMaterialTexture(0, driver->getTexture("../../media/earth.jpg"));
      node2->setPosition(core::vector3df(0,0,200));
      node2->setRotation(vector3df(0,0,nodeRot));
      node2->setScale(vector3df(120,120,120));
     }


   scene::ICameraSceneNode *camera = device->getSceneManager()->addCameraSceneNode();

   while(device->run())
   {
        driver->beginScene(true, true, video::SColor(0,0,0,0));
        smgr->drawAll();

        { // direction control
        if(keys[irr::KEY_LEFT])
        {
            turn(node1, -0.1);
        }
        if(keys[irr::KEY_RIGHT])
        {
            turn(node1, 0.1);
        }
        if(keys[irr::KEY_UP])
        {
            pitch(node1, 0.1);
        }
        if(keys[irr::KEY_DOWN])
        {
            pitch(node1, -0.1);
        }
        if(keys[irr::KEY_COMMA])
        {
            roll(node1, 0.1);
        }
        if(keys[irr::KEY_PERIOD])
        {
            roll(node1, -0.1);
        }
        }

        {// movement control
        if(keys[irr::KEY_KEY_W])
        {
            move(node1, core::vector3df(0,0,0.1));
        }
        if(keys[irr::KEY_KEY_S])
        {
            move(node1, core::vector3df(0,0,-0.1));
        }
        }

        makeCockpit(camera, node1, core::vector3df(0,7,-30));



          if(nodeRot <= 360)
        {
          nodeRot++;
          nodeRot = (nodeRot - 0.99);
        }
        else
        {
          nodeRot = 0;
        }

         node2->setRotation(vector3df(0,nodeRot,0));



        driver->endScene();
    }

   device->drop();

   return 1;
}
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: spaghetti code and how to avoid it?

Post by randomMesh »

humbrol wrote:since im new to programming
You are trying to make that flight sim since 2007...
You call that new to programming? Is that kind of trolling or are you really that resistant to learning?
"Whoops..."
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post by humbrol »

no, i had to put this all aside for work and family issues and just picking it all back up again.
Josie
Posts: 44
Joined: Sat Jul 25, 2009 4:08 am
Location: Griffin, Georgia, US
Contact:

Post by Josie »

Regardless of time spent, you won't get very far in any sort of programing with concepts, I highly suggest that you go learn concepts of programming before attempting a huge project. Especially OOP concepts being as you're using C++. A quick google search brought up a plethora of pages, and there's always the infinitely helpful C++ FAQ Lite.
humbrol
Posts: 83
Joined: Sun Nov 18, 2007 8:22 pm

Post by humbrol »

I guess its a leap between the concept and implementation,, after some trial and error was able to put all the cockpit control into a class and wrap it up nice and neat i hope.

now just trying to find the best way to wrap all the

Code: Select all

if(keys[irr::KEY_LEFT])
        {
            Cockpit.turn(node1, -0.1);
        } 

cals into a class or function.
Josie
Posts: 44
Joined: Sat Jul 25, 2009 4:08 am
Location: Griffin, Georgia, US
Contact:

Post by Josie »

Your best bet for event handling is to use a dispatching (or delegating) even receiver. There's a code example from CuteAlien on here somewhere. The basic concept is that you have on event receiver that sends events to a bunch of other receivers, and your ship would be one of those receivers.
Post Reply