Informacje wstępne Zakładam, że masz znajomości programowania C + + i jesteś w stanie skonfigurować i opracować aplikacje w Ogre. Ten samouczek bazuje na poprzednim i zakłada, że już pracowałeś z nim.
Wprowadzenie W tej instrukcji zbadamy sposób korzystania z CEGUI (wbudowany system GUI) z Ogre. Ten podręcznik pokaże jak dodać w CEGUI podstawowe elementy funkcjonalności aplikacji. UWAGA: Ten podręcznik nie nauczy Cię, jak używać CEGUI. Ten przewodnik pomoże Ci zacząć. Wszelkie dalsze pytania i pomoc w CEGUI należy kierować do ich strony głównej.
W module kod jest dostępny kompletny kod źródłowy. Czytając ten podręcznik należy powoli dodawać kod do własnego projektu i oglądać jego wyniki.
Rozpoczęcie Początkowy kod W tym samouczku, będziemy używać wstępny kod źródłowy jako punkt wyjścia. Jeśli pracowałeś w poprzednich samouczkach, powinno być to znane do Ciebie. Utwórz projekt w kompilatorze wybranym dla tego projektu i dodaj plik źródłowy, który zawiera ten kod:
#include "ExampleApplication.h" #include #include #include
class TutorialListener : public ExampleFrameListener, public OIS::MouseListener, public OIS::KeyListener { public: TutorialListener(RenderWindow* win, Camera* cam) : ExampleFrameListener(win, cam, true, true)
mContinue=true; mMouse->setEventCallback(this); mKeyboard->setEventCallback(this); } // CEGUIDemoListener
bool frameStarted(const FrameEvent &evt)
mKeyboard->capture(); mMouse->capture();
return mContinue && !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
bool quit(const CEGUI::EventArgs &e)
mContinue = false; return true;
// MouseListener bool mouseMoved(const OIS::MouseEvent &arg)
return true;
bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
return true;
bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
return true;
// KeyListener bool keyPressed(const OIS::KeyEvent &arg)
return true;
bool keyReleased(const OIS::KeyEvent &arg)
return true;
private: bool mContinue;
class CEGUIDemoApplication : public ExampleApplication { public: CEGUIDemoApplication() : mSystem(0), mRenderer(0)
~CEGUIDemoApplication()
if (mSystem) delete mSystem;
if (mRenderer) delete mRenderer;
protected: CEGUI::System *mSystem; CEGUI::OgreCEGUIRenderer *mRenderer;
void createScene(void)
void createFrameListener(void)
mFrameListener= new TutorialListener(mWindow, mCamera); mFrameListener->showDebugOverlay(true); mRoot->addFrameListener(mFrameListener);
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 #define WIN32_LEAN_AND_MEAN #include "windows.h"
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) #else int main(int argc, char **argv) #endif { // Create application object CEGUIDemoApplication app;
try { app.go(); } catch(Exception& e) { #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL); #else fprintf(stderr, "An exception has occurred: %s\n", e.getFullDescription().c_str()); #endif
return 0;
Kompilacja Teraz musisz sprawdzić, czy skompilujesz i uruchomisz ten kod. Wynik powinien wyświetlić pusty ekran (naciśnij ESC aby wyjść). Jeśli masz błędy linkera podczas próby kompilacji, należy się upewnić, że CEGUIBase_d.lib i OgreGUIRenderer_d.lib linker dodaje do wejścia (w trybie debugowania, po dopuszczeniu trybu usunąć _d z nich).
A Brief Introduction
CEGUI is a fully featured GUI library that can be embedded in 3D applications such as Ogre (it also supports pure DirectX and OpenGL as well). Much in the same way that Ogre is only a graphics library (and doesn't do other things such as sound, physics, etc), CEGUI is only a GUI library, meaning it does not do its own rendering nor does it hook into any mouse or keyboard events. In fact, in order for CEGUI to render at all, you have to provide a renderer for it (which is the OgreGUIRenderer library included in the SDK), and in order for it to even understand mouse and keyboard events you have to manually inject them into the system. This may seem like a pain at first, but in reality very little code is required to make this happen. It also allows you to have full control over the rendering and the input, CEGUI will never get in the way.
There are many aspects to CEGUI and many quirks that will be unfamiliar to you (even if you have used GUI systems before). I will try to slowly introduce them to you as we go along.
Integracja z OGRE
Inicjalizacja CEGUI
Poznaliśmy trochę sposób włączania CEGUI w poprzednim akapicie. Na razie wiedzy tej nie będziemy pogłębiać.
Znajdź funkcję createScene i dodaj do niej następujący kod:
mRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr); mSystem = new CEGUI::System(mRenderer);
Teraz, kiedy włączyliśmy CEGUI, musimy wybrać szate graficzną, jakiej będziemy używać. Wygląd CEGUI można dowolnie zmieniać. Więcej informacji na ten temat znajdziesz na stronie CEGUI.
Następująca linijka kodu definiuje skórkę:
CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
Domyślnie Ogre nie zawiera innych skórek CEGUI, ale jeżeli pobierzesz CEGUI z ich strony, znajdziesz więcej skórek (możesz też zrobić swoją własną). Następną rzeczą którą musimy zrobić jest zaimplementowanie kursora myszy oraz domyślnej czcionki:
mSystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow"); mSystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");
Throughout this tutorial series we will be using CEGUI to display the mouse cursor, even when we have no other use for the GUI library. It is possible to use another GUI library to render the mouse, or to simply create your own mouse cursor using Ogre directly (though this latter option can be a bit involved). If you are only using CEGUI for the mouse cursor and are concerned about memory usage or the disk space that your game takes up, you can look into one of these options to replace CEGUI.
Lastly note that in that last code snippet we have set the default mouse cursor, but we did not set the mouse cursor directly using the MouseCursor::setImage function as we will in later tutorials. This is because in this tutorial we will always be over some kind of CEGUI window (though it may be invisible), so setting the default cursor will, in effect, make the mouse cursor be the image we selected. If we set the mouse cursor directly and did not set the default, the mouse cursor would be invisible every time it passed over a CEGUI window (which, in this tutorial, will be all the time). On the other hand, setting the default mouse image does nothing if you do not have any CEGUI windows displayed, as will be the case in later tutorials. In that situation, calling MouseCursor::setImage will display the cursor for the application. Example:
CEGUI::MouseCursor::getSingleton().setImage(CEGUI::System::getSingleton().getDefaultMouseCursor());
Injecting Key Events
CEGUI does not handle input in any way. It does not read mouse movements or keyboard input. Instead it relies on the user to inject key and mouse events into the system. The next thing we will need to do is to handle the key events. If you are working with CEGUI, you will need to have the mouse and keyboard in buffered mode so you can receive the events directly and inject them as they happen. Find the keyPressed function and add the following code to it:
CEGUI::System *sys = CEGUI::System::getSingletonPtr(); sys->injectKeyDown(arg.key); sys->injectChar(arg.text);
After getting the system object, we need to do two things. The first is to inject the key down event into CEGUI. The second is to inject the actual character that was pressed. It is very important to inject the character properly since injecting the key down will not always bring about the desired result when using a non-English keyboard. The injectChar was designed with Unicode support in mind.
Now we need to inject the key up event into the system. Find the keyReleased function and add the following code:
CEGUI::System::getSingleton().injectKeyUp(arg.key);
Note that we do not need to inject a character up event, only the key up event is required.
Converting and Injecting Mouse Events
Now that we have finished dealing with keyboard input, we will now need to take care of mouse input. We have a small issue that we will need to address however. When we injected the key up and down events into CEGUI we never had to convert the key. Both OIS and CEGUI use the same key codes for keyboard input. The same is not true for mouse buttons. Before we can inject mouse button presses into CEGUI, we will need to write a function which converts OIS button IDs into CEGUI button IDs. Add the following function near the top of your source code, just before the TutorialListener class:
CEGUI::MouseButton convertButton(OIS::MouseButtonID buttonID) { switch (buttonID)
case OIS::MB_Left: return CEGUI::LeftButton;
case OIS::MB_Right: return CEGUI::RightButton;
case OIS::MB_Middle: return CEGUI::MiddleButton;
default: return CEGUI::LeftButton;
Now we are ready to inject mouse events. Find the mousePressed function and add the following code:
CEGUI::System::getSingleton().injectMouseButtonDown(convertButton(id));
This should be roughly self-explanatory. We convert the button ID which was passed in, and pass the result to CEGUI. Find the mouseReleased function and add this line of code:
CEGUI::System::getSingleton().injectMouseButtonUp(convertButton(id));
Lastly, we need to inject mouse motion into CEGUI. The CEGUI::System object has an injectMouseMove function which expects relative mouse movements. The OIS::mouseMoved handler gives us those relative movements in the state.X.rel variable and the state.Y.rel variables. Find the mouseMoved function and add the following code:
CEGUI::System::getSingleton().injectMouseMove(arg.state.X.rel, arg.state.Y.rel);
That's it. Now CEGUI is fully setup and receiving mouse and keyboard events.
Windows, Sheets, and Widgets
Introduction
CEGUI is very different from most GUI systems. In CEGUI, everything that is displayed is a subclass of the CEGUI::Window class, and a window can have any number of children windows. This means that when you create a frame to contain multiple buttons, that frame is a Window. This also leads to some strange things to happen. You can place a button inside another button, though that would really never happen in practice. The reason that I mention all of this is when you are looking for a particular widget that you have placed in the application, they are all called Windows, and are accessed by functions which call them as such.
In most practical uses of CEGUI, you will not create each individual object through code. Instead you create a GUI layout for your application in an editor such as the CEGUI Layout Editor. After placing all of your windows, buttons, and other widgets onto the screen as you like them, the editor saves the layout into a text file. You may later load this layout into what CEGUI calls a GUI sheet (which is also a subclass of CEGUI::Window).
Lastly, know that CEGUI contains a large number of widgets that you can use in your application. We will not cover them in this tutorial, so if you decide to use CEGUI, be sure to take a good look at their website for more information.
Loading a Sheet
In CEGUI loading a sheet is very easy to do. The WindowManager class provides a "loadWindowLayout" function which loads the sheet and puts it into a CEGUI::Window object. Then you call CEGUI::System::setGUISheet to display it. We will not be using this in this tutorial, but I would feel remiss if I did not at least show you an example of its use. Do not add this to the tutorial (or if you do, remove it after you have seen the results):
// Do not add this to the program