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.6KB

  1. #include "ScriptSystem_Image.h"
  2. #include "TextureImage.h"
  3. namespace BlueCore
  4. {
  5. //------------------------------------------------------------------------------
  6. static weak_ptr<TextureManager> gTextureManager;
  7. static weak_ptr<RenderDevice> gRenderDevice;
  8. //------------------------------------------------------------------------------
  9. static SQInteger _image_releasehook(SQUserPointer p, SQInteger size)
  10. {
  11. TextureImage *image = (TextureImage *)p;
  12. if (image)
  13. image->removeReference();
  14. return 1;
  15. }
  16. //------------------------------------------------------------------------------
  17. static SQInteger _image_constructor(HSQUIRRELVM vm)
  18. {
  19. SQInteger argc = sq_gettop (vm );
  20. if (argc < 6)
  21. return 0;
  22. TextureImage *image = 0;
  23. const SQChar *texturename = 0;
  24. SQFloat ax, ay, bx, by;
  25. sq_getstring(vm, 2, &texturename );
  26. sq_getfloat(vm, 3, &ax );
  27. sq_getfloat(vm, 4, &ay );
  28. sq_getfloat(vm, 5, &bx );
  29. sq_getfloat(vm, 6, &by );
  30. Texture *texture = gTextureManager->loadTexture(texturename, 0, 0);
  31. image = new TextureImage( gRenderDevice, texture, ax, ay, bx, by );
  32. image->addReference();
  33. sq_setinstanceup(vm, 1, (void *)image );
  34. sq_setreleasehook (vm, 1, _image_releasehook );
  35. return 0;
  36. }
  37. //------------------------------------------------------------------------------
  38. static SQInteger _image_draw(HSQUIRRELVM vm)
  39. {
  40. int argc = sq_gettop (vm );
  41. if (argc < 3)
  42. return 0;
  43. TextureImage *image = 0;
  44. sq_getinstanceup (vm, 1, ( void ** ) &image, 0);
  45. if (image )
  46. {
  47. SQFloat x, y, r = 0.0;
  48. SQInteger halign = 0, valign = 0;
  49. sq_getfloat (vm, 2, &x );
  50. sq_getfloat (vm, 3, &y );
  51. if (argc > 3)
  52. {
  53. sq_getinteger(vm, 4, &halign );
  54. }
  55. if (argc > 4)
  56. {
  57. sq_getinteger(vm, 5, &valign );
  58. }
  59. if (argc > 5)
  60. {
  61. sq_getfloat(vm, 6, &r );
  62. }
  63. image->draw(x, y, halign, valign, r );
  64. }
  65. return 0;
  66. }
  67. //------------------------------------------------------------------------------
  68. void setupScriptSystem_Image(ScriptSystem* scriptsystem,
  69. TextureManager* texturemanager, RenderDevice* renderdevice)
  70. {
  71. if (scriptsystem == 0|| texturemanager == 0|| renderdevice == 0)
  72. return;
  73. gTextureManager = texturemanager;
  74. gRenderDevice = renderdevice;
  75. HSQUIRRELVM vm = scriptsystem->getVM();
  76. sq_pushroottable (vm );
  77. // push class
  78. sq_pushstring (vm, "Image", -1);
  79. if (SQ_SUCCEEDED (sq_newclass (vm, SQFalse ) ) )
  80. {
  81. // register constructor
  82. sq_pushstring (vm, "constructor", -1);
  83. sq_newclosure (vm, _image_constructor, 0);
  84. sq_newslot (vm, -3, false);
  85. sq_pushstring (vm, "draw", -1);
  86. sq_newclosure (vm, _image_draw, 0);
  87. sq_newslot (vm, -3, false);
  88. // create class
  89. sq_newslot (vm, -3, false);
  90. }
  91. sq_poptop (vm );
  92. }
  93. }