-
Notifications
You must be signed in to change notification settings - Fork 5
Getting Started
NasNas is built on top of SFML. If you are not familiar with SFML, I suggest you to read its documentation and tutorials first.
To create an application, you have to create a class derived from ns::App
.
To do this, create Game.hpp
under include
and Game.cpp
under src
.
Don't forget to add these files (and any other file you will create) to CMakeLists.txt.
Define a class named Game
, and make it inherit from ns::App
.
ns::App
class provides 2 virtual methods you have to implement in your own Game
class.
-
void onEvent(const sf::Event& event)
: this method is called everytime an event occurs. -
void update()
: this method is usually called at least one time every frame, game logic is usually written here.
Your Game.hpp
file should look like this:
#pragma once
#include <NasNas/Core.hpp>
class Game: public ns::App {
public:
Game();
void onEvent(const sf::Event& event) override ;
void update() override ;
};
and Game.cpp
:
#include "Game.hpp"
// Game constructor initializes ns::App with
// window title, window width and window height parameters
Game::Game() : ns::App("Title", {720, 480})
{}
// close the window when the X button is clicked
void Game::onEvent(const sf::Event& event) {
if (event.type == sf::Event::Closed)
getWindow().close();
}
// nothing to do in update for now
void Game::update() {}
ns::App
constructors can take multiple arguments, like resolution, scale, fps, ups.
Check the documentation for more details.
Now, you need to run the application. In main.cpp
, create an instance of Game
and call
its run()
method.
#include "Game.hpp"
int main() {
// constructing the game and running it
Game game;
game.run();
return 0;
}
The application can be configured from the main before creating it by creating a ns::AppConfig
.
Let's add some configurations :
#include "Game.hpp"
int main() {
// configure application window
ns::AppConfig config;
config.title = "Title";
config.resolution = {720, 480};
config.frame_rate = 60;
config.cursor_visble = false;
config.vertical_sync = true;
// don't forget to update the settings
ns::Settings::setConfig(config);
// construct the game and running it
Game game;
game.run();
return 0;
}
Now that the application is configured, we don't need to initialize ns::App
in Game
constructor.
// Game.cpp : Game constructor.
// ns::App is initialized automatically using ns::Settings data
Game::Game()
{}
See the documentation for more information about ns::AppConfig
.
NasNas often relies on smart pointer, which are useful to automatically delete objects when they are no more used. To avoid deleting already deleted objects and thus avoid crashes in your application, there are some precautions to take :
-
If a method name starts with
add
, it means the pointer passed as argument will be managed by the framework. It means you must not delete manually that pointer (addRaw
method tells the framework to not manage the pointer, the user is the owner) -
Some methods return a raw pointer. Don't do anything fancy with them like deleting or making them shared.