00001 #include "IrrConsole/testApp.h"
00002
00003 #ifdef WIN32
00004 #ifdef _DEBUG
00005 #pragma comment(lib,"IrrConsole-DEBUG")
00006 #else
00007 #pragma comment(lib,"IrrConsole")
00008 #endif
00009 #endif
00010
00011 void IC_RunTestApp(int argc,char **argv);
00012 void IC_TestParser();
00013
00014 int main(int argc,char **argv)
00015 {
00016 IC_RunTestApp(argc,argv);
00017 return 0;
00018 }
00019
00020 void IC_RunTestApp(int argc,char **argv)
00021 {
00022 IC_TestApp app (argv[0]);
00023 array<String> args;
00024 for(s32 i = 1; i < argc; i++)
00025 {
00026 args.push_back(argv[i]);
00027 }
00028 try
00029 {
00030 app.main(args);
00031 }
00032 catch(IC_Error& err)
00033 {
00034 cerr<<endl;
00035 cerr<<"Error Caught ::"<<endl;
00036 cerr<<" (Type = "<<err.getType()<<")"<<endl;
00037 cerr<<" (Message = "<<err.getMessage()<<")"<<endl;
00038 cerr<<" (Code = "<<err.getCode()<<")"<<endl;
00039 cerr<<endl;
00040 }
00041 }
00042
00043 void IC_TestParser()
00044 {
00045
00046 WideString cmdLine = L"echo \"Hello World\" du\\de h\\\"o\\\"w";
00047 array<WideString> args;
00048 WideString cmdName = L"";
00049 IC_CmdLineParser parser(cmdLine);
00050 parser.parse(cmdName,args);
00051 cout<<"Command Line : ["<<cmdLine<<"]"<<endl;
00052 cout<<"Command Name : ["<<cmdName<<"]"<<endl;
00053 cout<<"Arguments : ["<<args.size()<<"]"<<endl;
00054 for(u32 x = 0; x < args.size(); x++)
00055 {
00056 cout<<" arg["<<x<<"]==>{"<<args[x]<<"}"<<endl;
00057 }
00058 }
00059
00060 IC_TestApp::IC_TestApp(const String str) : device(0),
00061 name(str),
00062 sceneNode(0),
00063 levelMesh(0),
00064 modelMesh(0),
00065 modelTexture(0),
00066 testTexture(0),
00067 captionFont(0)
00068 {
00069 }
00070
00071 IC_TestApp::~IC_TestApp()
00072 {
00073 if(device)
00074 {
00075 device->drop();
00076 device = 0;
00077 }
00078 }
00079
00080 bool IC_TestApp::OnEvent(irr::SEvent event)
00081 {
00082 if(event.EventType == irr::EET_KEY_INPUT_EVENT )
00083 {
00084 if(event.KeyInput.PressedDown)
00085 {
00086 if(event.KeyInput.Key == irr::KEY_ESCAPE)
00087 {
00088 if(console.isVisible())
00089 {
00090 console.setVisible(false);
00091 return true;
00092 }
00093 else
00094 {
00095 setRunning(false);
00096 return true;
00097 }
00098 }
00099 else if(event.KeyInput.Key == IC_Console::IC_KEY_TILDE)
00100 {
00101 if(!console.isVisible())
00102 {
00103 console.setVisible(true);
00104 return true;
00105 }
00106 else if(!event.KeyInput.Control)
00107 {
00108 console.setVisible(false);
00109 return true;
00110 }
00111
00112 }
00113
00114 if(console.isVisible())
00115 {
00116 console.handleKeyPress(event.KeyInput.Char, event.KeyInput.Key,event.KeyInput.Shift, event.KeyInput.Control);
00117 return true;
00118 }
00119
00120 }
00121
00122
00123 }
00124 else if(event.EventType == irr::EET_LOG_TEXT_EVENT)
00125 {
00126 console.logMessage_ANSI(event.LogEvent.Level,event.LogEvent.Text);
00127 return true;
00128 }
00129 else if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
00130 {
00131 return console.isVisible();
00132 }
00133 return false;
00134 }
00135
00136 void IC_TestApp::main(array<String>& args)
00137 {
00138
00139 init(args);
00140
00141
00142 setRunning(true);
00143 while(device && device->run() && isRunning())
00144 {
00145 if(!run())
00146 {
00147 setRunning(false);
00148 }
00149 }
00150
00151 }
00152
00153
00154 void IC_TestApp::init(array<String>& args)
00155 {
00156 irr::video::E_DRIVER_TYPE driverType = irr::video::EDT_DIRECTX9;
00157 s32 bitsPerPixel = 32;
00158 dimension2d<s32> screenDim(800,600);
00159 bool fullscreen = false;
00160 bool stencil = true;
00161 bool vsync = true;
00162 device = irr::createDevice(driverType, screenDim, bitsPerPixel, fullscreen,stencil,vsync, this);
00163 if(!device)
00164 {
00165 throw IC_Error(L"could not create irrlicht device");
00166 }
00167
00168
00169 captionFont = device->getGUIEnvironment()->getFont("data/font/caption.bmp");
00170 if(!captionFont)
00171 {
00172 captionFont = device->getGUIEnvironment()->getBuiltInFont();
00173 }
00174
00175 captionText.push_back(L"IrrConsole : Quake Style Drop Down Console Demo For Irrlicht");
00176 captionText.push_back(L"Author : Saurav Mohapatra (mohaps@gmail.com)");
00177 captionText.push_back(L"");
00178 captionText.push_back(L"HELP TEXT:");
00179 captionText.push_back(L"============================================================");
00180 captionText.push_back(L" Press the tilde (~) key to toggle console");
00181 captionText.push_back(L" Use the UP/DN arrow keys to access command history");
00182 captionText.push_back(L" Type commands at the prompt");
00183 captionText.push_back(L" To execute a command put \\ in front");
00184 captionText.push_back(L" e.g. \\list");
00185 captionText.push_back(L" use the \"list\" or \"help\" commands to find out more");
00186 captionText.push_back(L" to start try the command \"\\help show_node\");");
00187 captionText.push_back(L"============================================================");
00188 captionText.push_back(L" ");
00189 captionText.push_back(L"TO QUIT THIS DEMO PRESS ESCAPE KEY");
00190
00191
00192
00193 captionHeight = captionFont->getDimension(L"X").Height + 2;
00194
00195
00196 console.getConfig().dimensionRatios.Y = 0.8f;
00197
00198 console.initializeConsole(device->getGUIEnvironment(),screenDim);
00199
00200
00201 console.loadDefaultCommands(device);
00202
00203
00204
00205 console.registerCommand(new TestCommand_SHOWNODE(this));
00206
00207 console.registerCommand(new TestCommand_HIDENODE(this));
00208
00209
00210
00211
00212
00213 testTexture = device->getVideoDriver()->getTexture("data/texture/test.jpg");
00214
00215
00216 modelMesh = device->getSceneManager()->getMesh("data/model/faerie.md2");
00217 modelTexture = device->getVideoDriver()->getTexture("data/model/faerie.bmp");
00218
00219
00220 device->getFileSystem()->addZipFileArchive("data/pak/map-20kdm2.pk3");
00221 levelMesh = device->getSceneManager()->getMesh("20kdm2.bsp");
00222
00223
00224 sceneNode = 0;
00225
00226 device->getSceneManager()->addCameraSceneNodeFPS();
00227
00228
00229 showModel();
00230
00231
00232
00233
00234
00235 }
00236 bool IC_TestApp::isRunning()
00237 {
00238 return bRunning;
00239 }
00240 void IC_TestApp::setRunning(bool bVal)
00241 {
00242 bRunning = bVal;
00243 }
00244 bool IC_TestApp::run()
00245 {
00246
00247 irr::video::IVideoDriver* videoDriver = device->getVideoDriver();
00248 irr::scene::ISceneManager* sceneManager = device->getSceneManager();
00249 irr::gui::IGUIEnvironment* guienv = device->getGUIEnvironment();
00250 s32 fps = videoDriver->getFPS();
00251 u32 deltaMillis = 0;
00252 if(fps > 0)
00253 {
00254 deltaMillis = (u32) (1000.0f / (f32)fps);
00255 }
00256
00257 videoDriver->beginScene(true,true,irr::video::SColor(200,10,10,100));
00258 sceneManager->drawAll();
00259 guienv->drawAll();
00260 console.renderConsole(guienv,videoDriver,deltaMillis);
00261
00262
00263 if(!console.isVisible())
00264 {
00265 drawCaptions();
00266 }
00267 videoDriver->endScene();
00268
00269 return true;
00270 }
00271 void IC_TestApp::showModel()
00272 {
00273 hideNode();
00274 if(modelMesh)
00275 {
00276
00277 sceneNode = device->getSceneManager()->addAnimatedMeshSceneNode(modelMesh);
00278
00279 sceneNode->setMaterialTexture(0, modelTexture);
00280
00281 sceneNode->setMaterialFlag(irr::video::EMF_LIGHTING,false);
00282
00283 sceneNode->setPosition(vector3df(0,10,80));
00284
00285 ((irr::scene::IAnimatedMeshSceneNode *)sceneNode)->setFrameLoop(0, modelMesh->getFrameCount());
00286 }
00287 else
00288 {
00289 throw IC_Error(L"Model could not be loaded");
00290 }
00291 device->setWindowCaption(L"IrrConsole Demo App [ Showing MD2 Model]");
00292 }
00293 void IC_TestApp::showMap()
00294 {
00295 hideNode();
00296 if(levelMesh)
00297 {
00298
00299 sceneNode = device->getSceneManager()->addOctTreeSceneNode(levelMesh->getMesh(0));
00300
00301 sceneNode->setMaterialFlag(irr::video::EMF_LIGHTING,false);
00302
00303 sceneNode->setPosition(vector3df(-1300,-144,-1249));
00304 }
00305 else
00306 {
00307 throw IC_Error(L"Level/Map could not be loaded");
00308 }
00309 device->setWindowCaption(L"IrrConsole Demo App [ Showing Quake III Map]");
00310 }
00311 void IC_TestApp::showTestNode()
00312 {
00313 hideNode();
00314 if(testTexture)
00315 {
00316
00317 sceneNode = device->getSceneManager()->addTestSceneNode(20);
00318
00319 sceneNode->setMaterialTexture(0, testTexture);
00320
00321 sceneNode->setMaterialFlag(irr::video::EMF_LIGHTING,false);
00322
00323
00324 sceneNode->setPosition(vector3df(0,0,30));
00325
00326
00327 irr::scene::ISceneNodeAnimator* anim = device->getSceneManager()->createRotationAnimator(vector3df(1,2,1));
00328 sceneNode->addAnimator(anim);
00329 anim->drop();
00330
00331 }
00332 else
00333 {
00334 throw IC_Error(L"Test Texture Could not be loaded");
00335 }
00336 device->setWindowCaption(L"IrrConsole Demo App [ Showing Test Node]");
00337 }
00338 void IC_TestApp::hideNode()
00339 {
00340 if(sceneNode)
00341 {
00342 sceneNode->remove();
00343 sceneNode = 0;
00344 }
00345 device->setWindowCaption(L"IrrConsole Demo App [ Node Hidden ]");
00346 }
00347 void IC_TestApp::drawCaptions()
00348 {
00349 static const irr::video::SColor fontColor(200,200,200,200);
00350 dimension2d<s32> screenDim = device->getVideoDriver()->getScreenSize();
00351 rect<s32> lineRect(10,10,screenDim.Width, screenDim.Height);
00352 for(u32 i = 0; i < captionText.size(); i++)
00353 {
00354 captionFont->draw(captionText[i].c_str(),lineRect,fontColor);
00355 lineRect.UpperLeftCorner.Y += captionHeight;
00356 lineRect.LowerRightCorner.Y += captionHeight;
00357 }
00358 }