Showing posts with label irrlicht. Show all posts
Showing posts with label irrlicht. Show all posts

Friday, June 8, 2012

Simply Make My First Irrlicht App for iPhone(iOS) with XCode 4.2

Before you start, you need to successfully compile Irrlicht library. To do it, you can refer to this posting.
Hereafter I adapt the first one of the official Irrlicht tutorials.
  1. Irrlicht for OpenGL ES only deals with textures with a 2-powered size.
    Get 'media/sydney.bmp' in Irrlicht library source folder(Irrlicht_ogles from the prerequisite guide). Open it with your graphic application and scale it to width 256 pixels and height 256 pixels. Changing only the size without scaling doesn't work. Save it as sydney256x256.bmp.

  2. Let's create an XCode project. Run XCode 4.2.
    On the menu, choose File - New - New Project. In the dialog box that pops up, choose the top-right item, Simple View Application in iOS-Application section.
    Let's name it IrrlichtApp. Choose the parent folder of Irrlicht library source folder so that this new project resides next to Irrlicht library source folder.
    Change all the extensions .m to .mm (ViewController.m, AppDelegate.m, [Supporting Files]/main.mm) to .mm in order to compile C++. 

  3. Let's add a few graphic assets.
    Run Finder and open the folder of this project. Next to your IrrlichtApp.xcodeproj, create a folder named Assets. Copy sydney256x256.bmp into Assets folder and copy sydney.md2 from 'media' folder in Irrlicht library source folder into Assets folder. Right-click on the project head(red rectangle in the picture) in the leftmost column and Choose Add Files to "IrrlichtApp"... on the popup menu.
    In the dialogbox that pops up, find and click on Assets folder and in the lower part therein choose Create folder references for any added folders among the options. Click Add button.

  4. Let's make this app full-screen without the status bar at the top.
    Expand IrrlichtApp/Supporting Files under the project head and click on IrrlichtApp-Info.plist. A table show up to the right. Right-click on the white space under the last table item. Choose Add Row on the popup menu.
    A new item at the end shows up with a pop-up list as in the next picture. Choose Status bar is initially hidden from the list.
    Click the spinner(red rectangle in the next picture) on the right side of the third column(Value) of the new row in the table.
    Choose YES from the pop-up list.

  5. Let's add Irrlicht library as project.
    Right-click on the project head in the leftmost column and choose Add Files to "IrrlichtApp"... on the popup menu. Find source/Irrlicht/MacOSX/MacOSX.xcodeproj in Irrlicht library folder and choose it.
    Click on IrrlichtApp project head in the leftmost column. To the right, click on IrrlichtApp under PROJECT, the first section in the second column, To the right, click on Build Settings tab at the top in the table. Scroll the long table down a lot until you find Search Paths section and double-click the input cell on the right of Header Search Paths row(instead you can use the search box at the top of this table). In the popup dialogbox that shows up, click on the plus-sign button in the lower-left corner as in the next picture.
    The first row will be activated for input. Type '../Irrlicht_ogles/include' into the activated right cell and click Done button in the bottom-right corner.

  6. Let's add necessary libraries for linking.
    In the second column from the left, click on IrrlichtApp under TARGETS, the second section and to the right, click on Build Phases tab at the top.
    Expand the third bar by clicking on the dark small triangle at the left of the bar and click on the plus-sign button in the bottom-left corner of the opened bar.
    In the dialogbox that has shown up, choose libIrrlicht.a under Workspace section as in the above picture and click Add button in the bottom-left corner. Also add OpenGLES.framework, QuartzCore.framework under iOS 5.0 section in the same way.

  7. Let's establish the project dependency.
    You still stay at [Build Phases] tab. Loot at the first bar named [Target Dependencies] and by clicking the dark triangle at the left of the bar, expand it.
    Click the plus-sign in the bottom-left corner of the opened bar and the dialogbox in the next picture pops up.
    Choose libIrrlicht.a under [IrrlichtApp/MacOSX]. Make sure that a new item named [libIrricht.a (MacOSX)] is inserted in the opened bar.

  8. Let's add actual code.
    Note that in the following, lines in the boldface need to be added.
    Open IrrlichtApp-Prefix.pch, the precompiled header under IrrlichtApp/Supporting Files and add the following code.
    #ifdef __OBJC__
        #import <UIKit/UIKit.h>
        #import <Foundation/Foundation.h>
        #import <QuartzCore/CoreAnimation.h>
    #endif
    
    #ifdef __cplusplus
    #include <irrlicht.h>
    using namespace irr ;
    using namespace core ;
    using namespace scene ;
    using namespace video ;
    using namespace io ;
    using namespace gui ;
    #endif
    
    Open ViewController.h and add the following code.
    @interface ViewController : UIViewController
    {
        CADisplayLink* m_oDispLnk ;
        IrrlichtDevice* m_pDevice ;
    }
    @end
    
    Open ViewController.mm and add the following code.
    -(void) viewWillAppear:(BOOL)a_animated
    {
        [super viewWillAppear:a_animated];
        
        SIrrlichtCreationParameters params;
        params.DriverType = video::EDT_OGLES1;
        params.WindowSize = core::dimension2d<u32>(320,480);
        params.WindowId   = self.view;
        params.Bits       = 32;
        m_pDevice = createDeviceEx( (const SIrrlichtCreationParameters)params ) ;
        if( ! m_pDevice ) return ;
        
        NSString* oBunPath = [ [NSBundle mainBundle] bundlePath ] ;
        m_pDevice->getFileSystem()->changeWorkingDirectoryTo( [oBunPath cStringUsingEncoding:NSASCIIStringEncoding] ) ;
        
        IVideoDriver* driver = m_pDevice->getVideoDriver() ;
        ISceneManager* smgr = m_pDevice->getSceneManager() ;
        
        IAnimatedMesh* mesh = smgr->getMesh( "./Assets/sydney.md2" ) ;
        if( ! mesh ) return ;
        
        IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
        if( node )
        {
            node->setMaterialFlag(EMF_LIGHTING, false);
            node->setMD2Animation(scene::EMAT_STAND);
            ITexture* pTex = driver->getTexture( "./Assets/sydney256x256.bmp" ) ;
            node->setMaterialTexture( 0, pTex ) ;
        }
        smgr->addCameraSceneNode( 0, vector3df(0,30,-40), vector3df(0,5,0) ) ;
        
        // iOS timer.
        m_oDispLnk = [ [UIScreen mainScreen] displayLinkWithTarget:self selector:@selector(updateDisplay) ] ;
        [ m_oDispLnk addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes ] ;
    }
    
    
    -(void) updateDisplay
    {
        m_pDevice->run() ;
        
        m_pDevice->getVideoDriver()->beginScene( true, true, SColor(255,100,101,140) ) ;
        
        m_pDevice->getSceneManager()->drawAll() ;
        //m_pDevice->getGUIEnvironment()->drawAll() ;
        
        m_pDevice->getVideoDriver()->endScene();
    }
    
    
    -(void) viewDidDisappear:(BOOL)a_animated
    {
        [super viewDidDisappear:a_animated];
        
        if( m_oDispLnk != nil )
            [ m_oDispLnk invalidate ] ;
        
        if( m_oDispLnk != NULL )
            m_pDevice->drop();
    }
    
    Save all the files(Opt-Cmd+S).

  9. Now build and run it(Cmd+R).

