Skip to content

Optimization

Fosowl edited this page May 30, 2020 · 2 revisions

How to use the function in an efficient way and reduce lag.

CPU

You can use starset_entities_get_propreties(...) to optimize your code in term of speed.

Exemple

    entities_t *player = starset_entities_get_propreties(entities, "player"); // get the pointer of the "player" element of the chain list, this will allow us to reduce the number of chain list element iteration in the next function.

    starset_add_animation(player, "player", "torch:static", v_2u{95, 80});
    starset_add_animation(player, "player", "pistol:static", v_2u{95, 80});
    starset_add_animation(player, "player", "pistol:move", v_2u{95, 80});
    starset_add_animation(player, "player", "pistol:attack", v_2u{95, 80});
    starset_add_animation(player, "player", "pistol:reload", v_2u{95, 80});
    starset_add_animation(player, "player", "rifle:static", v_2u{95, 80});
    starset_add_animation(player, "player", "rifle:move", v_2u{95, 80});
    starset_add_animation(player, "player", "rifle:attack", v_2u{98, 80});
    starset_add_animation(player, "player", "rifle:reload", v_2u{95, 80});
    starset_add_animation(player, "player", "knife:static", v_2u{95, 80});
    starset_add_animation(player, "player", "knife:move", v_2u{95, 80});
    starset_add_animation(player, "player", "knife:attack", v_2u{95, 95});

In the exemple above we limit the number of iteration by giving the "player" pointer to every starset_add_animation function, since those function are looping on the entities chain list every time we call them it is better to first get the pointer by using starset_entities_get_propreties so we only have to iterate one time for one entitie.

Memory

You can use sprite prefab to optimize your code in term of memory usage.

Exemple

sfSprite *prefab = starset_create_prefab("./assets/zombie.png");

entities_t *entities_list = starset_entities_add_from_prefab(NULL, prefab, "zombie:1", false);
entities_list = starset_entities_add_from_prefab(entities_list, prefab, "zombie:1", false);
entities_list = starset_entities_add_from_prefab(entities_list, prefab, "zombie:2", false);
entities_list = starset_entities_add_from_prefab(entities_list, prefab, "zombie:3", false);
entities_list = starset_entities_add_from_prefab(entities_list, prefab, "zombie:4", false);
entities_list = starset_entities_add_from_prefab(entities_list, prefab, "zombie:5", false);
// etc..

In the exemple above we use starset_create_prefab(...) to avoid creating a texture every time we add an entities that use the same texture as the other one.