By Jim Sager, james_sager3@yahoo.com.
This code documents how to take input from the keyboard and move sprites around. Its compilable if you save this code as simple2.cpp and make tutsimp2
/*
Copyright (C) 2001 by Jorrit Tyberghein
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.
*/
// How you'd use these:
//if (kbd->GetKeyState (CSKEY_SHIFT))
//
/// "Ctrl" key mask
//#define CSMASK_CTRL 0x00000002
/// "Alt" key mask
//#define CSMASK_ALT 0x00000004
/// All shift keys
//#define CSMASK_ALLSHIFTS (CSMASK_SHIFT | CSMASK_CTRL | CSMASK_ALT)
/// Key is pressed for first time or it is an autorepeat?
//#define CSMASK_FIRST 0x80000000
/*
* Control key codes.
* Not every existing key on any existing platform is supported by
* Crystal Space. Instead, we tried to list here all the keys that
* are common among all platforms on which Crystal Space runs.
*/
/// ESCape key
//#define CSKEY_ESC 27
/// Enter key
//#define CSKEY_ENTER '\n'
/// Tab key
//#define CSKEY_TAB '\t'
/// Back-space key
//#define CSKEY_BACKSPACE '\b'
/// Space key
//#define CSKEY_SPACE ' '
/// Up arrow key
//#define CSKEY_UP 1000
/// Down arrow key
//#define CSKEY_DOWN 1001
/// Left arrow key
//#define CSKEY_LEFT 1002
/// Right arrow key
//#define CSKEY_RIGHT 1003
/// PageUp key
//#define CSKEY_PGUP 1004
/// PageDown key
//#define CSKEY_PGDN 1005
/// Home key
//#define CSKEY_HOME 1006
/// End key
//#define CSKEY_END 1007
/// Insert key
//#define CSKEY_INS 1008
/// Delete key
//#define CSKEY_DEL 1009
/// Control key
//#define CSKEY_CTRL 1010
/// Alternative shift key
//#define CSKEY_ALT 1011
/// Shift key
//#define CSKEY_SHIFT 1012
/// Function key F1
//#define CSKEY_F1 1013
/// Function key F2
//#define CSKEY_F2 1014
/// Function key F3
//#define CSKEY_F3 1015
/// Function key F4
//#define CSKEY_F4 1016
/// Function key F5
//#define CSKEY_F5 1017
/// Function key F6
//#define CSKEY_F6 1018
/// Function key F7
//#define CSKEY_F7 1019
/// Function key F8
//#define CSKEY_F8 1020
/// Function key F9
//#define CSKEY_F9 1021
/// Function key F10
//#define CSKEY_F10 1022
/// Function key F11
//#define CSKEY_F11 1023
/// Function key F12
//#define CSKEY_F12 1024
//// The "center" key ("5" on numeric keypad)
//#define CSKEY_CENTER 1025
/// Numeric keypad '+'
//#define CSKEY_PADPLUS 1026
/// Numeric keypad '-'
//#define CSKEY_PADMINUS 1027
/// Numeric keypad '*'
//#define CSKEY_PADMULT 1028
/// Numeric keypad '/'
//#define CSKEY_PADDIV 1029
// First and last control key code
//#define CSKEY_FIRST 1000
//#define CSKEY_LAST 1029
void Simple::FinishFrame ()
{
g3d->FinishDraw ();
g3d->Print (NULL);
}
bool Simple::HandleEvent (iEvent& ev)
{
if (ev.Type == csevBroadcast && ev.Command.Code == cscmdProcess)
{
simple->SetupFrame ();
return true;
}
else if (ev.Type == csevBroadcast && ev.Command.Code == cscmdFinalProcess)
{
simple->FinishFrame ();
return true;
}
else if (ev.Type == csevKeyDown && ev.Key.Code == CSKEY_ESC)
{
iEventQueue* q = CS_QUERY_REGISTRY (object_reg, iEventQueue);
if (q)
{
q->GetEventOutlet()->Broadcast (cscmdQuit);
q->DecRef ();
}
return true;
}
return false;
}
bool Simple::SimpleEventHandler (iEvent& ev)
{
return simple->HandleEvent (ev);
}
bool Simple::Initialize (int argc, const char* const argv[])
{
object_reg = csInitializer::CreateEnvironment (argc, argv);
if (!object_reg) return false;
if (!csInitializer::RequestPlugins (object_reg,
CS_REQUEST_VFS,
CS_REQUEST_SOFTWARE3D,
CS_REQUEST_ENGINE,
CS_REQUEST_FONTSERVER,
CS_REQUEST_IMAGELOADER,
CS_REQUEST_LEVELLOADER,
CS_REQUEST_REPORTER,
CS_REQUEST_REPORTERLISTENER,
CS_REQUEST_END))
{
csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
"crystalspace.application.simple2",
"Can't initialize plugins!");
return false;
}
if (!csInitializer::SetupEventHandler (object_reg, SimpleEventHandler))
{
csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
"crystalspace.application.simple2",
"Can't initialize event handler!");
return false;
}
// Check for commandline help.
if (csCommandLineHelper::CheckHelp (object_reg))
{
csCommandLineHelper::Help (object_reg);
return false;
}
// The virtual clock.
vc = CS_QUERY_REGISTRY (object_reg, iVirtualClock);
if (!vc)
{
csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
"crystalspace.application.simple2",
"Can't find the virtual clock!");
return false;
}
// Find the pointer to engine plugin
engine = CS_QUERY_REGISTRY (object_reg, iEngine);
if (!engine)
{
csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
"crystalspace.application.simple2",
"No iEngine plugin!");
return false;
}
loader = CS_QUERY_REGISTRY (object_reg, iLoader);
if (!loader)
{
csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
"crystalspace.application.simple2",
"No iLoader plugin!");
return false;
}
g3d = CS_QUERY_REGISTRY (object_reg, iGraphics3D);
if (!g3d)
{
csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
"crystalspace.application.simple2",
"No iGraphics3D plugin!");
return false;
}
kbd = CS_QUERY_REGISTRY (object_reg, iKeyboardDriver);
if (!kbd)
{
csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
"crystalspace.application.simple2",
"No iKeyboardDriver plugin!");
return false;
}
// 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.simple2",
"Error opening system!");
return false;
}
// First disable the lighting cache. Our app is simple enough
// not to need this.
engine->SetLightingCacheMode (0);
if (!loader->LoadTexture ("stone", "/lib/std/stone4.gif"))
{
csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
"crystalspace.application.simple2",
"Error loading 'stone4' texture!");
return false;
}
iMaterialWrapper* tm = engine->GetMaterialList ()->FindByName ("stone");
room = engine->CreateSector ("room");
iMeshWrapper* walls = engine->CreateSectorWallsMesh (room, "walls");
iThingState* walls_state = SCF_QUERY_INTERFACE (walls->GetMeshObject (), iThingState);
iPolygon3D* p;
p = walls_state->CreatePolygon ();
p->SetMaterial (tm);
p->CreateVertex (csVector3 (-5, 0, 5));
p->CreateVertex (csVector3 (5, 0, 5));
p->CreateVertex (csVector3 (5, 0, -5));
p->CreateVertex (csVector3 (-5, 0, -5));
p->SetTextureSpace (p->GetVertex (0), p->GetVertex (1), 3);
p = walls_state->CreatePolygon ();
p->SetMaterial (tm);
p->CreateVertex (csVector3 (-5, 20, -5));
p->CreateVertex (csVector3 (5, 20, -5));
p->CreateVertex (csVector3 (5, 20, 5));
p->CreateVertex (csVector3 (-5, 20, 5));
p->SetTextureSpace (p->GetVertex (0), p->GetVertex (1), 3);
p = walls_state->CreatePolygon ();
p->SetMaterial (tm);
p->CreateVertex (csVector3 (-5, 20, 5));
p->CreateVertex (csVector3 (5, 20, 5));
p->CreateVertex (csVector3 (5, 0, 5));
p->CreateVertex (csVector3 (-5, 0, 5));
p->SetTextureSpace (p->GetVertex (0), p->GetVertex (1), 3);
p = walls_state->CreatePolygon ();
p->SetMaterial (tm);
p->CreateVertex (csVector3 (5, 20, 5));
p->CreateVertex (csVector3 (5, 20, -5));
p->CreateVertex (csVector3 (5, 0, -5));
p->CreateVertex (csVector3 (5, 0, 5));
p->SetTextureSpace (p->GetVertex (0), p->GetVertex (1), 3);
p = walls_state->CreatePolygon ();
p->SetMaterial (tm);
p->CreateVertex (csVector3 (-5, 20, -5));
p->CreateVertex (csVector3 (-5, 20, 5));
p->CreateVertex (csVector3 (-5, 0, 5));
p->CreateVertex (csVector3 (-5, 0, -5));
p->SetTextureSpace (p->GetVertex (0), p->GetVertex (1), 3);
p = walls_state->CreatePolygon ();
p->SetMaterial (tm);
p->CreateVertex (csVector3 (5, 20, -5));
p->CreateVertex (csVector3 (-5, 20, -5));
p->CreateVertex (csVector3 (-5, 0, -5));
p->CreateVertex (csVector3 (5, 0, -5));
p->SetTextureSpace (p->GetVertex (0), p->GetVertex (1), 3);
walls_state->DecRef ();
walls->DecRef ();
iStatLight* light;
iLightList* ll = room->GetLights ();
light = engine->CreateLight (NULL, csVector3 (-3, 5, 0), 10,
csColor (1, 0, 0), false);
ll->Add (light->QueryLight ());
light->DecRef ();
light = engine->CreateLight (NULL, csVector3 (3, 5, 0), 10,
csColor (0, 0, 1), false);
ll->Add (light->QueryLight ());
light->DecRef ();
light = engine->CreateLight (NULL, csVector3 (0, 5, -3), 10,
csColor (0, 1, 0), false);
ll->Add (light->QueryLight ());
light->DecRef ();
engine->Prepare ();
//define a new camera
view = new csView (engine, g3d);
view->GetCamera ()->SetSector (room);
view->GetCamera ()->GetTransform ().SetOrigin (csVector3 (0, 5, -3));
iGraphics2D* g2d = g3d->GetDriver2D ();
view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ());
iTextureManager* txtmgr = g3d->GetTextureManager ();
txtmgr->SetPalette ();
//end of camera define
// Load a texture for our sprite.
// iTextureWrapper* txt = loader->LoadTexture ("spark",
// "/lib/std/spark.png", CS_TEXTURE_3D, txtmgr, true);
iTextureWrapper* txt = loader->LoadTexture ("spark",
"/lib/stdtex/bricks.jpg", CS_TEXTURE_3D, txtmgr, true);
//std
//stdtex
if (txt == NULL)
{
csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
"crystalspace.application.simple2",
"Error loading texture!");
return false;
}
// Load a sprite template from disk.
iMeshFactoryWrapper* imeshfact = loader->LoadMeshObjectFactory (
"/lib/std/sprite1");
if (imeshfact == NULL)
{
csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
"crystalspace.application.simple2",
"Error loading mesh object factory!");
return false;
}
// Create the sprite and add it to the engine.
iMeshWrapper* sprite = engine->CreateMeshWrapper (
imeshfact, "MySprite", room,
csVector3 (-3, 5, 3)); //-3 5 3
csMatrix3 m; m.Identity (); m *= 5.; //5,
sprite->GetMovable ()->SetTransform (m);
sprite->GetMovable ()->UpdateMove ();
iSprite3DState* spstate = SCF_QUERY_INTERFACE (sprite->GetMeshObject (),
iSprite3DState);
spstate->SetAction ("default");
imeshfact->DecRef ();
spstate->DecRef ();
// The following two calls are not needed since CS_ZBUF_USE and
// Object render priority are the default but they show how you
// can do this.
sprite->SetZBufMode (CS_ZBUF_USE);
sprite->SetRenderPriority (engine->GetObjectRenderPriority ());
sprite->DeferUpdateLighting (CS_NLIGHT_STATIC|CS_NLIGHT_DYNAMIC, 10);
sprite->DecRef ();
/*
iMeshWrapper* sprite2 = engine->CreateMeshWrapper (
imeshfact, "MySprite2", room,
csVector3 (-3, 4, 3)); //-3 5 3
csMatrix3 e; e.Identity ();
e *= 2.; //5,
sprite2->GetMovable ()->SetTransform (e);
sprite2->GetMovable ()->UpdateMove ();
iSprite3DState* spstate2 = SCF_QUERY_INTERFACE (sprite2->GetMeshObject (),
iSprite3DState);
*/
iMeshWrapper* sprite3 = engine->CreateMeshWrapper (
imeshfact, "MySprite3", room,
csVector3 (-3, 6, 3)); //-3 5 3
iMeshWrapper* sprite2 = engine->CreateMeshWrapper (
imeshfact, "MySprite2", room,
csVector3 (-3, 4, 3)); //-3 5 3
/*
csMatrix3 e; e.Identity ();
e *= 2.; //5,
sprite2->GetMovable ()->SetTransform (m);
sprite2->GetMovable ()->UpdateMove ();
iSprite3DState* spstate2 = SCF_QUERY_INTERFACE (sprite2->GetMeshObject (),
iSprite3DState);
spstate2->SetAction ("default");
imeshfact->DecRef ();
spstate2->DecRef ();
// The following two calls are not needed since CS_ZBUF_USE and
// Object render priority are the default but they show how you
// can do this.
sprite2->SetZBufMode (CS_ZBUF_USE);
sprite2->SetRenderPriority (engine->GetObjectRenderPriority ());
sprite2->DeferUpdateLighting (CS_NLIGHT_STATIC|CS_NLIGHT_DYNAMIC, 10);
sprite2->DecRef ();
*/
sg1=sprite;
sg2=sprite2;
sg3=sprite3;
return true;
}
void Simple::Start ()
{
csDefaultRunLoop (object_reg);
}
/*---------------------------------------------------------------------*
* Main function
*---------------------------------------------------------------------*/
int main (int argc, char* argv[])
{
simple = new Simple ();
if (simple->Initialize (argc, argv))
simple->Start ();
delete simple;
return 0;
}