Ogre Hello World without example app

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
tcneng
Gnoblar
Posts: 1
Joined: Mon Aug 22, 2005 11:01 am

Ogre Hello World without example app

Post by tcneng »

Hi,

taken me a while to do this from zero knowledge of Ogre. I literally just downloaded ogre like 2 days ago. There are bits and pieces of information around the forum but none of them are complete and most of them are based on ExampleApp.

This is one for the noobs (like me) who dont wanna use ExampleApp or script files.

so here we go, Ogre Hello World:

Headers

Code: Select all

#include <Ogre.h>
#include <OgreTextAreaOverlayElement.h> 
#include <OgreFontManager.h> 
Ogre::RenderWindow *renderWindow = NULL;
Simple Frame Listener

Code: Select all

class FrameListener : public Ogre::FrameListener
{
public:
	FrameListener(){}
	bool frameStarted(const Ogre::FrameEvent& evt)
    {
		// stop render in main window is closed
        if(renderWindow->isClosed()){return false;}
		return true;
	}
	bool frameEnded(const Ogre::FrameEvent &evt){return true;}
};
And the main line

Code: Select all

int main(int argc, char* argv[])
{
	try
	{
		//************************************************
		// Ogre Initialization
		//************************************************
		// create the root
		Ogre::Root *root = new Ogre::Root();

		// configure the renderer
		root->showConfigDialog();
		// init root
		root->initialise(false);
		// create the main window
		renderWindow = root->createRenderWindow("Main",640,480,false);
		// create a scene
		Ogre::SceneManager *sceneMan = root->getSceneManager(Ogre::ST_GENERIC);
		// createa a camera		
		Ogre::Camera *cam = sceneMan->createCamera("mainCam");
		// create a view port		
		Ogre::Viewport *vp = renderWindow->addViewport(cam);

		//************************************************
		// Load a font
		//************************************************
		// get the resource manager
		Ogre::ResourceGroupManager &resgroup = Ogre::ResourceGroupManager::getSingleton();
		// tell it to look at this location
		resgroup.addResourceLocation("<.ttf folder>", "FileSystem");
		// get the font manager
		Ogre::FontManager &fman = Ogre::FontManager::getSingleton();
		// create a font resource
		Ogre::ResourcePtr resource = fman.create("Arial",Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
		// set as truetype
		resource->setParameter("type","truetype");
		// set the .ttf file name
		resource->setParameter("source","<somefont.ttf>");
		// set the size
		resource->setParameter("size","16");
		// set the dpi
		resource->setParameter("resolution","96");
		// load the ttf
		resource->load();
		
		//************************************************
		// Create the text area
		//************************************************
		// adapted from http://www.ogre3d.org/wiki/index.php/Creating_Overlays_via_Code
		// credit to DWORD
		Ogre::OverlayManager& overlayManager = Ogre::OverlayManager::getSingleton();

		// Create a panel
		Ogre::OverlayContainer* panel = static_cast<Ogre::OverlayContainer*>(
			overlayManager.createOverlayElement("Panel", "PanelName"));
		panel->setMetricsMode(Ogre::GMM_PIXELS);
		panel->setPosition(10, 10);
		panel->setDimensions(100, 100);
		
		// Create a text area
		Ogre::TextAreaOverlayElement* textArea = static_cast<Ogre::TextAreaOverlayElement*>(
			overlayManager.createOverlayElement("TextArea", "TextAreaName"));
		textArea->setMetricsMode(Ogre::GMM_PIXELS);
		textArea->setPosition(0, 0);
		textArea->setDimensions(100, 100);
		textArea->setCharHeight(16);
		// set the font name to the font resource that you just created.
		textArea->setFontName("Arial");
		// say something
		textArea->setCaption("Hello, World!");
		
		// Add the text area to the panel
		panel->addChild(textArea);
		// Create an overlay, and add the panel
		Ogre::Overlay* overlay = overlayManager.create("OverlayName");
		overlay->add2D(panel);
		// Show the overlay
		overlay->show();

		//************************************************
		// Set the frame listener and start rendering
		//************************************************
		// add the simple frame listener
		FrameListener listener;
		root->addFrameListener(&listener);
		
		// start rendering
		root->startRendering();

	} catch(Ogre::Exception& e ) {
        printf("An exception has occured: %s\n",
                e.getFullDescription().c_str());
    }
	return 0;
}
hope it's of some use. credit to DWORD for the text area code. and sorry for any typo here.
User avatar
:wumpus:
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 3067
Joined: Tue Feb 10, 2004 12:53 pm
Location: The Netherlands
x 1

Post by :wumpus: »

Please wiki this, might be useful for other people and on the forum it could get lost.
davidotcom
Gnoblar
Posts: 18
Joined: Tue Oct 23, 2007 12:05 am

Re: Ogre Hello World without example app

Post by davidotcom »

Just a quick fix on the code. Instead of...
Ogre::SceneManager *sceneMan = root->getSceneManager(Ogre::ST_GENERIC);
Use...
Ogre::SceneManager *sceneMan = root->createSceneManager(Ogre::ST_GENERIC);
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: Ogre Hello World without example app

Post by xavier »

Wow...talk about necromancing...I think 4 years is the oldest I've seen dug up yet. ;)

For something like this, I would recommend just changing the article text in the Wiki directly; it will get lost in here.
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
NIKI_MC
Halfling
Posts: 62
Joined: Fri Jun 25, 2010 3:15 pm
Location: Macedonia

Re: Ogre Hello World without example app

Post by NIKI_MC »

is it working on ogre 1.7.1 ,because I found this useful but dont have instaled ogre on this pc