Add Mouse behaviour

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
jesus

Add Mouse behaviour

Post by jesus »

Hi,
i wrote this small script around a tutor source to free mouse (e.g. to close window or use GUI elements)....

Code: Select all

#include <stdio.h>
#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")

class EngineState {

        bool freemouse;
        IrrlichtDevice *device;
        ICameraSceneNode *camera;

public:

        void init( IrrlichtDevice *_device, ICameraSceneNode *_camera ) {
                device = _device;
                camera = _camera;
        }

        bool isFreeMouse() {
                return freemouse;
        }

        void setFreeMouse( bool _free ) {
                freemouse = _free;
                device->getCursorControl()->setVisible( freemouse );
                camera->setInputReceiverEnabled( ! freemouse );
        }

};

class MyEventReceiver : public IEventReceiver
{

        EngineState *state;

public:
        MyEventReceiver( EngineState *_state ) {
                state = _state;
        }

        virtual bool OnEvent(SEvent event)
        {
printf( "event\n" );

                if ( event.EventType == irr::EET_KEY_INPUT_EVENT ) {
                        switch(event.KeyInput.Key)
                 {
                        case KEY_ESCAPE:
                                if ( ! state->isFreeMouse() ) state->setFreeMouse( true );
                        }

                }
                else
                if ( event.EventType == irr::EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN ) {

                        if ( state->isFreeMouse() ) state->setFreeMouse( false );

                }

                return false;
        }
};

int main()
{
        EngineState state;
        MyEventReceiver receiver( &state );

        IrrlichtDevice *device =
                createDevice(EDT_SOFTWARE, dimension2d<s32>(512, 384), 16, false, false, &receiver );
        device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
        IVideoDriver* driver = device->getVideoDriver();
        ISceneManager* smgr = device->getSceneManager();
        IGUIEnvironment* guienv = device->getGUIEnvironment();


        IAnimatedMesh* mesh = smgr->getMesh("../media/sydney.md2");
        IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );

        if (node)
        {
                node->setMaterialFlag(EMF_LIGHTING, false);
           //     node->setFrameLoop(0, 310);
                node->setMaterialTexture( 0, driver->getTexture("../media/sydney.bmp") );
        }

        ICameraSceneNode *camera = smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);

        state.init( device, camera );


        state.setFreeMouse( false );
        while(device->run())
        {
                /*
                Anything can be drawn between a beginScene() and an endScene()
                call. The beginScene clears the screen with a color and also the
                depth buffer if wanted. Then we let the Scene Manager and the
                GUI Environment draw their content. With the endScene() call
                everything is presented on the screen.
                */
                driver->beginScene(true, true, SColor(0,100,100,100));

                smgr->drawAll();
                guienv->drawAll();

                driver->endScene();
        }
        device->drop();

        return 0;
}
Post Reply