-
Notifications
You must be signed in to change notification settings - Fork 94
tutorial_nel_2
title: Tutorial NeL Part 2 - Displaying "Hello, World!" Text description: published: true date: 2023-03-18T06:30:04.497Z tags: editor: markdown dateCreated: 2023-03-18T02:03:07.327Z
In this part of the tutorial, you will learn how to display "Hello, World!" text on the screen using Ryzom Core and the NeL 3D library. You will create a simple text renderer, render the text, and position it on the screen.
Start by including the necessary headers in your main.cpp
file:
#include <nel/3d/u_text_context.h>
In the CMyGame
class, add a member variable to hold the text context:
class CMyGame : public IEventListener
{
public:
// ...
private:
// ...
NL3D::UTextContext *m_TextContext;
};
In the CMyGame
constructor, initialize the text context using the createTextContext()
method of the driver:
CMyGame::CMyGame()
: m_CloseWindow(false)
{
// ...
// Initialize the text context
m_TextContext = m_Driver->createTextContext("font_filename.ttf");
// Set text properties
m_TextContext->setFontSize(24);
m_TextContext->setColor(NLMISC::CRGBA(255, 255, 255));
// ...
}
Replace font_filename.ttf
with the path to a TTF font file in your project's resources.
In the CMyGame
destructor, release the text context:
CMyGame::~CMyGame()
{
// Release resources
m_TextContext->release();
m_Driver->release();
delete m_Driver;
}
In the run()
method of the CMyGame
class, render the "Hello, World!" text after clearing the screen and before swapping buffers:
void CMyGame::run()
{
// Main game loop
while (m_Driver->isActive() && !m_CloseWindow)
{
// ...
// Clear the screen
m_Driver->clearBuffers(NLMISC::CRGBA(0, 0, 128));
// Render the text
m_TextContext->setHotSpot(NL3D::UTextContext::TopLeft);
m_TextContext->printfAt(0.05f, 0.95f, "Hello, World!");
// Swap the buffers
m_Driver->swapBuffers();
}
}
This will display the "Hello, World!" text in the top-left corner of the screen.
By following this tutorial, you have learned how to display text on the screen using Ryzom Core and the NeL 3D library. This knowledge will be useful for creating in-game text elements such as menus, HUDs, and other user interfaces. In the next part of the tutorial, you will learn how to render basic 3D shapes.
This code initializes the NeL driver, sets up the display mode, creates a window, and runs a basic rendering loop that clears the screen and swaps buffers. The window will be closed when the user closes it.
In this tutorial, we will cover how to display "Hello, World!" text on the screen using the NeL engine. This will involve loading a font, creating a text context, and rendering the text on screen using the text context and the NeL driver.
Before starting this tutorial, make sure you have completed the following steps:
- Set up a NeL game project as described in the Creating a Blank NeL Game Project section.
- Initialize the NeL driver as described in the Initializing the NeL Driver section.
Here's the complete source code for displaying "Hello, World!" text in a NeL project:
#include <nel/misc/app_context.h>
#include <nel/misc/debug.h>
#include <nel/misc/path.h>
#include <nel/misc/file.h>
#include <nel/misc/config_file.h>
#include <nel/3d/u_driver.h>
#include <nel/3d/u_text_context.h>
#include <nel/3d/u_shape_bank.h>
using namespace std;
using namespace NLMISC;
using namespace NL3D;
// Main function
int main(int argc, char *argv[])
{
// Create the application context
CApplicationContext applicationContext;
// Initialize the driver
UDriver *driver = nullptr;
try
{
driver = UDriver::createDriver(0, nullptr, true);
if (!driver)
{
nlerror("Unable to create a NeL driver.");
return 1;
}
// Set up the display mode
UDriver::CMode mode;
mode.Width = 800;
mode.Height = 600;
mode.Depth = 32;
// Initialize the window
driver->setDisplay(mode, 32, false, "NeL Hello World");
}
catch (const Exception &e)
{
nlerror("NeL driver initialization error: %s", e.what());
return 1;
}
// Load a font and create a text context
UTextContext *textContext = driver->createTextContext("n019003l.pfb");
if (!textContext)
{
nlerror("Failed to create text context.");
delete driver;
return 1;
}
// Set the font size and color
textContext->setFontSize(24);
textContext->setColor(CRGBA(255, 255, 255));
// Main rendering loop
while (!driver->AsyncDownloader->isTerminated() && !driver->AsyncTextureUploader->isTerminated() && !driver->Exit)
{
// Clear the screen
driver->clearBuffers();
// Render the text on screen
textContext->printAt(driver, 0.5f, 0.5f, "Hello, World!");
// Swap buffers
driver->swapBuffers();
}
// Release resources
delete textContext;
delete driver;
return 0;
}
This code initializes the NeL driver, sets up the display mode, creates a window, loads a font, and creates a text context. The text context is used to render "Hello, World!" text on the screen within the main rendering loop. The window will be closed when the user closes it.
In this tutorial, we will show you how to display "Hello, World!" text on the screen in a NeL project. This involves loading a font, creating a text context, and rendering the text using the NeL driver.
Before starting this tutorial, make sure you have completed the following steps:
- Set up a NeL game project as described in the Creating a Blank NeL Game Project section.
- Initialize the NeL driver as described in the Initializing the NeL Driver section.
- Include necessary headers: Add the following headers to your source code to handle text rendering in NeL:
#include <nel/3d/u_text_context.h>
- **Load a font
Person:
- I received up to Prerequisites of the Hello world tutorial, please resume
ChatGPT:
- Before starting this tutorial, make sure you have completed the following steps:
- Set up a NeL game project as described in the Creating a Blank NeL Game Project section.
- Initialize the NeL driver as described in the Initializing the NeL Driver section.
- Include necessary headers: Add the following headers to your source code to handle text rendering in NeL:
#include <nel/3d/u_text_context.h>
-
Load a font: Load a font file (TTF or Type 1 format) from the filesystem using the
loadFont
method of theUTextContext
class:
std::string fontPath = "path/to/your/font.ttf";
uint fontSize = 18;
NL3D::UTextContext *textContext = nullptr;
textContext = driver->createTextContext(fontPath, fontSize);
- Check for font loading errors: If the font cannot be loaded, display an error message and exit the application:
if (!textContext)
{
nlerror("Unable to load font.");
delete driver;
return 1;
}
-
Set the text to display: Set the text to display using the
setText
method of theUTextContext
class:
textContext->setText("Hello, World!");
-
Set the text position and color: Specify the position and color of the text using the
setHotSpot
andsetColor
methods of theUTextContext
class:
textContext->setHotSpot(NL3D::UTextContext::BottomLeft);
textContext->setColor(NL3D::CRGBA(255, 255, 255));
-
Render the text: In your main rendering loop, call the
render
method of theUTextContext
class to draw the text on the screen:
textContext->render(driver, 10, 10);
Congratulations! You have successfully displayed "Hello, World!" text on the screen in your NeL project.
This tutorial will guide you through the process of displaying a "Hello, World!" message and rendering basic 3D shapes using NeL in your blank game project.
Before proceeding, make sure you have completed the steps in sections 7.1 through 7.4, ensuring that you have set up the project directory structure, created the main application source file, configured the build with CMake, and built your NeL game project.
To display a "Hello, World!" text message on the screen, you will need to use the NeL text rendering classes. Here's a step-by-step implementation:
- Include the required headers at the top of your main application source file:
#include <nel/misc/app_context.h>
#include <nel/misc/debug.h>
#include <nel/misc/path.h>
#include <nel/3d/u_driver.h>
#include <nel/3d/u_text_context.h>
- Add a new method called
displayHelloWorld()
in your main application class:
void displayHelloWorld()
{
// Get the text context
NL3D::UTextContext *textContext = Driver->createTextContext(NLMISC::CPath::lookup("font.packed"));
// Set the text color and font size
textContext->setColor(NLMISC::CRGBA(255, 255, 255));
textContext->setFontSize(18);
// Set the text position on the screen
textContext->setHotSpot(NL3D::UTextContext::BottomLeft);
textContext->setPos(0.05f, 0.05f);
// Display the text
textContext->printAt(0, "Hello, World!");
// Release the text context
Driver->deleteTextContext(textContext);
}
- Call the
displayHelloWorld()
method inside your main rendering loop, after clearing the screen and before swapping buffers:
Driver->clearBuffers(NLMISC::CRGBA(0, 0, 0, 255));
displayHelloWorld();
Driver->swapBuffers();
To render basic 3D shapes, you will need to create and manipulate 3D models using NeL classes. In this example, we will render a simple cube:
- Include the required headers at the top of your main application source file:
#include <nel/3d/u_shape.h>
#include <nel/3d/u_instance.h>
- Add a new method called
renderCube()
in your main application class:
void renderCube()
{
// Load the cube model
NL3D::UShape *cubeShape = Driver->createShape("cube.shape");
NL3D::UInstance cubeInstance = Driver->createInstance(cubeShape);
// Set the cube position and scale
cubeInstance.setPos(NLMISC::CVector(0.0f, 0.0f, 0.0f));
cubeInstance.setScale(NLMISC::CVector(1.0f, 1.0f, 1.0f));
// Render the cube
Driver->drawInstance(cubeInstance);
// Release the cube resources
Driver->deleteShape(cubeShape);
Driver->deleteInstance(cubeInstance);
}
- Call the
renderCube()
method inside your main rendering loop, after clearing the screen and before displaying the "Hello, World!" text and swapping buffers:
Driver->clearBuffers(NLMISC::CRGBA(0, 0, 0, 255));
renderCube();
displayHelloWorld();
Driver->swapBuffers();
With these changes, your NeL game project should now display a "Hello, World!" text message on the screen and render a simple 3D cube. You can expand on this code to display more text or render additional 3D shapes as needed.