Code Search for Developers
 
 
  

kmode.cpp from Spatial Knowledge Experiments at Krugle


Show kmode.cpp syntax highlighted

/*
    Copyright (C) 2003, 2004, 2005 by Luca Cappa
    Written by Luca Cappa groton@users.sourceforge.net
  
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
  
    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "cssysdef.h"

#include "csutil/sysfunc.h"
#include "csutil/cmdhelp.h"
#include "csutil/cscolor.h"
#include "csutil/cmdhelp.h"
#include "csutil/cspmeter.h"
#include "csutil/csstring.h"
#include "csutil/scfstr.h"

#include "cstool/csview.h"
#include "cstool/initapp.h"
#include "cstool/collider.h"

#include "ivideo/graph3d.h"
#include "ivideo/graph2d.h"
#include "ivideo/natwin.h"
#include "ivideo/txtmgr.h"
#include "ivideo/texture.h"
#include "ivideo/material.h"
#include "ivideo/fontserv.h"

#include "imesh/sprite2d.h"
#include "imesh/ball.h"
#include "imesh/object.h"

#include "iengine/mesh.h"
#include "iengine/engine.h"
#include "iengine/sector.h"
#include "iengine/mesh.h"
#include "iengine/sector.h"
#include "iengine/engine.h"
#include "iengine/camera.h"
#include "iengine/light.h"
#include "iengine/texture.h"
#include "iengine/movable.h"
#include "iengine/material.h"

#include "igraphic/imageio.h"

#include "iutil/vfs.h"
#include "iutil/eventq.h"
#include "iutil/event.h"
#include "iutil/eventh.h"
#include "iutil/objreg.h"
#include "iutil/csinput.h"
#include "iutil/virtclk.h"
#include "iutil/plugin.h"
#include "iutil/string.h"
#include "iutil/comp.h"

#include "imesh/thing.h"
#include "imesh/object.h"
#include "imesh/sprite3d.h"
#include "imesh/genmesh.h"

#include "imap/loader.h"

#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/conout.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/collider.h"

#include "csgeom/quaterni.h"
#include "csgeom/transfrm.h"
#include "csgeom/math3d_d.h"
#include "csgeom/math3d.h"

#include "igeom/polymesh.h"

#include "iaws/aws.h"
#include "iaws/awscnvs.h"


#include "ske.h"
#include "kcamera.h"
#include "kutil.h"
//Includes for this file.
#include "kmode.h"


KMode::KMode ()
{
  mG3d = CS_QUERY_REGISTRY (g_objReg, iGraphics3D);
  mEngine = CS_QUERY_REGISTRY (g_objReg, iEngine);
  mFontServer = CS_QUERY_REGISTRY (g_objReg, iFontServer);
}

KMode::~KMode ()
{
}

csPtr<iTextureWrapper> KMode::CreateTextureText (char const* pText,
  csColor /*const*/& pTransColor)
{
  csRef<iGraphics2D> lG2d = mG3d->GetDriver2D ();
  size_t lWidth = lG2d->GetWidth ();
  size_t lHeight = lG2d->GetHeight ();
  
  int lFontHeight = 28; //??(float)lHeight / 20.f + 1;
  csRef<iFont> lFont;
  lFont = mFontServer->LoadFont ("VeraSansMono", lFontHeight);
  int lFontWidth;
  lFont->GetMaxSize (lFontWidth, lFontHeight);
  
  csRef<iTextureWrapper> lTexWrap = mEngine->CreateBlackTexture 
    (pText, 20 * (lFontWidth + 2)/* * strlen (pText)*/, lFontHeight + 5, 
    &pTransColor , CS_TEXTURE_2D);
  lTexWrap->Register (mG3d->GetTextureManager ());

  mG3d->SetRenderTarget (lTexWrap->GetTextureHandle ());
  
  //Save engine context.  
  //I thought this is what keeps this stuff from showing on screen.
  //??iTextureHandle *l_oldContext = mEngine->GetContext ();
  //??mEngine->SetContext (lTexWrap->GetTextureHandle ());
  // actually draw the texture now
  
  mG3d->BeginDraw (CSDRAW_2DGRAPHICS);
  lG2d->ClearAll (g_ske->FindRGBColor (pTransColor));//It will be transparent
  /*lG2d->Write (lFont, 1, 1, lG2d->FindRGB (255, 255, 255),
    g_ske->FindRGBColor (pTransColor), pText);*/

  lG2d->Write (lFont, 2, 2, lG2d->FindRGB (1, 1, 1, 255),
    -1, pText, CS_WRITE_NOANTIALIAS/*|CS_WRITE_BASELINE*/);
  lG2d->Write (lFont, 0, 0, lG2d->FindRGB (255, 255, 255, 255),
    -1, pText, CS_WRITE_NOANTIALIAS/*|CS_WRITE_BASELINE*/);

  mG3d->FinishDraw();
  //??mEngine->SetContext (l_oldContext);
  return csPtr<iTextureWrapper> (lTexWrap);
}



