How to get mouse click...[solved]

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.
ViperGuy911
Posts: 31
Joined: Tue Apr 12, 2005 12:20 am

Post by ViperGuy911 »

SARIN and trekker

Thanks for all the help. I took both of what you said and got this code:

Code: Select all

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


using namespace irr;
using namespace audiere;

bool play;

#pragma comment(lib, "Irrlicht.lib")
#pragma comment(lib, "audiere.lib")

class MyEventReceiver : public IEventReceiver 
{ 
public: 
    virtual bool OnEvent(SEvent event) 
    { 
        if(event.EventType == EET_MOUSE_INPUT_EVENT) 
        { 
            if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
				play = true;
			if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
				play = false;
		}
	}
};


int main()
{	
	MyEventReceiver MyReceiver;
	
  video::E_DRIVER_TYPE driverType;


  printf("Please select the driver you want for this example:\n"\

    " (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.2\n"\

    " (d) Software Renderer\n (e) NullDevice\n (otherKey) exit\n\n");


  char i;

  std::cin >> i;


  switch(i)

  {

  case 'a': driverType = video::EDT_DIRECTX9;break;

  case 'b': driverType = video::EDT_DIRECTX8;break;

  case 'c': driverType = video::EDT_OPENGL;   break;

  case 'd': driverType = video::EDT_SOFTWARE; break;

  case 'e': driverType = video::EDT_NULL;     break;

  default: return 0;

  }


  // create device

  IrrlichtDevice *device = createDevice(driverType,
     core::dimension2d<s32>(1024, 768), 32, true, false, false, MyReceiver);


  if (device == 0)

    return 1; // could not create selected driver.


  video::IVideoDriver* driver = device->getVideoDriver();

  scene::ISceneManager* smgr = device->getSceneManager();

  device->getFileSystem()->addZipFileArchive

      ("map.pk3");


scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
scene::ISceneNode* q3node = 0;

if (q3levelmesh)
q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));

scene::ITriangleSelector* selector = 0;

if (q3node)
{
q3node->setPosition(core::vector3df(-1370,-130,-1400));

selector = smgr->createOctTreeTriangleSelector(
            q3levelmesh->getMesh(0), q3node, 128);
q3node->setTriangleSelector(selector);
selector->drop();
}

scene::ICameraSceneNode* camera = 

camera = smgr->addCameraSceneNodeFPS(0,50.0f,300.0f);

camera->setPosition(core::vector3df(-100,30,-150));

scene::ISceneNodeAnimator* anim =

    smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(30,50,30),
core::vector3df(0,-3,0),
core::vector3df(0,50,0));


camera->addAnimator(anim);
anim->drop();

// disable mouse cursor

device->getCursorControl()->setVisible(false);

smgr->addLightSceneNode(0, core::vector3df(-60,100,400),
video::SColorf(1.0f,1.0f,1.0f,1.0f),
600.0f);


// add skybox
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);

smgr->addSkyBoxSceneNode(
	driver->getTexture("irrlicht2_up.jpg"),
	driver->getTexture("irrlicht2_dn.jpg"),
	driver->getTexture("irrlicht2_lf.jpg"),
	driver->getTexture("irrlicht2_rt.jpg"),
	driver->getTexture("irrlicht2_ft.jpg"),
	driver->getTexture("irrlicht2_bk.jpg"));


// play background music
AudioDevicePtr sound_device(OpenDevice()); 
  
OutputStreamPtr stream(OpenSound(sound_device, "Theme.mp3", true)); 
OutputStreamPtr sound(OpenSound(sound_device, "shoot.wav", false));
	
stream->setRepeat(true); 
stream->setVolume(0.5f); 
stream->play();

video::ITexture* image = driver->getTexture("ak47.bmp");
driver->makeColorKeyTexture(image,video::SColor(0,0,61,132));

scene::ISceneNode* selectedSceneNode = 0;
scene::ISceneNode* lastSelectedSceneNode = 0;

while(device->run())

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

smgr->drawAll();

if (play == true)
{
	sound->setRepeat(true);
	sound->setVolume(1.5f);
	sound->play();
}

if (play == false)
{
	sound->stop();
}

driver->draw2DImage(image, core::position2d<s32>(400,515), 
           core::rect<s32>(0,0,256,256), 0, video::SColor(255,255,255,255), true);

driver->endScene();
}

device->drop();

return 0;
}
and got only 1 error message this time. <-big accomplishmet huh? lol

Code: Select all

--------------------Configuration: FPS - Win32 Debug--------------------
Compiling...
main.cpp
C:\Documents and Settings\Tigran\My Documents\C++ TUTS\FPS\main.cpp(71) : error C2664: 'createDevice' : cannot convert parameter 7 from 'class MyEventReceiver' to 'class irr::IEventReceiver *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

