2011-08-14 04:09:11 +02:00
|
|
|
#include "fontmanager.h"
|
|
|
|
|
2011-08-15 16:06:15 +02:00
|
|
|
#include <framework/graphics/graphics.h>
|
|
|
|
#include <framework/graphics/texture.h>
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GL/glu.h>
|
|
|
|
#include <GL/glext.h>
|
2010-11-18 23:13:35 +01:00
|
|
|
|
|
|
|
Graphics g_graphics;
|
|
|
|
|
|
|
|
void Graphics::init()
|
|
|
|
{
|
|
|
|
// setup opengl
|
2010-11-23 22:56:28 +01:00
|
|
|
glEnable(GL_ALPHA_TEST); // enable alpha by default
|
2011-08-14 04:09:11 +02:00
|
|
|
glAlphaFunc(GL_GREATER, 0.0f); // default alpha func
|
|
|
|
glDisable(GL_DEPTH_TEST); // we are rendering 2D only, we don't need depth buffer
|
2011-08-14 19:45:25 +02: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);
|
2011-05-12 00:16:11 +02:00
|
|
|
glEnable(GL_BLEND);
|
2011-02-08 23:48:26 +01:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2011-04-17 22:39:03 +02:00
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
2010-11-19 13:37:02 +01:00
|
|
|
|
2011-07-17 13:52:20 +02:00
|
|
|
logInfo("GPU ", glGetString(GL_RENDERER));
|
|
|
|
logInfo("OpenGL ", glGetString(GL_VERSION));
|
2011-08-14 19:45:25 +02:00
|
|
|
|
|
|
|
m_drawing = false;
|
|
|
|
m_opacity = 255;
|
2011-08-15 21:15:49 +02:00
|
|
|
m_emptyTexture = TexturePtr(new Texture);
|
|
|
|
|
|
|
|
bindColor(Color::white);
|
2010-11-18 23:13:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::terminate()
|
|
|
|
{
|
2011-08-14 04:09:11 +02:00
|
|
|
g_fonts.releaseFonts();
|
2011-08-15 21:15:49 +02:00
|
|
|
m_emptyTexture.reset();
|
2010-11-18 23:13:35 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-04-08 07:10:00 +02:00
|
|
|
void Graphics::resize(const Size& size)
|
2010-11-23 16:30:43 +01:00
|
|
|
{
|
2011-04-08 07:10:00 +02:00
|
|
|
m_screenSize = size;
|
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()
|
|
|
|
{
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glLoadIdentity();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::endRender()
|
|
|
|
{
|
2011-08-14 19:45:25 +02:00
|
|
|
assert(!m_drawing);
|
2010-11-18 23:13:35 +01:00
|
|
|
}
|
2010-11-25 00:36:15 +01:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
void Graphics::drawTexturedRect(const Rect& screenCoords,
|
|
|
|
const TexturePtr& texture,
|
2011-08-14 19:45:25 +02:00
|
|
|
const Rect& textureCoords)
|
2011-04-07 09:30:32 +02:00
|
|
|
{
|
2011-08-15 21:15:49 +02:00
|
|
|
if(screenCoords.isEmpty() || texture->getId() == 0)
|
2011-04-10 00:55:58 +02:00
|
|
|
return;
|
|
|
|
|
2010-11-25 00:36:15 +01:00
|
|
|
// rect correction for opengl
|
2011-04-05 02:39:57 +02:00
|
|
|
int right = screenCoords.right() + 1;
|
|
|
|
int bottom = screenCoords.bottom() + 1;
|
2010-11-25 00:36:15 +01:00
|
|
|
int top = screenCoords.top();
|
|
|
|
int left = screenCoords.left();
|
|
|
|
|
2011-08-15 16:06:15 +02:00
|
|
|
float textureRight;
|
|
|
|
float textureBottom;
|
|
|
|
float textureTop;
|
|
|
|
float textureLeft;
|
|
|
|
|
|
|
|
if(textureCoords.isEmpty()) {
|
|
|
|
textureRight = 1.0f;
|
|
|
|
textureBottom = 1.0f;
|
|
|
|
textureTop = 0.0f;
|
|
|
|
textureLeft = 0.0f;
|
|
|
|
} else {
|
2011-08-15 23:02:52 +02:00
|
|
|
const Size& textureSize = texture->getGlSize();
|
2011-04-05 02:39:57 +02:00
|
|
|
textureRight = (float)(textureCoords.right() + 1) / textureSize.width();
|
|
|
|
textureBottom = (float)(textureCoords.bottom() + 1) / textureSize.height();
|
|
|
|
textureTop = (float)textureCoords.top() / textureSize.height();
|
|
|
|
textureLeft = (float)textureCoords.left() / textureSize.width();
|
2010-11-25 00:36:15 +01:00
|
|
|
}
|
2011-04-07 02:58:36 +02:00
|
|
|
|
2011-08-14 19:45:25 +02:00
|
|
|
if(!m_drawing) {
|
|
|
|
bindTexture(texture);
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
}
|
|
|
|
|
2011-04-07 09:30:32 +02:00
|
|
|
glTexCoord2f(textureLeft, textureTop); glVertex2i(left, top);
|
|
|
|
glTexCoord2f(textureLeft, textureBottom); glVertex2i(left, bottom);
|
|
|
|
glTexCoord2f(textureRight, textureBottom); glVertex2i(right, bottom);
|
|
|
|
glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
|
2011-08-14 19:45:25 +02:00
|
|
|
|
|
|
|
if(!m_drawing)
|
|
|
|
glEnd();
|
2011-04-08 11:50:26 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
|
|
|
|
const TexturePtr& texture,
|
2011-08-14 19:45:25 +02:00
|
|
|
const Rect& textureCoords)
|
2011-04-08 11:50:26 +02:00
|
|
|
{
|
2011-08-15 21:15:49 +02:00
|
|
|
if(screenCoords.isEmpty() || texture->getId() == 0 || textureCoords.isEmpty())
|
2011-04-10 00:55:58 +02:00
|
|
|
return;
|
|
|
|
|
2011-08-14 19:45:25 +02:00
|
|
|
if(!m_drawing) {
|
|
|
|
bindTexture(texture);
|
|
|
|
startDrawing();
|
|
|
|
}
|
|
|
|
|
2011-04-08 11:50:26 +02:00
|
|
|
// render many repeated texture rects
|
|
|
|
Rect virtualScreenCoords(0,0,screenCoords.size());
|
|
|
|
for(int y = 0; y <= virtualScreenCoords.height(); y += textureCoords.height()) {
|
|
|
|
for(int x = 0; x <= virtualScreenCoords.width(); x += textureCoords.width()) {
|
|
|
|
Rect partialCoords(x, y, textureCoords.size());
|
|
|
|
Rect partialTextureCoords = textureCoords;
|
|
|
|
|
|
|
|
// partialCoords to screenCoords bottomRight
|
|
|
|
if(partialCoords.bottom() > virtualScreenCoords.bottom()) {
|
2011-08-14 04:09:11 +02:00
|
|
|
partialTextureCoords.setBottom(partialTextureCoords.bottom() +
|
|
|
|
(virtualScreenCoords.bottom() - partialCoords.bottom()));
|
2011-04-08 11:50:26 +02:00
|
|
|
partialCoords.setBottom(virtualScreenCoords.bottom());
|
|
|
|
}
|
|
|
|
if(partialCoords.right() > virtualScreenCoords.right()) {
|
2011-08-14 04:09:11 +02:00
|
|
|
partialTextureCoords.setRight(partialTextureCoords.right() +
|
|
|
|
(virtualScreenCoords.right() - partialCoords.right()));
|
2011-04-08 11:50:26 +02:00
|
|
|
partialCoords.setRight(virtualScreenCoords.right());
|
|
|
|
}
|
|
|
|
|
|
|
|
partialCoords.translate(screenCoords.topLeft());
|
2011-08-14 19:45:25 +02:00
|
|
|
drawTexturedRect(partialCoords, texture, partialTextureCoords);
|
2011-04-08 11:50:26 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-14 19:45:25 +02:00
|
|
|
|
|
|
|
if(!m_drawing)
|
|
|
|
stopDrawing();
|
2011-04-08 11:50:26 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
void Graphics::drawFilledRect(const Rect& screenCoords,
|
|
|
|
const Color& color)
|
2010-11-25 00:36:15 +01:00
|
|
|
{
|
2011-08-14 19:45:25 +02:00
|
|
|
assert(!m_drawing);
|
|
|
|
|
2011-04-15 17:46:30 +02:00
|
|
|
if(screenCoords.isEmpty())
|
2011-04-10 00:55:58 +02:00
|
|
|
return;
|
|
|
|
|
2010-11-25 00:36:15 +01:00
|
|
|
// rect correction for opengl
|
2011-04-05 02:39:57 +02:00
|
|
|
int right = screenCoords.right() + 1;
|
|
|
|
int bottom = screenCoords.bottom() + 1;
|
2010-11-25 00:36:15 +01:00
|
|
|
int top = screenCoords.top();
|
|
|
|
int left = screenCoords.left();
|
|
|
|
|
2011-08-14 20:13:33 +02:00
|
|
|
glPushAttrib(GL_CURRENT_BIT);
|
2011-08-14 19:45:25 +02:00
|
|
|
glColor4ubv(color.rgbaPtr());
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
2010-11-25 00:36:15 +01:00
|
|
|
glVertex2i(left, top);
|
|
|
|
glVertex2i(left, bottom);
|
|
|
|
glVertex2i(right, bottom);
|
|
|
|
glVertex2i(right, top);
|
2011-08-14 19:45:25 +02:00
|
|
|
|
|
|
|
glEnd();
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
2011-08-14 20:13:33 +02:00
|
|
|
glPopAttrib();
|
2010-11-25 00:36:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
void Graphics::drawBoundingRect(const Rect& screenCoords,
|
|
|
|
const Color& color,
|
|
|
|
int innerLineWidth)
|
2010-11-25 00:36:15 +01:00
|
|
|
{
|
2011-08-14 19:45:25 +02:00
|
|
|
assert(!m_drawing);
|
|
|
|
|
2011-08-15 16:06:15 +02:00
|
|
|
if(screenCoords.isEmpty() || 2 * innerLineWidth > screenCoords.height())
|
2010-11-25 00:36:15 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
// rect correction for opengl
|
|
|
|
int right = screenCoords.right()+1;
|
|
|
|
int bottom = screenCoords.bottom()+1;
|
|
|
|
int top = screenCoords.top();
|
|
|
|
int left = screenCoords.left();
|
|
|
|
|
2011-08-14 20:13:33 +02:00
|
|
|
glPushAttrib(GL_CURRENT_BIT);
|
2011-08-14 19:45:25 +02:00
|
|
|
glColor4ubv(color.rgbaPtr());
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
glBegin(GL_QUADS);
|
2011-04-16 18:08:55 +02:00
|
|
|
|
|
|
|
// top line
|
|
|
|
glVertex2i(left, top);
|
|
|
|
glVertex2i(left, top + innerLineWidth);
|
|
|
|
glVertex2i(right, top + innerLineWidth);
|
|
|
|
glVertex2i(right, top);
|
|
|
|
|
|
|
|
// left
|
|
|
|
glVertex2i(left, screenCoords.top() + innerLineWidth);
|
|
|
|
glVertex2i(left, bottom - innerLineWidth);
|
|
|
|
glVertex2i(left + innerLineWidth, bottom - innerLineWidth);
|
|
|
|
glVertex2i(left + innerLineWidth, screenCoords.top() + innerLineWidth);
|
|
|
|
|
|
|
|
// bottom line
|
|
|
|
glVertex2i(left, bottom);
|
|
|
|
glVertex2i(left, bottom - innerLineWidth);
|
|
|
|
glVertex2i(right, bottom - innerLineWidth);
|
|
|
|
glVertex2i(right, bottom);
|
|
|
|
|
|
|
|
// right line
|
|
|
|
glVertex2i(right , top + innerLineWidth);
|
|
|
|
glVertex2i(right , bottom - innerLineWidth);
|
|
|
|
glVertex2i(right - innerLineWidth, bottom - innerLineWidth);
|
|
|
|
glVertex2i(right - innerLineWidth, top + innerLineWidth);
|
2011-08-14 19:45:25 +02:00
|
|
|
|
|
|
|
glEnd();
|
2011-08-14 20:13:33 +02:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glPopAttrib();
|
2011-04-16 18:08:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::bindColor(const Color& color)
|
|
|
|
{
|
2011-08-14 19:45:25 +02:00
|
|
|
Color tmp = color;
|
|
|
|
tmp.setAlpha(std::min((uint8)m_opacity, color.a()));
|
|
|
|
glColor4ubv(tmp.rgbaPtr());
|
2011-04-16 18:08:55 +02:00
|
|
|
}
|
2010-11-25 00:36:15 +01:00
|
|
|
|
2011-08-14 19:45:25 +02:00
|
|
|
void Graphics::bindTexture(const TexturePtr& texture)
|
2011-04-16 18:08:55 +02:00
|
|
|
{
|
2011-08-15 16:06:15 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->getId());
|
2011-08-14 19:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::startDrawing()
|
|
|
|
{
|
|
|
|
assert(!m_drawing);
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
m_drawing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::stopDrawing()
|
|
|
|
{
|
2011-08-14 20:13:33 +02:00
|
|
|
assert(m_drawing);
|
|
|
|
glEnd();
|
|
|
|
m_drawing = false;
|
2011-04-08 11:50:26 +02:00
|
|
|
}
|
2011-08-14 19:45:25 +02:00
|
|
|
|