No Description
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.

graphics.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "graphics.h"
  24. #include "logger.h"
  25. #include "texture.h"
  26. Graphics g_graphics;
  27. Graphics::Graphics()
  28. {
  29. }
  30. Graphics::~Graphics()
  31. {
  32. }
  33. void Graphics::init()
  34. {
  35. // setup opengl
  36. glEnable(GL_ALPHA_TEST); // enable alpha by default
  37. glAlphaFunc(GL_GREATER, 0.0f); // default alpha mode
  38. glDisable(GL_DEPTH_TEST); // we are rendering 2D only, we don't need it
  39. glEnable(GL_TEXTURE_2D); // enable textures by default
  40. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  41. glShadeModel(GL_SMOOTH);
  42. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  43. notice("GPU %s", (const char*)glGetString(GL_RENDERER));
  44. notice("OpenGL %s", (const char*)glGetString(GL_VERSION));
  45. }
  46. void Graphics::terminate()
  47. {
  48. }
  49. bool Graphics::isExtensionSupported(const char *extension)
  50. {
  51. const GLubyte *extensions = NULL;
  52. const GLubyte *start;
  53. GLubyte *where, *terminator;
  54. where = (GLubyte *)strchr(extension, ' ');
  55. if(where || *extension == '\0')
  56. return 0;
  57. extensions = glGetString(GL_EXTENSIONS);
  58. start = extensions;
  59. while(true) {
  60. where = (GLubyte *) strstr((const char *)start, extension);
  61. if(!where)
  62. break;
  63. terminator = where + strlen(extension);
  64. if(where == start || *(where - 1) == ' ')
  65. if(*terminator == ' ' || *terminator == '\0')
  66. return 1;
  67. start = terminator;
  68. }
  69. return 0;
  70. }
  71. void Graphics::resize(int width, int height)
  72. {
  73. m_screenSize.setWidth(width);
  74. m_screenSize.setHeight(height);
  75. restoreViewport();
  76. }
  77. void Graphics::restoreViewport()
  78. {
  79. const int& width = m_screenSize.width();
  80. const int& height = m_screenSize.height();
  81. // resize gl viewport
  82. glViewport(0, 0, width, height);
  83. /*
  84. 0,0---------0,w
  85. | |
  86. | |
  87. | |
  88. h,0---------h,w
  89. */
  90. // setup view region like above
  91. glMatrixMode(GL_PROJECTION);
  92. glLoadIdentity();
  93. gluOrtho2D(0.0f, width, height, 0.0f);
  94. // back to model view
  95. glMatrixMode(GL_MODELVIEW);
  96. glLoadIdentity();
  97. }
  98. void Graphics::beginRender()
  99. {
  100. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  101. glClear(GL_COLOR_BUFFER_BIT);
  102. glLoadIdentity();
  103. }
  104. void Graphics::endRender()
  105. {
  106. }
  107. void Graphics::drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& texCoords)
  108. {
  109. // rect correction for opengl
  110. int right = screenCoords.right()+1;
  111. int bottom = screenCoords.bottom()+1;
  112. int top = screenCoords.top();
  113. int left = screenCoords.left();
  114. float tright;
  115. float tbottom;
  116. float ttop;
  117. float tleft;
  118. if(!texCoords.isEmpty()) {
  119. const Size& textureSize = texture->getSize();
  120. tright = (float)(texCoords.right()+1)/textureSize.width();
  121. tbottom = (float)(texCoords.bottom()+1)/textureSize.height();
  122. ttop = (float)texCoords.top()/textureSize.height();
  123. tleft = (float)texCoords.left()/textureSize.width();
  124. } else {
  125. tright = 0.0f;
  126. tbottom = 1.0f;
  127. ttop = 0.0f;
  128. tleft = 1.0f;
  129. }
  130. glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
  131. glBegin(GL_QUADS);
  132. glTexCoord2f(tleft, ttop); glVertex2i(left, top);
  133. glTexCoord2f(tleft, tbottom); glVertex2i(left, bottom);
  134. glTexCoord2f(tright, tbottom); glVertex2i(right, bottom);
  135. glTexCoord2f(tright, ttop); glVertex2i(right, top);
  136. glEnd();
  137. }
  138. void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color)
  139. {
  140. glDisable(GL_TEXTURE_2D);
  141. glColor4ubv(color.rgbaPtr());
  142. // rect correction for opengl
  143. int right = screenCoords.right()+1;
  144. int bottom = screenCoords.bottom()+1;
  145. int top = screenCoords.top();
  146. int left = screenCoords.left();
  147. glBegin(GL_QUADS);
  148. glVertex2i(left, top);
  149. glVertex2i(left, bottom);
  150. glVertex2i(right, bottom);
  151. glVertex2i(right, top);
  152. glEnd();
  153. glEnable(GL_TEXTURE_2D);
  154. }
  155. void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth)
  156. {
  157. if(2*innerLineWidth > screenCoords.height())
  158. return;
  159. glDisable(GL_TEXTURE_2D);
  160. glColor4ubv(color.rgbaPtr());
  161. // rect correction for opengl
  162. int right = screenCoords.right()+1;
  163. int bottom = screenCoords.bottom()+1;
  164. int top = screenCoords.top();
  165. int left = screenCoords.left();
  166. glBegin(GL_QUADS);
  167. // top line
  168. glVertex2i(left, top);
  169. glVertex2i(left, top+innerLineWidth);
  170. glVertex2i(right, top+innerLineWidth);
  171. glVertex2i(right, top);
  172. // left
  173. glVertex2i(left, screenCoords.top()+innerLineWidth);
  174. glVertex2i(left, bottom-innerLineWidth);
  175. glVertex2i(left+innerLineWidth, bottom-innerLineWidth);
  176. glVertex2i(left+innerLineWidth, screenCoords.top()+innerLineWidth);
  177. // bottom line
  178. glVertex2i(left, bottom);
  179. glVertex2i(left, bottom-innerLineWidth);
  180. glVertex2i(right, bottom-innerLineWidth);
  181. glVertex2i(right, bottom);
  182. // right line
  183. glVertex2i(right, top+innerLineWidth);
  184. glVertex2i(right, bottom-innerLineWidth);
  185. glVertex2i(right-innerLineWidth, bottom-innerLineWidth);
  186. glVertex2i(right-innerLineWidth, top+innerLineWidth);
  187. glEnd();
  188. glEnable(GL_TEXTURE_2D);
  189. }