lua_FontGlyph.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_FontGlyph.h"
  4. #include "Base.h"
  5. #include "Bundle.h"
  6. #include "FileSystem.h"
  7. #include "Font.h"
  8. #include "Game.h"
  9. #include "Ref.h"
  10. #include "lua_FontJustify.h"
  11. #include "lua_FontStyle.h"
  12. namespace gameplay
  13. {
  14. void luaRegister_FontGlyph()
  15. {
  16. ScriptController* sc = ScriptController::getInstance();
  17. const luaL_Reg lua_members[] =
  18. {
  19. {"code", lua_FontGlyph_code},
  20. {"uvs", lua_FontGlyph_uvs},
  21. {"width", lua_FontGlyph_width},
  22. {NULL, NULL}
  23. };
  24. const luaL_Reg* lua_statics = NULL;
  25. std::vector<std::string> scopePath;
  26. scopePath.push_back("Font");
  27. sc->registerClass("FontGlyph", lua_members, lua_FontGlyph__init, lua_FontGlyph__gc, lua_statics, scopePath);
  28. }
  29. static Font::Glyph* getInstance(lua_State* state)
  30. {
  31. void* userdata = luaL_checkudata(state, 1, "FontGlyph");
  32. luaL_argcheck(state, userdata != NULL, 1, "'FontGlyph' expected.");
  33. return (Font::Glyph*)((ScriptController::LuaObject*)userdata)->instance;
  34. }
  35. int lua_FontGlyph__gc(lua_State* state)
  36. {
  37. // Get the number of parameters.
  38. int paramCount = lua_gettop(state);
  39. // Attempt to match the parameters to a valid binding.
  40. switch (paramCount)
  41. {
  42. case 1:
  43. {
  44. if ((lua_type(state, 1) == LUA_TUSERDATA))
  45. {
  46. void* userdata = luaL_checkudata(state, 1, "FontGlyph");
  47. luaL_argcheck(state, userdata != NULL, 1, "'FontGlyph' expected.");
  48. ScriptController::LuaObject* object = (ScriptController::LuaObject*)userdata;
  49. if (object->owns)
  50. {
  51. Font::Glyph* instance = (Font::Glyph*)object->instance;
  52. SAFE_DELETE(instance);
  53. }
  54. return 0;
  55. }
  56. else
  57. {
  58. lua_pushstring(state, "lua_FontGlyph__gc - Failed to match the given parameters to a valid function signature.");
  59. lua_error(state);
  60. }
  61. break;
  62. }
  63. default:
  64. {
  65. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  66. lua_error(state);
  67. break;
  68. }
  69. }
  70. return 0;
  71. }
  72. int lua_FontGlyph__init(lua_State* state)
  73. {
  74. // Get the number of parameters.
  75. int paramCount = lua_gettop(state);
  76. // Attempt to match the parameters to a valid binding.
  77. switch (paramCount)
  78. {
  79. case 0:
  80. {
  81. void* returnPtr = (void*)new Font::Glyph();
  82. if (returnPtr)
  83. {
  84. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  85. object->instance = returnPtr;
  86. object->owns = true;
  87. luaL_getmetatable(state, "FontGlyph");
  88. lua_setmetatable(state, -2);
  89. }
  90. else
  91. {
  92. lua_pushnil(state);
  93. }
  94. return 1;
  95. break;
  96. }
  97. default:
  98. {
  99. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  100. lua_error(state);
  101. break;
  102. }
  103. }
  104. return 0;
  105. }
  106. int lua_FontGlyph_code(lua_State* state)
  107. {
  108. // Validate the number of parameters.
  109. if (lua_gettop(state) > 2)
  110. {
  111. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  112. lua_error(state);
  113. }
  114. Font::Glyph* instance = getInstance(state);
  115. if (lua_gettop(state) == 2)
  116. {
  117. // Get parameter 2 off the stack.
  118. unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 2);
  119. instance->code = param2;
  120. return 0;
  121. }
  122. else
  123. {
  124. unsigned int result = instance->code;
  125. // Push the return value onto the stack.
  126. lua_pushunsigned(state, result);
  127. return 1;
  128. }
  129. }
  130. int lua_FontGlyph_uvs(lua_State* state)
  131. {
  132. // Validate the number of parameters.
  133. if (lua_gettop(state) > 2)
  134. {
  135. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  136. lua_error(state);
  137. }
  138. Font::Glyph* instance = getInstance(state);
  139. if (lua_gettop(state) == 2)
  140. {
  141. // Get parameter 2 off the stack.
  142. float* param2 = ScriptController::getInstance()->getFloatPointer(2);
  143. memcpy(instance->uvs, param2, sizeof(float) * 4);
  144. return 0;
  145. }
  146. else
  147. {
  148. float* result = instance->uvs;
  149. // Push the return value onto the stack.
  150. lua_pushlightuserdata(state, result);
  151. return 1;
  152. }
  153. }
  154. int lua_FontGlyph_width(lua_State* state)
  155. {
  156. // Validate the number of parameters.
  157. if (lua_gettop(state) > 2)
  158. {
  159. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  160. lua_error(state);
  161. }
  162. Font::Glyph* instance = getInstance(state);
  163. if (lua_gettop(state) == 2)
  164. {
  165. // Get parameter 2 off the stack.
  166. unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 2);
  167. instance->width = param2;
  168. return 0;
  169. }
  170. else
  171. {
  172. unsigned int result = instance->width;
  173. // Push the return value onto the stack.
  174. lua_pushunsigned(state, result);
  175. return 1;
  176. }
  177. }
  178. }