Utilities.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #define UTILITIES_CPP
  2. /*************************************************************************************************\
  3. Utilities.cpp : Implementation of the Utilities component.
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. \*************************************************************************************************/
  8. #include "Utilities.h"
  9. #include "txmmgr.h"
  10. #include "IniFile.h"
  11. #include "McLib.h"
  12. #pragma warning(disable:4514)
  13. void drawRect( const GUI_RECT& area, unsigned long color )
  14. {
  15. if ( color & 0xff000000 )
  16. {
  17. GUI_RECT clientArea = area;
  18. if ( clientArea.left < 0 )
  19. clientArea.left = 0;
  20. if ( clientArea.right < 0 )
  21. clientArea.right = 0;
  22. if ( clientArea.top < 0 )
  23. clientArea.top = 0;
  24. if ( clientArea.bottom < 0 )
  25. clientArea.bottom = 0;
  26. if ( clientArea.left >= Environment.screenWidth )
  27. clientArea.left = Environment.screenWidth;
  28. if ( clientArea.right >= Environment.screenWidth )
  29. clientArea.right = Environment.screenWidth;
  30. if ( clientArea.top >= Environment.screenHeight )
  31. clientArea.top = Environment.screenHeight;
  32. if ( clientArea.bottom >= Environment.screenHeight )
  33. clientArea.bottom = Environment.screenHeight;
  34. gos_VERTEX v[4];
  35. memset( v,0,sizeof(v) );
  36. v[0].rhw = v[1].rhw = v[2].rhw = v[3].rhw = 1.0;
  37. v[0].argb = v[1].argb = v[2].argb = v[3].argb = color;
  38. v[0].x = (float)clientArea.left;
  39. v[0].y = (float)clientArea.top;
  40. v[1].x = (float)clientArea.left;
  41. v[1].y = (float)clientArea.bottom;
  42. v[2].x = (float)clientArea.right;
  43. v[2].y = (float)clientArea.bottom;
  44. v[3].x = (float)clientArea.right;
  45. v[3].y = (float)clientArea.top;
  46. gos_SetRenderState( gos_State_Texture, 0 );
  47. gos_SetRenderState( gos_State_ZWrite, 0 );
  48. gos_SetRenderState( gos_State_ZCompare, 0 );
  49. gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha);
  50. gos_DrawQuads( v, 4 );
  51. }
  52. }
  53. void drawEmptyRect( const GUI_RECT& area, unsigned long leftTopBorderColor,
  54. unsigned long rightBottomBorderColor )
  55. {
  56. gos_VERTEX v[4];
  57. memset( v,0,sizeof(v) );
  58. v[0].rhw = v[1].rhw = v[2].rhw = v[3].rhw = 1.0;
  59. GUI_RECT clientArea = area;
  60. if ( (clientArea.left < 0 && clientArea.right < 0 )
  61. || (clientArea.right > Environment.screenWidth && clientArea.left > Environment.screenWidth )
  62. || (clientArea.top < 0 && clientArea.bottom < 0 )
  63. || ( clientArea.top > Environment.screenHeight && clientArea.bottom > Environment.screenHeight ) )
  64. return;
  65. if ( clientArea.left < 0 )
  66. clientArea.left = 0;
  67. if ( clientArea.right < 0 )
  68. clientArea.right = 0;
  69. if ( clientArea.top < 0 )
  70. clientArea.top = 0;
  71. if ( clientArea.bottom < 0 )
  72. clientArea.bottom = 0;
  73. if ( clientArea.left >= Environment.screenWidth - 1 )
  74. clientArea.left = Environment.screenWidth - 1;
  75. if ( clientArea.right >= Environment.screenWidth - 1 )
  76. clientArea.right = Environment.screenWidth - 1;
  77. if ( clientArea.top >= Environment.screenHeight - 1 )
  78. clientArea.top = Environment.screenHeight - 1;
  79. if ( clientArea.bottom >= Environment.screenHeight - 1 )
  80. clientArea.bottom = Environment.screenHeight - 1;
  81. gos_SetRenderState( gos_State_Texture, 0 );
  82. gos_SetRenderState( gos_State_ZWrite, 0 );
  83. gos_SetRenderState( gos_State_ZCompare, 0 );
  84. gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha );
  85. v[0].x = (float)(clientArea.left);
  86. v[0].y = (float)(clientArea.top);
  87. v[0].argb = leftTopBorderColor;
  88. v[1].x = (float)(clientArea.right);
  89. v[1].y = (float)(clientArea.top);
  90. v[1].argb = leftTopBorderColor;
  91. v[2] = v[0];
  92. v[3].x = (float)(clientArea.left);
  93. v[3].y = (float)(clientArea.bottom);
  94. v[3].argb = leftTopBorderColor;
  95. gos_DrawLines( v, 4 );
  96. v[0].x = (float)(clientArea.right);
  97. v[0].y = (float)(clientArea.top);
  98. v[0].argb = rightBottomBorderColor;
  99. v[1].x = (float)(clientArea.right);
  100. v[1].y = (float)(clientArea.bottom);
  101. v[1].argb = rightBottomBorderColor;
  102. v[3] = v[1];
  103. v[3].x += 1;
  104. v[2].x = (float)(clientArea.left);
  105. v[2].y = (float)(clientArea.bottom) + .1/256.f;
  106. v[2].argb = rightBottomBorderColor;
  107. gos_DrawLines( v, 4 );
  108. }
  109. // STATIC STUFF
  110. StaticInfo::~StaticInfo()
  111. {
  112. if (mcTextureManager)
  113. {
  114. unsigned long gosID = mcTextureManager->get_gosTextureHandle( textureHandle );
  115. mcTextureManager->removeTexture( gosID );
  116. }
  117. }
  118. void StaticInfo::render()
  119. {
  120. unsigned long gosID = mcTextureManager->get_gosTextureHandle( textureHandle );
  121. gos_SetRenderState( gos_State_Texture, gosID );
  122. gos_SetRenderState(gos_State_Filter, gos_FilterNone);
  123. gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha );
  124. gos_SetRenderState( gos_State_Clipping, 1);
  125. gos_DrawQuads( location, 4 );
  126. }
  127. void StaticInfo::showGUIWindow( bool bShow )
  128. {
  129. int mask = bShow ? 0xff000000 : 0x00ffffff;
  130. for ( int i = 0; i < 4; i++ )
  131. {
  132. if ( bShow )
  133. location[i].argb |= mask;
  134. else
  135. location[i].argb &= mask;
  136. }
  137. }
  138. bool StaticInfo::isInside( int mouseX, int mouseY )
  139. {
  140. if ( (location[0].x) <= mouseX &&
  141. location[3].x >= mouseX &&
  142. location[0].y <= mouseY &&
  143. location[1].y >= mouseY )
  144. return true;
  145. return false;
  146. }
  147. //Fills the buffer with the bitmap data.
  148. // Used by new mouse cursor draw routines.
  149. void StaticInfo::getData(unsigned char * buffer)
  150. {
  151. if ((vHeight > 32) || (uWidth > 32))
  152. {
  153. memset(buffer,0,sizeof(DWORD) * 32 * 32);
  154. return;
  155. }
  156. unsigned long gosID = mcTextureManager->get_gosTextureHandle( textureHandle );
  157. if (gosID)
  158. {
  159. TEXTUREPTR textureData;
  160. gos_LockTexture( gosID, 0, 0, &textureData );
  161. DWORD *bufMem = (DWORD *)buffer;
  162. DWORD localU = location[0].u * textureData.Width;
  163. DWORD localV = location[0].v * textureData.Width;
  164. //Make sure we don't fall off of the texture!
  165. if ((localU > textureData.Width) ||
  166. ((localU + uWidth) > textureData.Width) ||
  167. (localV > textureData.Width) ||
  168. ((localV + vHeight) > textureData.Width))
  169. {
  170. memset(buffer,0,sizeof(DWORD) * 32 * 32);
  171. gos_UnLockTexture( gosID );
  172. return;
  173. }
  174. for (long y=0;y<vHeight;y++)
  175. {
  176. DWORD *textureMemory = textureData.pTexture + (localU + ((localV+y) * textureData.Width));
  177. for (long x=0;x<uWidth;x++)
  178. {
  179. *bufMem = *textureMemory;
  180. bufMem++;
  181. textureMemory++;
  182. }
  183. }
  184. gos_UnLockTexture( gosID );
  185. }
  186. }
  187. void StaticInfo::init( FitIniFile& file, char* blockName, long hiResOffsetX, long hiResOffsetY, DWORD neverFlush )
  188. {
  189. memset( location, 0, sizeof( location ) );
  190. char fileName[256];
  191. textureHandle = 0;
  192. textureWidth = 0;
  193. if ( NO_ERR != file.seekBlock( blockName ) )
  194. {
  195. char errBuffer[256];
  196. sprintf( errBuffer, "couldn't find static block %s", blockName );
  197. Assert( 0, 0, errBuffer );
  198. return;
  199. }
  200. long x, y, width, height;
  201. file.readIdLong( "XLocation", x );
  202. file.readIdLong( "YLocation", y );
  203. x += hiResOffsetX;
  204. y += hiResOffsetY;
  205. file.readIdLong( "Width", width );
  206. file.readIdLong( "Height", height );
  207. file.readIdString( "FileName", fileName, 32 );
  208. if ( !textureHandle )
  209. {
  210. FullPathFileName fullPath;
  211. _strlwr( fileName );
  212. fullPath.init( artPath, fileName, ".tga" );
  213. int ID = mcTextureManager->loadTexture( fullPath, gos_Texture_Alpha, 0, 0, 0x2 );
  214. textureHandle = ID;
  215. unsigned long gosID = mcTextureManager->get_gosTextureHandle( ID );
  216. TEXTUREPTR textureData;
  217. gos_LockTexture( gosID, 0, 0, &textureData );
  218. textureWidth = textureData.Width;
  219. gos_UnLockTexture( gosID );
  220. }
  221. bool bRotated = 0;
  222. file.readIdLong( "UNormal", u );
  223. file.readIdLong( "VNormal", v );
  224. file.readIdLong( "UWidth", uWidth );
  225. file.readIdLong( "VHeight", vHeight );
  226. file.readIdBoolean( "texturesRotated", bRotated );
  227. for ( int k = 0; k < 4; k++ )
  228. {
  229. location[k].argb = 0xffffffff;
  230. location[k].frgb = 0;
  231. location[k].x = x;
  232. location[k].y = y;
  233. location[k].z = 0.f;
  234. location[k].rhw = .5;
  235. location[k].u = (float)u/(float)textureWidth + (.1f / (float)textureWidth);
  236. location[k].v = (float)v/(float)textureWidth + (.1f / (float)textureWidth);
  237. }
  238. location[3].x = location[2].x = x + width;
  239. location[2].y = location[1].y = y + height;
  240. location[2].u = location[3].u = ((float)(u + uWidth))/((float)textureWidth) + (.1f / (float)textureWidth);
  241. location[1].v = location[2].v = ((float)(v + vHeight))/((float)textureWidth) + (.1f / (float)textureWidth);
  242. if ( bRotated )
  243. {
  244. location[0].u = (u + uWidth)/(float)textureWidth + (.1f / (float)textureWidth);;
  245. location[1].u = u/(float)textureWidth + (.1f / (float)textureWidth);;
  246. location[2].u = u/(float)textureWidth + (.1f / (float)textureWidth);
  247. location[3].u = (u + uWidth)/(float)textureWidth + (.1f / (float)textureWidth);
  248. location[0].v = v/(float)textureWidth + (.1f / (float)textureWidth);;
  249. location[1].v = v/(float)textureWidth + (.1f / (float)textureWidth);;
  250. location[2].v = (v + vHeight)/(float)textureWidth + (.1f / (float)textureWidth);;
  251. location[3].v = (v + vHeight)/(float)textureWidth + (.1f / (float)textureWidth);;
  252. }
  253. }
  254. void StaticInfo::setLocation( float newX, float newY )
  255. {
  256. float height = location[1].y - location[0].y;
  257. float width = location[3].x - location[0].x;
  258. location[0].x = location[1].x = newX;
  259. location[2].x = location[3].x = newX + width;
  260. location[0].y = location[3].y = newY;
  261. location[1].y = location[2].y = newY + height;
  262. }
  263. void StaticInfo::move( float deltaX, float deltaY )
  264. {
  265. for ( int i = 0; i < 4; i++ )
  266. {
  267. location[i].x += deltaX;
  268. location[i].y += deltaY;
  269. }
  270. }
  271. void StaticInfo::setColor( long newColor )
  272. {
  273. for ( int i = 0; i < 4; i++ )
  274. location[i].argb = newColor;
  275. }
  276. void StaticInfo::setNewUVs( float uLeft, float vTop, float uRight, float vBottom )
  277. {
  278. location[0].u = location[1].u = uLeft;
  279. location[2].u = location[3].u = uRight;
  280. location[0].v = location[3].v = vTop;
  281. location[1].v = location[2].v = vBottom;
  282. }
  283. void drawShadowText( long colorTop, long colorShadow, HGOSFONT3D font, long left, long top,
  284. bool proportional, const char* text, bool bold, float scale )
  285. {
  286. drawShadowText( colorTop, colorShadow, font, left, top, proportional, text, bold, scale, -1, 1 );
  287. }
  288. void drawShadowText( long colorTop, long colorShadow, HGOSFONT3D font,
  289. long left, long top, bool proportional, const char* text, bool bold, float scale,
  290. long xOffset, long yOffset)
  291. {
  292. gos_TextSetAttributes( font, colorShadow, scale, false, proportional, bold, false, 0 );
  293. gos_TextSetRegion( 0, 0, Environment.screenWidth, Environment.screenHeight );
  294. gos_TextSetPosition( left + xOffset, top + yOffset );
  295. gos_TextDraw( text );
  296. gos_TextSetAttributes( font, colorTop, scale, false, proportional, bold, false, 0 );
  297. gos_TextSetPosition( left, top );
  298. gos_TextDraw( text );
  299. }
  300. void drawShadowText( long colorTop, long colorShadow, HGOSFONT3D font,
  301. long left, long top, long right, long bottom, bool proportional, const char* text, bool bold, float scale,
  302. long xOffset, long yOffset)
  303. {
  304. gos_TextSetAttributes( font, colorShadow, scale, true, proportional, bold, false, 2 );
  305. gos_TextSetRegion( left + xOffset, top + yOffset, right + xOffset, bottom + yOffset);
  306. gos_TextSetPosition( left + xOffset, top + yOffset );
  307. gos_TextDraw( text );
  308. gos_TextSetAttributes( font, colorTop, scale, true, proportional, bold, false, 2 );
  309. gos_TextSetRegion( left, top, right, bottom );
  310. gos_TextSetPosition( left, top );
  311. gos_TextDraw( text );
  312. }
  313. long interpolateColor( long color1, long color2, float percent )
  314. {
  315. long color = 0xffffffff;
  316. if ( percent > 1.0 )
  317. color = color2;
  318. else if ( percent < 0.0 )
  319. color = color1;
  320. else
  321. {
  322. long alphaMin = (color1 >> 24) & 0xff;
  323. long alphaMax = (color2 >> 24)& 0xff;
  324. long newAlpha = (float)alphaMin + ((float)(alphaMax - alphaMin))*percent;
  325. long redMin = (color1 & 0x00ff0000)>>16;
  326. long redMax = (color2 & 0x00ff0000)>>16;
  327. long newRed = (float)redMin + ((float)(redMax - redMin))*percent;
  328. long greenMin = (color1 & 0x0000ff00)>>8;
  329. long greenMax = (color2 & 0x0000ff00)>>8;
  330. long newGreen = (float)greenMin + ((float)(greenMax - greenMin))*percent;
  331. long blueMin = color1 & 0x000000ff;
  332. long blueMax = color2 & 0x000000ff;
  333. long newBlue = (float)blueMin + ((float)(blueMax - blueMin))*percent;
  334. color = newBlue + (newGreen << 8) + (newRed << 16) + (newAlpha <<24);
  335. }
  336. return color;
  337. }