LCOV - code coverage report
Current view: top level - src - api.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 32.7 % 113 37
Test Date: 2026-04-03 02:26:39 Functions: 30.0 % 10 3

            Line data    Source code
       1              : /**
       2              :  * @file api.cpp
       3              :  * @brief Krig Game Engine Lua Scripting API
       4              :  *
       5              :  * API calls are split into seven classes of functions. The classes are grouped
       6              :  * by the type of functionality they provide. For example, the
       7              :  * krig.object.set_position function is supported for a game objects, while
       8              :  * krig.level.add_object function is supported for game levels. The seven classes
       9              :  * of functions are: Game Object, Game Level, Text, Terrain, Camera, Vector Math,
      10              :  * and Game (Engine).
      11              :  */
      12              : #include "api.h"
      13              : #include "api_camera.h"
      14              : #include "api_engine.h"
      15              : #include "api_level.h"
      16              : #include "api_object.h"
      17              : #include "api_rotation.h"
      18              : #include "api_terrain.h"
      19              : #include "api_vector.h"
      20              : 
      21            0 : Vector loadVector(lua_State *L) {
      22            0 :   int index = 2;
      23            0 :   return loadVector(L, index);
      24              : }
      25              : 
      26            7 : Vector loadVector(lua_State *L, int &index) {
      27            7 :   Vector t;
      28              : 
      29            7 :   if (lua_istable(L, index)) {
      30              :     // x
      31            7 :     lua_pushnumber(L, 1);
      32            7 :     lua_gettable(L, index);
      33            7 :     t.x = (float)lua_tonumber(L, -1);
      34            7 :     lua_pop(L, 1);
      35              : 
      36              :     // y
      37            7 :     lua_pushnumber(L, 2);
      38            7 :     lua_gettable(L, index);
      39            7 :     t.y = (float)lua_tonumber(L, -1);
      40            7 :     lua_pop(L, 1);
      41              : 
      42              :     // z
      43            7 :     lua_pushnumber(L, 3);
      44            7 :     lua_gettable(L, index);
      45            7 :     t.z = (float)lua_tonumber(L, -1);
      46            7 :     lua_pop(L, 1);
      47              : 
      48            7 :     index++;
      49            7 :   }
      50              :   else {
      51            0 :     t.setVector(
      52            0 :       lua_tonumber(L, index),
      53            0 :       lua_tonumber(L, index + 1),
      54            0 :       lua_tonumber(L, index + 2)
      55              :     );
      56              : 
      57            0 :     index += 3;
      58              :   }
      59              : 
      60            7 :   return t;
      61              : }
      62              : 
      63            2 : void returnVector(lua_State *L, const Vector &t) {
      64            2 :   lua_newtable(L);
      65              : 
      66            2 :   lua_pushnumber(L, 1);
      67            2 :   lua_pushnumber(L, t.x);
      68            2 :   lua_rawset(L, -3);
      69              : 
      70            2 :   lua_pushnumber(L, 2);
      71            2 :   lua_pushnumber(L, t.y);
      72            2 :   lua_rawset(L, -3);
      73              : 
      74            2 :   lua_pushnumber(L, 3);
      75            2 :   lua_pushnumber(L, t.z);
      76            2 :   lua_rawset(L, -3);
      77            2 : }
      78              : 
      79            0 : void loadArray(lua_State *L, float array[], int len, int index) {
      80            0 :   if (lua_istable(L, index)) {
      81            0 :     for (int i = 0; i < len; ++i) {
      82              :       /*
      83              :       lua_pushnumber(L, (i + 1));
      84              :       lua_gettable(L, index);
      85              :       array[i] = (float)lua_tonumber(L, -1);
      86              :       lua_pop(L, 1);
      87              :       */
      88            0 :       array[i] = 1.0f;
      89            0 :     }
      90            0 :   }
      91            0 : }
      92              : 
      93            0 : void returnArray(lua_State *L, float array[], int len) {
      94            0 :   lua_newtable(L);
      95            0 :   for (int i = 0; i < len; ++i) {
      96            0 :     lua_pushnumber(L, (i + 1));
      97            0 :     lua_pushnumber(L, array[i]);
      98            0 :     lua_rawset(L, -3);
      99            0 :   }
     100            0 : }
     101              : 
     102            0 : Quaternion loadQuaternion(lua_State *L, const int &index) {
     103              :   float x, y, z, w;
     104              : 
     105            0 :   if (lua_istable(L, index)) {
     106              :     // x
     107            0 :     lua_pushnumber(L, 1);
     108            0 :     lua_gettable(L, index);
     109            0 :     x = (float)lua_tonumber(L, -1);
     110            0 :     lua_pop(L, 1);
     111              : 
     112              :     // y
     113            0 :     lua_pushnumber(L, 2);
     114            0 :     lua_gettable(L, index);
     115            0 :     y = (float)lua_tonumber(L, -1);
     116            0 :     lua_pop(L, 1);
     117              : 
     118              :     // z
     119            0 :     lua_pushnumber(L, 3);
     120            0 :     lua_gettable(L, index);
     121            0 :     z = (float)lua_tonumber(L, -1);
     122            0 :     lua_pop(L, 1);
     123              : 
     124              :     // w
     125            0 :     lua_pushnumber(L, 4);
     126            0 :     lua_gettable(L, index);
     127            0 :     w = (float)lua_tonumber(L, -1);
     128            0 :     lua_pop(L, 1);
     129            0 :   }
     130              : 
     131            0 :   return Quaternion(x, y, z, w);
     132              : }
     133              : 
     134            0 : void returnQuaternion(lua_State *L, const Quaternion &t) {
     135            0 :   lua_newtable(L);
     136              : 
     137            0 :   lua_pushnumber(L, 1);
     138            0 :   lua_pushnumber(L, t.getX());
     139            0 :   lua_rawset(L, -3);
     140              : 
     141            0 :   lua_pushnumber(L, 2);
     142            0 :   lua_pushnumber(L, t.getY());
     143            0 :   lua_rawset(L, -3);
     144              : 
     145            0 :   lua_pushnumber(L, 3);
     146            0 :   lua_pushnumber(L, t.getZ());
     147            0 :   lua_rawset(L, -3);
     148              : 
     149            0 :   lua_pushnumber(L, 4);
     150            0 :   lua_pushnumber(L, t.getW());
     151            0 :   lua_rawset(L, -3);
     152            0 : }
     153              : 
     154            0 : Object* loadObject(lua_State *L, const int &index) {
     155            0 :   int id = -1;
     156            0 :   if (lua_istable(L, index)) {
     157            0 :     lua_pushstring(L, "id");
     158            0 :     lua_gettable(L, index);
     159            0 :     id = lua_tonumber(L, -1);
     160            0 :     lua_pop(L, 1);
     161            0 :   }
     162            0 :   return g_KRIG_ENGINE.getCurrentLevel()->getObjectFromId(id);
     163              : }
     164              : 
     165            0 : void returnObject(lua_State *L, Object* object) {
     166            0 :   lua_newtable(L);
     167              : 
     168            0 :   lua_pushstring(L, "id");
     169            0 :   object != NULL ?
     170            0 :     lua_pushnumber(L, object->getGameLevelId()) :
     171            0 :     lua_pushnumber(L, -1);
     172            0 :   lua_rawset(L, -3);
     173            0 :   luaL_setfuncs(L, krigObjectLib, 0);
     174            0 : }
     175              : 
     176            9 : void luaopen_krigApi(lua_State *L) {
     177            9 :   luaopen_krigEngine(L);
     178            9 :   luaopen_krigLevel(L);
     179            9 :   luaopen_krigTerrain(L);
     180            9 :   luaopen_krigVector(L);
     181            9 :   luaopen_krigRotation(L);
     182            9 : }
        

Generated by: LCOV version 2.4-0