]> git.lizzy.rs Git - irrlicht.git/blob - examples/AutomatedTest/main.cpp
0141b2bb4c67b6aeaa7dd23d17b4d233df4cc5aa
[irrlicht.git] / examples / AutomatedTest / main.cpp
1 #include <iostream>\r
2 #include <irrlicht.h>\r
3 #include "exampleHelper.h"\r
4 \r
5 using namespace irr;\r
6 \r
7 static IrrlichtDevice *device = nullptr;\r
8 static int test_fail = 0;\r
9 \r
10 void test_irr_array();\r
11 void test_irr_string();\r
12 \r
13 static video::E_DRIVER_TYPE chooseDriver(core::stringc arg_)\r
14 {\r
15         if (arg_ == "null")\r
16                 return video::EDT_NULL;\r
17         if (arg_ == "ogles1")\r
18                 return video::EDT_OGLES1;\r
19         if (arg_ == "ogles2")\r
20                 return video::EDT_OGLES2;\r
21         if (arg_ == "opengl")\r
22                 return video::EDT_OPENGL;\r
23         std::cerr << "Unknown driver type: " << arg_.c_str() << ". Trying OpenGL." << std::endl;\r
24         return video::EDT_OPENGL;\r
25 }\r
26 \r
27 static inline void check(bool ok, const char *msg)\r
28 {\r
29         if (!ok)\r
30         {\r
31                 test_fail++;\r
32                 device->getLogger()->log((core::stringc("FAILED TEST: ") + msg).c_str(), ELL_ERROR);\r
33         }\r
34 }\r
35 \r
36 void run_unit_tests() {\r
37         std::cout << "Running unit tests:" << std::endl;\r
38         try {\r
39                 test_irr_array();\r
40                 test_irr_string();\r
41         } catch (const std::exception &e) {\r
42                 std::cerr << e.what() << std::endl;\r
43                 test_fail++;\r
44         }\r
45         std::cout << std::endl;\r
46 }\r
47 \r
48 int main(int argc, char *argv[])\r
49 {\r
50         run_unit_tests();\r
51 \r
52         SIrrlichtCreationParameters p;\r
53         p.DriverType = chooseDriver(argc > 1 ? argv[1] : "");\r
54         p.WindowSize = core::dimension2du(640, 480);\r
55         p.Vsync = true;\r
56         p.LoggingLevel = ELL_DEBUG;\r
57 \r
58         device = createDeviceEx(p);\r
59         if (!device)\r
60                 return 1;\r
61 \r
62         {\r
63                 u32 total = 0;\r
64                 device->getOSOperator()->getSystemMemory(&total, nullptr);\r
65                 core::stringc message = core::stringc("Total RAM in MiB: ") + core::stringc(total >> 10);\r
66                 device->getLogger()->log(message.c_str(), ELL_INFORMATION);\r
67                 check(total > 130 * 1024, "RAM amount");\r
68         }\r
69 \r
70         device->setWindowCaption(L"Hello World!");\r
71         device->setResizable(true);\r
72 \r
73         video::IVideoDriver* driver = device->getVideoDriver();\r
74         scene::ISceneManager* smgr = device->getSceneManager();\r
75         gui::IGUIEnvironment* guienv = device->getGUIEnvironment();\r
76 \r
77         guienv->addStaticText(L"sample text", core::rect<s32>(10,10,110,22), false);\r
78 \r
79         gui::IGUIButton* button = guienv->addButton(\r
80                 core::rect<s32>(10,30,110,30 + 32), 0, -1, L"sample button",\r
81                 L"sample tooltip");\r
82 \r
83         gui::IGUIEditBox* editbox = guienv->addEditBox(L"",\r
84                 core::rect<s32>(10,70,60,70 + 16));\r
85 \r
86         const io::path mediaPath = getExampleMediaPath();\r
87 \r
88         scene::IAnimatedMesh* mesh = smgr->getMesh(mediaPath + "coolguy_opt.x");\r
89         check(mesh, "mesh loading");\r
90         if (mesh)\r
91         {\r
92                 video::ITexture* tex = driver->getTexture(mediaPath + "cooltexture.png");\r
93                 check(tex, "texture loading");\r
94                 scene::IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);\r
95                 if (node)\r
96                 {\r
97                         node->setMaterialFlag(video::EMF_LIGHTING, false);\r
98                         node->setFrameLoop(0, 29);\r
99                         node->setAnimationSpeed(30);\r
100                         node->setMaterialTexture(0, tex);\r
101                 }\r
102         }\r
103 \r
104         smgr->addCameraSceneNode(0, core::vector3df(0,4,5), core::vector3df(0,2,0));\r
105 \r
106         s32 n = 0;\r
107         SEvent event;\r
108         device->getTimer()->start();\r
109 \r
110         while (device->run())\r
111         {\r
112                 if (device->getTimer()->getTime() >= 1000)\r
113                 {\r
114                         device->getTimer()->setTime(0);\r
115                         ++n;\r
116                         if (n == 1) // Tooltip display\r
117                         {\r
118                                 bzero(&event, sizeof(SEvent));\r
119                                 event.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
120                                 event.MouseInput.Event = irr::EMIE_MOUSE_MOVED;\r
121                                 event.MouseInput.X = button->getAbsolutePosition().getCenter().X;\r
122                                 event.MouseInput.Y = button->getAbsolutePosition().getCenter().Y;\r
123                                 device->postEventFromUser(event);\r
124                         }\r
125                         else if (n == 2) // Text input focus\r
126                                 guienv->setFocus(editbox);\r
127                         else if (n == 3) // Keypress for Text input\r
128                         {\r
129                                 bzero(&event, sizeof(SEvent));\r
130                                 event.EventType = irr::EET_KEY_INPUT_EVENT;\r
131                                 event.KeyInput.Char = L'a';\r
132                                 event.KeyInput.Key = KEY_KEY_A;\r
133                                 event.KeyInput.PressedDown = true;\r
134                                 device->postEventFromUser(event);\r
135                                 event.KeyInput.PressedDown = false;\r
136                                 device->postEventFromUser(event);\r
137                         }\r
138                         else\r
139                                 device->closeDevice();\r
140                 }\r
141 \r
142                 driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH,\r
143                         video::SColor(255,100,100,150));\r
144                 smgr->drawAll();\r
145                 guienv->drawAll();\r
146                 driver->endScene();\r
147         }\r
148 \r
149         check(core::stringw(L"a") == editbox->getText(), "EditBox text");\r
150 \r
151         device->getLogger()->log("Done.", ELL_INFORMATION);\r
152         device->drop();\r
153         return test_fail > 0 ? 1 : 0;\r
154 }\r