Wednesday, June 6, 2012

Compile Irrlicht for iPhone(iOS) in XCode 4.2

  1. Run Finder, open Applications/Utilities folder under FAVORITES, or run Launchpad on Dock and open Utilities folder on the first page. Run Terminal in either of them.
    By using 'cd' command, go to the location where you want to put the Irrlicht source files.
    Create a folder called Irrlicht_ogles by typing in and running "mkdir Irrlicht_ogles", which Irrlicht Engine source files will be downloaded into.
    Go into the folder by typing in and running "cd Irrlicht_ogles"

  2. Download the source files by running the next command at the Terminal prompt. Make sure that you put a space and a dot at the end. I got the URL address from here.
    $ svn co -r 4073 http://irrlicht.svn.sourceforge.net/svnroot/irrlicht/branches/ogl-es .

  3. Find the file source/Irrlicht/MacOSX/MacOSX.xcodeproj in the folder. Double-click it to run XCode 4.2 or open it in XCode 4.2

  4. Click Active Scheme button in the topleft corner, which is in the red rectangle in the next picture.
    The scheme popup menu appears. Choose libIrrlicht.a as in the next picture.

  5. Click MacOSX at the top of the left column. To the right, choose MacOSX under PROJECT in the next column. In the table to the right, find Base SDK under Architectures and click the input cell to the right of it. When a popup menu appears, choose Latest iOS as in the next picture. 
    At Architectures entry right above Base SDK, click the input cell to the right of it. Choose Standard in the popup menu as in the next picture.

  6. In the second column from the right, choose libIrricht.a (in the red rectangle) right under TARGETS, the next entry after PROJECT which we just worked in. In the table to the right, find Architectures under Architectures and click the input cell to the right. Choose Standard in the popup menu as in the next picture.
    Find Compiler for C/C++/Objective-C under Build Options, the third section, click the input cell to the right. Choose Default Compiler in the popup menu as in the next picture.

  7. In the toolbar at the top, click right on text "iOS Device" on Active Scheme button on the left part. Choose iPhone 5.0 Simulator in the popup menu.

  8. In the bottom left corner, type "OSX" into the filter box in the red rectangle in the next picture. Some pertinent files appear above in the leftmost column. Right-click OSXClipboard.mm and choose delete. When the dialogbox pops up. choose Remove Reference Only.

  9. Clear the text you typed in the filter box and expand MacOSX in the left-most column to Irrlicht/Engine/irr/device. Create a new group called iOS within "device" and add files CIrrDeviceIPhone.* in the folder source/Irrlicht into the new group. Expand to Irrlicht/Engine/video and create another new group called OpenGL_ES1 within "video", Add files COpenGLES*.* in the folder source/Irrlicht into the new group but don't choose COpenGLES2*.*

  10. Try to build(cmd+B). Some build errors show up with CImageLoaderPNG.cpp and CImageWriterPNG.cpp.
    Change the erroneous line
    longjmp(png_ptr->jmpbuf, 1);
    
    to
    longjmp(png_jmpbuf(png_ptr), 1);
    and the erroneous line
    io::IReadFile* file=(io::IReadFile*)png_ptr->io_ptr;
    to
    io::IReadFile* file=(io::IReadFile*)png_get_io_ptr(png_ptr);
    Now you should see that Build libIrrlicht.a has succeeded.
You can build your first Irrlicht app by working through this guide.