Hi, this is a long-ish post, and you may want to print it out or something
Ok. A couple of points I have to make first, then an example of how to actually do this - which is, I believe, the first time I think I have demonstrated this here on the forum.
In CEGUI you need each windows sub-class to have an associated WindowFactory registered with the system; this is the system we use, and if you don't do this, things can quickly go wrong - especially when it comes to destroying things again.
The requestRedraw call didn't ought to go in the populateRenderCache member, since it effectively nullifies the gains achieved through caching.
Anyway, on to an example. This is generally based off of the code and what have you that you already posted.
First you need a window class, all windows have a name and a type; the base Window class takes this information in its constructor and the idiom is that the window factory will pass this information to you. Note that this is not required to be done this way, but in keeping with accepted norms, this is how I have arranged this example also.
Class declaration:
Code: Select all
class MyWindow : public CEGUI::Window
{
public:
MyWindow(const CEGUI::String& type, const CEGUI::String& name);
~MyWindow() {}
protected:
void populateRenderCache();
};
The implementations for the two members here are as follows:
In the constructor we pass our parameters onto the parent constructor, and we also re-default from a (0,0) size to a size that covers the entire parent element (i.e. the screen):
Code: Select all
MyWindow::MyWindow(const CEGUI::String& type, const CEGUI::String& name) : Window (type, name)
{
setSize(CEGUI::UVector2(CEGUI::UDim(1,0), CEGUI::UDim(1,0)));
}
The populateRenderCache member is basically your code verbatim, except without the call to requestRedraw:
Code: Select all
void MyWindow::populateRenderCache()
{
d_renderCache.cacheText("this is some text",
CEGUI::FontManager::getSingleton().getFont("Commonwealth-10"),
CEGUI::Centred,
CEGUI::Rect(100, 100, 500, 500), 0,
CEGUI::ColourRect(CEGUI::colour(1.0f, 0.0f, 0.0f)));
}
As stated, you need a WindowFactory based object that can be registered with the system to create instances of the window for you. A simple declaration of a WindowFactory class might look like this:
Code: Select all
class MyWindowFactory : public CEGUI::WindowFactory
{
public:
MyWindowFactory();
CEGUI::Window* createWindow(const CEGUI::String& name);
void destroyWindow(CEGUI::Window* window);
};
The implementation of the WindowFactory members is really simple...
The constructor just passes a type string to the parent class:
Code: Select all
MyWindowFactory::MyWindowFactory() : CEGUI::WindowFactory("MyWindow")
{}
The createWindow member just creates an instance of the MYWindow class passing the type 'd_type' which is a field in the WindowFactory base class initialised earlier, and a name that we are passed by the system:
Code: Select all
CEGUI::Window* MyWindowFactory::createWindow(const CEGUI::String& name)
{
return new MyWindow(d_type, name);
}
The destroyWindow member is trivial, and its purpose obvious:
Code: Select all
void MyWindowFactory::destroyWindow(CEGUI::Window* window)
{
delete window;
}
Ok. Now that's over, before using the new window class in the system we have to add the factory so CEGUI knows about it. This is simple, but you need an instance of the factory class somewhere. In the core lib, these are kept around as static objects, in my test code I have a global instance - how you do it is up to you
Code: Select all
// global factory instance
MyWindowFactory G_My_Window_Factory;
Finally, in your code you just need to register the factory, create an instance of the window, and ask for it to be redrawn:
Code: Select all
// register new window factory with the system
CEGUI::WindowFactoryManager::getSingleton().addFactory(&G_My_Window_Factory);
// create an instance of the new window type, named "root"
CEGUI::Window* root = CEGUI::WindowManager::getSingleton().createWindow("MyWindow", "root");
// set this window as the GUI root sheet
System::getSingleton().setGUISheet(root);
// request that the window be redrawn in the next rendering pass
root->requestRedraw();
HTH
CE