Exemple #1
0
void Simple::CreateRoom ()
{
  // We create a new sector called "room".
  room = engine->CreateSector ("room");

  // Create our world.
  ReportInfo ("Creating world!...");

  loader->LoadTexture ("stone", "/lib/std/stone4.gif");
  
  csRef<iPluginManager> plugin_mgr (
    csQueryRegistry<iPluginManager> (GetObjectRegistry()));
  // Initialize the python plugin.
  csRef<iScript> is = csQueryRegistry<iScript> (GetObjectRegistry());
  if (is)
  {
    char const* module = "pysimp";
    csRef<iCommandLineParser> cmd =
      csQueryRegistry<iCommandLineParser> (GetObjectRegistry());
    if (cmd.IsValid())
    {
      char const* file = cmd->GetName(0);
      if (file != 0)
        module = file;
    }

    // Load a python module.
    ReportInfo ("Loading script file %s...", CS::Quote::Single (module));
    if (!is->LoadModule (module))
      return;

    // Set up our room.
    // Execute one method defined in pysimp.py
    // This will create the polygons in the room.
    csString run;
    run << module << ".CreateRoom";
    // prepare arguments
    csRefArray<iScriptValue> args;
    args.Push(csRef<iScriptValue>(is->RValue("stone")));
    // run method
    csRef<iScriptValue> ret = is->Call(run,args);
    if(!ret.IsValid())
    {
      ReportError ("Failed running '%s.CreateRoom'...",module);
    }
  }
  else
    ReportError ("Could not load Python plugin");

  csRef<iLight> light;
  light = engine->CreateLight (0, csVector3 (0, 5, 0), 10,
    csColor (1, 0, 0));
  room->GetLights ()->Add (light);
}
Exemple #2
0
iShader* csStencilShadowType::GetShadow ()
{
  if (!shadowLoaded)
  {
    shadowLoaded = true;

    csRef<iPluginManager> plugin_mgr (
      csQueryRegistry<iPluginManager> (object_reg));

    // Load the shadow vertex program 
    csRef<iShaderManager> shmgr = csQueryRegistryOrLoad<iShaderManager> (
    	object_reg, "crystalspace.graphics3d.shadermanager");
    if (!shmgr) return 0;

    csRef<iShaderCompiler> shcom (shmgr->GetCompiler ("XMLShader"));
    
    csRef<iVFS> vfs = csQueryRegistry<iVFS> (object_reg);
    csRef<iDataBuffer> buf = vfs->ReadFile ("/shader/shadow.xml");
    //csRef<iDataBuffer> buf = vfs->ReadFile ("/shader/shadowdebug.xml");
    csRef<iDocumentSystem> docsys (
      csQueryRegistry<iDocumentSystem> (object_reg));
    if (docsys == 0)
    {
      docsys.AttachNew (new csTinyDocumentSystem ());
    }
    csRef<iDocument> shaderDoc = docsys->CreateDocument ();
    shaderDoc->Parse (buf, true);

    // @@@ TODO: Try to get a right ldr_context here???
    shadow = shcom->CompileShader (0,
	shaderDoc->GetRoot ()->GetNode ("shader"));
    
    if (!shadow)
    {
      Report (CS_REPORTER_SEVERITY_ERROR, "Unable to load shadow shader");
      return 0;
    }
    
  }
  return shadow;
}
Exemple #3
0
csPtr<iBase> csStencilShadowLoader::Parse (iDocumentNode* node,
					   iStreamSource*,
					   iLoaderContext* /*ldr_context*/,
					   iBase* /*context*/)
{
  csRef<iPluginManager> plugin_mgr (
  	csQueryRegistry<iPluginManager> (object_reg));
  csRef<iRenderStepType> type = csLoadPlugin<iRenderStepType> (plugin_mgr,
  	"crystalspace.renderloop.step.shadow.stencil.type");

  csRef<iRenderStepFactory> factory = type->NewFactory();
  csRef<iRenderStep> step = factory->Create ();

  csRef<iRenderStepContainer> steps =
    scfQueryInterface<iRenderStepContainer> (step);

  csRef<iDocumentNodeIterator> it = node->GetNodes ();
  while (it->HasNext ())
  {
    csRef<iDocumentNode> child = it->Next ();
    if (child->GetType () != CS_NODE_ELEMENT) continue;
    csStringID id = tokens.Request (child->GetValue ());
    switch (id)
    {
      case XMLTOKEN_STEPS:
	{
	  if (!rsp.ParseRenderSteps (steps, child))
	    return 0;
	}
	break;
      default:
        if (synldr) synldr->ReportBadToken (child);
	return 0;
    }
  }

  return csPtr<iBase> (step);
}
Exemple #4
0
int main (int argc, char *argv[])
{
  G2DTestSystemDriver System (argc, argv);
  Sys = &System;
  iObjectRegistry* object_reg = System.object_reg;

  if (!csInitializer::SetupEventHandler (object_reg, G2DEventHandler))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.g2dtest",
        "Unable to init app!");
    return false;
  }

  // Check for commandline help.
  if (csCommandLineHelper::CheckHelp (object_reg))
  {
    csCommandLineHelper::Help (object_reg);
    exit (0);
  }

  csRef<iPluginManager> plugin_mgr (
  	csQueryRegistry<iPluginManager> (object_reg));
  csRef<iCommandLineParser> cmdline (
  	csQueryRegistry<iCommandLineParser> (object_reg));

  System.myG3D = csQueryRegistry<iGraphics3D> (object_reg);
  // Now load the renderer plugin
  if (!System.myG3D)
  {
    csString canvas = cmdline->GetOption ("video");
    if (!canvas || !*canvas)
      canvas = "crystalspace.graphics3d.opengl";
    else if (strncmp ("crystalspace.", canvas, 13))
    {
      canvas = "crystalspace.graphics3d." + canvas;
    }
    System.myG3D = csLoadPlugin<iGraphics3D> (plugin_mgr, canvas);
    if (!object_reg->Register (System.myG3D, "iGraphics3D"))
    {
      csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	  "crystalspace.application.g2dtest",
	  "Unable to register renderer!");
      return -1;
    }
  }

  if (!System.myG3D)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.g2dtest",
	"Unable to load canvas driver!");
    return -1;
  }
  System.myG2D = System.myG3D->GetDriver2D ();
    
  System.SystemOpen = csevSystemOpen (object_reg);
  System.KeyboardDown = csevKeyboardDown (object_reg);
  System.Frame = csevFrame (object_reg);
  System.CanvasResize = csevCanvasResize (object_reg, System.myG2D);

  System.framePrinter.AttachNew (new FramePrinter (object_reg));

  if (!csInitializer::OpenApplication (object_reg))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.g2dtest",
        "Unable to open drivers!");
    return -1;
  }
  
  System.cursorPlugin = csQueryRegistry<iCursor> (object_reg);
  if (System.cursorPlugin)
  {
    csRef<iConfigManager> cfg (csQueryRegistry<iConfigManager> (object_reg));
    if (System.cursorPlugin->Setup (System.myG3D))
    {
      System.cursorPlugin->ParseConfigFile ((iConfigManager*)cfg);
    }
    else
      System.cursorPlugin = 0;
  }

  iNativeWindow* nw = System.myG2D->GetNativeWindow ();
  if (nw) nw->SetTitle (APP_TITLE);

  csDefaultRunLoop(object_reg);

  System.myG2D = 0;
  System.myG3D->Close();
  System.myG3D = 0;
  plugin_mgr = 0;
  cmdline = 0;

  return 0;
}
Exemple #5
0
bool csWaterDemo::Initialize ()
{
  if (!csInitializer::SetupConfigManager (object_reg, 
    "/config/waterdemo.cfg"))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR, 
      "crystalspace.application.waterdemo",
      "Failed to initialize config!");
    return false;
  }

  if (!csInitializer::RequestPlugins (object_reg,
  	CS_REQUEST_VFS,
        CS_REQUEST_PLUGIN ("crystalspace.graphics3d.opengl", iGraphics3D),
        CS_REQUEST_ENGINE,
	CS_REQUEST_IMAGELOADER,
	CS_REQUEST_FONTSERVER,
	CS_REQUEST_REPORTER,
	CS_REQUEST_REPORTERLISTENER,
        CS_REQUEST_CONSOLEOUT,
        CS_REQUEST_LEVELLOADER,
 	CS_REQUEST_END))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.waterdemo",
	"Can't initialize plugins!");
    return false;
  }

  FocusGained = csevFocusGained (object_reg);
  FocusLost = csevFocusLost (object_reg);
  Frame = csevFrame (object_reg);
  KeyboardDown = csevKeyboardDown (object_reg);

  if (!csInitializer::SetupEventHandler (object_reg, SimpleEventHandler))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.waterdemo",
	"Can't initialize event handler!");
    return false;
  }

  // Check for commandline help.
  if (csCommandLineHelper::CheckHelp (object_reg))
  {
    csCommandLineHelper::Help (object_reg);
    return false;
  }

  vc = csQueryRegistry<iVirtualClock> (object_reg);
  if (vc == 0)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.waterdemo",
    	"No iVirtualClock plugin!");
    return false;
  }

  vfs = csQueryRegistry<iVFS> (object_reg);
  if (vfs == 0)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.waterdemo",
    	"No iVFS plugin!");
    return false;
  }

  r3d = csQueryRegistry<iGraphics3D> (object_reg);
  if (r3d == 0)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.waterdemo",
    	"No iGraphics3D plugin!");
    return false;
  }

  font = r3d->GetDriver2D ()->GetFontServer()->LoadFont(CSFONT_LARGE);

  engine = csQueryRegistry<iEngine> (object_reg);
  if (engine == 0)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.waterdemo",
    	"No iEngine plugin!");
    return false;
  }

  kbd = csQueryRegistry<iKeyboardDriver> (object_reg);
  if (kbd == 0)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.waterdemo",
    	"No iKeyboardDriver plugin!");
    return false;
  }

  mouse = csQueryRegistry<iMouseDriver> (object_reg);
  if (mouse == 0)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.waterdemo",
    	"No iMouseDriver plugin!");
    return false;
  }

  csRef<iLoader> loader = csQueryRegistry<iLoader> (object_reg);
  if (loader == 0)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.waterdemo",
    	"No iLoader plugin!");
    return false;
  }

  vfs->ChDir ("/tmp");
  
  console = csQueryRegistry<iConsoleOutput> (object_reg);

  // Open the main system. This will open all the previously loaded plug-ins.
  if (!csInitializer::OpenApplication (object_reg))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    	"crystalspace.application.waterdemo",
    	"Error opening system!");
    return false;
  }

  csRef<iSector> room = engine->CreateSector ("room");

  view = csPtr<iView> (new csView (engine, r3d));
  view->GetCamera ()->SetSector (room);
  view->GetCamera ()->GetTransform ().SetOrigin (csVector3 (0, 5, 0));
  view->GetCamera ()->GetTransform ().LookAt (csVector3(5,-5,20), csVector3(0,1,0));
  csRef<iGraphics2D> g2d = r3d->GetDriver2D ();
  view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ());

  bool hasAccel;
  if (g2d->PerformExtension ("hardware_accelerated", &hasAccel))
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_NOTIFY,
      "crystalspace.application.waterdemo",
      "Hardware acceleration %s.\n",
      hasAccel ? "present" : "not present");
  }
  else
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_NOTIFY,
      "crystalspace.application.waterdemo",
      "Hardware acceleration check not available.\n");
  }

  r3d->GetDriver2D ()->SetMouseCursor( csmcNone );

  csRef<iPluginManager> plugin_mgr (
    csQueryRegistry<iPluginManager> (object_reg));

  csRef<iStringSet> strings = 
    csQueryRegistryTagInterface<iStringSet> 
    (object_reg, "crystalspace.shared.stringset");

  csRef<iShaderVarStringSet> stringsSvName = 
    csQueryRegistryTagInterface<iShaderVarStringSet> 
    (object_reg, "crystalspace.shader.variablenameset");

  //get a custom renderloop
  csRef<iRenderLoop> rl = engine->GetRenderLoopManager ()->Create ();
  
  csRef<iRenderStepType> genType = csLoadPlugin<iRenderStepType> (
    plugin_mgr, "crystalspace.renderloop.step.generic.type");

  csRef<iRenderStepFactory> genFact = genType->NewFactory ();

  csRef<iRenderStep> step;
  csRef<iGenericRenderStep> genStep;

  step = genFact->Create ();
  rl->AddStep (step);
  genStep = scfQueryInterface<iGenericRenderStep> (step);

  genStep->SetShaderType ("general");
  genStep->SetZBufMode (CS_ZBUF_USE);
  genStep->SetZOffset (false);

  engine->GetRenderLoopManager ()->Register ("waterdemoRL", rl);
  engine->SetCurrentDefaultRenderloop (rl);

  // Load in lighting shaders
  csRef<iVFS> vfs (csQueryRegistry<iVFS> (object_reg));
  csRef<iFile> shaderFile = vfs->Open ("/shader/water.xml", VFS_FILE_READ);

  csRef<iDocumentSystem> docsys (
    csQueryRegistry<iDocumentSystem> (object_reg));
  csRef<iDocument> shaderDoc = docsys->CreateDocument ();
  shaderDoc->Parse (shaderFile, true);

  csRef<iShader> shader;
  csRef<iShaderManager> shmgr (csQueryRegistry<iShaderManager> (object_reg));
  csRef<iShaderCompiler> shcom (shmgr->GetCompiler ("XMLShader"));
  csRef<iLoaderContext> ldr_context = engine->CreateLoaderContext ();
  shader = shcom->CompileShader (ldr_context,
      shaderDoc->GetRoot ()->GetNode ("shader"));

  // setup the mesh 
  csRef<iMeshObjectType> gType = csLoadPluginCheck<iMeshObjectType> (
  	plugin_mgr, "crystalspace.mesh.object.genmesh");
  
  if (!gType)
  {
    csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
        "crystalspace.application.waterdemo",
        "Error loading genmesh baseobject!");
    return false;
  }

  
  gFact = gType->NewFactory ();
  csRef<iMeshFactoryWrapper> fw = engine->CreateMeshFactory (gFact, "waterFactory");
  gFactState = scfQueryInterface<iGeneralFactoryState> (gFact);

  gMesh = gFact->NewInstance ();
  gMeshState = scfQueryInterface<iGeneralMeshState> (gMesh);
  gMeshState->SetShadowCasting (false);
  gMeshState->SetShadowReceiving (false);

  //setup a wrapper too
  gMeshW = engine->CreateMeshWrapper (gMesh, "water", room);
  csMatrix3 m;
  m.Identity ();
  gMeshW->GetMovable ()->SetTransform (m);
  gMeshW->GetMovable ()->SetPosition (csVector3(0,-5,0));
  gMeshW->GetMovable ()->UpdateMove ();
  
  //setup a material
  csRef<iMaterial> mat = engine->CreateBaseMaterial (0);

  mat->SetShader (strings->Request ("general"), shader);

  csRef<iMaterialWrapper> matW = engine->GetMaterialList ()->NewMaterial (mat,
  	"waterMaterial");


  csRef<csImageCubeMapMaker> cubeMaker;
  cubeMaker.AttachNew (new csImageCubeMapMaker ());

  csRef<iImage> img = loader->LoadImage ("/lib/cubemap/cubemap_rt.jpg");
  cubeMaker->SetSubImage (0, img);
  img = loader->LoadImage ("/lib/cubemap/cubemap_lf.jpg");
  cubeMaker->SetSubImage (1, img);

  img = loader->LoadImage ("/lib/cubemap/cubemap_up.jpg");
  cubeMaker->SetSubImage (2, img);
  img = loader->LoadImage ("/lib/cubemap/cubemap_dn.jpg");
  cubeMaker->SetSubImage (3, img);

  img = loader->LoadImage ("/lib/cubemap/cubemap_fr.jpg");
  cubeMaker->SetSubImage (4, img);
  img = loader->LoadImage ("/lib/cubemap/cubemap_bk.jpg");
  cubeMaker->SetSubImage (5, img);


  csRef<iTextureHandle> tex = r3d->GetTextureManager ()->RegisterTexture (
    cubeMaker, CS_TEXTURE_3D | CS_TEXTURE_CLAMP | CS_TEXTURE_NOMIPMAPS);

  csRef<csShaderVariable> attvar (csPtr<csShaderVariable> (
    new csShaderVariable (stringsSvName->Request ("tex diffuse"))));
  attvar->SetValue (tex);
  mat->AddVariable (attvar);  


  gMesh->SetMaterialWrapper (matW);
  
  Width = Height = 64;

  water = new float[Width*Height];
  water1 = new float[Width*Height];
  water2 = new float[Width*Height];

  memset(water,0,Width*Height*sizeof(float));
  memset(water1,0,Width*Height*sizeof(float));
  memset(water2,0,Width*Height*sizeof(float));

  WaveSpeed = 0.3f;
  WaveLife = 0.1f;
  GridSize = 0.25f;
  TimeDelta = 0.12f;


  //setup the mesh
  gFactState->SetVertexCount (Width*Height);
  gFactState->SetTriangleCount (2*((Width-1)*(Height-1)));

  //setup ibuf
  int x, z, cnt=0,idx;
  csTriangle *ibuf=gFactState->GetTriangles ();
  for(x=0;x<(Height-1);x++)
  {
    for(z=0;z<(Width-1);z++)
    {
      idx = 2*(x*(Width-1)+z);
      ibuf[idx].a = x*Width+z;
      ibuf[idx].b = x*Width+z+1;
      ibuf[idx].c = (x+1)*Width+z;
      idx++;
      ibuf[idx].a = x*Width+z+1;
      ibuf[idx].b = (x+1)*Width+(z+1);
      ibuf[idx].c = (x+1)*Width+z;
    }
  }

  //setup our vbuf
  csVector3 *vbuf = gFactState->GetVertices ();
  cnt=0;
  for(x=0;x<Height;x++)
  {
    for(z=0;z<Width;z++)
    {
      idx = x*Width+z;
      vbuf[idx].x = x*GridSize;
      vbuf[idx].y = water[idx];
      vbuf[idx].z = z*GridSize;
    }
  }

  //setup texture
  csVector2 *tbuf = gFactState->GetTexels ();
  for(x=0;x<Height;x++)
  {
    for(z=0;z<Width;z++)
    {
      idx = x*Width+z;
      tbuf[idx].x = (float)x/(float)Height;
      tbuf[idx].y = (float)z/(float)Width;
    }
  }

  lastSimTime = nextSimTime = 0.0f;

  gFactState->Invalidate ();

  engine->Prepare ();

  console->SetVisible (false);
  hasfocus = true;
  int w = r3d->GetDriver2D ()->GetWidth()/2;
  int h = r3d->GetDriver2D ()->GetHeight()/2;
  r3d->GetDriver2D ()->SetMousePosition (w, h);

  printer.AttachNew (new FramePrinter (object_reg));

  return true;
}
const char* csGenmeshSkelAnimationControlFactory::Load (iDocumentNode* node)
{
  csRef<iPluginManager> plugin_mgr (
    csQueryRegistry<iPluginManager> (object_reg));

  csRef<iLoaderPlugin> ldr_plg = CS_QUERY_PLUGIN_CLASS(plugin_mgr, 
    "crystalspace.graveyard.loader", iLoaderPlugin);

  if (!ldr_plg)
  {
    ldr_plg = csLoadPlugin<iLoaderPlugin> (plugin_mgr, 
      "crystalspace.graveyard.loader");
    if (!ldr_plg )
    {
      printf("Missing <crystalspace.graveyard.loader> plugin!\n");
      return 0;
    }
  }

  csRef<iDocumentNodeIterator> it = node->GetNodes ();
  while (it->HasNext ())
  {
    csRef<iDocumentNode> child = it->Next ();
    if (child->GetType () != CS_NODE_ELEMENT) continue;
    const char* value = child->GetValue ();
    csStringID id = xmltokens.Request (value);
    switch (id)
    {
    case XMLTOKEN_SKELFACT:
      {
        csRef<iDocumentNodeIterator> it = child->GetNodes ();
        while (it->HasNext ())
        {
          csRef<iDocumentNode> child = it->Next ();
          if (child->GetType () != CS_NODE_ELEMENT) continue;
          const char* value = child->GetValue ();
          csStringID id = xmltokens.Request (value);
          switch (id)
          {
          case XMLTOKEN_USE_PARENT:
            {
              use_parent = true;
            }
            break;
          }
        }
        if (!use_parent)
        {
          csRef<iBase> skf = ldr_plg->Parse(child, 0, 0, 0);
          skeleton_factory = scfQueryInterface<iSkeletonFactory> (skf);
          the_graveyard = skeleton_factory->GetGraveyard();
        }
      }
      break;
    case XMLTOKEN_SKELFILE:
      {
        csRef<iVFS> vfs = csQueryRegistry<iVFS> (object_reg);
	const char* filename = child->GetContentsValue ();
        csRef<iDataBuffer> buf (vfs->ReadFile(filename));
        if (buf && buf->GetSize())
        {
          csRef<iDocument> doc;

          const char* b = **buf;
          const char* error = 0;
          while (*b == ' ' || *b == '\n' || *b == '\t') b++;
          if (*b == '<')
          {
            csRef<iDocumentSystem> xml (csQueryRegistry<iDocumentSystem> (object_reg));
            if (!xml) xml = csPtr<iDocumentSystem> (new csTinyDocumentSystem ());
            doc = xml->CreateDocument ();
            error = doc->Parse(buf);
          }
          if (!error)
          {
            csRef<iBase> skf = ldr_plg->Parse(doc->GetRoot(), 0, 0, 0);
            skeleton_factory = scfQueryInterface<iSkeletonFactory> (skf);
            the_graveyard = skeleton_factory->GetGraveyard();
          }
          else
          {
            return error;
          }
        }
	else
	{
          error_buf.Format (
            "Can't load skeleton file %s!", CS::Quote::Single (filename));
          return error_buf;
	}
      }
      break;
    case XMLTOKEN_USE_BONES:
      {
        csRef<iDocumentNodeIterator> it = child->GetNodes ();
        while (it->HasNext ())
        {
          csRef<iDocumentNode> child = it->Next ();
          if (child->GetType () != CS_NODE_ELEMENT) continue;
          const char* value = child->GetValue ();
          csStringID id = xmltokens.Request (value);
          switch (id)
          {
          case XMLTOKEN_BONE:
            {
              used_bones.Push(child->GetContentsValueAsInt());
            }
            break;
          }
        }
      }
      break;
    case XMLTOKEN_RUN:
      {
        const char* scriptname = child->GetAttributeValue ("script");
        if (!scriptname)
          return "Missing script name attribute for <run>!";
        autorun_scripts.Push (scriptname);
      }
      break;
    default:
      error_buf.Format (
        "Don't recognize token %s in anim control!",
        CS::Quote::Single (value));
      return error_buf;
    }
  }

  return 0;
}