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