JSAtomic3D.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Graphics/StaticModel.h>
  23. #include <Atomic/Graphics/CustomGeometry.h>
  24. #include <Atomic/Graphics/BillboardSet.h>
  25. #include "JSAtomic3D.h"
  26. namespace Atomic
  27. {
  28. static int StaticModel_SetMaterialIndex(duk_context* ctx) {
  29. unsigned index = (unsigned) duk_require_number(ctx, 0);
  30. Material* material = js_to_class_instance<Material>(ctx, 1, 0);
  31. duk_push_this(ctx);
  32. // event receiver
  33. StaticModel* model = js_to_class_instance<StaticModel>(ctx, -1, 0);
  34. model->SetMaterial(index, material);
  35. return 0;
  36. }
  37. static int CustomGeometry_SetMaterialIndex(duk_context* ctx) {
  38. unsigned index = (unsigned)duk_require_number(ctx, 0);
  39. Material* material = js_to_class_instance<Material>(ctx, 1, 0);
  40. duk_push_this(ctx);
  41. // event receiver
  42. CustomGeometry* geometry = js_to_class_instance<CustomGeometry>(ctx, -1, 0);
  43. geometry->SetMaterial(index, material);
  44. return 0;
  45. }
  46. static int BillboardSet_GetBillboard(duk_context* ctx)
  47. {
  48. duk_push_this(ctx);
  49. BillboardSet* billboardSet = js_to_class_instance<BillboardSet>(ctx, -1, 0);
  50. unsigned index = (unsigned)duk_to_number(ctx, 0);
  51. Billboard* billboard = billboardSet->GetBillboard(index);
  52. js_push_class_object_instance(ctx, billboard, "Billboard");
  53. return 1;
  54. }
  55. void jsapi_init_atomic3d(JSVM* vm)
  56. {
  57. duk_context* ctx = vm->GetJSContext();
  58. js_class_get_prototype(ctx, "Atomic", "StaticModel");
  59. duk_push_c_function(ctx, StaticModel_SetMaterialIndex, 2);
  60. duk_put_prop_string(ctx, -2, "setMaterialIndex");
  61. duk_pop(ctx); // pop AObject prototype
  62. js_class_get_prototype(ctx, "Atomic", "CustomGeometry");
  63. duk_push_c_function(ctx, CustomGeometry_SetMaterialIndex, 2);
  64. duk_put_prop_string(ctx, -2, "setMaterialIndex");
  65. duk_pop(ctx); // pop AObject prototype
  66. js_class_get_prototype(ctx, "Atomic", "BillboardSet");
  67. duk_push_c_function(ctx, BillboardSet_GetBillboard, 1);
  68. duk_put_prop_string(ctx, -2, "getBillboard");
  69. duk_pop(ctx); // pop AObject prototype
  70. }
  71. }