LCOV - code coverage report
Current view: top level - src - api_engine.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 3.4 % 87 3
Test Date: 2026-04-03 02:26:39 Functions: 6.2 % 16 1

            Line data    Source code
       1              : /**
       2              :  * @file api_engine.cpp
       3              :  * @brief Krig Game Engine Lua Scripting API - Game (Engine)
       4              :  *
       5              :  * Game (engine) API functionality.
       6              :  */
       7              : #include "api.h"
       8              : #include "api_engine.h"
       9              : #include "api_object.h"
      10              : 
      11              : #if DOXYGEN_ONLY
      12              : /**
      13              :  * Initiate the Game/Engine shutdown process. This command will kick off clean-up
      14              :  * operations and will end with terminating the game.
      15              :  * @return n/a
      16              :  */
      17              : void shutdown();
      18              : #endif
      19            0 : static int shutdown(lua_State *L) {
      20            0 :   g_KRIG_ENGINE.shutdown();
      21            0 :   return 0;
      22              : }
      23              : 
      24              : #if DOXYGEN_ONLY
      25              : /**
      26              :  * Query the engine for the last calculated value of frames per second (FPS).
      27              :  * Frames per second is calculated each iteration of the game loop.
      28              :  * @return float frames per second
      29              :  */
      30              : float get_fps();
      31              : #endif
      32            0 : static int get_fps(lua_State *L) { 
      33            0 :   lua_pushnumber(L, g_KRIG_ENGINE.getFps());
      34            0 :   return 1;
      35              : }
      36              : 
      37              : #if DOXYGEN_ONLY
      38              : /**
      39              :  * Display 2D text to the screen.
      40              :  * @param string text.
      41              :  * @param float x coordinate.
      42              :  * @param float y coordinate.
      43              :  * @return n/a
      44              :  */
      45              : void render_text(string, float, float);
      46              : #endif
      47            0 : static int render_text(lua_State *L) {
      48            0 :   const char *s = lua_tostring(L, 1);
      49            0 :   float x = lua_tonumber(L, 2);
      50            0 :   float y = lua_tonumber(L, 3);
      51              : 
      52            0 :   g_KRIG_ENGINE.renderText(s, x, y);
      53              : 
      54            0 :   return 0;
      55              : }
      56              : 
      57              : #if DOXYGEN_ONLY
      58              : /**
      59              :  * Display 3D text to the screen.
      60              :  * @param string text.
      61              :  * @param float x coordinate.
      62              :  * @param float y coordinate.
      63              :  * @param float z coordinate.
      64              :  * @param float scale x.
      65              :  * @param float scale y.
      66              :  * @return n/a
      67              :  */
      68              : void display_text(string, float, float, float, float, float);
      69              : #endif
      70            0 : static int display_text(lua_State *L) {
      71            0 :   const char *s = lua_tostring(L, 1);
      72            0 :   char * t = (char*)s;
      73            0 :   float x = lua_tonumber(L, 2);
      74            0 :   float y = lua_tonumber(L, 3);
      75            0 :   float z = lua_tonumber(L, 4);
      76            0 :   float sx = lua_tonumber(L, 5);
      77            0 :   float sy = lua_tonumber(L, 6);
      78              : 
      79            0 :   TextGameObject::displayText(t, x, y, z, sx, sy);
      80              : 
      81            0 :   return 0;
      82              : }
      83              : 
      84              : #if DOXYGEN_ONLY
      85              : /**
      86              :  * Query the engine to determine if the specified key has been pressed this game cycle.
      87              :  * @param int - integer representation of the key
      88              :  * @return bool
      89              :  */
      90              : bool test_key_pressed(int);
      91              : #endif
      92            0 : static int test_key_pressed(lua_State *L) {
      93            0 :   int key = (int)lua_tonumber(L, 1);
      94            0 :   int result = (int)(g_KRIG_ENGINE.getKeyState()->testKeyPressed(key));
      95            0 :   lua_pushnumber(L, result);
      96              : 
      97            0 :   return 1;
      98              : }
      99              : 
     100              : #if DOXYGEN_ONLY
     101              : /**
     102              :  * Query the engine to determine if the specified key has been released this game cycle.
     103              :  * @param int - integer representation of the key
     104              :  * @return bool
     105              :  */
     106              : bool test_key_released(int);
     107              : #endif
     108            0 : static int test_key_released(lua_State *L) {
     109            0 :   int key = (int)lua_tonumber(L, 1);
     110            0 :   int result = (int)(g_KRIG_ENGINE.getKeyState()->testKeyReleased(key));
     111            0 :   lua_pushnumber(L, result);
     112            0 :   return 1;
     113              : }
     114              : 
     115              : #if DOXYGEN_ONLY
     116              : /**
     117              :  * Query the engine to determine if the specified key has been pressed this game cycle.
     118              :  * @param int - integer representation of the special key
     119              :  * @return bool
     120              :  */
     121              : bool test_special_key_pressed(int);
     122              : #endif
     123            0 : static int test_special_key_pressed(lua_State *L) {
     124            0 :   int key = (int)lua_tonumber(L, 1);
     125            0 :   int result = (int)(g_KRIG_ENGINE.getSpecialKeyState()->testKeyPressed(key));
     126            0 :   lua_pushnumber(L, result);
     127            0 :   return 1;
     128              : }
     129              : 
     130              : #if DOXYGEN_ONLY
     131              : /**
     132              :  * Query the engine to determine if the specified key has been released this game cycle.
     133              :  * @param int - integer representation of the special key
     134              :  * @return bool
     135              :  */
     136              : bool test_special_key_release(int);
     137              : #endif
     138            0 : static int test_special_key_released(lua_State *L) {
     139            0 :   int key = (int)lua_tonumber(L, 1);
     140            0 :   int result = (int)(g_KRIG_ENGINE.getSpecialKeyState()->testKeyReleased(key));
     141            0 :   lua_pushnumber(L, result);
     142            0 :   return 1;
     143              : }
     144              : 
     145              : #if DOXYGEN_ONLY
     146              : /**
     147              :  * Query the engine for the state of the compiler's DEBUG flag.
     148              :  * @return bool
     149              :  */
     150              : bool test_debug_enabled();
     151              : #endif
     152            0 : static int test_debug_enabled(lua_State *L) {
     153            0 :   lua_pushnumber(L, DEBUG);
     154            0 :   return 1;
     155              : }
     156              : 
     157              : #if DOXYGEN_ONLY
     158              : /**
     159              :  * Query the engine to determine the mouse's last known coordinates.
     160              :  * @return Vertex2 - x, y coordinate of the mouse.
     161              :  */
     162              : Vertex2 get_mouse_coordinates();
     163              : #endif
     164            0 : static int get_mouse_coordinates(lua_State *L) {
     165            0 :   float x = g_KRIG_ENGINE.getMouseX();
     166            0 :   float y = g_KRIG_ENGINE.getMouseY();
     167              : 
     168            0 :   lua_newtable(L);
     169              : 
     170            0 :   lua_pushnumber(L, 1);
     171            0 :   lua_pushnumber(L, x);
     172            0 :   lua_rawset(L, -3);
     173              : 
     174            0 :   lua_pushnumber(L, 2);
     175            0 :   lua_pushnumber(L, y);
     176            0 :   lua_rawset(L, -3);
     177            0 :   return 1;
     178              : }
     179              : 
     180              : #if DOXYGEN_ONLY
     181              : /**
     182              :  * Play the sound in the specified ogg file.
     183              :  * @param GameObjectReference
     184              :  * @param string - path to ogg file to play.
     185              :  * @return n/a
     186              :  */
     187              : void play_sound(GameObjectReference, string);
     188              : #endif
     189            0 : static int play_sound(lua_State *L) {
     190            0 :   luaL_checktype(L, 1, LUA_TTABLE);
     191            0 :   Object *object = static_cast<Object*>(loadObject(L, 1));
     192              : 
     193            0 :   const char *s = lua_tostring(L, 2);
     194            0 :   std::string sound = std::string(s);
     195              : 
     196            0 :   SoundFX *soundFx = g_KRIG_ENGINE.getSoundFxClass();
     197              : 
     198            0 :   if (soundFx != NULL)
     199            0 :     soundFx->PlaySFX(sound);
     200              : 
     201              :   return 0;
     202            0 : }
     203              : 
     204              : #if DOXYGEN_ONLY
     205              : /**
     206              :  * Get a game object reference to the active camera object.
     207              :  * @return GameObjectReference
     208              :  */
     209              : GameObjectReference get_camera();
     210              : #endif
     211            0 : static int get_camera(lua_State *L) {
     212            0 :   returnObject(L, g_KRIG_ENGINE.getCurrentLevel()->getCamera());
     213            0 :   return 1;
     214              : }
     215              : 
     216              : #if DOXYGEN_ONLY
     217              : /**
     218              :  * Get a game object reference to the player object.
     219              :  * @return GameObjectReference
     220              :  */
     221              : GameObjectReference get_player();
     222              : #endif
     223            0 : static int get_player(lua_State *L) {
     224            0 :   returnObject(L, g_KRIG_ENGINE.getCurrentLevel()->getPlayer());
     225            0 :   return 1;
     226              : }
     227              : 
     228              : #if DOXYGEN_ONLY
     229              : /**
     230              :  * Fetch variable value from specified game object's script.
     231              :  * @param GameObjectReference
     232              :  * @return string - variable name.
     233              :  */
     234              : float get_script_value(GameObjectReference, string);
     235              : #endif
     236            0 : static int get_script_value(lua_State *L) {
     237            0 :   luaL_checktype(L, 1, LUA_TTABLE);
     238            0 :   Object *object = static_cast<Object*>(loadObject(L, 1));
     239            0 :   const char *s = lua_tostring(L, 2);
     240              : 
     241            0 :   lua_pushnumber(L, object->getScriptValue(s));
     242            0 :   return 1;
     243              : }
     244              : 
     245              : #if DOXYGEN_ONLY
     246              : /**
     247              :  * Set variable value in specified game object's script.
     248              :  * @param GameObjectReference
     249              :  * @param float value.
     250              :  * @return string - variable name.
     251              :  */
     252              : void set_script_value(GameObjectReference, string, float);
     253              : #endif
     254            0 : static int set_script_value(lua_State *L) {
     255            0 :   luaL_checktype(L, 1, LUA_TTABLE);
     256            0 :   Object *object = static_cast<Object*>(loadObject(L, 1));
     257            0 :   const char *s = lua_tostring(L, 2);
     258            0 :   float value = (int)lua_tonumber(L, 3);
     259              : 
     260            0 :   object->setScriptValue(s, value);
     261            0 :   return 0;
     262              : }
     263              : 
     264              : static const luaL_Reg krigEngineLib[] = {
     265              :   {"display_text", display_text},
     266              :   {"get_camera", get_camera},
     267              :   {"get_fps", get_fps},
     268              :   {"get_mouse_coordinates", get_mouse_coordinates},
     269              :   {"get_player", get_player},
     270              :   {"get_script_value", get_script_value},
     271              :   {"play_sound", play_sound},
     272              :   {"render_text", render_text},
     273              :   {"set_script_value", set_script_value},
     274              :   {"shutdown", shutdown},
     275              :   {"test_debug_enabled", test_debug_enabled},
     276              :   {"test_key_pressed", test_key_pressed},
     277              :   {"test_key_released", test_key_released},
     278              :   {"test_special_key_pressed", test_special_key_pressed},
     279              :   {"test_special_key_released", test_special_key_released},
     280              :   {NULL, NULL}
     281              : };
     282              : 
     283            9 : int luaopen_krigEngine (lua_State *L) {
     284            9 :   luaL_openlib(L, "krig", krigEngineLib, 0);
     285            9 :   return 1;
     286              : }
        

Generated by: LCOV version 2.4-0