|
|
@ -10,13 +10,11 @@ Graphics g_graphics;
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::init()
|
|
|
|
void Graphics::init()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
m_drawMode = DRAW_NONE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// setup opengl
|
|
|
|
// setup opengl
|
|
|
|
glEnable(GL_ALPHA_TEST); // enable alpha by default
|
|
|
|
glEnable(GL_ALPHA_TEST); // enable alpha by default
|
|
|
|
glAlphaFunc(GL_GREATER, 0.0f); // default alpha func
|
|
|
|
glAlphaFunc(GL_GREATER, 0.0f); // default alpha func
|
|
|
|
glDisable(GL_DEPTH_TEST); // we are rendering 2D only, we don't need depth buffer
|
|
|
|
glDisable(GL_DEPTH_TEST); // we are rendering 2D only, we don't need depth buffer
|
|
|
|
//glEnable(GL_TEXTURE_2D); // enable textures by default
|
|
|
|
glEnable(GL_TEXTURE_2D); // enable textures by default
|
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
|
|
glShadeModel(GL_SMOOTH);
|
|
|
|
glShadeModel(GL_SMOOTH);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
@ -25,6 +23,10 @@ void Graphics::init()
|
|
|
|
|
|
|
|
|
|
|
|
logInfo("GPU ", glGetString(GL_RENDERER));
|
|
|
|
logInfo("GPU ", glGetString(GL_RENDERER));
|
|
|
|
logInfo("OpenGL ", glGetString(GL_VERSION));
|
|
|
|
logInfo("OpenGL ", glGetString(GL_VERSION));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_drawing = false;
|
|
|
|
|
|
|
|
bindColor(Color::white);
|
|
|
|
|
|
|
|
m_opacity = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::terminate()
|
|
|
|
void Graphics::terminate()
|
|
|
@ -69,8 +71,6 @@ void Graphics::resize(const Size& size)
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::restoreViewport()
|
|
|
|
void Graphics::restoreViewport()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
disableDrawing();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const int& width = m_screenSize.width();
|
|
|
|
const int& width = m_screenSize.width();
|
|
|
|
const int& height = m_screenSize.height();
|
|
|
|
const int& height = m_screenSize.height();
|
|
|
|
|
|
|
|
|
|
|
@ -98,35 +98,16 @@ void Graphics::beginRender()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glLoadIdentity();
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
|
|
|
|
// bind white color by default
|
|
|
|
|
|
|
|
glColor4ubv(Color::white.rgbaPtr());
|
|
|
|
|
|
|
|
m_boundColor = Color::white;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::endRender()
|
|
|
|
void Graphics::endRender()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
disableDrawing();
|
|
|
|
assert(!m_drawing);
|
|
|
|
|
|
|
|
|
|
|
|
// clear any bound texture
|
|
|
|
|
|
|
|
m_boundTexture.reset();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::disableDrawing()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(m_drawMode != DRAW_NONE) {
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
m_drawMode = DRAW_NONE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_boundTexture.reset();
|
|
|
|
|
|
|
|
glColor4ubv(Color::white.rgbaPtr());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::drawTexturedRect(const Rect& screenCoords,
|
|
|
|
void Graphics::drawTexturedRect(const Rect& screenCoords,
|
|
|
|
const TexturePtr& texture,
|
|
|
|
const TexturePtr& texture,
|
|
|
|
const Rect& textureCoords,
|
|
|
|
const Rect& textureCoords)
|
|
|
|
const Color& color)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(screenCoords.isEmpty() || textureCoords.isEmpty())
|
|
|
|
if(screenCoords.isEmpty() || textureCoords.isEmpty())
|
|
|
|
return;
|
|
|
|
return;
|
|
|
@ -150,21 +131,33 @@ void Graphics::drawTexturedRect(const Rect& screenCoords,
|
|
|
|
textureLeft = (float)textureCoords.left() / textureSize.width();
|
|
|
|
textureLeft = (float)textureCoords.left() / textureSize.width();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bindTexture(texture, color);
|
|
|
|
if(!m_drawing) {
|
|
|
|
|
|
|
|
bindTexture(texture);
|
|
|
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
glTexCoord2f(textureLeft, textureTop); glVertex2i(left, top);
|
|
|
|
glTexCoord2f(textureLeft, textureTop); glVertex2i(left, top);
|
|
|
|
glTexCoord2f(textureLeft, textureBottom); glVertex2i(left, bottom);
|
|
|
|
glTexCoord2f(textureLeft, textureBottom); glVertex2i(left, bottom);
|
|
|
|
glTexCoord2f(textureRight, textureBottom); glVertex2i(right, bottom);
|
|
|
|
glTexCoord2f(textureRight, textureBottom); glVertex2i(right, bottom);
|
|
|
|
glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
|
|
|
|
glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!m_drawing)
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
|
|
|
|
void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
|
|
|
|
const TexturePtr& texture,
|
|
|
|
const TexturePtr& texture,
|
|
|
|
const Rect& textureCoords,
|
|
|
|
const Rect& textureCoords)
|
|
|
|
const Color& color)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(screenCoords.isEmpty() || textureCoords.isEmpty())
|
|
|
|
if(screenCoords.isEmpty() || textureCoords.isEmpty())
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!m_drawing) {
|
|
|
|
|
|
|
|
bindTexture(texture);
|
|
|
|
|
|
|
|
startDrawing();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// render many repeated texture rects
|
|
|
|
// render many repeated texture rects
|
|
|
|
Rect virtualScreenCoords(0,0,screenCoords.size());
|
|
|
|
Rect virtualScreenCoords(0,0,screenCoords.size());
|
|
|
|
for(int y = 0; y <= virtualScreenCoords.height(); y += textureCoords.height()) {
|
|
|
|
for(int y = 0; y <= virtualScreenCoords.height(); y += textureCoords.height()) {
|
|
|
@ -185,14 +178,19 @@ void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
partialCoords.translate(screenCoords.topLeft());
|
|
|
|
partialCoords.translate(screenCoords.topLeft());
|
|
|
|
drawTexturedRect(partialCoords, texture, partialTextureCoords, color);
|
|
|
|
drawTexturedRect(partialCoords, texture, partialTextureCoords);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!m_drawing)
|
|
|
|
|
|
|
|
stopDrawing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::drawFilledRect(const Rect& screenCoords,
|
|
|
|
void Graphics::drawFilledRect(const Rect& screenCoords,
|
|
|
|
const Color& color)
|
|
|
|
const Color& color)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(!m_drawing);
|
|
|
|
|
|
|
|
|
|
|
|
if(screenCoords.isEmpty())
|
|
|
|
if(screenCoords.isEmpty())
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
@ -202,11 +200,18 @@ void Graphics::drawFilledRect(const Rect& screenCoords,
|
|
|
|
int top = screenCoords.top();
|
|
|
|
int top = screenCoords.top();
|
|
|
|
int left = screenCoords.left();
|
|
|
|
int left = screenCoords.left();
|
|
|
|
|
|
|
|
|
|
|
|
bindColor(color);
|
|
|
|
|
|
|
|
|
|
|
|
glColor4ubv(color.rgbaPtr());
|
|
|
|
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
|
|
|
|
|
|
|
glVertex2i(left, top);
|
|
|
|
glVertex2i(left, top);
|
|
|
|
glVertex2i(left, bottom);
|
|
|
|
glVertex2i(left, bottom);
|
|
|
|
glVertex2i(right, bottom);
|
|
|
|
glVertex2i(right, bottom);
|
|
|
|
glVertex2i(right, top);
|
|
|
|
glVertex2i(right, top);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -214,6 +219,8 @@ void Graphics::drawBoundingRect(const Rect& screenCoords,
|
|
|
|
const Color& color,
|
|
|
|
const Color& color,
|
|
|
|
int innerLineWidth)
|
|
|
|
int innerLineWidth)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
assert(!m_drawing);
|
|
|
|
|
|
|
|
|
|
|
|
if(2 * innerLineWidth > screenCoords.height() || screenCoords.isEmpty())
|
|
|
|
if(2 * innerLineWidth > screenCoords.height() || screenCoords.isEmpty())
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
@ -223,7 +230,9 @@ void Graphics::drawBoundingRect(const Rect& screenCoords,
|
|
|
|
int top = screenCoords.top();
|
|
|
|
int top = screenCoords.top();
|
|
|
|
int left = screenCoords.left();
|
|
|
|
int left = screenCoords.left();
|
|
|
|
|
|
|
|
|
|
|
|
bindColor(color);
|
|
|
|
glColor4ubv(color.rgbaPtr());
|
|
|
|
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
|
|
|
|
|
|
|
// top line
|
|
|
|
// top line
|
|
|
|
glVertex2i(left, top);
|
|
|
|
glVertex2i(left, top);
|
|
|
@ -248,40 +257,34 @@ void Graphics::drawBoundingRect(const Rect& screenCoords,
|
|
|
|
glVertex2i(right , bottom - innerLineWidth);
|
|
|
|
glVertex2i(right , bottom - innerLineWidth);
|
|
|
|
glVertex2i(right - innerLineWidth, bottom - innerLineWidth);
|
|
|
|
glVertex2i(right - innerLineWidth, bottom - innerLineWidth);
|
|
|
|
glVertex2i(right - innerLineWidth, top + innerLineWidth);
|
|
|
|
glVertex2i(right - innerLineWidth, top + innerLineWidth);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::bindColor(const Color& color)
|
|
|
|
void Graphics::bindColor(const Color& color)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// switch drawing to colored quads
|
|
|
|
Color tmp = color;
|
|
|
|
if(m_drawMode != DRAW_COLOR_QUADS || m_boundColor != color) {
|
|
|
|
tmp.setAlpha(std::min((uint8)m_opacity, color.a()));
|
|
|
|
if(m_drawMode != DRAW_NONE)
|
|
|
|
glColor4ubv(tmp.rgbaPtr());
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
if(m_boundColor != color) {
|
|
|
|
|
|
|
|
glColor4ubv(color.rgbaPtr());
|
|
|
|
|
|
|
|
m_boundColor = color;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
|
|
|
m_drawMode = DRAW_COLOR_QUADS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Graphics::bindTexture(const TexturePtr& texture, const Color& color)
|
|
|
|
void Graphics::bindTexture(const TexturePtr& texture)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// switch drawing to textured quads
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
|
|
|
|
if(m_drawMode != DRAW_TEXTURE_QUADS || m_boundTexture != texture || m_boundColor != color) {
|
|
|
|
}
|
|
|
|
if(m_drawMode != DRAW_NONE)
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
void Graphics::startDrawing()
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
{
|
|
|
|
if(m_boundTexture != texture) {
|
|
|
|
assert(!m_drawing);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
m_boundTexture = texture;
|
|
|
|
m_drawing = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(m_boundColor != color) {
|
|
|
|
|
|
|
|
glColor4ubv(color.rgbaPtr());
|
|
|
|
void Graphics::stopDrawing()
|
|
|
|
m_boundColor = color;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
if(m_drawing) {
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glEnd();
|
|
|
|
m_drawMode = DRAW_TEXTURE_QUADS;
|
|
|
|
m_drawing = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|