2010-11-19 16:55:55 +01:00
|
|
|
/* The MIT License
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2010-11-21 22:48:58 +01:00
|
|
|
|
2010-11-18 23:13:35 +01:00
|
|
|
#include "graphics.h"
|
2010-11-19 13:37:02 +01:00
|
|
|
#include "logger.h"
|
2010-11-23 22:56:28 +01:00
|
|
|
#include "texture.h"
|
2010-11-18 23:13:35 +01:00
|
|
|
|
|
|
|
Graphics g_graphics;
|
|
|
|
|
|
|
|
Graphics::Graphics()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Graphics::~Graphics()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::init()
|
|
|
|
{
|
|
|
|
// setup opengl
|
2010-11-23 22:56:28 +01:00
|
|
|
glEnable(GL_ALPHA_TEST); // enable alpha by default
|
2010-11-18 23:13:35 +01:00
|
|
|
glAlphaFunc(GL_GREATER, 0.0f); // default alpha mode
|
|
|
|
glDisable(GL_DEPTH_TEST); // we are rendering 2D only, we don't need it
|
2010-11-23 22:56:28 +01:00
|
|
|
glEnable(GL_TEXTURE_2D); // enable textures by default
|
2011-02-08 23:48:26 +01:00
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
|
|
glShadeModel(GL_SMOOTH);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2010-11-19 13:37:02 +01:00
|
|
|
|
|
|
|
notice("GPU %s", (const char*)glGetString(GL_RENDERER));
|
|
|
|
notice("OpenGL %s", (const char*)glGetString(GL_VERSION));
|
2010-11-18 23:13:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::terminate()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-19 01:22:29 +01:00
|
|
|
bool Graphics::isExtensionSupported(const char *extension)
|
|
|
|
{
|
|
|
|
const GLubyte *extensions = NULL;
|
|
|
|
const GLubyte *start;
|
|
|
|
GLubyte *where, *terminator;
|
|
|
|
where = (GLubyte *)strchr(extension, ' ');
|
|
|
|
|
|
|
|
if(where || *extension == '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
extensions = glGetString(GL_EXTENSIONS);
|
|
|
|
|
|
|
|
start = extensions;
|
|
|
|
while(true) {
|
|
|
|
where = (GLubyte *) strstr((const char *)start, extension);
|
|
|
|
if(!where)
|
|
|
|
break;
|
|
|
|
|
|
|
|
terminator = where + strlen(extension);
|
|
|
|
|
|
|
|
if(where == start || *(where - 1) == ' ')
|
|
|
|
if(*terminator == ' ' || *terminator == '\0')
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
start = terminator;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-18 23:13:35 +01:00
|
|
|
void Graphics::resize(int width, int height)
|
2010-11-23 16:30:43 +01:00
|
|
|
{
|
2010-11-25 00:36:15 +01:00
|
|
|
m_screenSize.setWidth(width);
|
|
|
|
m_screenSize.setHeight(height);
|
2010-11-23 16:30:43 +01:00
|
|
|
restoreViewport();
|
|
|
|
}
|
|
|
|
|
2010-11-23 22:56:28 +01:00
|
|
|
void Graphics::restoreViewport()
|
2010-11-18 23:13:35 +01:00
|
|
|
{
|
2010-11-25 00:36:15 +01:00
|
|
|
const int& width = m_screenSize.width();
|
|
|
|
const int& height = m_screenSize.height();
|
|
|
|
|
2010-11-18 23:13:35 +01:00
|
|
|
// resize gl viewport
|
2010-11-25 00:36:15 +01:00
|
|
|
glViewport(0, 0, width, height);
|
2010-11-18 23:13:35 +01:00
|
|
|
|
|
|
|
/*
|
2010-11-23 22:56:28 +01:00
|
|
|
0,0---------0,w
|
2010-11-23 16:30:43 +01:00
|
|
|
| |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
h,0---------h,w
|
|
|
|
*/
|
2010-11-18 23:13:35 +01:00
|
|
|
// setup view region like above
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2010-11-25 00:36:15 +01:00
|
|
|
gluOrtho2D(0.0f, width, height, 0.0f);
|
2010-11-18 23:13:35 +01:00
|
|
|
|
|
|
|
// back to model view
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::beginRender()
|
|
|
|
{
|
2010-11-23 22:56:28 +01:00
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
2010-11-18 23:13:35 +01:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glLoadIdentity();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::endRender()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2010-11-25 00:36:15 +01:00
|
|
|
|
|
|
|
void Graphics::drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& texCoords)
|
|
|
|
{
|
|
|
|
// rect correction for opengl
|
|
|
|
int right = screenCoords.right()+1;
|
|
|
|
int bottom = screenCoords.bottom()+1;
|
|
|
|
int top = screenCoords.top();
|
|
|
|
int left = screenCoords.left();
|
|
|
|
|
|
|
|
float tright;
|
|
|
|
float tbottom;
|
|
|
|
float ttop;
|
|
|
|
float tleft;
|
|
|
|
|
|
|
|
if(!texCoords.isEmpty()) {
|
|
|
|
const Size& textureSize = texture->getSize();
|
|
|
|
tright = (float)(texCoords.right()+1)/textureSize.width();
|
|
|
|
tbottom = (float)(texCoords.bottom()+1)/textureSize.height();
|
|
|
|
ttop = (float)texCoords.top()/textureSize.height();
|
|
|
|
tleft = (float)texCoords.left()/textureSize.width();
|
|
|
|
} else {
|
|
|
|
tright = 0.0f;
|
|
|
|
tbottom = 1.0f;
|
|
|
|
ttop = 0.0f;
|
|
|
|
tleft = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glTexCoord2f(tleft, ttop); glVertex2i(left, top);
|
|
|
|
glTexCoord2f(tleft, tbottom); glVertex2i(left, bottom);
|
|
|
|
glTexCoord2f(tright, tbottom); glVertex2i(right, bottom);
|
|
|
|
glTexCoord2f(tright, ttop); glVertex2i(right, top);
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color)
|
|
|
|
{
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
glColor4ubv(color.rgbaPtr());
|
|
|
|
|
|
|
|
// rect correction for opengl
|
|
|
|
int right = screenCoords.right()+1;
|
|
|
|
int bottom = screenCoords.bottom()+1;
|
|
|
|
int top = screenCoords.top();
|
|
|
|
int left = screenCoords.left();
|
|
|
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glVertex2i(left, top);
|
|
|
|
glVertex2i(left, bottom);
|
|
|
|
glVertex2i(right, bottom);
|
|
|
|
glVertex2i(right, top);
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-08 23:48:26 +01:00
|
|
|
void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth)
|
2010-11-25 00:36:15 +01:00
|
|
|
{
|
2011-02-08 23:48:26 +01:00
|
|
|
if(2*innerLineWidth > screenCoords.height())
|
2010-11-25 00:36:15 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
glColor4ubv(color.rgbaPtr());
|
|
|
|
|
|
|
|
// rect correction for opengl
|
|
|
|
int right = screenCoords.right()+1;
|
|
|
|
int bottom = screenCoords.bottom()+1;
|
|
|
|
int top = screenCoords.top();
|
|
|
|
int left = screenCoords.left();
|
|
|
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
|
|
|
// top line
|
|
|
|
glVertex2i(left, top);
|
2011-02-08 23:48:26 +01:00
|
|
|
glVertex2i(left, top+innerLineWidth);
|
|
|
|
glVertex2i(right, top+innerLineWidth);
|
2010-11-25 00:36:15 +01:00
|
|
|
glVertex2i(right, top);
|
|
|
|
|
|
|
|
// left
|
2011-02-08 23:48:26 +01:00
|
|
|
glVertex2i(left, screenCoords.top()+innerLineWidth);
|
|
|
|
glVertex2i(left, bottom-innerLineWidth);
|
|
|
|
glVertex2i(left+innerLineWidth, bottom-innerLineWidth);
|
|
|
|
glVertex2i(left+innerLineWidth, screenCoords.top()+innerLineWidth);
|
2010-11-25 00:36:15 +01:00
|
|
|
|
|
|
|
// bottom line
|
|
|
|
glVertex2i(left, bottom);
|
2011-02-08 23:48:26 +01:00
|
|
|
glVertex2i(left, bottom-innerLineWidth);
|
|
|
|
glVertex2i(right, bottom-innerLineWidth);
|
2010-11-25 00:36:15 +01:00
|
|
|
glVertex2i(right, bottom);
|
|
|
|
|
|
|
|
// right line
|
2011-02-08 23:48:26 +01:00
|
|
|
glVertex2i(right, top+innerLineWidth);
|
|
|
|
glVertex2i(right, bottom-innerLineWidth);
|
|
|
|
glVertex2i(right-innerLineWidth, bottom-innerLineWidth);
|
|
|
|
glVertex2i(right-innerLineWidth, top+innerLineWidth);
|
2010-11-25 00:36:15 +01:00
|
|
|
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
}
|