]> git.lizzy.rs Git - irrlicht.git/blob - examples/AutomatedTest/main.cpp
Add OpenGL3 support to the autotests
[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         scene::IAnimatedMesh* mesh = smgr->getMesh(mediaPath + "coolguy_opt.x");\r
91         check(mesh, "mesh loading");\r
92         if (mesh)\r
93         {\r
94                 video::ITexture* tex = driver->getTexture(mediaPath + "cooltexture.png");\r
95                 check(tex, "texture loading");\r
96                 scene::IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);\r
97                 if (node)\r
98                 {\r
99                         node->setMaterialFlag(video::EMF_LIGHTING, false);\r
100                         node->setFrameLoop(0, 29);\r
101                         node->setAnimationSpeed(30);\r
102                         node->setMaterialTexture(0, tex);\r
103                 }\r
104         }\r
105 \r
106         smgr->addCameraSceneNode(0, core::vector3df(0,4,5), core::vector3df(0,2,0));\r
107 \r
108         s32 n = 0;\r
109         SEvent event;\r
110         device->getTimer()->start();\r
111 \r
112         while (device->run())\r
113         {\r
114                 if (device->getTimer()->getTime() >= 1000)\r
115                 {\r
116                         device->getTimer()->setTime(0);\r
117                         ++n;\r
118                         if (n == 1) // Tooltip display\r
119                         {\r
120                                 bzero(&event, sizeof(SEvent));\r
121                                 event.EventType = irr::EET_MOUSE_INPUT_EVENT;\r
122                                 event.MouseInput.Event = irr::EMIE_MOUSE_MOVED;\r
123                                 event.MouseInput.X = button->getAbsolutePosition().getCenter().X;\r
124                                 event.MouseInput.Y = button->getAbsolutePosition().getCenter().Y;\r
125                                 device->postEventFromUser(event);\r
126                         }\r
127                         else if (n == 2) // Text input focus\r
128                                 guienv->setFocus(editbox);\r
129                         else if (n == 3) // Keypress for Text input\r
130                         {\r
131                                 bzero(&event, sizeof(SEvent));\r
132                                 event.EventType = irr::EET_KEY_INPUT_EVENT;\r
133                                 event.KeyInput.Char = L'a';\r
134                                 event.KeyInput.Key = KEY_KEY_A;\r
135                                 event.KeyInput.PressedDown = true;\r
136                                 device->postEventFromUser(event);\r
137                                 event.KeyInput.PressedDown = false;\r
138                                 device->postEventFromUser(event);\r
139                         }\r
140                         else\r
141                                 device->closeDevice();\r
142                 }\r
143 \r
144                 driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH,\r
145                         video::SColor(255,100,100,150));\r
146                 smgr->drawAll();\r
147                 guienv->drawAll();\r
148                 driver->endScene();\r
149         }\r
150 \r
151         check(core::stringw(L"a") == editbox->getText(), "EditBox text");\r
152 \r
153         device->getLogger()->log("Done.", ELL_INFORMATION);\r
154         device->drop();\r
155         return test_fail > 0 ? 1 : 0;\r
156 }\r