Merge branch 'fixes' into main/gingo-test
[ryzomcore.git] / nel / samples / 3d / cegui / main.cpp
blobeda1ac666b7d34594e3d263cc5e549c656255e65
1 /**
2 * \file main.cpp
3 * \date November 2004
4 * \author Matt Raykowski
5 * \author Henri Kuuste
6 */
8 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
9 // Copyright (C) 2010 Winch Gate Property Limited
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU Affero General Public License as
13 // published by the Free Software Foundation, either version 3 of the
14 // License, or (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Affero General Public License for more details.
21 // You should have received a copy of the GNU Affero General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "globals.h"
25 #include "resource.h"
28 // System Includes
30 #ifdef NL_OS_WINDOWS
31 # define NOMINMAX
32 # include <windows.h>
33 HINSTANCE ghInstance = 0;
34 #endif
35 #include <string>
38 // CEGUI Includes
40 #include <nel/cegui/inellibrary.h>
41 #include <nel/cegui/nelrenderer.h>
42 //#include <nel/cegui/nellogger.h>
43 #include <CEGUI.h>
44 #include "NeLDriver.h"
46 #include <nel/misc/hierarchical_timer.h>
47 #include <nel/misc/dynloadlib.h>
49 // Namespaces
51 using namespace std;
54 // NEL GLOBALS
56 NeLDriver *gDriver;
57 uint16 gScreenWidth;
58 uint16 gScreenHeight;
61 // CEGUI GLOBALS
63 //CEGUI::NeLLogger *gGuiLogger;
64 CEGUI::System *gGuiSystem;
65 CEGUI::Renderer *gGuiRenderer;
66 bool gStopDemo;
68 void createDemoWindows();
69 void initDemoEventWiring();
70 bool handleQuit(const CEGUI::EventArgs& e);
71 bool handleSlider(const CEGUI::EventArgs& e);
72 bool handleRadio(const CEGUI::EventArgs& e);
73 bool handleCheck(const CEGUI::EventArgs& e);
75 #ifndef CEGUI_DATA_DIR
76 #define CEGUI_DATA_DIR "datafiles"
77 #endif // CEGUI_DATA_DIR
79 #ifdef NL_OS_WINDOWS
80 int WINAPI WinMain( HINSTANCE hInstance,
81 HINSTANCE hPrevInstance,
82 LPSTR lpCmdLine,
83 int nCmdShow ) {
84 ghInstance = hInstance;
85 #else
86 int main(int argc, char **argv)
88 #endif
89 NLMISC::CApplicationContext myApplicationContext;
90 try {
91 gScreenWidth=800;
92 gScreenHeight=600;
93 NLMISC::CPath::addSearchPath(CEGUI_DATA_DIR,true,false);
95 // Load the CEGUI renderer and get a handle to the library.
96 NLMISC::CLibrary driverLib;
97 if(!driverLib.loadLibrary("nelceguirenderer", true, true , true)) {
98 nlerror("Failed to load NeL CEGUI Renderer library.");
100 NELRENDERER_CREATE_PROC createNelRenderer = reinterpret_cast<NELRENDERER_CREATE_PROC>(driverLib.getSymbolAddress(NELRENDERER_CREATE_PROC_NAME));
102 // CCeguiRendererNelLibrary *nelCeguiDriverLib = dynamic_cast<CCeguiRendererNelLibrary *>(driverLib.getNelLibraryInterface());
104 NL3D::UDriver *driver;
106 // Create a driver
107 uint icon = 0;
108 #ifdef NL_OS_WINDOWS
109 icon = (uint)LoadIcon(ghInstance,MAKEINTRESOURCE(IDI_ICON1));
110 #endif
111 bool useD3D = false;
112 #ifdef NL_INDEX_BUFFER_H //new 3d
113 NL3D::UDriver *driver = NL3D::UDriver::createDriver(icon,useD3D);
114 #else
115 driver = NL3D::UDriver::createDriver(icon);
116 #endif
117 nlassert(driver);
119 nlinfo("Start initializing the 3D system");
120 gDriver=new NeLDriver(driver);
121 gDriver->init();
123 // start up the Gui system.
124 //gGuiLogger = nelCeguiDriverLib->createNelLogger();
125 //gGuiRenderer = new CEGUI::NeLRenderer(driver);
127 gGuiRenderer = createNelRenderer(driver, true);
128 gGuiSystem = new CEGUI::System(gGuiRenderer);
129 CEGUI::NeLRenderer *rndr = (CEGUI::NeLRenderer *)gGuiRenderer;
130 rndr->activateInput();
131 rndr->captureCursor(true);
133 // load some GUI stuff for demo.
134 nlinfo("Start up and configure the GUI system.");
135 try {
136 using namespace CEGUI;
138 Logger::getSingleton().setLoggingLevel(Insane);
140 // load scheme and set up defaults
141 SchemeManager::getSingleton().loadScheme("TaharezLook.scheme");
142 System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");
144 // create the font and set it up as the default.
145 FontManager::getSingleton().createFont("Commonwealth-10.font");
147 // load an image to use as a background
148 ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "GPN-2000-001437.tga");
150 // here we will use a StaticImage as the root, then we can use it to place a background image
151 Window* background = WindowManager::getSingleton().createWindow("TaharezLook/StaticImage", "background_wnd");
152 // set position and size
153 background->setPosition(UVector2(cegui_reldim(0), cegui_reldim( 0)));
154 background->setSize(UVector2(cegui_reldim(1), cegui_reldim( 1)));
155 // disable frame and standard background
156 background->setProperty("FrameEnabled", "false");
157 background->setProperty("BackgroundEnabled", "false");
158 // set the background image
159 background->setProperty("Image", "set:BackgroundImage image:full_image");
160 // install this as the root GUI sheet
161 System::getSingleton().setGUISheet(background);
163 // load the windows for Demo7 from the layout file.
164 Window* sheet = WindowManager::getSingleton().loadWindowLayout("Demo7Windows.layout");
165 // attach this to the 'real' root
166 background->addChildWindow(sheet);
168 initDemoEventWiring();
169 } catch(CEGUI::Exception) { // catch to prevent exit (errors will be logged).
173 NLMISC::CRGBA pClearColor(0,0,0,255);
174 const CEGUI::Font *fnt = gGuiSystem->getDefaultFont();
175 gStopDemo=false;
176 // BEGIN MAIN LOOP
177 //NLMISC::CHTimer::startBench();
178 //NLMISC::CHTimer frameTimer("MAIN_DemoLoop");
179 //NLMISC::CHTimer renderGUITimer("MAIN_RenderGUI");
180 while(!gStopDemo) {
181 //frameTimer.before();
182 // stop the demo if the driver stops.
183 if(!gDriver->getDriver().isActive()) {
184 gStopDemo=true;
185 continue;
187 gDriver->getDriver().clearBuffers(pClearColor);
188 // RUN UPDATES
189 // 3D
190 gDriver->update();
192 // handle some input
193 if(gDriver->getDriver().AsyncListener.isKeyPushed(NLMISC::KeyESCAPE)) {
194 gStopDemo=true;
197 // RUN RENDERS
198 // draw display
199 //renderGUITimer.before();
200 gGuiSystem->renderGUI();
201 //renderGUITimer.after();
203 // 3D
204 gDriver->render();
205 // End 3D
206 // 3D Swap ** ALWAYS THE LAST PART
207 gDriver->getDriver().swapBuffers();
208 if(gDriver->getDriver().AsyncListener.isKeyPushed(NLMISC::KeyF2)) {
209 NLMISC::CBitmap btm;
210 gDriver->getDriver().getBuffer(btm);
211 std::string filename = NLMISC::CFile::findNewFile("screenshot.jpg");
212 NLMISC::COFile fs(filename);
213 btm.writeJPG(fs);
214 nlinfo("Screenshot '%s' saved", filename.c_str());
216 // 3D Swap ** NOTHING AFTER THIS
217 //frameTimer.after();
218 if(gDriver->getDriver().AsyncListener.isKeyPushed(NLMISC::KeyF3)) {
219 NLMISC::CHTimer::display();
222 // END MAIN LOOP
223 } catch(NLMISC::Exception &e) {
224 fprintf (stderr,"main trapped an exception: '%s'", e.what ());
226 return 0;
230 /*************************************************************************
231 Sample sub-class for ListboxTextItem that auto-sets the selection brush
232 image.
233 *************************************************************************/
234 class MyListItem : public CEGUI::ListboxTextItem
236 public:
237 MyListItem(const CEGUI::String& text) : ListboxTextItem(text)
239 setSelectionBrushImage((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MultiListSelectionBrush");
245 /*************************************************************************
246 Perform required event hook-ups for this demo.
247 *************************************************************************/
248 void initDemoEventWiring(void)
250 using namespace CEGUI;
252 WindowManager::getSingleton().getWindow("Demo7/Window1/Quit")->
253 subscribeEvent(PushButton::EventClicked, handleQuit);
255 WindowManager::getSingleton().getWindow("Demo7/Window1/Slider1")->
256 subscribeEvent(Slider::EventValueChanged, handleSlider);
258 WindowManager::getSingleton().getWindow("Demo7/Window1/Checkbox")->
259 subscribeEvent(Checkbox::EventCheckStateChanged, handleCheck);
261 WindowManager::getSingleton().getWindow("Demo7/Window1/Radio1")->
262 subscribeEvent(RadioButton::EventSelectStateChanged, handleRadio);
264 WindowManager::getSingleton().getWindow("Demo7/Window1/Radio2")->
265 subscribeEvent(RadioButton::EventSelectStateChanged, handleRadio);
267 WindowManager::getSingleton().getWindow("Demo7/Window1/Radio3")->
268 subscribeEvent(RadioButton::EventSelectStateChanged, handleRadio);
272 bool handleQuit(const CEGUI::EventArgs& e)
274 gStopDemo=true;
276 return true;
279 bool handleSlider(const CEGUI::EventArgs& e)
281 using namespace CEGUI;
283 float val = ((Slider*)((const WindowEventArgs&)e).window)->getCurrentValue();
285 ((ProgressBar*)WindowManager::getSingleton().getWindow("Demo7/Window2/Progbar1"))->setProgress(val);
286 ((ProgressBar*)WindowManager::getSingleton().getWindow("Demo7/Window2/Progbar2"))->setProgress(1.0f - val);
288 WindowManager::getSingleton().getWindow("root_wnd")->setAlpha(val);
290 return true;
293 bool handleRadio(const CEGUI::EventArgs& e)
295 using namespace CEGUI;
297 CEGUI::uint id = ((RadioButton*)((const WindowEventArgs&)e).window)->getSelectedButtonInGroup()->getID();
299 if (id == 0)
301 WindowManager::getSingleton().getWindow("Demo7/Window2/Image1")->setProperty("Image", "set:WerewolfGuiImagery image:CeguiLogo");
303 else if (id == 1)
305 WindowManager::getSingleton().getWindow("Demo7/Window2/Image1")->setProperty("Image", "set:WerewolfGuiImagery image:NeLLogo");
307 else
309 WindowManager::getSingleton().getWindow("Demo7/Window2/Image1")->setProperty("Image", "set:WerewolfGuiImagery image:WerewolfLogo");
313 return true;
316 bool handleCheck(const CEGUI::EventArgs& e)
318 using namespace CEGUI;
320 if (((Checkbox*)((const WindowEventArgs&)e).window)->isSelected())
322 WindowManager::getSingleton().getWindow("Demo7/Window3")->show();
324 else
326 WindowManager::getSingleton().getWindow("Demo7/Window3")->hide();
329 return true;