Setting up your project in Toyvox. - mrpedrobraga/Toyvox GitHub Wiki

Setting up!

(!) This page assumes you have basic C++ knowledge.

First, your main file should include "tvxcore.h" to access all of Toyvox's features! Second, a recommended way of organizing your code is using header files. In the end, it's up to you, but organizing the declarations is important to speed up the workflow.

All the types in tvxcore are inside the 'tvx' namespace. You can use tvx::, or using namespace tvx; at the top of your project.

You can have two files, a header main.h and a body main.cpp or one header for each stage. It's all up to you, really, but keep in mind that there's an order to initialize our modules.

For simplicity, here I'm gonna pack all together.

  1. Create a Game: Game myGame = Game();

  2. Create a init() and call it on your main(): You can set your game title using set_title(string title) and the screen resolution using set_resolution(ivec2 resolution). Notice that the type ivec2 is part of OpenGL Mathematics, so you should include it (include <glm/glm.hpp>). OpenGL Mathematics already comes with Toyvox, as a submodule.

At this point, your code should look like this:

Note exactly like this, but similar.

#include "tvxcore.h"
#include <glm/glm.hpp>

Game myGame = Game();

void init()
{
  myGame.set_title("My Wonderful Game");
  myGame.set_resolution(glm::ivec2(640, 360));
}

int main()
{
init();
return 0;
}
  1. Let's create our first scene: A scene is where we can have objects and systems. Objects in Toyvox Engine, aren't 'real' in the OO sense. An entity is simply an ID, that you can add components to. The components are just data. For example, Toyvox comes by default with the TypeTag component, that holds a single string value.

However to acess entities and components on a scene, we first have to create handlers for them.

Scene scene1 = Scene(strdup("My Scene"));	
ComponentHandler componentHandler = ComponentHandler();
EntityHandler entityHandler = EntityHandler();

We create a scene, a component handler and an entity handler! Now, we need to aggregate them to scene1!

//Register all handlers to scene1
scene1.add_handler( &componentHandler );
scene1.add_handler( &entityHandler );

In init(), add the handlers to the scene using add_handler( handler* ). Note the '&' adressof operator: the function add_handler requires a pointer!

Each scene can only have one component handler and one entity handler. Also, one entity is stuck on an entityHandler forever. You can, however, share handlers among scenes!

You can move an object between scenes by creating an identical object on the next scene.

3.5. Adding something to happen when the scene is loaded (it's really not necessary, tho):

scene1.on_load = &testFunc;

Do that inside init() as well. I'm assigining a function pointer to on_load. Here's the declaration of testFunc():

void testFunc() {
   cout << "Worked!" << endl;
}

Of course, you can also use lambda expressions to shorten the code and not have functions lingering around. Keep in mind that you can change the "scene functions" at any time!

scene1.on_load = [](){
  cout << "Worked!" << endl;
}
  1. Setting my scene as the current one:

Let's make this scene, scene1, the first scene of our game.

myGame.set_current_scene(&scene1); //Note the '&'; pass a scene pointer!

Note that this process will automatically call the given function's on_load() function! In fact, if you compile right now you should see "Worked!" on the output.

  1. Creating a new Component Type:

Let's say we have a component struct. For simplicity, I'm going to be using the default TypeTag component that Toyvox provides, but you can use any struct.

First, create a ComponentSet for this new Component Type:

ComponentSet<TypeTag> TypeTags = ComponentSet<TypeTag>();

Now, register this new component set to the component handler (on init()).

componentHandler.add("Type Tag", TypeTags);

You can choose any name for your component type.

  1. Now let's create some entities, just to test if we did everything correctly:

Go to main() after we called init().

cout << myGame.get_current_scene().get_title() << endl;

This will print the current scene's title. Nice.

How do I create objects? How do I set their values?

To create a new object use:

EntityUID ent1 = testGame.get_current_scene().create_entity();

create_entity() creates a new entity for us and returns its id so we can do stuff with it. Like, for example, set properties. If we want to set its type tag, we can just do that directly on our set.

TypeTags.set(ent1, TypeTag("Player"));

One thing about this method is that we're calling it on TypeTags, which is inside componentHandler which is inside scene1. If we want to set a component of an entity on the current scene, giving its id uid, we do the following:

myGame.get_current_scene().componentHandler->get_c("Type Tag")->set(uid, value);
  1. Let's print the value we just assigned and have a nice check if everything's alright!:
cout << TypeTags.get(ent1).get_value() << endl;
  1. Done! Now you get a grasp of how Toyvox works. Kinda.
⚠️ **GitHub.com Fallback** ⚠️