2.5 Using keyboard input to move a sprite

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.
*/

// Modified June 10th, 2002 by James Sager // Added tutorial examples of how to: // Grab keyboard input for every possible key on your keyboard. // Move sprites around :) // To use this example, copy it over simple2.cpp #include "cssysdef.h" #include "cssys/sysfunc.h" #include "iutil/vfs.h" #include "csutil/cscolor.h" #include "cstool/csview.h" #include "cstool/initapp.h" #include "simple2.h" #include "iutil/eventq.h" #include "iutil/event.h" #include "iutil/objreg.h" #include "iutil/csinput.h" #include "iutil/virtclk.h" #include "iengine/sector.h" #include "iengine/engine.h" #include "iengine/camera.h" #include "iengine/light.h" #include "iengine/statlght.h" #include "iengine/texture.h" #include "iengine/mesh.h" #include "iengine/movable.h" #include "iengine/material.h" #include "imesh/thing/polygon.h" #include "imesh/thing/thing.h" #include "imesh/sprite3d.h" #include "imesh/object.h" #include "ivideo/graph3d.h" #include "ivideo/graph2d.h" #include "ivideo/txtmgr.h" #include "ivideo/texture.h" #include "ivideo/material.h" #include "ivideo/fontserv.h" #include "igraphic/imageio.h" #include "imap/parser.h" #include "ivaria/reporter.h" #include "ivaria/stdrep.h" #include "csutil/cmdhelp.h" CS_IMPLEMENT_APPLICATION //----------------------------------------------------------------------------- //These are some global variables I made so I can keep track of the three sprites I allocated. //Later you can see these being created as sprite, sprite2, sprite3. //I use sg1 to denote Sprite Global 1. Then I can modify their attributes later like position. //A lot of people will say globals aren't good style. This program is for newbies //though, written by a newbie, so who cares. iMeshWrapper *sg1; iMeshWrapper *sg2; iMeshWrapper *sg3; // The global pointer to simple Simple *simple; Simple::Simple () { engine = NULL; loader = NULL; g3d = NULL; kbd = NULL; vc = NULL; view = NULL; } Simple::~Simple () { if (vc) vc->DecRef (); if (engine) engine->DecRef (); if (loader) loader->DecRef(); if (g3d) g3d->DecRef (); if (kbd) kbd->DecRef (); if (view) view->DecRef (); csInitializer::DestroyApplication (object_reg); } //Ok; this SetupFrame function is super important. //This is where user control can influence the 3d world. //Controls: //As was provided with simple2: //left arrow, turns left //right arrow, turns right //forward arrow, moves forward //back arrow, moves backwards //page up, looks up //page down, looks down //movement of sprite1: //a-right A-left //q-down Q-Up //w-into page W-out of page // // Not user friendly, but demonstrates the use of multiple keyboard presses // You can't just do: // //if (kbd->GetKeyState ('A')) // // You need to do: // //if (kbd->GetKeyState (CSKEY_SHIFT) && kbd->GetKeyState ('a')) //Check that both shift and a are pressed //Note that this example doesn't include use of the "Caps Lock" key //Some complex input routines can be found in: CS/include/iutil/evdefs.h //but don't look there unless you know how to read it... I'll try to provide every way to read the //keyboard immediately at the end this this setupframe function. void Simple::SetupFrame () { // First get elapsed time from the virtual clock. csTicks elapsed_time = vc->GetElapsedTicks (); // Now rotate the camera according to keyboard state float speed = (elapsed_time / 1000.0) * (0.03 * 20); iCamera* c = view->GetCamera(); //Move sprite1 right if shift is not held and a is pressed if (!kbd->GetKeyState (CSKEY_SHIFT) && kbd->GetKeyState ('a')) { sg1->GetMovable()->MovePosition(csVector3(.1,0,0)); sg1->GetMovable()->UpdateMove(); } //Move sg1 left if shift is held and a is pressed if (kbd->GetKeyState (CSKEY_SHIFT) && kbd->GetKeyState ('a')) { sg1->GetMovable()->MovePosition(csVector3(-.1,0,0)); sg1->GetMovable()->UpdateMove(); } //Move sprite1 down if shift is not held and q is pressed if (!kbd->GetKeyState (CSKEY_SHIFT) && kbd->GetKeyState ('q')) { sg1->GetMovable()->MovePosition(csVector3(0,-.1,0)); sg1->GetMovable()->UpdateMove(); } //Move sg1 up if shift is held and q is pressed if (kbd->GetKeyState (CSKEY_SHIFT) && kbd->GetKeyState ('q')) { sg1->GetMovable()->MovePosition(csVector3(0,.1,0)); sg1->GetMovable()->UpdateMove(); } //Move sprite1 into the page if shift is not held and q is pressed if (!kbd->GetKeyState (CSKEY_SHIFT) && kbd->GetKeyState ('w')) { sg1->GetMovable()->MovePosition(csVector3(0,0,.1)); sg1->GetMovable()->UpdateMove(); } //Move sg1 out of the page if shift is held and q is pressed if (kbd->GetKeyState (CSKEY_SHIFT) && kbd->GetKeyState ('w')) { sg1->GetMovable()->MovePosition(csVector3(0,0,-.1)); sg1->GetMovable()->UpdateMove(); } //Start of every keypress //WHOLE LOTTA KEYPRESSES! all make sprite 2 go right /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// if (kbd->GetKeyState ('`')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('1')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('2')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('3')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('4')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('5')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('6')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('7')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('8')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('9')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('0')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('-')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('=')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('\')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('e')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('r')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('t')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('y')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('u')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('i')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('o')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('p')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('[')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState (']')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('s')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('d')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('f')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('g')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('h')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('i')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('j')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('k')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('l')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState (';')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('z')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('x')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('c')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('v')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('b')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('n')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('m')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState (',')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('.')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('/')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } if (kbd->GetKeyState ('\\')) { sg2->GetMovable()->MovePosition(csVector3(.1,0,0)); sg2->GetMovable()->UpdateMove(); } /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// if (kbd->GetKeyState (CSKEY_RIGHT)) c->GetTransform ().RotateThis (CS_VEC_ROT_RIGHT, speed); if (kbd->GetKeyState (CSKEY_LEFT)) c->GetTransform ().RotateThis (CS_VEC_ROT_LEFT, speed); if (kbd->GetKeyState (CSKEY_PGUP)) c->GetTransform ().RotateThis (CS_VEC_TILT_UP, speed); if (kbd->GetKeyState (CSKEY_PGDN)) c->GetTransform ().RotateThis (CS_VEC_TILT_DOWN, speed); if (kbd->GetKeyState (CSKEY_UP)) c->Move (CS_VEC_FORWARD * 4 * speed); if (kbd->GetKeyState (CSKEY_DOWN)) c->Move (CS_VEC_BACKWARD * 4 * speed); // Tell 3D driver we're going to display 3D things. if (!g3d->BeginDraw (engine->GetBeginDrawFlags () | CSDRAW_3DGRAPHICS)) return; // Tell the camera to render into the frame buffer. view->Draw (); } //Every keyboard read I can possibly think of: //Normal: // `,1,2,3,4,5,6,7,8,9,0,-,=,q,w,e,r,t,y,u,i,o,p,[,],a,s,d,f,g,h,i // j,k,l,;,',z,x,c,v,b,n,m,',',.,/ //Exceptions: // #1: The key \ should be typed '\\' not '\' IE: if (kbd->GetKeyState ('\\')) // #2: Also the key ' should be typed '\'' not ''' IE: if (kbd->GetKeyState ('\'')) // The key \ is some sort of absolute key; most coders already know about that. // Further exceptions as extracted from the CS/include/iutil/evdefs.h file: // //#define CSMASK_SHIFT 0x00000001<br> // 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; }