tibia-client/src/framework/graphics/framebuffer.cpp

157 lines
4.4 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
2017-01-13 11:47:07 +01:00
* Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* 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.
*/
2011-08-14 04:09:11 +02:00
#include "framebuffer.h"
#include "graphics.h"
2011-08-15 16:06:15 +02:00
#include "texture.h"
#include <framework/platform/platformwindow.h>
#include <framework/core/application.h>
2011-08-15 16:06:15 +02:00
2011-12-25 00:14:12 +01:00
uint FrameBuffer::boundFbo = 0;
2010-11-23 16:30:43 +01:00
2012-01-30 01:00:12 +01:00
FrameBuffer::FrameBuffer()
{
internalCreate();
2012-01-30 01:00:12 +01:00
}
void FrameBuffer::internalCreate()
{
m_prevBoundFbo = 0;
m_fbo = 0;
if(g_graphics.canUseFBO()) {
glGenFramebuffers(1, &m_fbo);
if(!m_fbo)
2012-06-01 22:39:23 +02:00
g_logger.fatal("Unable to create framebuffer object");
}
}
2011-12-25 00:14:12 +01:00
FrameBuffer::~FrameBuffer()
{
#ifndef NDEBUG
assert(!g_app.isTerminated());
#endif
if(g_graphics.ok() && m_fbo != 0)
glDeleteFramebuffers(1, &m_fbo);
2011-12-25 00:14:12 +01:00
}
2010-11-23 16:30:43 +01:00
2011-12-25 00:14:12 +01:00
void FrameBuffer::resize(const Size& size)
{
assert(size.isValid());
2012-01-30 01:00:12 +01:00
2012-01-17 06:36:25 +01:00
if(m_texture && m_texture->getSize() == size)
return;
2012-06-08 18:58:08 +02:00
m_texture = TexturePtr(new Texture(size));
2012-06-22 07:26:22 +02:00
m_texture->setSmooth(m_smooth);
2012-06-02 16:43:27 +02:00
m_texture->setUpsideDown(true);
2011-08-12 04:04:28 +02:00
if(m_fbo) {
internalBind();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture->getId(), 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
2012-06-01 22:39:23 +02:00
g_logger.fatal("Unable to setup framebuffer object");
internalRelease();
} else {
2012-06-08 18:58:08 +02:00
if(m_backuping) {
m_screenBackup = TexturePtr(new Texture(size));
m_screenBackup->setUpsideDown(true);
}
}
}
void FrameBuffer::bind()
2010-11-23 16:30:43 +01:00
{
g_painter->saveAndResetState();
2011-12-25 00:14:12 +01:00
internalBind();
2012-06-14 20:26:55 +02:00
g_painter->setResolution(m_texture->getSize());
2010-11-23 16:30:43 +01:00
}
2011-12-07 01:31:55 +01:00
void FrameBuffer::release()
2010-11-23 16:30:43 +01:00
{
2011-12-25 00:14:12 +01:00
internalRelease();
g_painter->restoreSavedState();
}
2010-11-23 16:30:43 +01:00
void FrameBuffer::draw()
{
Rect rect(0,0, getSize());
g_painter->drawTexturedRect(rect, m_texture, rect);
}
2012-01-30 01:00:12 +01:00
void FrameBuffer::draw(const Rect& dest, const Rect& src)
{
g_painter->drawTexturedRect(dest, m_texture, src);
2012-01-30 01:00:12 +01:00
}
2011-12-07 01:31:55 +01:00
void FrameBuffer::draw(const Rect& dest)
{
g_painter->drawTexturedRect(dest, m_texture, Rect(0,0, getSize()));
2010-11-23 16:30:43 +01:00
}
2011-12-25 00:14:12 +01:00
void FrameBuffer::internalBind()
{
if(m_fbo) {
assert(boundFbo != m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
m_prevBoundFbo = boundFbo;
boundFbo = m_fbo;
2012-06-08 18:58:08 +02:00
} else if(m_backuping) {
// backup screen color buffer into a texture
m_screenBackup->copyFromScreen(Rect(0, 0, getSize()));
}
2011-12-25 00:14:12 +01:00
}
void FrameBuffer::internalRelease()
{
if(m_fbo) {
assert(boundFbo == m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_prevBoundFbo);
boundFbo = m_prevBoundFbo;
} else {
Rect screenRect(0, 0, getSize());
// copy the drawn color buffer into the framebuffer texture
m_texture->copyFromScreen(screenRect);
// restore screen original content
2012-06-08 18:58:08 +02:00
if(m_backuping) {
glDisable(GL_BLEND);
g_painter->setColor(Color::white);
2012-06-08 18:58:08 +02:00
g_painter->drawTexturedRect(screenRect, m_screenBackup, screenRect);
glEnable(GL_BLEND);
2012-06-08 18:58:08 +02:00
}
}
2011-12-25 00:14:12 +01:00
}
Size FrameBuffer::getSize()
{
if(m_fbo == 0) {
// the buffer size is limited by the window size
return Size(std::min<int>(m_texture->getWidth(), g_window.getWidth()),
std::min<int>(m_texture->getHeight(), g_window.getHeight()));
}
return m_texture->getSize();
}