void KMode::Initialize ()
{
  csRef<iTextureWrapper> lEditingTex = CreateTextureText ("EDITING",
    csColor (0.f, 0.f, 0.f));
  mEditingMat = mEngine->CreateMaterial ("EDITING", lEditingTex);
  //??mEditingMat->Register (mG3d->GetTextureManager ());

  csRef<iTextureWrapper> lMission1Tex = CreateTextureText ("MISSION STARTING...",
    csColor (0, 0, 0));
  mMission1Mat = mEngine->CreateMaterial ("MISSION1", lMission1Tex);
  //??mMission1Mat->Register (mG3d->GetTextureManager ());

  csRef<iTextureWrapper> lMission2Tex = CreateTextureText ("MISSION IS RUNNING",
    csColor (0, 0, 0));
  mMission2Mat = mEngine->CreateMaterial ("MISSION2", lMission2Tex);
  //??mMission2Mat->Register (mG3d->GetTextureManager ());

  csRef<iTextureWrapper> lMission3Tex = CreateTextureText ("MISSION ENDED...",
    csColor (0, 0, 0));
  mMission3Mat = mEngine->CreateMaterial ("MISSION3", lMission3Tex);
  //??mMission3Mat->Register (mG3d->GetTextureManager ());

  csRef<iTextureWrapper> lReplayTex = CreateTextureText ("REPLAY",
    csColor (0, 0, 0));
  mReplayMat = mEngine->CreateMaterial ("REPLAY", lReplayTex);
  //??mReplayMat->Register (mG3d->GetTextureManager ());

  //
  //Create the sprite2d mesh.
  csRef<iMeshFactoryWrapper> lSpr2dFactW = mEngine->
    CreateMeshFactory ("crystalspace.mesh.object.sprite.2d", "spr2dFact");
  
  csRef<iSprite2DFactoryState > l_spr2dFactState = SCF_QUERY_INTERFACE (
    lSpr2dFactW->GetMeshObjectFactory (), iSprite2DFactoryState);
    
  //
  //Create the mesh object by the factory.
  mSpr2dMW =lSpr2dFactW->CreateMeshWrapper ();
  lSpr2dFactW->QueryObject ()->SetName ("kmode");
  
  mSpr2dMW->SetZBufMode (CS_ZBUF_FILL);//Just fill the zbuffer.
  //Set priority to alpha.
  mSpr2dMW->SetRenderPriority (mEngine->GetRenderPriority ("alpha"));
  
  //
  //Get the state interface from the mesh object created.
  mSpr2dState = SCF_QUERY_INTERFACE (mSpr2dMW->GetMeshObject (),
    iSprite2DState);
  
  mSpr2dState->CreateRegularVertices (4, true);
  csColoredVertices& v = mSpr2dState->GetVertices();
  v[0].pos.x = .0f; v[0].u = 0;
  v[0].pos.y = .1f; v[0].v = 0;
  
  v[1].pos.x = 1.f; v[1].u = 1;
  v[1].pos.y = .1f; v[1].v = 0;
  
  v[2].pos.x = 1.f; v[2].u = 1;
  v[2].pos.y = .0f; v[2].v = 1;
  
  v[3].pos.x = 0.f; v[3].u = 0;
  v[3].pos.y = .0f; v[3].v = 1;

  //mSpr2dState->SetMixMode (CS_FX_COPY);//CS_FX_SETALPHA (0.1f));
  mSpr2dState->SetLighting (false);
  
  //
  //Init to initial state.
  SetDisplay (NONE);
}

void KMode::SetupFrame (KCamera* pKCam, float pMSecs)
{
  if (g_ske->m_appState == KAppState::EDITING)
    SetDisplay (EDITING);
  else if (g_ske->m_appState == KAppState::POINTINGBEFOREWALKING)
    SetDisplay (MISSION1);
  else if (g_ske->m_appState == KAppState::WALKING)
    SetDisplay (MISSION2);
  else if (g_ske->m_appState == KAppState::STANDING)
    SetDisplay (MISSION3);
  else if (g_ske->m_appState == KAppState::REPLAY)
    SetDisplay (REPLAY);

  csOrthoTransform lOT = pKCam->GetCamera ()->GetTransform ();
  csVector2 lScreenSpace (0.1f, 4.1f);
  csVector3 lCameraSpace;
  //z = 1.0f is correct.
  pKCam->GetCamera ()->InvPerspective (lScreenSpace, 1.0f, lCameraSpace);
  csVector3 lWorldPos = g_ske->m_kcam->GetCamera ()->
      GetTransform ().This2Other (lCameraSpace);
  mSpr2dMW->GetMovable ()->SetPosition (lWorldPos);
  mSpr2dMW->GetMovable ()->UpdateMove ();

  static float sMilliSecsAccumulated = 0;
  //?? BUG! the alpha value doe snot matter at all, so actually i put it invisible
  //after 5 seconds.
  float lAlpha = 1.f;
  if (g_ske->m_appState == KAppState::WALKING)
  {
    sMilliSecsAccumulated += pMSecs;
    
    //Display the sprite2d for 5 seconds, fading out smootly.
    if (sMilliSecsAccumulated > 5000.f)
    {
      SetDisplay (NONE);
      lAlpha = 0.0f;  
    }//if
    else
      lAlpha = 1.f/5.f * (sMilliSecsAccumulated / 1000.f);
  }
  else
    sMilliSecsAccumulated = 0;
  //###mSpr2dState->SetMixMode (CS_FX_SETALPHA (lAlpha));
}

