mkxp-freebird/src/graphics.cpp

1244 lines
25 KiB
C++
Raw Normal View History

2013-09-01 14:27:21 +00:00
/*
** graphics.cpp
**
** This file is part of mkxp.
**
2021-09-06 18:50:44 +00:00
** Copyright (C) 2021 Amaryllis Kulla <amaryllis.kulla@protonmail.com>
2013-09-01 14:27:21 +00:00
**
** mkxp is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** mkxp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/
#include "graphics.h"
#include "util.h"
#include "gl-util.h"
#include "sharedstate.h"
#include "config.h"
2013-09-01 14:27:21 +00:00
#include "glstate.h"
#include "shader.h"
#include "scene.h"
#include "quad.h"
#include "eventthread.h"
#include "texpool.h"
#include "bitmap.h"
#include "etc-internal.h"
#include "disposable.h"
#include "intrulist.h"
2013-09-01 14:27:21 +00:00
#include "binding.h"
#include "debugwriter.h"
2021-10-06 21:43:55 +00:00
#include "steamshim/steamshim_child.h"
2013-09-01 14:27:21 +00:00
#include <SDL_video.h>
#include <SDL_timer.h>
#include <SDL_image.h>
2013-09-01 14:27:21 +00:00
#include <time.h>
#include <sys/time.h>
#include <errno.h>
2013-12-08 12:19:22 +00:00
#include <algorithm>
#define DEF_SCREEN_W (rgssVer == 1 ? 640 : 544)
#define DEF_SCREEN_H (rgssVer == 1 ? 480 : 416)
#define DEF_FRAMERATE (rgssVer == 1 ? 40 : 60)
2013-09-01 14:27:21 +00:00
struct PingPong
{
2013-09-06 10:26:41 +00:00
TEXFBO rt[2];
2013-12-08 12:19:22 +00:00
uint8_t srcInd, dstInd;
2013-09-01 14:27:21 +00:00
int screenW, screenH;
PingPong(int screenW, int screenH)
: srcInd(0), dstInd(1),
screenW(screenW), screenH(screenH)
{
for (int i = 0; i < 2; ++i)
{
2013-09-06 10:26:41 +00:00
TEXFBO::init(rt[i]);
TEXFBO::allocEmpty(rt[i], screenW, screenH);
TEXFBO::linkFBO(rt[i]);
gl.ClearColor(0, 0, 0, 1);
FBO::clear();
2013-09-01 14:27:21 +00:00
}
}
~PingPong()
{
for (int i = 0; i < 2; ++i)
2013-09-06 10:26:41 +00:00
TEXFBO::fini(rt[i]);
2013-09-01 14:27:21 +00:00
}
TEXFBO &backBuffer()
2013-09-01 14:27:21 +00:00
{
return rt[srcInd];
}
TEXFBO &frontBuffer()
{
return rt[dstInd];
2013-09-01 14:27:21 +00:00
}
/* Better not call this during render cycles */
void resize(int width, int height)
{
screenW = width;
screenH = height;
2013-09-01 14:27:21 +00:00
for (int i = 0; i < 2; ++i)
TEXFBO::allocEmpty(rt[i], width, height);
2013-09-01 14:27:21 +00:00
}
void startRender()
{
bind();
}
void swapRender()
{
2013-12-08 12:19:22 +00:00
std::swap(srcInd, dstInd);
2013-09-01 14:27:21 +00:00
bind();
}
void clearBuffers()
{
glState.clearColor.pushSet(Vec4(0, 0, 0, 1));
for (int i = 0; i < 2; ++i)
{
FBO::bind(rt[i].fbo);
FBO::clear();
}
glState.clearColor.pop();
}
2013-09-01 14:27:21 +00:00
private:
void bind()
{
FBO::bind(rt[dstInd].fbo);
2013-09-01 14:27:21 +00:00
}
};
class ScreenScene : public Scene
{
public:
ScreenScene(int width, int height)
2014-01-01 01:13:02 +00:00
: pp(width, height)
2013-09-01 14:27:21 +00:00
{
updateReso(width, height);
2013-10-06 07:55:27 +00:00
brightEffect = false;
2013-09-01 14:27:21 +00:00
brightnessQuad.setColor(Vec4());
}
void composite()
{
const int w = geometry.rect.w;
const int h = geometry.rect.h;
shState->prepareDraw();
2013-09-01 14:27:21 +00:00
pp.startRender();
glState.viewport.set(IntRect(0, 0, w, h));
2013-09-01 14:27:21 +00:00
FBO::clear();
2013-09-01 14:27:21 +00:00
Scene::composite();
if (brightEffect)
{
SimpleColorShader &shader = shState->shaders().simpleColor;
shader.bind();
shader.applyViewportProj();
shader.setTranslation(Vec2i());
2013-09-01 14:27:21 +00:00
brightnessQuad.draw();
}
}
2015-07-07 13:56:21 +00:00
void requestViewportRender(const Vec4 &c, const Vec4 &f, const Vec4 &t)
2013-09-01 14:27:21 +00:00
{
const IntRect &viewpRect = glState.scissorBox.get();
const IntRect &screenRect = geometry.rect;
const bool toneRGBEffect = t.xyzNotNull();
2015-07-07 13:56:21 +00:00
const bool toneGrayEffect = t.w != 0;
const bool colorEffect = c.w > 0;
const bool flashEffect = f.w > 0;
if (toneGrayEffect)
{
pp.swapRender();
if (!viewpRect.encloses(screenRect))
{
/* Scissor test _does_ affect FBO blit operations,
* and since we're inside the draw cycle, it will
* be turned on, so turn it off temporarily */
glState.scissorTest.pushSet(false);
GLMeta::blitBegin(pp.frontBuffer());
GLMeta::blitSource(pp.backBuffer());
GLMeta::blitRectangle(geometry.rect, Vec2i());
GLMeta::blitEnd();
glState.scissorTest.pop();
}
GrayShader &shader = shState->shaders().gray;
shader.bind();
shader.setGray(t.w);
shader.applyViewportProj();
shader.setTexSize(screenRect.size());
TEX::bind(pp.backBuffer().tex);
glState.blend.pushSet(false);
screenQuad.draw();
glState.blend.pop();
}
if (!toneRGBEffect && !colorEffect && !flashEffect)
return;
2013-09-01 14:27:21 +00:00
FlatColorShader &shader = shState->shaders().flatColor;
2013-09-01 14:27:21 +00:00
shader.bind();
shader.applyViewportProj();
2013-09-01 14:27:21 +00:00
if (toneRGBEffect)
{
/* First split up additive / substractive components */
Vec4 add, sub;
if (t.x > 0)
add.x = t.x;
if (t.y > 0)
add.y = t.y;
if (t.z > 0)
add.z = t.z;
if (t.x < 0)
sub.x = -t.x;
if (t.y < 0)
sub.y = -t.y;
if (t.z < 0)
sub.z = -t.z;
/* Then apply them using hardware blending */
gl.BlendFuncSeparate(GL_ONE, GL_ONE, GL_ZERO, GL_ONE);
if (add.xyzNotNull())
{
gl.BlendEquation(GL_FUNC_ADD);
shader.setColor(add);
screenQuad.draw();
}
if (sub.xyzNotNull())
{
gl.BlendEquation(GL_FUNC_REVERSE_SUBTRACT);
shader.setColor(sub);
screenQuad.draw();
}
}
if (colorEffect || flashEffect)
{
gl.BlendEquation(GL_FUNC_ADD);
gl.BlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
GL_ZERO, GL_ONE);
}
if (colorEffect)
{
shader.setColor(c);
screenQuad.draw();
}
2013-09-01 14:27:21 +00:00
if (flashEffect)
{
shader.setColor(f);
screenQuad.draw();
}
2013-09-01 14:27:21 +00:00
glState.blendMode.refresh();
2013-09-01 14:27:21 +00:00
}
void setBrightness(float norm)
{
brightnessQuad.setColor(Vec4(0, 0, 0, 1.0f - norm));
2013-09-01 14:27:21 +00:00
brightEffect = norm < 1.0f;
2013-09-01 14:27:21 +00:00
}
void updateReso(int width, int height)
{
geometry.rect.w = width;
geometry.rect.h = height;
screenQuad.setTexPosRect(geometry.rect, geometry.rect);
brightnessQuad.setTexPosRect(geometry.rect, geometry.rect);
notifyGeometryChange();
}
void setResolution(int width, int height)
{
pp.resize(width, height);
updateReso(width, height);
}
PingPong &getPP()
{
return pp;
}
private:
PingPong pp;
Quad screenQuad;
2013-10-06 07:55:27 +00:00
2013-09-01 14:27:21 +00:00
Quad brightnessQuad;
bool brightEffect;
};
/* Nanoseconds per second */
#define NS_PER_S 1000000000
2013-09-01 14:27:21 +00:00
struct FPSLimiter
{
uint64_t lastTickCount;
/* ticks per frame */
int64_t tpf;
/* Ticks per second */
const uint64_t tickFreq;
/* Ticks per milisecond */
const uint64_t tickFreqMS;
/* Ticks per nanosecond */
const double tickFreqNS;
bool disabled;
/* Data for frame timing adjustment */
struct
{
/* Last tick count */
uint64_t last;
/* How far behind/in front we are for ideal frame timing */
int64_t idealDiff;
bool resetFlag;
} adj;
2013-09-01 14:27:21 +00:00
FPSLimiter(uint16_t desiredFPS)
: lastTickCount(SDL_GetPerformanceCounter()),
tickFreq(SDL_GetPerformanceFrequency()),
tickFreqMS(tickFreq / 1000),
tickFreqNS((double) tickFreq / NS_PER_S),
disabled(false)
2013-09-01 14:27:21 +00:00
{
setDesiredFPS(desiredFPS);
adj.last = SDL_GetPerformanceCounter();
adj.idealDiff = 0;
adj.resetFlag = false;
2013-09-01 14:27:21 +00:00
}
void setDesiredFPS(uint16_t value)
2013-09-01 14:27:21 +00:00
{
tpf = tickFreq / value;
2013-09-01 14:27:21 +00:00
}
void delay()
{
if (disabled)
return;
int64_t tickDelta = SDL_GetPerformanceCounter() - lastTickCount;
int64_t toDelay = tpf - tickDelta;
/* Compensate for the last delta
* to the ideal timestep */
toDelay -= adj.idealDiff;
2013-09-01 14:27:21 +00:00
if (toDelay < 0)
toDelay = 0;
delayTicks(toDelay);
uint64_t now = lastTickCount = SDL_GetPerformanceCounter();
int64_t diff = now - adj.last;
adj.last = now;
/* Recalculate our temporal position
* relative to the ideal timestep */
adj.idealDiff = diff - tpf + adj.idealDiff;
if (adj.resetFlag)
{
adj.idealDiff = 0;
adj.resetFlag = false;
}
}
void resetFrameAdjust()
{
adj.resetFlag = true;
}
/* If we're more than a full frame's worth
* of ticks behind the ideal timestep,
* there's no choice but to skip frame(s)
* to catch up */
bool frameSkipRequired() const
{
if (disabled)
return false;
return adj.idealDiff > tpf;
}
private:
void delayTicks(uint64_t ticks)
{
#if defined(HAVE_NANOSLEEP)
struct timespec req;
uint64_t nsec = ticks / tickFreqNS;
req.tv_sec = nsec / NS_PER_S;
req.tv_nsec = nsec % NS_PER_S;
errno = 0;
while (nanosleep(&req, &req) == -1)
{
int err = errno;
errno = 0;
if (err == EINTR)
continue;
Debug() << "nanosleep failed. errno:" << err;
SDL_Delay(ticks / tickFreqMS);
break;
}
#else
SDL_Delay(ticks / tickFreqMS);
#endif
2013-09-01 14:27:21 +00:00
}
};
struct GraphicsPrivate
{
2013-09-23 08:39:16 +00:00
/* Screen resolution, ie. the resolution at which
2013-09-23 09:18:20 +00:00
* RGSS renders at (settable with Graphics.resize_screen).
2013-09-23 08:39:16 +00:00
* Can only be changed from within RGSS */
2013-09-01 14:27:21 +00:00
Vec2i scRes;
2013-09-23 08:39:16 +00:00
/* Screen size, to which the rendered frames are scaled up.
* This can be smaller than the window size when fixed aspect
* ratio is enforced */
2013-09-01 14:27:21 +00:00
Vec2i scSize;
2013-09-23 08:39:16 +00:00
/* Actual physical size of the game window */
Vec2i winSize;
/* Offset in the game window at which the scaled game screen
* is blitted inside the game window */
Vec2i scOffset;
2013-09-01 14:27:21 +00:00
ScreenScene screen;
RGSSThreadData *threadData;
SDL_GLContext glCtx;
2013-09-01 14:27:21 +00:00
int frameRate;
int frameCount;
int brightness;
FPSLimiter fpsLimiter;
bool frozen;
2013-09-06 10:26:41 +00:00
TEXFBO frozenScene;
2013-09-01 14:27:21 +00:00
Quad screenQuad;
Vec2i integerScaleFactor;
TEXFBO integerScaleBuffer;
bool integerScaleActive;
bool integerLastMileScaling;
/* Global list of all live Disposables
* (disposed on reset) */
IntruList<Disposable> dispList;
GraphicsPrivate(RGSSThreadData *rtData)
: scRes(DEF_SCREEN_W, DEF_SCREEN_H),
2013-09-01 14:27:21 +00:00
scSize(scRes),
winSize(rtData->config.defScreenW, rtData->config.defScreenH),
2013-09-01 14:27:21 +00:00
screen(scRes.x, scRes.y),
threadData(rtData),
glCtx(SDL_GL_GetCurrentContext()),
frameRate(DEF_FRAMERATE),
2013-09-01 14:27:21 +00:00
frameCount(0),
brightness(255),
fpsLimiter(frameRate),
frozen(false),
integerScaleFactor(0, 0),
integerScaleActive(rtData->config.integerScaling.active),
integerLastMileScaling(rtData->config.integerScaling.lastMileScaling)
2013-09-01 14:27:21 +00:00
{
if (integerScaleActive)
{
integerScaleFactor = Vec2i(1, 1);
rebuildIntegerScaleBuffer();
}
recalculateScreenSize(rtData->config.fixedAspectRatio);
updateScreenResoRatio(rtData);
2013-09-06 10:26:41 +00:00
TEXFBO::init(frozenScene);
TEXFBO::allocEmpty(frozenScene, scRes.x, scRes.y);
TEXFBO::linkFBO(frozenScene);
2013-09-01 14:27:21 +00:00
FloatRect screenRect(0, 0, scRes.x, scRes.y);
screenQuad.setTexPosRect(screenRect, screenRect);
fpsLimiter.resetFrameAdjust();
2013-09-01 14:27:21 +00:00
}
~GraphicsPrivate()
{
2013-09-06 10:26:41 +00:00
TEXFBO::fini(frozenScene);
TEXFBO::fini(integerScaleBuffer);
2013-09-01 14:27:21 +00:00
}
void updateScreenResoRatio(RGSSThreadData *rtData)
2013-09-01 14:27:21 +00:00
{
Vec2 &ratio = rtData->sizeResoRatio;
2013-09-01 14:27:21 +00:00
ratio.x = (float) scRes.x / scSize.x;
ratio.y = (float) scRes.y / scSize.y;
2013-09-23 08:39:16 +00:00
rtData->screenOffset = scOffset;
2013-09-23 08:39:16 +00:00
}
/* Enforces fixed aspect ratio, if desired */
void recalculateScreenSize(bool fixedAspectRatio)
2013-09-23 08:39:16 +00:00
{
scSize = winSize;
if (!fixedAspectRatio && integerLastMileScaling)
2013-09-23 08:39:16 +00:00
{
scOffset = Vec2i(0, 0);
return;
}
/* Last mile scaling disabled: just center the integer scale buffer
* inside the window space */
if (integerScaleActive && !integerLastMileScaling)
{
scOffset.x = (winSize.x - scRes.x * integerScaleFactor.x) / 2;
scOffset.y = (winSize.y - scRes.y * integerScaleFactor.y) / 2;
scSize = Vec2i(scRes.x * integerScaleFactor.x, scRes.y * integerScaleFactor.y);
return;
}
2013-09-23 08:39:16 +00:00
float resRatio = (float) scRes.x / scRes.y;
float winRatio = (float) winSize.x / winSize.y;
if (resRatio > winRatio)
scSize.y = scSize.x / resRatio;
else if (resRatio < winRatio)
scSize.x = scSize.y * resRatio;
scOffset.x = (winSize.x - scSize.x) / 2.f;
scOffset.y = (winSize.y - scSize.y) / 2.f;
2013-09-01 14:27:21 +00:00
}
static int findHighestFittingScale(int base, int target)
{
int scale = 1;
while (base * scale <= target)
scale += 1;
return scale - 1;
}
/* Returns whether a new scale was found */
bool findHighestIntegerScale()
{
Vec2i newScale(findHighestFittingScale(scRes.x, winSize.x),
findHighestFittingScale(scRes.y, winSize.y));
if (threadData->config.fixedAspectRatio)
{
/* Limit both factors to the smaller of the two */
newScale.x = newScale.y = std::min(newScale.x, newScale.y);
}
if (newScale == integerScaleFactor)
return false;
integerScaleFactor = newScale;
return true;
}
void rebuildIntegerScaleBuffer()
{
TEXFBO::fini(integerScaleBuffer);
TEXFBO::init(integerScaleBuffer);
TEXFBO::allocEmpty(integerScaleBuffer, scRes.x * integerScaleFactor.x,
scRes.y * integerScaleFactor.y);
TEXFBO::linkFBO(integerScaleBuffer);
}
bool integerScaleStepApplicable() const
{
if (!integerScaleActive)
return false;
if (integerScaleFactor.x < 1 || integerScaleFactor.y < 1) // XXX should be < 2, this is for testing only
return false;
return true;
}
2013-09-01 14:27:21 +00:00
void checkResize()
{
if (threadData->windowSizeMsg.poll(winSize))
2013-09-01 14:27:21 +00:00
{
/* Query the acutal size in pixels, not units */
SDL_GL_GetDrawableSize(threadData->window, &winSize.x, &winSize.y);
/* Make sure integer buffers are rebuilt before screen offsets are
* calculated so we have the final allocated buffer size ready */
if (integerScaleActive)
if (findHighestIntegerScale())
rebuildIntegerScaleBuffer();
2014-01-04 12:49:08 +00:00
/* some GL drivers change the viewport on window resize */
glState.viewport.refresh();
recalculateScreenSize(threadData);
updateScreenResoRatio(threadData);
SDL_Rect screen = { scOffset.x, scOffset.y, scSize.x, scSize.y };
threadData->ethread->notifyGameScreenChange(screen);
2013-09-01 14:27:21 +00:00
}
}
void checkShutDownReset()
{
shState->checkShutdown();
shState->checkReset();
}
2013-09-01 14:27:21 +00:00
void shutdown()
{
threadData->rqTermAck.set();
shState->texPool().disable();
2013-09-01 14:27:21 +00:00
scriptBinding->terminate();
}
void swapGLBuffer()
{
fpsLimiter.delay();
// Steam overlay renderer blindly assumes that the default
// framebuffer is bound at swap time when it ReadPixels's to
// aquire screenshots.
FBO::unbind();
SDL_GL_SwapWindow(threadData->window);
2013-09-01 14:27:21 +00:00
++frameCount;
threadData->ethread->notifyFrame();
2013-09-01 14:27:21 +00:00
}
void compositeToBuffer(TEXFBO &buffer)
2013-09-01 14:27:21 +00:00
{
screen.composite();
GLMeta::blitBegin(buffer);
2014-07-16 02:53:08 +00:00
GLMeta::blitSource(screen.getPP().frontBuffer());
GLMeta::blitRectangle(IntRect(0, 0, scRes.x, scRes.y), Vec2i());
2014-07-16 02:53:08 +00:00
GLMeta::blitEnd();
2013-09-01 14:27:21 +00:00
}
void metaBlitBufferFlippedScaled()
2013-09-01 14:27:21 +00:00
{
metaBlitBufferFlippedScaled(scRes);
}
void metaBlitBufferFlippedScaled(const Vec2i &sourceSize, bool forceNearestNeighbour = false)
{
GLMeta::blitRectangle(IntRect(0, 0, sourceSize.x, sourceSize.y),
IntRect(scOffset.x, scSize.y+scOffset.y, scSize.x, -scSize.y),
!forceNearestNeighbour && threadData->config.smoothScaling);
2013-09-01 14:27:21 +00:00
}
void redrawScreen()
{
screen.composite();
// maybe unspaghetti this later
if (integerScaleStepApplicable() && !integerLastMileScaling)
{
GLMeta::blitBeginScreen(winSize);
GLMeta::blitSource(screen.getPP().frontBuffer());
FBO::clear();
metaBlitBufferFlippedScaled(scRes, true);
GLMeta::blitEnd();
swapGLBuffer();
return;
}
if (integerScaleStepApplicable())
{
assert(integerScaleBuffer.tex != TEX::ID(0));
GLMeta::blitBegin(integerScaleBuffer);
GLMeta::blitSource(screen.getPP().frontBuffer());
GLMeta::blitRectangle(IntRect(0, 0, scRes.x, scRes.y),
IntRect(0, 0, integerScaleBuffer.width, integerScaleBuffer.height),
false);
GLMeta::blitEnd();
}
GLMeta::blitBeginScreen(winSize);
Vec2i sourceSize;
if (integerScaleActive)
{
GLMeta::blitSource(integerScaleBuffer);
sourceSize = Vec2i(integerScaleBuffer.width, integerScaleBuffer.height);
}
else
{
GLMeta::blitSource(screen.getPP().frontBuffer());
sourceSize = scRes;
}
FBO::clear();
metaBlitBufferFlippedScaled(sourceSize);
2014-07-16 02:53:08 +00:00
GLMeta::blitEnd();
2013-09-01 14:27:21 +00:00
swapGLBuffer();
}
void checkSyncLock()
{
if (!threadData->syncPoint.mainSyncLocked())
return;
/* Releasing the GL context before sleeping and making it
* current again on wakeup seems to avoid the context loss
* when the app moves into the background on Android */
SDL_GL_MakeCurrent(threadData->window, 0);
threadData->syncPoint.waitMainSync();
SDL_GL_MakeCurrent(threadData->window, glCtx);
fpsLimiter.resetFrameAdjust();
}
2013-09-01 14:27:21 +00:00
};
Graphics::Graphics(RGSSThreadData *data)
{
p = new GraphicsPrivate(data);
2013-10-17 00:18:16 +00:00
if (data->config.syncToRefreshrate)
{
p->frameRate = data->refreshRate;
p->fpsLimiter.disabled = true;
}
else if (data->config.fixedFramerate > 0)
{
2013-10-17 00:18:16 +00:00
p->fpsLimiter.setDesiredFPS(data->config.fixedFramerate);
}
else if (data->config.fixedFramerate < 0)
{
p->fpsLimiter.disabled = true;
}
2013-09-01 14:27:21 +00:00
}
Graphics::~Graphics()
{
delete p;
}
void Graphics::update()
{
p->checkShutDownReset();
p->checkSyncLock();
2013-09-01 14:27:21 +00:00
if (p->frozen)
return;
if (p->fpsLimiter.frameSkipRequired())
{
if (p->threadData->config.frameSkip)
{
/* Skip frame */
p->fpsLimiter.delay();
++p->frameCount;
p->threadData->ethread->notifyFrame();
return;
}
else
{
/* Just reset frame adjust counter */
p->fpsLimiter.resetFrameAdjust();
}
}
2013-09-01 14:27:21 +00:00
p->checkResize();
p->redrawScreen();
2021-10-06 21:43:55 +00:00
STEAMSHIM_pump();
2013-09-01 14:27:21 +00:00
}
void Graphics::freeze()
{
p->frozen = true;
p->checkShutDownReset();
2013-09-01 14:27:21 +00:00
p->checkResize();
/* Capture scene into frozen buffer */
p->compositeToBuffer(p->frozenScene);
2013-09-01 14:27:21 +00:00
}
void Graphics::transition(int duration,
const char *filename,
int vague)
{
p->checkSyncLock();
if (!p->frozen)
return;
vague = clamp(vague, 1, 256);
Bitmap *transMap = *filename ? new Bitmap(filename) : 0;
2013-09-01 14:27:21 +00:00
setBrightness(255);
/* Capture new scene */
p->screen.composite();
/* The PP frontbuffer will hold the current scene after the
* composition step. Since the backbuffer is unused during
* the transition, we can reuse it as the target buffer for
* the final rendered image. */
TEXFBO &currentScene = p->screen.getPP().frontBuffer();
TEXFBO &transBuffer = p->screen.getPP().backBuffer();
/* If no transition bitmap is provided,
* we can use a simplified shader */
TransShader &transShader = shState->shaders().trans;
SimpleTransShader &simpleShader = shState->shaders().simpleTrans;
2013-09-01 14:27:21 +00:00
if (transMap)
{
TransShader &shader = transShader;
2013-09-01 14:27:21 +00:00
shader.bind();
shader.applyViewportProj();
2013-09-01 14:27:21 +00:00
shader.setFrozenScene(p->frozenScene.tex);
shader.setCurrentScene(currentScene.tex);
2013-09-01 14:27:21 +00:00
shader.setTransMap(transMap->getGLTypes().tex);
shader.setVague(vague / 256.0f);
shader.setTexSize(p->scRes);
2013-09-01 14:27:21 +00:00
}
else
{
SimpleTransShader &shader = simpleShader;
2013-09-01 14:27:21 +00:00
shader.bind();
shader.applyViewportProj();
2013-09-01 14:27:21 +00:00
shader.setFrozenScene(p->frozenScene.tex);
shader.setCurrentScene(currentScene.tex);
shader.setTexSize(p->scRes);
2013-09-01 14:27:21 +00:00
}
glState.blend.pushSet(false);
2013-09-01 14:27:21 +00:00
for (int i = 0; i < duration; ++i)
{
/* We need to clean up transMap properly before
* a possible longjmp, so we manually test for
* shutdown/reset here */
2013-09-01 14:27:21 +00:00
if (p->threadData->rqTerm)
{
glState.blend.pop();
2013-09-01 14:27:21 +00:00
delete transMap;
p->shutdown();
return;
}
if (p->threadData->rqReset)
{
glState.blend.pop();
delete transMap;
scriptBinding->reset();
return;
2013-09-01 14:27:21 +00:00
}
p->checkSyncLock();
const float prog = i * (1.0f / duration);
2013-09-01 14:27:21 +00:00
if (transMap)
{
transShader.bind();
transShader.setProg(prog);
}
2013-09-01 14:27:21 +00:00
else
{
simpleShader.bind();
simpleShader.setProg(prog);
}
2013-09-01 14:27:21 +00:00
/* Draw the composed frame to a buffer first
* (we need this because we're skipping PingPong) */
FBO::bind(transBuffer.fbo);
FBO::clear();
2013-09-01 14:27:21 +00:00
p->screenQuad.draw();
p->checkResize();
/* Then blit it flipped and scaled to the screen */
FBO::unbind();
FBO::clear();
GLMeta::blitBeginScreen(Vec2i(p->winSize));
GLMeta::blitSource(transBuffer);
p->metaBlitBufferFlippedScaled();
2014-07-16 02:53:08 +00:00
GLMeta::blitEnd();
2013-09-01 14:27:21 +00:00
p->swapGLBuffer();
}
glState.blend.pop();
2013-09-01 14:27:21 +00:00
delete transMap;
p->frozen = false;
}
2013-10-06 07:55:27 +00:00
void Graphics::frameReset()
2013-09-01 14:27:21 +00:00
{
p->fpsLimiter.resetFrameAdjust();
2013-10-06 07:55:27 +00:00
}
2013-09-01 14:27:21 +00:00
static void guardDisposed() {}
2013-10-06 07:55:27 +00:00
DEF_ATTR_RD_SIMPLE(Graphics, FrameRate, int, p->frameRate)
DEF_ATTR_SIMPLE(Graphics, FrameCount, int, p->frameCount)
void Graphics::setFrameRate(int value)
{
p->frameRate = clamp(value, 10, 120);
2013-10-17 00:18:16 +00:00
if (p->threadData->config.syncToRefreshrate)
return;
2013-10-17 00:18:16 +00:00
if (p->threadData->config.fixedFramerate > 0)
return;
2013-10-06 07:55:27 +00:00
p->fpsLimiter.setDesiredFPS(p->frameRate);
2013-09-01 14:27:21 +00:00
}
2013-10-06 07:55:27 +00:00
void Graphics::wait(int duration)
{
for (int i = 0; i < duration; ++i)
{
p->checkShutDownReset();
2013-10-06 07:55:27 +00:00
p->redrawScreen();
}
}
void Graphics::fadeout(int duration)
{
FBO::unbind();
2013-12-11 20:07:41 +00:00
float curr = p->brightness;
float diff = 255.0f - curr;
2013-10-06 07:55:27 +00:00
for (int i = duration-1; i > -1; --i)
{
setBrightness(diff + (curr / duration) * i);
2013-10-06 07:55:27 +00:00
if (p->frozen)
{
GLMeta::blitBeginScreen(p->scSize);
2014-07-16 02:53:08 +00:00
GLMeta::blitSource(p->frozenScene);
2013-12-11 20:07:41 +00:00
FBO::clear();
p->metaBlitBufferFlippedScaled();
2014-07-16 02:53:08 +00:00
GLMeta::blitEnd();
2013-10-06 07:55:27 +00:00
p->swapGLBuffer();
}
else
{
update();
}
}
}
void Graphics::fadein(int duration)
{
FBO::unbind();
2013-12-11 20:07:41 +00:00
float curr = p->brightness;
float diff = 255.0f - curr;
for (int i = 1; i <= duration; ++i)
2013-10-06 07:55:27 +00:00
{
setBrightness(curr + (diff / duration) * i);
2013-10-06 07:55:27 +00:00
if (p->frozen)
{
GLMeta::blitBeginScreen(p->scSize);
2014-07-16 02:53:08 +00:00
GLMeta::blitSource(p->frozenScene);
2013-12-11 20:07:41 +00:00
FBO::clear();
p->metaBlitBufferFlippedScaled();
2014-07-16 02:53:08 +00:00
GLMeta::blitEnd();
2013-10-06 07:55:27 +00:00
p->swapGLBuffer();
}
else
{
update();
}
}
}
Bitmap *Graphics::snapToBitmap()
2013-09-01 14:27:21 +00:00
{
2013-10-06 07:55:27 +00:00
Bitmap *bitmap = new Bitmap(width(), height());
2013-09-01 14:27:21 +00:00
p->compositeToBuffer(bitmap->getGLTypes());
2013-10-06 07:55:27 +00:00
/* Taint entire bitmap */
bitmap->taintArea(IntRect(0, 0, width(), height()));
2013-10-06 07:55:27 +00:00
return bitmap;
2013-09-01 14:27:21 +00:00
}
int Graphics::width() const
{
return p->scRes.x;
}
int Graphics::height() const
{
return p->scRes.y;
}
void Graphics::resizeScreen(int width, int height)
{
2013-09-03 13:31:29 +00:00
width = clamp(width, 1, 640);
height = clamp(height, 1, 480);
2013-09-01 14:27:21 +00:00
Vec2i size(width, height);
if (p->scRes == size)
return;
p->scRes = size;
p->screen.setResolution(width, height);
TEXFBO::allocEmpty(p->frozenScene, width, height);
2013-09-01 14:27:21 +00:00
FloatRect screenRect(0, 0, width, height);
p->screenQuad.setTexPosRect(screenRect, screenRect);
2014-08-22 21:54:26 +00:00
shState->eThread().requestWindowResize(width, height);
2013-09-01 14:27:21 +00:00
}
void Graphics::playMovie(const char *filename)
{
Debug() << "Graphics.playMovie(" << filename << ") not implemented";
}
2013-09-01 14:27:21 +00:00
DEF_ATTR_RD_SIMPLE(Graphics, Brightness, int, p->brightness)
void Graphics::setBrightness(int value)
{
2013-09-03 13:31:29 +00:00
value = clamp(value, 0, 255);
2013-09-01 14:27:21 +00:00
if (p->brightness == value)
return;
p->brightness = value;
p->screen.setBrightness(value / 255.0);
}
void Graphics::reset()
{
/* Dispose all live Disposables */
IntruListLink<Disposable> *iter;
for (iter = p->dispList.begin();
iter != p->dispList.end();
iter = iter->next)
{
iter->data->dispose();
}
p->dispList.clear();
/* Reset attributes (frame count not included) */
p->fpsLimiter.resetFrameAdjust();
p->frozen = false;
p->screen.getPP().clearBuffers();
setFrameRate(DEF_FRAMERATE);
setBrightness(255);
}
2013-09-01 14:27:21 +00:00
bool Graphics::getFullscreen() const
{
return p->threadData->ethread->getFullscreen();
}
void Graphics::setFullscreen(bool value)
{
p->threadData->ethread->requestFullscreenMode(value);
}
bool Graphics::getShowCursor() const
{
return p->threadData->ethread->getShowCursor();
}
void Graphics::setShowCursor(bool value)
{
p->threadData->ethread->requestShowCursor(value);
}
bool Graphics::getFixedAspectRatio() const
{
// It's a bit hacky to expose config values as a Graphics
// attribute, but there's really no point in state duplication
return shState->config().fixedAspectRatio;
}
void Graphics::setFixedAspectRatio(bool value)
{
shState->config().fixedAspectRatio = value;
p->findHighestIntegerScale();
p->recalculateScreenSize(p->threadData->config.fixedAspectRatio);
p->updateScreenResoRatio(p->threadData);
}
bool Graphics::getSmoothScaling() const
{
// Same deal as with fixed aspect ratio
return shState->config().smoothScaling;
}
void Graphics::setSmoothScaling(bool value)
{
shState->config().smoothScaling = value;
}
bool Graphics::getIntegerScaling() const
{
return p->integerScaleActive;
}
void Graphics::setIntegerScaling(bool value)
{
p->integerScaleActive = value;
p->findHighestIntegerScale();
p->rebuildIntegerScaleBuffer();
p->recalculateScreenSize(p->threadData->config.fixedAspectRatio);
p->updateScreenResoRatio(p->threadData);
}
bool Graphics::getLastMileScaling() const
{
return p->integerLastMileScaling;
}
void Graphics::setLastMileScaling(bool value)
{
p->integerLastMileScaling = value;
p->recalculateScreenSize(p->threadData->config.fixedAspectRatio);
p->updateScreenResoRatio(p->threadData);
}
2013-09-01 14:27:21 +00:00
Scene *Graphics::getScreen() const
{
return &p->screen;
}
void Graphics::repaintWait(const AtomicFlag &exitCond, bool checkReset)
2013-09-01 14:27:21 +00:00
{
if (exitCond)
2013-09-01 14:27:21 +00:00
return;
/* Repaint the screen with the last good frame we drew */
TEXFBO &lastFrame = p->screen.getPP().frontBuffer();
GLMeta::blitBeginScreen(p->winSize);
2014-07-16 02:53:08 +00:00
GLMeta::blitSource(lastFrame);
2013-09-01 14:27:21 +00:00
while (!exitCond)
2013-09-01 14:27:21 +00:00
{
shState->checkShutdown();
2013-09-01 14:27:21 +00:00
if (checkReset)
shState->checkReset();
FBO::clear();
p->metaBlitBufferFlippedScaled();
2013-09-01 14:27:21 +00:00
SDL_GL_SwapWindow(p->threadData->window);
p->fpsLimiter.delay();
p->threadData->ethread->notifyFrame();
2013-09-01 14:27:21 +00:00
}
2014-07-16 02:53:08 +00:00
GLMeta::blitEnd();
2013-09-01 14:27:21 +00:00
}
void Graphics::addDisposable(Disposable *d)
{
p->dispList.append(d->link);
}
void Graphics::remDisposable(Disposable *d)
{
p->dispList.remove(d->link);
}