Tutorial - Setup Horde with SDL

From Horde3D Wiki
Revision as of 05:45, 1 August 2008 by Wild13 (talk | contribs)
Jump to: navigation, search

Here is a small example on how to use Horde with sdl. Its a simple frame that closes when the x on the window is clicked.

Simple SDL Frame
#include <iostream>
#include <Horde3D.h>
#include <SDL.h>

using namespace std;

int main(int argc, char* args[]){
    //Creates are bool value for the while loop.
    bool running = true;
  
    //Inits sdl with only the video extention.
    SDL_Init(SDL_INIT_VIDEO);
    //Sets are window Caption
    SDL_WM_SetCaption("Simple SDL Frame",NULL);
    //Sets the sdl video mode width and height as well has creates are opengl context.
    SDL_SetVideoMode(800,600,32,SDL_OPENGL);
    //Creates Are Event Reciver
    SDL_Event event;
    
    //Inits Horde3D
    Horde3D::init();
    
    //Sizes the Horde3D View
    Horde3D::resize( 0, 0, 800, 600 );

    // Add pipeline resource
    ResHandle pipeRes = Horde3D::addResource( ResourceTypes::Pipeline, "standard.pipeline.xml", 0 );
    // Add model resource
    ResHandle modelRes = Horde3D::addResource( ResourceTypes::SceneGraph, "character.scene.xml", 0 );
    // Add animation resource
    ResHandle animRes = Horde3D::addResource( ResourceTypes::Animation, "walk.anim.xml", 0 );
    // Load added resources
    Horde3DUtils::loadResourcesFromDisk( "" );
    
    // Add model to scene
    model = Horde3D::addNodes( RootNode, modelRes );
    // Apply animation
    Horde3D::setupModelAnimStage( model, 0, animRes, "" );
    
    // Add light source
    NodeHandle light = Horde3D::addLightNode( RootNode, "Light1", 0, "LIGHTING", "SHADOWMAP" );
    // Set light position and radius
    Horde3D::setNodeTransform( light, 0, 20, 0, 0, 0, 0, 1, 1, 1 );

    //Sets the lightnodes radius
    Horde3D::setNodeParamf( light, LightNodeParams::Radius, 50 );
    //Sets the lightnodes Field of view
    Horde3D::setNodeParamf( light, LightNodeParams::FOV, 90 );
    //Sets the lightnodes ShadowMapCOunt
    Horde3D::setNodeParami( light, LightNodeParams::ShadowMapCount, 3 );
    //Sets the lightnodes ShadowSplitLambda
    Horde3D::setNodeParamf( light, LightNodeParams::ShadowSplitLambda, 0.9f );
    //Sets the lightnodes ShadowMapBias
    Horde3D::setNodeParamf( light, LightNodeParams::ShadowMapBias, 0.001f );
    //Sets the lightnodes red value
    Horde3D::setNodeParamf( light, LightNodeParams::Col_R, 0.9f );
    //Sets the lightnodes green value
    Horde3D::setNodeParamf( light, LightNodeParams::Col_G, 0.7f );
    //Sets the lightnodes blue value
    Horde3D::setNodeParamf( light, LightNodeParams::Col_B, 0.75f );    
	
    // Add camera
    cam = Horde3D::addCameraNode( RootNode, "Camera", pipeRes );

    // Our While loop
    while(running == true){
      static float t = 0;
    
      // Increase animation time
      t = t + 10.0f * (1 / 60);
      //Checks if there is a event that needs processing
      if(SDL_PollEvent(&event)){
         //Checks to see if the event is a SDL_QUIT*x on the window is clicked.
         if(event.type == SDL_QUIT){
            //Sets the while loop to false and ends program
            running = false;
         }
      }

      // Play animation
      Horde3D::setModelAnimParams( model, 0, t, 1.0f );
    
      // Set new model position
      Horde3D::setNodeTransform( model, t * 10, 0, 0,     // Translation
                                           0, 0, 0,     // Rotation
                                           1, 1, 1 );   // Scale									   
      // Render scene 
      Horde3D::render( cam );
    }
    //Releases the Horde3D Engine
    Horde3D::release();
    //Releases SDL and quits are program
    SDL_Quit();  

}

--Rj 05:28, 1 August 2008 (CEST)