main.obj - 1 error(s), 0 warning(s)
Whats the problem now?

TYVM!
Guest

Post by Guest »

ViperGuy911 wrote:SARIN and trekker

IrrlichtDevice *device = createDevice(driverType,
core::dimension2d<s32>(1024, 768), 32, true, false, false, MyReceiver);
put a "&" in front of MyReceiver. You want to pass the address of the event receiver into the createDevice function.
ViperGuy911
Posts: 31
Joined: Tue Apr 12, 2005 12:20 am

Post by ViperGuy911 »

EDIT: trekker. I looked at your reply with the code. The shooting sound is great btw. I got to actually look at your code today and I put it in my game.
There are no error messages now->YESS! FINALLY!. Though the sound does not play. I put the shooting sound like this

Code: Select all

while(device->run())

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

smgr->drawAll();

if (play == true)
{
	sound->setRepeat(false);
	sound->setVolume(1.5f);
	sound->play();
}

if (play == false)
{
	sound->setRepeat(false);
	sound->setVolume(0.0f);
	sound->play();
}

driver->draw2DImage(image, core::position2d<s32>(300,250), 
           core::rect<s32>(0,0,256,256), 0, video::SColor(255,255,255,255), true);

driver->endScene();
}

device->drop();

return 
and the sound doesn't play when I click the mouse. Whats wrong? and Is the way I coded the sound when it equals false good?

Thanks
trekker
Posts: 18
Joined: Sat Apr 09, 2005 6:40 pm

Post by trekker »

Hi,

Your parameter to SetVolume is 1.5
sound->setVolume(1.5f);
only ranges 0.0 to 1.0 are valid... if you set it to 1.0 then it will work and you can hear the sound






Code: Select all

void ADR_CALL audiere::MultipleSoundEffect::setVolume  	(float volume  	)   	
Sets the sound's volume.
Parameters:
    volume 	0.0 = silence, 1.0 = maximum volume (default)
ViperGuy911
Posts: 31
Joined: Tue Apr 12, 2005 12:20 am

Post by ViperGuy911 »

OMG! I think my game is retarded. The sound doesn't play. I set it to 1.0f.
I think its from the event receiver. Let me put that code up.

Code: Select all

 IrrlichtDevice* device = 0;
scene::ISceneNode* node1 = 0;

class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
/*
If the key 'W' or 'S' was left up, we get the position of the scene node,
and modify the Y coordinate a little bit. So if you press 'W', the node
moves up, and if you press 'S' it moves down.
*/

if (node1 != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT)
{
}
if (node1 != 0 && event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
switch(event.MouseInput.Event) {
case EMIE_LMOUSE_PRESSED_DOWN:
music = true;
break;
case EMIE_LMOUSE_LEFT_UP:
music = false;
break;
}      
} 
   return false; 
}
};

int main()
{
	MyEventReceiver receiver;

  // ask user for driver
  video::E_DRIVER_TYPE driverType;


  printf("Please select the driver you want for this example:\n"\
         " (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.2\n"\
         " (d) Software Renderer\n (e) NullDevice\n (otherKey) exit\n\n");
  char i;

  std::cin >> i;


  switch(i)
  {
  case 'a': driverType = video::EDT_DIRECTX9; break;
  case 'b': driverType = video::EDT_DIRECTX8; break;
  case 'c': driverType = video::EDT_OPENGL; break;
  case 'd': driverType = video::EDT_SOFTWARE; break;
  case 'e': driverType = video::EDT_NULL; break;
  default: return 1;
  } 
  
  // create device and exit if creation failed
  device = createDevice(driverType, core::dimension2d<s32>(640, 480), 32, false, false, false, &receiver);
Anything wrong?

Thanks for all the help, trekker.
trekker
Posts: 18
Joined: Sat Apr 09, 2005 6:40 pm

Post by trekker »

Hello agian,

In the code you have:

scene::ISceneNode* node1 = 0;


then in the event receiver's if statement you have:

if (node1 != 0 && event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{ .......
}

My guess is that node1 is not being initialized to anything besides the = 0 assignment. If node1 is not used anywhere else in you program then you can delete it in the if () statement, so you can have:
if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{ .......
}


Also please note that you will have to hold the mouse button down for the duration of the gun shot sound because of your if (play == false) {...}

Also your var in the main loop is "play" while the var in your event receiver is "music", you need the var to be the same in both and not two different variables.

-later
-trek
ViperGuy911
Posts: 31
Joined: Tue Apr 12, 2005 12:20 am

Post by ViperGuy911 »

TREKKER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

IT WORRKED!!!!!

THANK YOU SO MUCH!!!!!!

Gonna go take a shower now. Thanks again. Bye.

[solved]
Post Reply