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