mirror of
https://github.com/edubart/otclient.git
synced 2025-10-20 06:23:26 +02:00
rework on graphics.cpp, implement some GFX with lua
This commit is contained in:
@@ -28,8 +28,6 @@ AnimatedTexture::AnimatedTexture(int width, int height, int channels, int numFra
|
||||
|
||||
AnimatedTexture::~AnimatedTexture()
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
glDeleteTextures(m_numFrames, m_framesTextureId);
|
||||
delete[] m_framesTextureId;
|
||||
delete[] m_framesDelay;
|
||||
@@ -38,8 +36,6 @@ AnimatedTexture::~AnimatedTexture()
|
||||
|
||||
void AnimatedTexture::enableBilinearFilter()
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
for(int i=0;i<m_numFrames;++i) {
|
||||
glBindTexture(GL_TEXTURE_2D, m_framesTextureId[i]);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
|
@@ -116,6 +116,9 @@ void BorderImage::draw(const Rect& screenCoords)
|
||||
Rect rectCoords;
|
||||
Size centerSize = screenCoords.size() - m_bordersSize;
|
||||
|
||||
g_graphics.bindTexture(m_texture);
|
||||
g_graphics.startDrawing();
|
||||
|
||||
// first the center
|
||||
if(centerSize.area() > 0) {
|
||||
rectCoords = Rect(screenCoords.left() + m_leftBorderTexCoords.width(),
|
||||
@@ -175,4 +178,6 @@ void BorderImage::draw(const Rect& screenCoords)
|
||||
screenCoords.top() + m_topRightCornerTexCoords.height() + centerSize.height(),
|
||||
m_bottomRightCornerTexCoords.size());
|
||||
g_graphics.drawTexturedRect(rectCoords, m_texture, m_bottomRightCornerTexCoords);
|
||||
|
||||
g_graphics.stopDrawing();
|
||||
}
|
@@ -62,6 +62,10 @@ void Font::renderText(const std::string& text,
|
||||
Size textBoxSize;
|
||||
const std::vector<Point>& glyphsPositions = calculateGlyphsPositions(text, align, &textBoxSize);
|
||||
|
||||
g_graphics.bindColor(color);
|
||||
g_graphics.bindTexture(m_texture);
|
||||
g_graphics.startDrawing();
|
||||
|
||||
for(int i = 0; i < textLenght; ++i) {
|
||||
int glyph = (uchar)text[i];
|
||||
|
||||
@@ -122,8 +126,12 @@ void Font::renderText(const std::string& text,
|
||||
}
|
||||
|
||||
// render glyph
|
||||
g_graphics.drawTexturedRect(glyphScreenCoords, m_texture, glyphTextureCoords, color);
|
||||
g_graphics.drawTexturedRect(glyphScreenCoords, m_texture, glyphTextureCoords);
|
||||
}
|
||||
|
||||
g_graphics.stopDrawing();
|
||||
|
||||
g_graphics.bindColor(Color::white);
|
||||
}
|
||||
|
||||
const std::vector<Point>& Font::calculateGlyphsPositions(const std::string& text,
|
||||
|
@@ -13,8 +13,6 @@ PFNGLCHECKFRAMEBUFFERSTATUSPROC oglCheckFramebufferStatus = 0;
|
||||
|
||||
FrameBuffer::FrameBuffer(int width, int height)
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
m_fbo = 0;
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
@@ -68,8 +66,6 @@ FrameBuffer::FrameBuffer(int width, int height)
|
||||
|
||||
FrameBuffer::~FrameBuffer()
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
glDeleteTextures(1, &m_fboTexture);
|
||||
if(m_fbo)
|
||||
oglDeleteFramebuffers(1, &m_fbo);
|
||||
@@ -77,8 +73,6 @@ FrameBuffer::~FrameBuffer()
|
||||
|
||||
void FrameBuffer::bind()
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
if(!m_fallbackOldImp) {
|
||||
// bind framebuffer
|
||||
oglBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo);
|
||||
@@ -101,8 +95,6 @@ void FrameBuffer::bind()
|
||||
|
||||
void FrameBuffer::unbind()
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
if(!m_fallbackOldImp) {
|
||||
// bind back buffer again
|
||||
oglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
|
||||
@@ -127,9 +119,6 @@ void FrameBuffer::unbind()
|
||||
|
||||
void FrameBuffer::draw(int x, int y, int width, int height)
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glColor4ubv(Color::white.rgbaPtr());
|
||||
glBindTexture(GL_TEXTURE_2D, m_fboTexture);
|
||||
glBegin(GL_QUADS);
|
||||
|
@@ -10,13 +10,11 @@ Graphics g_graphics;
|
||||
|
||||
void Graphics::init()
|
||||
{
|
||||
m_drawMode = DRAW_NONE;
|
||||
|
||||
// setup opengl
|
||||
glEnable(GL_ALPHA_TEST); // enable alpha by default
|
||||
glAlphaFunc(GL_GREATER, 0.0f); // default alpha func
|
||||
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);
|
||||
glShadeModel(GL_SMOOTH);
|
||||
glEnable(GL_BLEND);
|
||||
@@ -25,6 +23,10 @@ void Graphics::init()
|
||||
|
||||
logInfo("GPU ", glGetString(GL_RENDERER));
|
||||
logInfo("OpenGL ", glGetString(GL_VERSION));
|
||||
|
||||
m_drawing = false;
|
||||
bindColor(Color::white);
|
||||
m_opacity = 255;
|
||||
}
|
||||
|
||||
void Graphics::terminate()
|
||||
@@ -69,8 +71,6 @@ void Graphics::resize(const Size& size)
|
||||
|
||||
void Graphics::restoreViewport()
|
||||
{
|
||||
disableDrawing();
|
||||
|
||||
const int& width = m_screenSize.width();
|
||||
const int& height = m_screenSize.height();
|
||||
|
||||
@@ -98,35 +98,16 @@ void Graphics::beginRender()
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glLoadIdentity();
|
||||
|
||||
// bind white color by default
|
||||
glColor4ubv(Color::white.rgbaPtr());
|
||||
m_boundColor = Color::white;
|
||||
}
|
||||
|
||||
void Graphics::endRender()
|
||||
{
|
||||
disableDrawing();
|
||||
|
||||
// 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());
|
||||
}
|
||||
assert(!m_drawing);
|
||||
}
|
||||
|
||||
void Graphics::drawTexturedRect(const Rect& screenCoords,
|
||||
const TexturePtr& texture,
|
||||
const Rect& textureCoords,
|
||||
const Color& color)
|
||||
const Rect& textureCoords)
|
||||
{
|
||||
if(screenCoords.isEmpty() || textureCoords.isEmpty())
|
||||
return;
|
||||
@@ -150,21 +131,33 @@ void Graphics::drawTexturedRect(const Rect& screenCoords,
|
||||
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, textureBottom); glVertex2i(left, bottom);
|
||||
glTexCoord2f(textureRight, textureBottom); glVertex2i(right, bottom);
|
||||
glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
|
||||
|
||||
if(!m_drawing)
|
||||
glEnd();
|
||||
|
||||
}
|
||||
|
||||
void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
|
||||
const TexturePtr& texture,
|
||||
const Rect& textureCoords,
|
||||
const Color& color)
|
||||
const Rect& textureCoords)
|
||||
{
|
||||
if(screenCoords.isEmpty() || textureCoords.isEmpty())
|
||||
return;
|
||||
|
||||
if(!m_drawing) {
|
||||
bindTexture(texture);
|
||||
startDrawing();
|
||||
}
|
||||
|
||||
// render many repeated texture rects
|
||||
Rect virtualScreenCoords(0,0,screenCoords.size());
|
||||
for(int y = 0; y <= virtualScreenCoords.height(); y += textureCoords.height()) {
|
||||
@@ -185,14 +178,19 @@ void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
|
||||
}
|
||||
|
||||
partialCoords.translate(screenCoords.topLeft());
|
||||
drawTexturedRect(partialCoords, texture, partialTextureCoords, color);
|
||||
drawTexturedRect(partialCoords, texture, partialTextureCoords);
|
||||
}
|
||||
}
|
||||
|
||||
if(!m_drawing)
|
||||
stopDrawing();
|
||||
}
|
||||
|
||||
void Graphics::drawFilledRect(const Rect& screenCoords,
|
||||
const Color& color)
|
||||
{
|
||||
assert(!m_drawing);
|
||||
|
||||
if(screenCoords.isEmpty())
|
||||
return;
|
||||
|
||||
@@ -202,11 +200,18 @@ void Graphics::drawFilledRect(const Rect& screenCoords,
|
||||
int top = screenCoords.top();
|
||||
int left = screenCoords.left();
|
||||
|
||||
bindColor(color);
|
||||
|
||||
glColor4ubv(color.rgbaPtr());
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
glVertex2i(left, top);
|
||||
glVertex2i(left, bottom);
|
||||
glVertex2i(right, bottom);
|
||||
glVertex2i(right, top);
|
||||
|
||||
glEnd();
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
|
||||
@@ -214,6 +219,8 @@ void Graphics::drawBoundingRect(const Rect& screenCoords,
|
||||
const Color& color,
|
||||
int innerLineWidth)
|
||||
{
|
||||
assert(!m_drawing);
|
||||
|
||||
if(2 * innerLineWidth > screenCoords.height() || screenCoords.isEmpty())
|
||||
return;
|
||||
|
||||
@@ -223,7 +230,9 @@ void Graphics::drawBoundingRect(const Rect& screenCoords,
|
||||
int top = screenCoords.top();
|
||||
int left = screenCoords.left();
|
||||
|
||||
bindColor(color);
|
||||
glColor4ubv(color.rgbaPtr());
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
// top line
|
||||
glVertex2i(left, top);
|
||||
@@ -248,40 +257,34 @@ void Graphics::drawBoundingRect(const Rect& screenCoords,
|
||||
glVertex2i(right , bottom - innerLineWidth);
|
||||
glVertex2i(right - innerLineWidth, bottom - innerLineWidth);
|
||||
glVertex2i(right - innerLineWidth, top + innerLineWidth);
|
||||
|
||||
glEnd();
|
||||
}
|
||||
|
||||
void Graphics::bindColor(const Color& color)
|
||||
{
|
||||
// switch drawing to colored quads
|
||||
if(m_drawMode != DRAW_COLOR_QUADS || m_boundColor != color) {
|
||||
if(m_drawMode != DRAW_NONE)
|
||||
glEnd();
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
if(m_boundColor != color) {
|
||||
glColor4ubv(color.rgbaPtr());
|
||||
m_boundColor = color;
|
||||
}
|
||||
glBegin(GL_QUADS);
|
||||
m_drawMode = DRAW_COLOR_QUADS;
|
||||
Color tmp = color;
|
||||
tmp.setAlpha(std::min((uint8)m_opacity, color.a()));
|
||||
glColor4ubv(tmp.rgbaPtr());
|
||||
}
|
||||
|
||||
void Graphics::bindTexture(const TexturePtr& texture)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
|
||||
}
|
||||
|
||||
void Graphics::startDrawing()
|
||||
{
|
||||
assert(!m_drawing);
|
||||
glBegin(GL_QUADS);
|
||||
m_drawing = true;
|
||||
}
|
||||
|
||||
void Graphics::stopDrawing()
|
||||
{
|
||||
if(m_drawing) {
|
||||
glEnd();
|
||||
m_drawing = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Graphics::bindTexture(const TexturePtr& texture, const Color& color)
|
||||
{
|
||||
// switch drawing to textured quads
|
||||
if(m_drawMode != DRAW_TEXTURE_QUADS || m_boundTexture != texture || m_boundColor != color) {
|
||||
if(m_drawMode != DRAW_NONE)
|
||||
glEnd();
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
if(m_boundTexture != texture) {
|
||||
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
|
||||
m_boundTexture = texture;
|
||||
}
|
||||
if(m_boundColor != color) {
|
||||
glColor4ubv(color.rgbaPtr());
|
||||
m_boundColor = color;
|
||||
}
|
||||
glBegin(GL_QUADS);
|
||||
m_drawMode = DRAW_TEXTURE_QUADS;
|
||||
}
|
||||
}
|
||||
|
@@ -5,15 +5,6 @@
|
||||
|
||||
class Graphics
|
||||
{
|
||||
enum DrawMode {
|
||||
DRAW_NONE = 0,
|
||||
DRAW_QUADS = 1,
|
||||
DRAW_TEXTURE = 2,
|
||||
DRAW_COLORED = 4,
|
||||
DRAW_COLOR_QUADS = DRAW_QUADS | DRAW_COLORED,
|
||||
DRAW_TEXTURE_QUADS = DRAW_QUADS | DRAW_TEXTURE | DRAW_COLORED
|
||||
};
|
||||
|
||||
public:
|
||||
/// Initialize default OpenGL states
|
||||
void init();
|
||||
@@ -35,18 +26,18 @@ public:
|
||||
|
||||
/// Called after every render
|
||||
void endRender();
|
||||
void disableDrawing();
|
||||
|
||||
void bindColor(const Color& color);
|
||||
void bindTexture(const TexturePtr& texture);
|
||||
|
||||
// drawing API
|
||||
void drawTexturedRect(const Rect& screenCoords,
|
||||
const TexturePtr& texture,
|
||||
const Rect& textureCoords = Rect(),
|
||||
const Color& color = Color::white);
|
||||
const Rect& textureCoords = Rect());
|
||||
|
||||
void drawRepeatedTexturedRect(const Rect& screenCoords,
|
||||
const TexturePtr& texture,
|
||||
const Rect& textureCoords,
|
||||
const Color& color = Color::white);
|
||||
const Rect& textureCoords);
|
||||
|
||||
void drawFilledRect(const Rect& screenCoords,
|
||||
const Color& color);
|
||||
@@ -57,14 +48,16 @@ public:
|
||||
|
||||
const Size& getScreenSize() const { return m_screenSize; }
|
||||
|
||||
private:
|
||||
void bindTexture(const TexturePtr& texture, const Color& color = Color::white);
|
||||
void bindColor(const Color& color);
|
||||
void startDrawing();
|
||||
void stopDrawing();
|
||||
|
||||
TexturePtr m_boundTexture;
|
||||
Color m_boundColor;
|
||||
int getOpacity() const { return m_opacity; }
|
||||
void setOpacity(int opacity) { m_opacity = opacity; }
|
||||
|
||||
private:
|
||||
bool m_drawing;
|
||||
int m_opacity;
|
||||
Size m_screenSize;
|
||||
DrawMode m_drawMode;
|
||||
};
|
||||
|
||||
extern Graphics g_graphics;
|
||||
|
@@ -11,8 +11,6 @@ Texture::Texture(int width, int height, int channels, uchar *pixels)
|
||||
|
||||
uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int height)
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
// get smax texture size supported by the driver
|
||||
static GLint maxTexSize = -1;
|
||||
if(maxTexSize == -1)
|
||||
@@ -95,8 +93,6 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
// free texture from gl memory
|
||||
if(m_textureId)
|
||||
glDeleteTextures(1, &m_textureId);
|
||||
@@ -104,8 +100,6 @@ Texture::~Texture()
|
||||
|
||||
void Texture::enableBilinearFilter()
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
// enable smooth texture
|
||||
glBindTexture(GL_TEXTURE_2D, m_textureId);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
@@ -114,8 +108,6 @@ void Texture::enableBilinearFilter()
|
||||
|
||||
uchar* Texture::getPixels()
|
||||
{
|
||||
g_graphics.disableDrawing();
|
||||
|
||||
// copy pixels from opengl memory
|
||||
uchar* pixels = new uchar[m_glSize.area()*4];
|
||||
glBindTexture(GL_TEXTURE_2D, m_textureId);
|
||||
|
Reference in New Issue
Block a user