Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Saturday, October 31, 2009

Irrlicht Engine - Easy Tutorial – Basics

This post is part of my tutorial on how to use Irrlicht Engine and a continuation to the article Irrlicht Engine - Easy Tutorial – Intro.

Let’s view the example I used in the previous post. Here is the code:

#include <irrlicht.h>

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;

using namespace io;
using namespace gui;

int main()

{
 IrrlichtDevice *device = createDevice(
   video::EDT_OPENGL,
   dimension2d<u32>(800, 600),
   16, false, false, false, 0);

 if (!device)
  return 1;

 IVideoDriver* driver = device->getVideoDriver();

 ISceneManager* sceneManager =
   device->getSceneManager();

 sceneManager->addCameraSceneNode();

 while(device->run())

 {
  driver->beginScene(
    true, true, SColor(255,50,130,50));

  sceneManager->drawAll();
  driver->endScene();
 }

 device->drop();
 return 0;
}


This is a very basic application. First thing we need to do is to create Irrlicht device with the createDevice function. The first parameter is video::E_DRIVER_TYPE type and can be video::EDT_DIRECT3D9, video::EDT_DIRECT3D8, video::EDT_OPENGL, video::EDT_NULL, video::EDT_SOFTWARE, video::EDT_BURNINGSVIDEO. I’ll use video::EDT_OPENGL because my Irrlicht binaries are from the official site and they are not built with DirectX. (And I am lazy.)

Next parameter as you can assume is the window size. I won’t discuss the other parameters now as they are not important for my tutorial. In fact I can call createDevice this way:

IrrlichtDevice *device = createDevice(

   video::EDT_OPENGL,
   dimension2d<u32>(800, 600));

When you finish using the device don’t forget to call:
device->drop(); 
You can learn more for the Irrlicht device class here - http://irrlicht.sourceforge.net/docu/classirr_1_1_irrlicht_device.html.

And now the cool part – the scene manager object. It manages the scene nodes, meshes, cameras, lights and so on. Your scene is represented as a hierarchical scene graph.
You obtain a reference to the scene manager object through the call:

device->getSceneManager();

Let’s do something fun with our example. I’ll change our camera to FPS mode – FPS camera in Irrlicht is a camera node suited for a first person shooter. It moves with the arrows and rotates with the mouse.
I’ll add some simple scene nodes as cube and sphere.

To add a FPS camera we must call:
ICameraSceneNode *camera = sceneManager->addCameraSceneNodeFPS();

Our cube:
ISceneNode *cube = sceneManager->

        addCubeSceneNode(15.0f, 0, -1, core::vector3df(150,10,10));

To make the camera look at the cube:
camera->setTarget(cube->getAbsolutePosition());

I’ll add a light source with the default parameters. Without it our objects will look ugly and black. Now they will be ugly but with some light on the front side.

Here is the full code:
#include <irrlicht.h>

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;

using namespace io;
using namespace gui;

int main()

{
 IrrlichtDevice *device = createDevice(
   video::EDT_OPENGL,
   dimension2d<u32>(800, 600));

 if (!device)
  return 1;

 IVideoDriver* driver = device->getVideoDriver();

 ISceneManager* sceneManager =
   device->getSceneManager();

 ICameraSceneNode *camera =
   sceneManager->addCameraSceneNodeFPS();

 ISceneNode *cube = sceneManager->
   addCubeSceneNode(15.0f, 0, -1,
     core::vector3df(150,10,10));

 sceneManager->addSphereSceneNode(
   5.0f, 16, 0, -1,
   core::vector3df(100,30,10));

 sceneManager->addLightSceneNode();

 camera->setTarget(cube->getAbsolutePosition());

 while(device->run())
 {
  driver->beginScene(

    true, true, SColor(255,50,130,50));

  sceneManager->drawAll();
  driver->endScene();
 }

 device->drop();
 return 0;
} 
 
 

And the result


Sunday, October 25, 2009

Irrlicht Engine – Easy Tutorial - Intro

Irrlicht Engine is a powerful way to make your own game even if you are not pro at game development. I decided to write a simple and easy tutorial for beginners on how to make games with Irrlicht. I’ll make it in several parts with a lot of examples.
What do you need to know for this tutorial?

  • Basic programming
  • C++

What do you need for this tutorial?
  • Irrlicht - http://irrlicht.sourceforge.net/
  • C++ IDE – I use Eclipse - http://eclipse.org/ for many of my projects, I’ll use it here too.
  • If you want to use Irrlicht with DirectX9 you must get DirectX SDK and build Irrlicht with it. I am lazy so I’ll use OpenGL.


Intro
How to make Eclipse build your project with Irrlicht support. Of course you don’t need to use Eclipse, if so skip this part.

Create new Eclipse project – in my case I’ll call it IrrlichtTutorial.




To add Irrlicht to your project you need to select it and go to Project->Properties from the main menu tab.


Then select C/C++ Build -> Settings. You will see something like this:


Go to GCC C++ Compiler->Directories and add the path to Irrlicht includes in my case D:\irrlicht-1.6\include.





Then go to the C++ Linker part and select Libraries Tab. Add Irrlicht in the first field (Libraries) and the path to the appropriate Irrlicht lib subfolder in the second. In my case it would be D:\irrlicht-1.6\lib\Win32-gcc.



That’s it. Let’s test if we can build a project. Of course to run an Irrlicht project you must have Irrlicht.dll in your path. If you use multiple Irrlicht versions you can add the correct dll in your project folder.
Now, lets build something simple yet useful. A simple window doing nothing is just fine. Here is the code:

#include <irrlicht.h>

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

int main()
{
 IrrlichtDevice *device = createDevice( video::EDT_OPENGL, dimension2d<u32>(800, 600), 16,
   false, false, false, 0);

 if (!device)
  return 1;

 IVideoDriver* driver = device->getVideoDriver();
 ISceneManager* sceneManager = device->getSceneManager();
 IGUIEnvironment* guiEnvironment = device->getGUIEnvironment();


 sceneManager->addCameraSceneNode();

 while(device->run())
 {
  driver->beginScene(true, true, SColor(255,50,130,50));
  sceneManager->drawAll();
  guiEnvironment->drawAll();
  driver->endScene();
 }

 device->drop();
 return 0;
}



And what we’ve done:



An empty window. Cool!

Next: Irrlicht Engine - Easy Tutorial - Basics

Saturday, March 22, 2008

Two online books for java game development

The first is Killer Game Programming in Java at http://fivedots.coe.psu.ac.th/~ad/jg/.
Really good book for 2d and 3d Java game development.

The second is Pro Java 6 3D Game Development at http://fivedots.coe.psu.ac.th/~ad/jg2/