BSP map and simple robot action control instance of Ogre

Source: Internet
Author: User
Tags map data structure


// This code BSP map simple robot action control

# Include <ogre. h> // main ogre File
# Include <OIS/Ois. h> // input control file

# Include "exampleframelistener. H" // listener class

Using namespace ogre; // use the namespace
Scenemanager * scenemgr; // main scenario manager object
Camera * Cam; // main camera object
Renderwindow * window; // renders the window object.
String mquakelevel; // BSP map configuration file path

Class myframelistener: Public exampleframelistener
{
Public:
Myframelistener (renderwindow * WND, Camera * cams, scenenode * nodes, scenenode * camnode): exampleframelistener (WND, cams, false, false ),
Mnode (nodes), mcamnode (camnode), mchildnode (0), mentity (0), manimationstate (0), mstarstate (true)
{
// Movement speed
Mmovespeed= 200;
// Showdebugoverlay (false );
// Read Model Animation
Animationstar ();
// Sets whether the animation is played cyclically.
If (manimationstate)
{
Manimationstate-> setloop (true );
Manimationstate2-> setloop (true );
}
}
~ Myframelistener ()
{

}

Bool framerenderingqueued (const frameevent & EVT)
{

// Exit if the window is closed
If (mwindow-> isclosed () return false;

Mspeedlimit = mmovescale * EVT. timesincelastframe;

// Obtain the keyboard and mouse status
Mkeyboard-> capture ();
Mmouse-> capture ();
If (mjoy) mjoy-> capture ();

Bool buffj = (mjoy )? Mjoy-> buffered (): true;

// Ogre: vector3 lastmotion = mtranslatevector;

If (! Mmouse-> buffered () |! Mkeyboard-> buffered () |! Buffj)
{
If (mtimeuntilnexttoggle> = 0)
Mtimeuntilnexttoggle-= EVT. timesincelastframe;

Mmovescale = mmovespeed * EVT. timesincelastframe;
Mrotscale = mrotatespeed * EVT. timesincelastframe;

Mrotx = 0;
Mroty = 0;
// If no input is specified, the variable for moving the character is initialized.
Mtranslatevector = ogre: vector3: zero;

}

// Call the keyboard and mouse control function
If (! Mkeyboard-> buffered ())
If (processunbufferedkeyinput (EVT) = false)
Return false;
If (! Mmouse-> buffered ())
If (processunbufferedmouseinput (EVT) = false)
Return false;

// If the character is being moved
If (mtranslatevector = ogre: vector3: zero)
{
Mcurrentspeed-= EVT. timesincelastframe * 0.3;
// Mtranslatevector = lastmotion;

If (mstarstate) // action control
{
Manimationstate-> setenabled (true );
Manimationstate2-> setenabled (false );
Mstarstate = false;
}
// Speed Control
Manimationstate-> addtime (EVT. timesincelastframe );
}
Else // static characters
{
If (! Mstarstate) // action control
{
Manimationstate2-> setenabled (true );
Manimationstate-> setenabled (false );
Mstarstate = true;
}
Mcurrentspeed + = EVT. timesincelastframe;
// Speed Control
Manimationstate2-> addtime (EVT. timesincelastframe );
}
If (mcurrentspeed> 1.0)
Mcurrentspeed= 1.0;
If (mcurrentspeed< 0.0)
Mcurrentspeed= 0.0;

Mtranslatevector * = mcurrentspeed;

// Call the function if the keyboard or mouse is used
If (! Mmouse-> buffered () |! Mkeyboard-> buffered () |! Buffj)
Movecamera ();


Return true;
}

// Move the character and camera
Void movecamera ()
{
Mnode-> yaw (mrotx );
Mcamnode-> pitch (mroty );
Mnode-> translate (mtranslatevector, node: ts_local );

}
// Obtain Model Actions
Void animationstar ()
{
// Obtain the first subnode (Hook model node)
Mchildnode = static_cast <scenenode *> (mnode-> getchild (0 ));
// Obtain the Model
Mentity = static_cast <entity *> (mchildnode-> getattachedobject (0 ));

// Obtain the model action.
Manimationstateset = mentity-> getallanimationstates ();
// Load the action to the map data structure
If (manimationstateset)
{
Animationstateiterator Mitr = manimationstateset-> getanimationstateiterator ();
While (Mitr. hasmoreelements () // false is returned to the end; otherwise, true is returned.
{
Animationstate * anistate = Mitr. getnext ();
// Insert a data entry
Manimationmap. insert (make_pair (anistate-> getanimationname (), anistate ));
}

}
// Search for read actions
If (! Manimationmap. Empty ())
{
Manmapit = manimationmap. Find ("idle ");
If (manmapit! = Manimationmap. End ())
Manimationstate = manmapit-> second;
Manmapit = manimationmap. Find ("walk ");
If (manmapit! = Manimationmap. End ())
Manimationstate2 = manmapit-> second;
}

}
Protected:
Scenenode * mnode, * mcamnode, * mchildnode;
Entity * mentity;
Animationstateset * manimationstateset;
Animationstate * manimationstate, * manimationstate2;
STD: Map <string, animationstate *> manimationmap;
STD: Map <string, animationstate *>: iterator manmapit;
Bool mstarstate;
Real mstargap;
PRIVATE:

};
// Initialize the Function
Void create_game (void)
{
// The maximum distance for video camera Cutting
Cam-> setfarclipdistance (4000 );
// The camera cannot rotate the Z axis.
Cam-> setfixedyawaxis (true, vector3: unit_z );
Viewpoint Vp = scenemgr-> getsuggestedviewpoint (true );

/*
Create scenario nodes and hook objects
Root
|
Nodec1
//
Nodea1 nodec1c1
//
[Entity] nodec1c1c1
/
[Camera]
*/
Scenenode * node = scenemgr-> getrootscenenode ()-> createchildscenenode ("nodec1", VP. position );
Node-> pitch (degree (90 ));
Entity * entity = scenemgr-> createentity ("entity2", "robot. mesh ");
Scenenode * nodec1 = node-> createchildscenenode ("nodea1 ");
// Bind the object
Nodec1-> attachobject (entity );
// Rotate 90 degrees
Nodec1-> yaw (degree (90 ));
Node-> Scale (0.5, 0.5, 0.5 );

Scenenode * nodec1c1 = node-> createchildscenenode ("nodec1c1 ");
Scenenode * nodec1c1c1 = nodec1c1-> createchildscenenode ("nodec1c1c1", vector3 (0,100,200 ));
Nodec1c1c1-> attachobject (CAM );
Cam-> setautotracking (true, nodec1, vector3 (0, 0, 30 ));

/*
// Set the interpolation method between key frames to Spline Interpolation
Animation: setdefaultinterpolationmode (Animation: im_spline );
// Obtain the animationstate class object for the object "going" Action
Manimstate = nodec1-> getanimationstate ("walk ");
// "Enable" (start) This action
Manimstate-> setenabled (true );
*/

}

// Load the resource configuration file
Void setupresources (void)
{
// Load the configuration file
Configfile CF;
Cf. Load ("resources. cfg ");

// Group iterator for reading the configuration file (used to read the entire group)
Configfile: sectioniterator SECI = Cf. getsectioniterator ();

String secname, typename, archname;
While (SECI. hasmoreelements ())
{
// Read the key name of the configuration file (read each group cyclically)
Secname = SECI. peeknextkey ();
Configfile: settingsmultimap * settings = SECI. getnext ();
Configfile: settingsmultimap: iterator I;
// Read the content in each group cyclically
For (I = settings-> begin (); I! = Settings-> end (); ++ I)
{
// I is of the make_pair type
Typename = I-> first;
Archname = I-> second;
// Add Resource path Resource Type Resource Name to Resource Group Manager
Resourcegroupmanager: getsingleton (). addresourcelocation (
Archname, typename, secname );
}
}

// Read the BSP map configuration file
Cf. Load ("quake3settings. cfg ");

// A compressed package pointing to a map file
String mquakepk3;
Mquakepk3 = Cf. getsetting ("pak0location ");
// Path in the compressed package
Mquakelevel = Cf. getsetting ("map ");
/// Add the resource package path type to the Resource Group Manager
Resourcegroupmanager: getsingleton (). addresourcelocation (
Mquakepk3, "Zip", resourcegroupmanager: getsingleton (). getworldresourcegroupname (), true );
}

Void loadresources (void)
{
// Create a map
// Scenemgr-> setworldgeometry (mquakelevel );

// Turn off rendering of everything indexes t Overlays
Scenemgr-> clearspecialcaserenderqueues ();
Scenemgr-> addspecialcaserenderqueue (render_queue_overlay );
Scenemgr-> setspecialcaserenderqueuemode (scenemanager: scrqm_include );

// Set up the world geometry link
Resourcegroupmanager: getsingleton (). linkworldgeometrytoresourcegroup (
Resourcegroupmanager: getsingleton (). getworldresourcegroupname (),
Mquakelevel, scenemgr );

// Initialise the rest of the resource groups, parse scripts etc
Resourcegroupmanager: getsingleton (). initialiseallresourcegroups ();
Resourcegroupmanager: getsingleton (). loadresourcegroup (
Resourcegroupmanager: getsingleton (). getworldresourcegroupname (),
False, true );

// Back to full rendering
Scenemgr-> clearspecialcaserenderqueues ();
Scenemgr-> setspecialcaserenderqueuemode (scenemanager: scrqm_exclude );

 

}

Int winapi winmain (hinstance hinst, hinstance, lpstr str1_line, INT)
{
// Create log management
Logmanager * logmgr = new logmanager;
Log * log1 = logmgr-> getsingleton (). createlog ("mylog. log", true, true, false );
// Create a primary object
Root * root = New Root ();
// Read resource file configuration
Setupresources ();
// Displays the Ogre configuration dialog box.
If (! Root-> showconfigdialog () return 0;
// Initialize the Root primary object and create a window
Window = root-> initialise (true, "Love Mnn ");

// Create a scenario Manager
Scenemgr = root-> createscenemanager ("bspscenemanager ");
// Create a camera
Cam = scenemgr-> createcamera ("maincamera ");
// Set the camera position to Z 500
// Cam-> setposition (vector3 (0, 0, 0 ));
// Camera viewpoint Z-300
// Cam-> lookat (vector3 (300 ));
// Set the proximity
Cam-> setnearclipdistance (5 );
// Create a view and bind the camera
Viewport * Vp = Window-> addviewport (CAM );
// Set the background color
VP-> setbackgroundcolour (colourvalue (1, 1 ));
// Set the camera screen ratio cam-> setaspectratio (1.333333); 1.77777
Cam-> setaspectratio (
Real (VP-> getactualwidth ()/real (VP-> getactualheight ()));
// Create texture Management
Texturemanager: getsingleton (). setdefanumnummipmaps (5 );
// Create resource management and load Resources
Resourcegroupmanager: getsingleton (). initialiseallresourcegroups ();

Loadresources ();
// Function implementation
Create_game ();

// Create a listener object
Myframelistener * mylistener = new myframelistener (window, Cam, scenemgr-> getscenenode ("nodec1"), scenemgr-> getscenenode ("nodec1c1 "));
// Close the FPS prompt box
// Mylistener-> showdebugoverlay (false );
// Register your frame listening object before calling the startrendering () method !!!
Root-> addframelistener (mylistener );
// Start Rendering
Root-> startrendering ();

If (mylistener)
Delete mylistener;
If (Root)
Delete root;

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.