-
Notifications
You must be signed in to change notification settings - Fork 5
Logging and Debugging
When creating a game, it can be useful to visualize variables values on screen and log data to the console.
To log data to the console, just use the ns_LOG
macro.
It takes as many arguments as you want, and prefix the output with file name and line
number from where ns_LOG
was called.
For example, the following program will output : [3|main.cpp] Hello world 5 Vector(5, 1)
.
#include <NasNas/Core.hpp>
int main() {
ns_LOG("Hello world", 5, sf::Vector2i(5, 1));
return 0;
}
The Logger can print any type with stream operator overload (operator<<
).
To activate "debug mode", set ns::Settings::debug_mode
to true
.
When in debug mode, debug texts and global bounds of all drawables will be drawn.
A debug text is an object that lets you visualize a variable value changing in real time on screen.
To add a DebugText, use the ns::App::addDebugText
method.
Let's go back to our Game class, and add an integer member variable that we increment every frame.
Game::Game() {
// setting the correct config
ns::Config::debug = true;
// the easiest way to add a DebugText is by using the address of the variable
// the template type is the type of the variable
this->addDebugText<int>("frame count :", &this->frame_count, {0, 0});
// you can also pass a lambda function as first argument,
// which allows a lot more flexibility
// the template type is the type returned by the lambda function
this->addDebugText<int>("random int :", [](){ return std::rand()%10; }, {0, 20});
// by default, DebugText color is black. You can change its appearance
// using DebugTextInterface, the style will be applied to all DebugTexts
// created afterwards.
ns::DebugTextInterface::color = sf::Color::Magenta;
ns::DebugTextInterface::outline_thickness = 1;
ns::DebugTextInterface::outline_color = sf::Color::Red;
// you can also pass a sf::Color directly in addDebugText as last argument
this->addDebugText("I am a label only debug text !", {0, 30}, sf::Color::White);
}
void Game::onEvent(const sf::Event& event) {
// toggling debug mode when F1 key pressed
if (event.type == sf::Event::KeyPressed)
if (event.key.code == sf::Keyboard::F1)
ns::Settigs::debug_mode = ! ns::Settigs::debug_mode;
}
void Game::update(){
this->frame_count += 1;
}
After adding DebugTexts in the constructor, they will be automatically updated displayed on the screen every frame.
DebugTexts can be useful to read player position or the number of objects in real time for example.
They are meant to be used by the developer only, and not as UI for your game.
For more detailed information, please check the documentation