hello_world - ryzom/ryzomcore GitHub Wiki


title: Create a Hello World sample application description: An introduction to NeL programming published: true date: 2026-02-15T10:21:52.212Z tags: editor: markdown dateCreated: 2022-03-07T02:32:57.317Z

Now that we've set up our new personal project, and written the main game loop, we're ready to add a NeL 3D text context and write "Hello, World!" on the screen.

Add necessary headers and member variables

First, open the main.cpp file in your project folder and include the necessary headers:

#include <nel/3d/u_text_context.h>

Then, add a member variable for the text renderer to the game class.

class CMyGame : public IEventListener
{
public:
  // ...
private:
  // Graphics driver
  NL3D::UDriver *m_Driver;
  NL3D::UTextContext *m_TextContext;
  bool m_CloseWindow;
};

Initialize the text renderer

In the constructor method, initialize the text renderer using the createTextContext() method of the NL3D::UDriver class.

CMyGame::CMyGame()
    : m_CloseWindow(false)
{
  // ...

  // Initialize the text renderer
  m_TextContext = m_Driver->createTextContext("n019003l.pfb");
  if (!m_TextContext)
  {
    nlerror("Failed to create text renderer");
    return;
  }
  m_TextContext->setFontSize(24);

  // ...
}

The createTextContext() function takes the path to a font file. The file n019003l.pfb is a Nimbus Sans font included in the Ryzom Core graphics repository under graphics/fonts/. You can use any .pfb or .ttf font file. The font file must be findable by the executable at runtime - either place it in the working directory, or use CPath::addSearchPath() to register the directory containing it. The setFontSize() method sets the font size in pixels.

Release the text renderer

In the destructor, delete the text renderer before releasing the driver.

CMyGame::~CMyGame()
{
  // Delete the text renderer
  m_Driver->deleteTextContext(m_TextContext);

  // ...
}

Render the text

In the run() method, update the text renderer and display the "Hello, World!" message on the screen.

void CMyGame::run()
{
  // Main game loop
  while (m_Driver->isActive() && !m_CloseWindow)
  {
    // ...

    // Clear the screen
    m_Driver->clearBuffers(NLMISC::CRGBA(0, 0, 128));

    // Update the text renderer
    m_TextContext->setHotSpot(NL3D::UTextContext::MiddleMiddle);
    m_TextContext->setColor(NLMISC::CRGBA(255, 255, 255));
    m_TextContext->printfAt(0.5f, 0.5f, "Hello, World!");

    // Swap buffers
    m_Driver->swapBuffers();
  }
}

Here, the setHotSpot() method defines the anchor point for the text, and the setColor() method sets the color of the text. The printfAt() method renders the text on the screen at the specified x and y coordinates. In this case, the coordinates are (0.5, 0.5), which corresponds to the center of the screen.

Test the application

Compile and run your project. You should see the "Hello, World!" message displayed in the center of the screen.

Complete source code

Here is the complete source code for the tutorial project:

#include <nel/misc/types_nl.h>
#include <nel/misc/app_context.h>
#include <nel/misc/event_listener.h>
#include <nel/misc/debug.h>
#include <nel/3d/u_driver.h>
#include <nel/3d/u_text_context.h>

using namespace std;
using namespace NLMISC;

// Main game class
class CMyGame : public IEventListener
{
public:
	CMyGame();
	~CMyGame();
	void run();

	virtual void operator()(const CEvent &event) NL_OVERRIDE;

private:
	// Graphics driver
	NL3D::UDriver *m_Driver;
	NL3D::UTextContext *m_TextContext;
	bool m_CloseWindow;
};

// Initialize game resources
CMyGame::CMyGame()
	: m_CloseWindow(false)
{
	// Create the graphics driver
	m_Driver = NL3D::UDriver::createDriver(0, NL3D::UDriver::OpenGl3);
	if (!m_Driver)
	{
		nlerror("Failed to create driver");
		return;
	}

	// Add event listeners
	m_Driver->EventServer.addListener(EventCloseWindowId, this);

	// Initialize the window
	m_Driver->setDisplay(NL3D::UDriver::CMode(800, 600, 32));

	// Set the window title
	m_Driver->setWindowTitle("My Game");

	// Initialize the text renderer
	m_TextContext = m_Driver->createTextContext("n019003l.pfb");
	if (!m_TextContext)
	{
		nlerror("Failed to create text renderer");
		return;
	}
	m_TextContext->setFontSize(24);
}

// Release game resources
CMyGame::~CMyGame()
{
	// Delete the text renderer
	m_Driver->deleteTextContext(m_TextContext);

	// Release the driver
	m_Driver->release();
	delete m_Driver;
}

// Main loop
void CMyGame::run()
{
	// Main game loop
	while (m_Driver->isActive() && !m_CloseWindow)
	{
		// Update window messages
		m_Driver->EventServer.pump();

		// Clear the screen
		m_Driver->clearBuffers(NLMISC::CRGBA(0, 0, 128));

		// Update the text renderer
		m_TextContext->setHotSpot(NL3D::UTextContext::MiddleMiddle);
		m_TextContext->setColor(NLMISC::CRGBA(255, 255, 255));
		m_TextContext->printfAt(0.5f, 0.5f, "Hello, World!");

		// Swap buffers
		m_Driver->swapBuffers();
	}
}

// Handle events
void CMyGame::operator()(const CEvent &event)
{
	// Handle events
	if (event == EventCloseWindowId)
	{
		m_CloseWindow = true;
	}
}

// Main function
int main(int argc, char *argv[])
{
	// Create the application context
	CApplicationContext applicationContext;

	// Run the game
	CMyGame myGame;
	myGame.run();

	return EXIT_SUCCESS;
}

What's next

⚠️ **GitHub.com Fallback** ⚠️