1
0

smap.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <precomp.h>
  2. #include "smap.h"
  3. #include <api/script/script.h>
  4. #include <api/script/scriptmgr.h>
  5. #include <api/script/objects/sregion.h>
  6. #include <api/script/objecttable.h>
  7. MapScriptController _mapController;
  8. MapScriptController *mapController=&_mapController;
  9. // -- Functions table -------------------------------------
  10. function_descriptor_struct MapScriptController::exportedFunction[] = {
  11. {L"getValue", 2, (void*)SMap::script_vcpu_getValue },
  12. {L"getARGBValue", 3, (void*)SMap::script_vcpu_getARGBValue },
  13. {L"inRegion", 2, (void*)SMap::script_vcpu_inRegion },
  14. {L"loadMap", 1, (void*)SMap::script_vcpu_loadMap },
  15. {L"getWidth", 0, (void*)SMap::script_vcpu_getWidth },
  16. {L"getHeight", 0, (void*)SMap::script_vcpu_getHeight },
  17. {L"getRegion", 0, (void*)SMap::script_vcpu_getRegion },
  18. // todo: stretch
  19. };
  20. // --------------------------------------------------------
  21. const wchar_t *MapScriptController::getClassName() {
  22. return L"Map";
  23. }
  24. const wchar_t *MapScriptController::getAncestorClassName() {
  25. return L"Object";
  26. }
  27. ScriptObjectController *MapScriptController::getAncestorController() { return rootScriptObjectController; }
  28. ScriptObject *MapScriptController::instantiate() {
  29. SMap *m = new SMap;
  30. ASSERT(m != NULL);
  31. return m->getScriptObject();
  32. }
  33. void MapScriptController::destroy(ScriptObject *o) {
  34. SMap *m = static_cast<SMap *>(o->vcpu_getInterface(mapGuid));
  35. ASSERT(m != NULL);
  36. delete m;
  37. }
  38. void *MapScriptController::encapsulate(ScriptObject *o) {
  39. return NULL; // no encapsulation for maps for now
  40. }
  41. void MapScriptController::deencapsulate(void *o) {
  42. }
  43. int MapScriptController::getNumFunctions() {
  44. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  45. }
  46. const function_descriptor_struct *MapScriptController::getExportedFunctions() {
  47. return exportedFunction;
  48. }
  49. GUID MapScriptController::getClassGuid() {
  50. return mapGuid;
  51. }
  52. SMap::SMap() {
  53. getScriptObject()->vcpu_setInterface(mapGuid, (void *)static_cast<SMap *>(this));
  54. getScriptObject()->vcpu_setClassName(L"Map");
  55. getScriptObject()->vcpu_setController(mapController);
  56. bmp = NULL;
  57. region_so = ObjectTable::instantiate(ObjectTable::getClassFromName(L"Region"));
  58. reg = static_cast<SRegion *>(region_so->vcpu_getInterface(regionGuid));
  59. }
  60. SMap::~SMap() {
  61. if (bmp) delete bmp;
  62. ObjectTable::destroy(region_so);
  63. }
  64. int SMap::getValue(int x, int y) {
  65. if (!bmp) return 0;
  66. ARGB32 c = bmp->getPixel(x, y);
  67. // if ((c & 0xFF000000) >> 24 != 0xFF) return -1;
  68. int v = MAX(MAX((c & 0xFF0000) >> 16, (c & 0xFF00) >> 8), c & 0xFF);
  69. return v;
  70. }
  71. int SMap::getARGBValue(int x, int y, int whichCol) {
  72. if (!bmp) return 0;
  73. ARGB32 c = bmp->getPixel(x, y);
  74. whichCol %= 4;
  75. /**
  76. whichCol
  77. 0: blue
  78. 1: green
  79. 2: red
  80. 3: alpha
  81. */
  82. ARGB32 a = c >> 24;
  83. a &= 0x000000FF; //just to be sure
  84. if (whichCol == 3) return a;
  85. c = c >> (whichCol * 8);
  86. c &= 0x000000FF;
  87. if (0 == a || 255 == a) return c;
  88. double d = c*255/a; // Correction for bitmaps w/ alpha channel, otherwise a lesser rgb value is returned
  89. d = ceil(d);
  90. return (int)d;
  91. }
  92. void SMap::loadMap(const wchar_t *b)
  93. {
  94. bmp = new SkinBitmap(b, 0);
  95. reg->loadFromBitmap(b);
  96. }
  97. int SMap::getWidth() {
  98. if (!bmp) return 0;
  99. return bmp->getWidth();
  100. }
  101. int SMap::getHeight() {
  102. if (!bmp) return 0;
  103. return bmp->getHeight();
  104. }
  105. int SMap::inRegion(int x, int y) {
  106. POINT pt={x,y};
  107. return reg->getRegion()->ptInRegion(&pt);
  108. }
  109. SRegion *SMap::getSRegion() {
  110. return reg;
  111. }
  112. // VCPU
  113. scriptVar SMap::script_vcpu_loadMap(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar bmp) {
  114. SCRIPT_FUNCTION_INIT;
  115. ASSERT(bmp.type == SCRIPT_STRING);
  116. SMap *m = static_cast<SMap *>(o->vcpu_getInterface(mapGuid));
  117. if (m) m->loadMap(bmp.data.sdata);
  118. RETURN_SCRIPT_VOID;
  119. }
  120. scriptVar SMap::script_vcpu_getValue(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar x, scriptVar y) {
  121. SCRIPT_FUNCTION_INIT;
  122. ASSERT(SOM::isNumeric(&x));
  123. ASSERT(SOM::isNumeric(&y));
  124. SMap *m = static_cast<SMap *>(o->vcpu_getInterface(mapGuid));
  125. if (m) return MAKE_SCRIPT_INT(m->getValue(SOM::makeInt(&x), SOM::makeInt(&y)));
  126. RETURN_SCRIPT_ZERO;
  127. }
  128. scriptVar SMap::script_vcpu_getARGBValue(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar x, scriptVar y, scriptVar wichCol) {
  129. SCRIPT_FUNCTION_INIT;
  130. ASSERT(SOM::isNumeric(&x));
  131. ASSERT(SOM::isNumeric(&y));
  132. ASSERT(SOM::isNumeric(&wichCol));
  133. SMap *m = static_cast<SMap *>(o->vcpu_getInterface(mapGuid));
  134. if (m) return MAKE_SCRIPT_INT(m->getARGBValue(SOM::makeInt(&x), SOM::makeInt(&y), SOM::makeInt(&wichCol)));
  135. RETURN_SCRIPT_ZERO;
  136. }
  137. scriptVar SMap::script_vcpu_getWidth(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  138. SCRIPT_FUNCTION_INIT;
  139. SMap *m = static_cast<SMap *>(o->vcpu_getInterface(mapGuid));
  140. if (m) return MAKE_SCRIPT_INT(m->getWidth());
  141. RETURN_SCRIPT_ZERO;
  142. }
  143. scriptVar SMap::script_vcpu_getHeight(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  144. SCRIPT_FUNCTION_INIT;
  145. SMap *m = static_cast<SMap *>(o->vcpu_getInterface(mapGuid));
  146. if (m) return MAKE_SCRIPT_INT(m->getHeight());
  147. RETURN_SCRIPT_ZERO;
  148. }
  149. scriptVar SMap::script_vcpu_inRegion(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar x, scriptVar y) {
  150. SCRIPT_FUNCTION_INIT;
  151. ASSERT(SOM::isNumeric(&x));
  152. ASSERT(SOM::isNumeric(&y));
  153. SMap *m = static_cast<SMap *>(o->vcpu_getInterface(mapGuid));
  154. if (m) return MAKE_SCRIPT_BOOLEAN(m->inRegion(SOM::makeInt(&x), SOM::makeInt(&y)));
  155. RETURN_SCRIPT_ZERO;
  156. }
  157. scriptVar SMap::script_vcpu_getRegion(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  158. SCRIPT_FUNCTION_INIT;
  159. SMap *m = static_cast<SMap *>(o->vcpu_getInterface(mapGuid));
  160. if (m) {
  161. SRegion *s = m->getSRegion();
  162. if (s) return MAKE_SCRIPT_OBJECT(s->getScriptObject());
  163. }
  164. RETURN_SCRIPT_ZERO;
  165. }