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!" in the top-left corner of the screen.
TODO: This tutorial is pending developer review.
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;
};
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(NLMISC::toString("font.pfb"));
if (!m_TextContext)
{
nlerror("Failed to create text renderer");
return;
}
m_TextContext->setFontSize(24);
// ...
}
Here, the createTextContext() function takes a single parameter, which is the font file to use for rendering text. You should replace "font.pfb" with the path to your desired font file. The setFontSize() method sets the font size for the text renderer.
In the destructor, release the text renderer.
CMyGame::~CMyGame()
{
// Release resources and delete the text renderer
m_TextContext->release();
// ...
}
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.
Compile and run your project. You should see the "Hello, World!" message displayed in the center of the screen.
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>
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();
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(ucstring("My Game"));
// Initialize the text renderer
m_TextContext = m_Driver->createTextContext(NLMISC::toString("font.pfb"));
if (!m_TextContext)
{
nlerror("Failed to create text renderer");
return;
}
m_TextContext->setFontSize(24);
}
// Release game resources
CMyGame::~CMyGame()
{
// Release resources and delete the text renderer
m_TextContext->release();
// Release resources and delete 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;
}