2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** viewport.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** 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 "viewport.h"
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "etc.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "quad.h"
|
|
|
|
#include "glstate.h"
|
2014-08-15 20:55:55 +00:00
|
|
|
#include "graphics.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <SDL_rect.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-12-04 16:48:37 +00:00
|
|
|
#include <sigc++/connection.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
struct ViewportPrivate
|
|
|
|
{
|
|
|
|
/* Needed for geometry changes */
|
|
|
|
Viewport *self;
|
|
|
|
|
|
|
|
Rect *rect;
|
|
|
|
sigc::connection rectCon;
|
|
|
|
|
|
|
|
Color *color;
|
|
|
|
Tone *tone;
|
|
|
|
|
|
|
|
IntRect screenRect;
|
|
|
|
int isOnScreen;
|
|
|
|
|
|
|
|
EtcTemps tmp;
|
|
|
|
|
|
|
|
ViewportPrivate(int x, int y, int width, int height, Viewport *self)
|
|
|
|
: self(self),
|
|
|
|
rect(&tmp.rect),
|
|
|
|
color(&tmp.color),
|
|
|
|
tone(&tmp.tone),
|
|
|
|
isOnScreen(false)
|
|
|
|
{
|
|
|
|
rect->set(x, y, width, height);
|
|
|
|
updateRectCon();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ViewportPrivate()
|
|
|
|
{
|
|
|
|
rectCon.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onRectChange()
|
|
|
|
{
|
|
|
|
self->geometry.rect = rect->toIntRect();
|
|
|
|
self->notifyGeometryChange();
|
|
|
|
recomputeOnScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateRectCon()
|
|
|
|
{
|
|
|
|
rectCon.disconnect();
|
|
|
|
rectCon = rect->valueChanged.connect
|
|
|
|
(sigc::mem_fun(this, &ViewportPrivate::onRectChange));
|
|
|
|
}
|
|
|
|
|
|
|
|
void recomputeOnScreen()
|
|
|
|
{
|
|
|
|
SDL_Rect r1 = { screenRect.x, screenRect.y,
|
|
|
|
screenRect.w, screenRect.h };
|
|
|
|
|
|
|
|
SDL_Rect r2 = { rect->x, rect->y,
|
|
|
|
rect->width, rect->height };
|
|
|
|
|
|
|
|
SDL_Rect result;
|
|
|
|
isOnScreen = SDL_IntersectRect(&r1, &r2, &result);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool needsEffectRender(bool flashing)
|
|
|
|
{
|
|
|
|
bool rectEffective = !rect->isEmpty();
|
|
|
|
bool colorToneEffective = color->hasEffect() || tone->hasEffect() || flashing;
|
|
|
|
|
|
|
|
return (rectEffective && colorToneEffective && isOnScreen);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Viewport::Viewport(int x, int y, int width, int height)
|
2013-10-09 10:30:33 +00:00
|
|
|
: SceneElement(*shState->screen()),
|
2013-09-01 14:27:21 +00:00
|
|
|
sceneLink(this)
|
|
|
|
{
|
|
|
|
initViewport(x, y, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
Viewport::Viewport(Rect *rect)
|
2013-10-09 10:30:33 +00:00
|
|
|
: SceneElement(*shState->screen()),
|
2013-09-01 14:27:21 +00:00
|
|
|
sceneLink(this)
|
|
|
|
{
|
|
|
|
initViewport(rect->x, rect->y, rect->width, rect->height);
|
|
|
|
}
|
|
|
|
|
2014-08-15 20:55:55 +00:00
|
|
|
Viewport::Viewport()
|
|
|
|
: SceneElement(*shState->screen()),
|
|
|
|
sceneLink(this)
|
|
|
|
{
|
|
|
|
const Graphics &graphics = shState->graphics();
|
|
|
|
initViewport(0, 0, graphics.width(), graphics.height());
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
void Viewport::initViewport(int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
p = new ViewportPrivate(x, y, width, height, this);
|
|
|
|
|
|
|
|
/* Set our own geometry */
|
|
|
|
geometry.rect = IntRect(x, y, width, height);
|
|
|
|
|
|
|
|
/* Handle parent geometry */
|
|
|
|
onGeometryChange(scene->getGeometry());
|
|
|
|
}
|
|
|
|
|
|
|
|
Viewport::~Viewport()
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
dispose();
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-09-23 19:12:58 +00:00
|
|
|
void Viewport::update()
|
|
|
|
{
|
|
|
|
guardDisposed();
|
|
|
|
|
|
|
|
Flashable::update();
|
2014-08-09 16:35:01 +00:00
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
DEF_ATTR_RD_SIMPLE(Viewport, OX, int, geometry.xOrigin)
|
|
|
|
DEF_ATTR_RD_SIMPLE(Viewport, OY, int, geometry.yOrigin)
|
|
|
|
|
2014-10-25 21:33:41 +00:00
|
|
|
DEF_ATTR_SIMPLE(Viewport, Rect, Rect&, *p->rect)
|
|
|
|
DEF_ATTR_SIMPLE(Viewport, Color, Color&, *p->color)
|
|
|
|
DEF_ATTR_SIMPLE(Viewport, Tone, Tone&, *p->tone)
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
void Viewport::setOX(int value)
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
guardDisposed();
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
if (geometry.xOrigin == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
geometry.xOrigin = value;
|
|
|
|
notifyGeometryChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Viewport::setOY(int value)
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
guardDisposed();
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
if (geometry.yOrigin == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
geometry.yOrigin = value;
|
|
|
|
notifyGeometryChange();
|
|
|
|
}
|
|
|
|
|
2014-09-04 23:26:03 +00:00
|
|
|
void Viewport::initDynAttribs()
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-09-04 23:26:03 +00:00
|
|
|
p->rect = new Rect(*p->rect);
|
|
|
|
p->color = new Color;
|
|
|
|
p->tone = new Tone;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
p->updateRectCon();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scene */
|
|
|
|
void Viewport::composite()
|
|
|
|
{
|
|
|
|
if (emptyFlashFlag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool renderEffect = p->needsEffectRender(flashing);
|
|
|
|
|
|
|
|
if (elements.getSize() == 0 && !renderEffect)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Setup scissor */
|
|
|
|
glState.scissorTest.pushSet(true);
|
|
|
|
glState.scissorBox.pushSet(p->rect->toIntRect());
|
|
|
|
|
|
|
|
Scene::composite();
|
|
|
|
|
|
|
|
/* If any effects are visible, request parent Scene to
|
|
|
|
* render them. */
|
|
|
|
if (renderEffect)
|
|
|
|
scene->requestViewportRender
|
|
|
|
(p->color->norm, flashColor, p->tone->norm);
|
|
|
|
|
|
|
|
glState.scissorBox.pop();
|
|
|
|
glState.scissorTest.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SceneElement */
|
|
|
|
void Viewport::draw()
|
|
|
|
{
|
|
|
|
composite();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Viewport::onGeometryChange(const Geometry &geo)
|
|
|
|
{
|
|
|
|
p->screenRect = geo.rect;
|
|
|
|
p->recomputeOnScreen();
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:12:58 +00:00
|
|
|
void Viewport::releaseResources()
|
|
|
|
{
|
|
|
|
unlink();
|
|
|
|
|
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-08-30 22:51:23 +00:00
|
|
|
ViewportElement::ViewportElement(Viewport *viewport, int z, int spriteY)
|
|
|
|
: SceneElement(viewport ? *viewport : *shState->screen(), z, spriteY),
|
2013-09-01 14:27:21 +00:00
|
|
|
m_viewport(viewport)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Viewport *ViewportElement::getViewport() const
|
|
|
|
{
|
|
|
|
return m_viewport;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewportElement::setViewport(Viewport *viewport)
|
|
|
|
{
|
|
|
|
m_viewport = viewport;
|
2013-10-09 10:30:33 +00:00
|
|
|
setScene(viewport ? *viewport : *shState->screen());
|
2013-09-01 14:27:21 +00:00
|
|
|
onViewportChange();
|
|
|
|
onGeometryChange(scene->getGeometry());
|
|
|
|
}
|