void KMode::SetDisplay (KMODE_STATE p_mode)
{
  switch (p_mode)
  {
  case NONE:
    {
      mSpr2dMW->GetMovable ()->SetSector (g_ske->m_hiddenWorld);
      mSpr2dMW->GetMovable ()->UpdateMove ();
      return;//Notice the return please.
    }//
  case EDITING:
    {
      mSpr2dState->SetMaterialWrapper (mEditingMat);
      break;
    }
  case MISSION1:
    {
      mSpr2dState->SetMaterialWrapper (mMission1Mat);
      break;
    }
  case MISSION2:
    {
      mSpr2dState->SetMaterialWrapper (mMission2Mat);
      break;
    }
  case MISSION3:
    {
      mSpr2dState->SetMaterialWrapper (mMission3Mat);
      break;
    }
  case REPLAY:
    {
      mSpr2dState->SetMaterialWrapper (mReplayMat);    
      break;
    }
  default:
    {
      SKE::Report (CS_REPORTER_SEVERITY_ERROR,
        "Error: KMode state not consistent!");
      break;
    }//default
  }//switch

  mSpr2dMW->GetMovable ()->SetSector (g_ske->m_world);
  mSpr2dMW->GetMovable ()->UpdateMove ();
}




See more files for this project here

Spatial Knowledge Experiments

A simulation of 3D virtual worlds for psychological experiments

Project homepage: http://sourceforge.net/projects/ske
Programming language(s): C,C++,Perl
License: other

  isense/
  joystick/
  Jamfile
  KImageCardinalDirection.h
  SKE.cpp
  SKE.h
  eulerangles.c
  eulerangles.h
  ikdraggable.cpp
  ikdraggable.h
  ikdraghandler.h
  ikmission.h
  ikxmlreader.h
  ikxmlwriter.h
  kappstate.cpp
  kappstate.h
  kbasedraggable.cpp
  kbasedraggable.h
  kbasedraghandler.cpp
  kbasedraghandler.h
  kblock.cpp
  kblock.h
  kblockId.h
  kblockid.cpp
  kcamera.cpp
  kcamera.h
  kcollision.cpp
  kcommandprocessor.cpp
  kcommandprocessor.h
  kconstant.cpp
  kconstant.h
  kcursor3d.cpp
  kcursor3d.h
  kdata.cpp
  kdata.h
  kdatalist.cpp
  kdatalist.h
  kdragmanager.cpp
  kdragmanager.h
  keventhandlermanager.cpp
  keventhandlermanager.h
  kflags.cpp
  kflags.h
  kimagecardinaldirection.cpp
  kkeys.cpp
  kkeys.h
  kkeystate.cpp
  kkeystate.h
  klandmark.cpp
  klandmark.h
  klandmarklist.cpp
  klandmarklist.h
  klight.cpp
  klight.h
  klightlist.cpp
  klightlist.h
  kmap.cpp
  kmap.h
  kmission.cpp
  kmission.h
  kmissiontype.cpp
  kmissiontype.h
  kmode.cpp
  kmode.h
  kmousemanager.cpp
  kmousemanager.h
  kobject3d.cpp
  kobject3d.h
  korientation.cpp
  korientation.h
  kpath.cpp
  kpath.h
  kpathfollower.cpp
  kpathfollower.h
  kplayer.cpp
  kplayer.h
  kposition.cpp
  kposition.h
  kquaternion.cpp
  kquaternion.h
  ksaveddatamanager.cpp
  ksaveddatamanager.h
  ksegment3.h
  ksign.cpp
  ksign.h
  ksprite3d.cpp
  ksprite3d.h
  ksprite3dlist.cpp
  ksprite3dlist.h
  kterrain.cpp
  kterrain.h
  kthing.cpp
  kthing.h
  kutil.cpp
  kutil.h
  kwireframe.cpp
  kwireframe.h
  kxmlhelper.h
  movement1d.h
  movement3d.h
  quattypes.h