Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / samples / 3d / shape_viewer / main.cpp
blobb4535c493a837542fff3d59a4d768fbfb2ef8827
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 // Includes
25 #include <nel/misc/path.h>
26 #include <nel/misc/file.h>
27 #include <nel/misc/common.h>
28 #include <nel/3d/u_scene.h>
29 #include <nel/3d/u_light.h>
30 #include <nel/3d/u_camera.h>
31 #include <nel/3d/u_driver.h>
32 #include <nel/3d/u_instance.h>
34 #ifdef NL_OS_WINDOWS
35 #ifndef NL_COMP_MINGW
36 #define NOMINMAX
37 #endif
38 #include <windows.h>
39 #endif // NL_OS_WINDOWS
41 using namespace NLMISC;
42 using namespace NL3D;
44 // function to split a string into several substrings delimited by specified characters
45 void split(const std::string &str, std::vector<std::string> &tokens, const std::string &delimiters)
47 // Skip delimiters at beginning.
48 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
49 // Find first "non-delimiter".
50 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
52 while (std::string::npos != pos || std::string::npos != lastPos)
54 // Found a token, add it to the vector.
55 tokens.push_back(str.substr(lastPos, pos - lastPos));
56 // Skip delimiters. Note the "not_of"
57 lastPos = str.find_first_not_of(delimiters, pos);
58 // Find next "non-delimiter"
59 pos = str.find_first_of(delimiters, lastPos);
63 #ifdef NL_OS_WINDOWS
64 sint WINAPI WinMain(HINSTANCE /* hInstance */, HINSTANCE /* hPrevInstance */, LPSTR cmdline, int /* nCmdShow */)
66 // we can specify several shapes on command line
67 // so we need to process it
68 std::vector<std::string> argv;
70 // argv[0] is the fullpath to the executable but since it's not used, we can let it empty
71 argv.push_back("");
73 // split the whole cmdline into args
74 split(cmdline, argv, " ");
76 // args number
77 int argc = (int)argv.size();
79 #else
80 sint main(int argc, char **argv)
82 #endif
83 NLMISC::CApplicationContext myApplicationContext;
85 try
87 // create OpenGL driver
88 NL3D::UDriver *Driver = UDriver::createDriver();
89 if (!Driver) throw 2;
91 // create a window in 800x600
92 Driver->setDisplay(UDriver::CMode(800, 600, 32, true));
94 // set the title
95 Driver->setWindowTitle(ucstring("NeL shape viewer"));
97 // can use both dds and tga textures for shapes
98 CPath::remapExtension ("dds", "tga", true);
100 // create the light
101 ULight *Light = ULight::createLight();
102 if (!Light) throw 3;
104 // set mode of the light
105 Light->setMode(ULight::DirectionalLight);
107 // set position of the light
108 Light->setPosition(CVector(-20.f, 30.f, 10.f));
110 // white light
111 Light->setAmbiant(CRGBA(255, 255, 255));
113 // set and enable the light
114 Driver->setLight(0, *Light);
115 Driver->enableLight(0);
117 // Create a scene
118 NL3D::UScene *Scene = Driver->createScene(true);
119 if (!Scene) throw 4;
121 // create the camera
122 UCamera Camera = Scene->getCam();
123 if (Camera.empty()) throw 5;
125 Camera.setTransformMode (UTransformable::DirectMatrix);
126 Camera.setPerspective ((float)Pi/2.f, 1.33f, 0.1f, 1000);
128 // camera will look at entities
129 Camera.lookAt (CVector(-10.f, 10.f, 0.f), CVector(0.f, 0.f, 0.f));
131 // create entities
132 std::vector<UInstance> Entities;
134 // create each entity
135 for(int i = 1; i < argc; ++i)
137 // use the path of the shape to find its textures
138 CPath::addSearchPath(CFile::getPath(argv[i]), true, false);
140 // add an entity to the scene
141 UInstance Entity = Scene->createInstance(argv[i]);
143 // if we can't create entity, skip it
144 if (Entity.empty()) continue;
146 // we will rotate it later so use Euler rotation transform mode
147 Entity.setTransformMode(UTransformable::RotEuler);
149 // add entity to the vector
150 Entities.push_back(Entity);
153 // initial angle
154 float angle = 0.f;
156 // main loop
157 while (Driver->isActive())
159 Driver->EventServer.pump();
161 // the background is black
162 Driver->clearBuffers(CRGBA(0, 0, 0));
164 // increase the angle
165 angle += 0.1f;
167 if (angle >= NLMISC::Pi*2) angle = 0.f;
169 // rotate all entities
170 for(size_t i = 0; i < Entities.size(); ++i)
172 Entities[i].setRotEuler(0.f, 0.f, angle);
175 // animate the scene
176 Scene->animate(NLMISC::CTime::getLocalTime() / 1000.0);
178 // render the scene
179 Scene->render();
181 // show the scene
182 Driver->swapBuffers();
184 // escape will leave the program
185 if (Driver->AsyncListener.isKeyPushed(KeyESCAPE))
187 break;
189 // F3 will change the render mode
190 else if (Driver->AsyncListener.isKeyPushed(KeyF3))
192 UDriver::TPolygonMode p = Driver->getPolygonMode();
193 p = UDriver::TPolygonMode(((int)p+1)%3);
194 Driver->setPolygonMode(p);
196 // F12 will take a screenshot
197 else if (Driver->AsyncListener.isKeyPushed(KeyF12))
199 CBitmap btm;
200 Driver->getBuffer(btm);
201 COFile fs(CFile::findNewFile("screenshot.png"));
202 btm.writePNG(fs,24);
206 // we are leaving the program
208 // delete all entities
209 for(size_t i = 0; i < Entities.size(); ++i)
211 Scene->deleteInstance(Entities[i]);
214 // delete the scene
215 Driver->deleteScene(Scene);
217 // delete the light
218 delete Light;
220 // release all textures and others elements
221 Driver->release();
223 // delete the driver
224 delete Driver;
226 catch(int a)
228 return a;
230 catch(...)
232 return EXIT_FAILURE;
235 return EXIT_SUCCESS;