Simple game engine with complete export to scripting language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

127 lines
2.4 KiB

  1. #include "ScriptSystem_Font.h"
  2. namespace BlueCore
  3. {
  4. //------------------------------------------------------------------------------
  5. static weak_ptr<FontManager> gFontManager;
  6. //------------------------------------------------------------------------------
  7. static SQInteger _font_releasehook(SQUserPointer p, SQInteger size)
  8. {
  9. Font *font = (Font *)p;
  10. if (font)
  11. font->removeReference();
  12. return 1;
  13. }
  14. //------------------------------------------------------------------------------
  15. static SQInteger _font_constructor(HSQUIRRELVM vm)
  16. {
  17. SQInteger argc = sq_gettop(vm);
  18. Font *font = 0;
  19. if (argc < 3)
  20. {
  21. if (gFontManager.valid())
  22. font = gFontManager->getDefaultFont();
  23. }
  24. else
  25. {
  26. const SQChar *name = 0;
  27. SQInteger size = 1;
  28. SQBool hinting = SQFalse;
  29. sq_getstring(vm, 2, &name);
  30. sq_getinteger(vm, 3, &size);
  31. if (argc > 3)
  32. sq_getbool(vm, 4, &hinting);
  33. if (gFontManager.valid())
  34. {
  35. font = gFontManager->loadFont(name, size, hinting == SQTrue);
  36. if (font)
  37. font->addReference();
  38. }
  39. }
  40. sq_setinstanceup(vm, 1, (void *)font );
  41. sq_setreleasehook(vm, 1, _font_releasehook);
  42. return 0;
  43. }
  44. //------------------------------------------------------------------------------
  45. static SQInteger _font_print(HSQUIRRELVM vm)
  46. {
  47. int argc = sq_gettop(vm);
  48. if (argc < 4)
  49. return 0;
  50. const Font *font = 0;
  51. sq_getinstanceup(vm, 1, ( void ** ) &font, 0);
  52. if (font)
  53. {
  54. SQFloat x, y;
  55. const char *text;
  56. SQInteger halign = 0, valign = 0;
  57. sq_getfloat(vm, 2, &x);
  58. sq_getfloat(vm, 3, &y);
  59. sq_getstring(vm, 4, &text);
  60. if (argc > 4)
  61. {
  62. sq_getinteger(vm, 5, &halign);
  63. }
  64. if (argc > 5)
  65. {
  66. sq_getinteger(vm, 6, &valign);
  67. }
  68. font->print( (float)x, (float)y, text, halign, valign);
  69. }
  70. return 0;
  71. }
  72. //------------------------------------------------------------------------------
  73. void setupScriptSystem_Font(ScriptSystem* scriptsystem, FontManager* fontmanager)
  74. {
  75. if (scriptsystem && fontmanager)
  76. {
  77. HSQUIRRELVM vm = scriptsystem->getVM();
  78. gFontManager = fontmanager;
  79. sq_pushroottable(vm);
  80. // push class
  81. sq_pushstring(vm, "Font", -1);
  82. if (SQ_SUCCEEDED(sq_newclass(vm, SQFalse) ) )
  83. {
  84. // register constructor
  85. sq_pushstring(vm, "constructor", -1);
  86. sq_newclosure(vm, _font_constructor, 0);
  87. sq_newslot(vm, -3, false);
  88. sq_pushstring(vm, "print", -1);
  89. sq_newclosure(vm, _font_print, 0);
  90. sq_newslot(vm, -3, false);
  91. // create class
  92. sq_newslot(vm, -3, false);
  93. }
  94. sq_poptop(vm);
  95. }
  96. }
  97. }