Skip to main content
Hello World with minimal Ogre init         Hello World without using ExampleApplication.h or resource scripts

Introduction

As with all tutorials, a Hello World is almost customary. It turns out that the process of create a Hello World application with Ogre without using ExampleRefApplication requires a few more lines of code than one would expect. Here's a simple tutorial for anyone who would like to see some text on screen without using the ExampleRefApplication.

The Main line

This is a standard cross-platform main line that does nothing other than return 0.

Copy to clipboard
#include <Ogre.h> #include <OgreTextAreaOverlayElement.h> #include <OgreFontManager.h> #include "ExampleFrameListener.h" #define FONT_FILE_NAME "solo5.ttf" // save some horizontal space using namespace Ogre; #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 #define FONT_FOLDER "C:\\My\\Font\\Folder" #define WIN32_LEAN_AND_MEAN #include "windows.h" INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) #else #define FONT_FOLDER "/usr/share/ogrenew/Samples/Media/fonts" int main(int argc, char **argv) #endif { return 0; }

Initialize Ogre



Now we do the most basic Ogre initialization. The basic steps are:

  1. Instantiate Root object
  2. Show the config dialog to select a renderer
  3. Initialize the Root object without creating a full screen window
  4. Create a small RenderWindow
  5. Create a generic scene manager
  6. Create a Camera for the scene
  7. Add a Viewport for the RenderWindow



Copy to clipboard
// create root Root *root = new Root(); // choose renderer if(!root->showConfigDialog()) { return 0; } // initialise root root->initialise(false); // create main window RenderWindow *renderWindow = root->createRenderWindow("Main",320,240,false); // create the scene SceneManager *sceneMgr = root->createSceneManager(Ogre::ST_GENERIC); // add a camera Camera *mainCam = sceneMgr->createCamera("MainCam"); // add viewport Viewport *vp = renderWindow->addViewport(mainCam);

Creating a Font resource



Here we need to load the font file into the font manager. Here are the basic steps

  1. Get the ResourceGroupManager
  2. Add the folder that contains the .ttf file as a resouce location of type 'FileSystem'
  3. Get the FontManager
  4. Use the FontManager to create a resource and give it a name
  5. Set the font parameters
  6. Load the font resource



Copy to clipboard
// get the resource manager ResourceGroupManager &resGroupMgr = ResourceGroupManager::getSingleton(); // tell it to look at this location resGroupMgr.addResourceLocation(FONT_FOLDER, "FileSystem"); // get the font manager FontManager &fontMgr = FontManager::getSingleton(); // create a font resource ResourcePtr font = fontMgr.create("MyFont","General"); // set as truetype font->setParameter("type","truetype"); // set the .ttf file name font->setParameter("source",FONT_FILE_NAME); // set the size font->setParameter("size","26"); // set the dpi font->setParameter("resolution","96"); // load the ttf font->load();

Create the TextAreaOverlayElement



Here we need to create TextAreaOverlayElement and render it on our window. Here are the basic steps:

  1. Create a Panel and set its position and size
  2. Create a TextAreaOverlayElement and set its position, size, caption and font name
  3. Add the TextAreaOverlayElement to the Panel as child
  4. Create an Overlay and add the Panel to the just created Overlay



Copy to clipboard
// get the overlay manager OverlayManager& overlayMgr = OverlayManager::getSingleton();

Here we create a panel and set its position and size.

Copy to clipboard
// Create a panel Ogre::OverlayContainer* panel = static_cast<OverlayContainer*>( overlayMgr.createOverlayElement("Panel", "PanelName")); panel->setMetricsMode(Ogre::GMM_PIXELS); panel->setPosition(10, 10); panel->setDimensions(300, 120);

Here we create a TextAreaOverlayElement and initialize its

  • Position
  • Size
  • Font and Font Size
  • Caption




And finally add the text area to the created panel

Copy to clipboard
// Create a text area TextAreaOverlayElement* textArea = static_cast<TextAreaOverlayElement*>( overlayMgr.createOverlayElement("TextArea", "TextAreaName")); textArea->setMetricsMode(Ogre::GMM_PIXELS); textArea->setPosition(0, 0); textArea->setDimensions(300, 120); textArea->setCharHeight(26); // set the font name to the font resource that you just created. textArea->setFontName("MyFont"); // say something textArea->setCaption("Hello, World!");

Here we create an Overlay to display the panel

Copy to clipboard
// Create an overlay, and add the panel Ogre::Overlay* overlay = overlayMgr.create("OverlayName"); overlay->add2D(panel);

We have to add the text area to the panel _after_ adding the panel to an overlay to avoid a segfault.

Copy to clipboard
// Add the text area to the panel panel->addChild(textArea);

And display our overlay.

Copy to clipboard
// Show the overlay overlay->show();

Setup the FrameListener and Hello World



The only thing left to do is to setup the nice frame listener
that will exit the main loop on ESC button or window close event.


Notice: We use the ExampleFrameListener class as a placeholder here before we have a lower level implementation of a frame listener.

Copy to clipboard
// setup frame listener ExampleFrameListener *frameListener= new ExampleFrameListener(renderWindow, mainCam); frameListener->showDebugOverlay(false); root->addFrameListener(frameListener);

After that we should start rendering.

Copy to clipboard
// start rendering root->startRendering();

Compile and run the project. The result is a tiny window like this, saying hello to the world:

Screenshot of Intermediate Tutorial 6