History: Hello World with minimal Ogre init
Source of version: 4 (current)
Copy to clipboard
{maketoc}
!!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.
{CODE(wrap="1", colors="c++")} #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;
}
{CODE}
!!Initialize Ogre
Now we do the most basic Ogre initialization. The basic steps are:
# Instantiate Root object
# Show the config dialog to select a renderer
# Initialize the Root object without creating a full screen window
# Create a small RenderWindow
# Create a generic scene manager
# Create a Camera for the scene
# Add a Viewport for the RenderWindow
{CODE(wrap="1", colors="c++")} // 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);
{CODE}
!!Creating a Font resource
Here we need to load the font file into the font manager. Here are the basic steps
# Get the ResourceGroupManager
# Add the folder that contains the .ttf file as a resouce location of type 'FileSystem'
# Get the FontManager
# Use the FontManager to create a resource and give it a name
# Set the font parameters
# Load the font resource
{CODE(wrap="1", colors="c++")} // 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();
{CODE}
!!Create the TextAreaOverlayElement
Here we need to create TextAreaOverlayElement and render it on our window. Here are the basic steps:
# Create a Panel and set its position and size
# Create a TextAreaOverlayElement and set its position, size, caption and font name
# Add the TextAreaOverlayElement to the Panel as child
# Create an Overlay and add the Panel to the just created Overlay
{CODE(wrap="1", colors="c++")} // get the overlay manager
OverlayManager& overlayMgr = OverlayManager::getSingleton();
{CODE}
Here we create a panel and set its position and size.
{CODE(wrap="1", colors="c++")} // 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);
{CODE}
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
{CODE(wrap="1", colors="c++")} // 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!");
{CODE}
Here we create an Overlay to display the panel
{CODE(wrap="1", colors="c++")} // Create an overlay, and add the panel
Ogre::Overlay* overlay = overlayMgr.create("OverlayName");
overlay->add2D(panel);
{CODE}
We have to add the text area to the panel _after_ adding the panel to an overlay to avoid a segfault.
{CODE(wrap="1", colors="c++")} // Add the text area to the panel
panel->addChild(textArea);
{CODE}
And display our overlay.
{CODE(wrap="1", colors="c++")} // Show the overlay
overlay->show();
{CODE}
!!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.
{CODE(wrap="1", colors="c++")} // setup frame listener
ExampleFrameListener *frameListener= new ExampleFrameListener(renderWindow, mainCam);
frameListener->showDebugOverlay(false);
root->addFrameListener(frameListener);
{CODE}
After that we should start rendering.
{CODE(wrap="1", colors="c++")} // start rendering
root->startRendering();
{CODE}
Compile and run the project. The result is a tiny window like this, saying hello to the world:
{img src="img/wiki_up/Hello_World_with_minimal_Ogre_init.png" alt="Screenshot of Intermediate Tutorial 6"}