ZVISION: Move rendering logic from ZVision class to RenderManager class

This commit is contained in:
richiesams 2013-07-24 11:33:58 -05:00
parent cd51b646f3
commit ed44907309
6 changed files with 100 additions and 105 deletions

View File

@ -27,6 +27,7 @@
#include "common/events.h"
#include "engines/util.h"
#include "zvision/render_manager.h"
namespace ZVision {
@ -61,13 +62,9 @@ void ZVision::processEvents() {
quitGame();
break;
case Common::KEYCODE_ESCAPE:
if (_currentVideo != 0) {
initGraphics(_width, _height, true, &_pixelFormat);
delete _currentVideo;
_currentVideo = 0;
delete[] _scaledVideoFrameBuffer;
_scaledVideoFrameBuffer = 0;
}
if (_renderManager->isVideoPlaying())
_renderManager->cancelVideo();
break;
default:
break;

View File

@ -24,8 +24,8 @@
#include "common/file.h"
#include "common/system.h"
#include "common/rect.h"
#include "engines/util.h"
#include "graphics/decoders/tga.h"
#include "zvision/render_manager.h"
@ -37,9 +37,29 @@ RenderManager::RenderManager(OSystem *system, const int width, const int height)
: _system(system),
_width(width),
_height(height),
_pixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0), // RGB555
_currentVideo(0),
_scaledVideoFrameBuffer(0),
_renderTable(width, height) {
}
/**
* Initialize graphics
*/
void RenderManager::initialize() {
initGraphics(_width, _height, true, &_pixelFormat);
}
void RenderManager::updateScreen(bool isConsoleActive) {
if (_currentVideo != 0)
continueVideo();
if (_needsScreenUpdate || isConsoleActive) {
_system->updateScreen();
_needsScreenUpdate = false;
}
}
void RenderManager::renderImageToScreen(const Common::String &fileName, uint32 x, uint32 y) {
Common::File file;
@ -86,40 +106,8 @@ void RenderManager::renderImageToScreen(const Common::String &fileName, uint32 x
_needsScreenUpdate = true;
}
void RenderManager::generatePanoramaLookupTable() {
float fieldOfView = _panoramaOptions.fieldOfView;
float scale = _panoramaOptions.linearScale;
_renderTable.clear();
double halfWidth = (double)_width / 2.0;
double halfHeight = (double)_height / 2.0;
double fovRadians = (fieldOfView * 3.14159265 / 180.0);
double halfHeightOverTan = halfHeight / tan(fovRadians);
double tanOverHalfHeight = tan(fovRadians) / halfHeight;
for (int32 x = 0; x < _width; x++) {
// Add an offset of 0.01 to overcome zero tan/atan issue (vertical line on half of screen)
double xPos = (double)x - halfWidth + 0.01;
double tempX = atan(xPos*tanOverHalfHeight);
double scaledX = scale * halfHeightOverTan * tempX;
double nn = cos(tempX);
double newHalfWidth = halfHeight * nn * halfHeightOverTan * tanOverHalfHeight*2.0;
int32 newX = floor(scaledX);// + half_w);
double yScale = newHalfWidth / (double)_height;
double et2 = ((double)_height - newHalfWidth) / 2.0;
for (int32 y = 0; y < _height; y++) {
double et1 = (double)y*yScale;
_renderTable(x, y).x = newX; //pixel index
int32 newY = floor(et2 + et1);
_renderTable(x, y).y = newY; //pixel index
}
}
void RenderManager::setRenderState(RenderTable::RenderState state) {
_renderTable.setRenderState(state);
}
} // End of namespace ZVision

View File

@ -24,14 +24,20 @@
#define ZVISION_RENDER_MANAGER_H
#include "common/types.h"
#include "common/rect.h"
#include "zvision/dense_2d_array.h"
#include "graphics/pixelformat.h"
#include "zvision/render_table.h"
class OSystem;
namespace Common {
class String;
class Point;
}
namespace Video {
class VideoDecoder;
}
namespace ZVision {
@ -40,37 +46,62 @@ class RenderManager {
public:
RenderManager(OSystem *system, const int width, const int height);
public:
enum RenderState {
PANORAMA,
TILT,
FLAT
};
private:
OSystem *_system;
const int _width;
const int _height;
RenderState _renderState;
const Graphics::PixelFormat _pixelFormat;
RenderTable _renderTable;
struct {
uint16 fieldOfView;
uint16 linearScale;
} _panoramaOptions;
// TODO: See if tilt and panorama need to have separate options
struct {
uint16 fieldOfView;
uint16 linearScale;
} _tiltOptions;
Dense2DArray<Common::Point> _renderTable;
Video::VideoDecoder *_currentVideo;
byte *_scaledVideoFrameBuffer;
bool _needsScreenUpdate;
public:
void initialize();
void updateScreen(bool isConsoleActive);
/**
* Start a video playing. It will also load the first frame of the video.
*
* @param videoDecoder The video to play
*/
void startVideo(Video::VideoDecoder *videoDecoder);
/**
* @return Is a video currently being played
*/
bool isVideoPlaying() { return _currentVideo == 0; }
/**
* Cancels a video prematurely. Any sound remaining in the queue will continue to play.
* The last frame of the video will remain on the screen until something else overwrites it
*/
void cancelVideo();
/**
* Blits the image to the screen. Actual screen updates won't happen until the end of the frame.
* The image will be clipped to fit inside the window.
*
* @param fileName Name of the image file
* @param x X position where the image should be put
* @param y Y position where the image should be put
*/
void renderImageToScreen(const Common::String &fileName, uint32 x, uint32 y);
void generatePanoramaLookupTable();
/**
* Set how the frame should be rendered
*
* @param state One of the RenderStates
*/
void setRenderState(RenderTable::RenderState state);
bool needsScreenUpdate() { return _needsScreenUpdate; };
private:
/**
* Checks the time since the last video frame, and blits the next frame to the screen
*/
void continueVideo();
};
} // End of namespace ZVision

View File

@ -23,11 +23,11 @@
#include "common/scummsys.h"
#include "common/system.h"
#include "video/video_decoder.h"
#include "engines/util.h"
#include "graphics/surface.h"
#include "zvision/zvision.h"
#include "zvision/render_manager.h"
namespace ZVision {
@ -71,7 +71,7 @@ void scale2x(const byte *src, byte *dst, int16 srcWidth, int16 srcHeight, byte b
}
}
void ZVision::startVideo(Video::VideoDecoder *videoDecoder) {
void RenderManager::startVideo(Video::VideoDecoder *videoDecoder) {
if (!videoDecoder)
return;
@ -89,12 +89,7 @@ void ZVision::startVideo(Video::VideoDecoder *videoDecoder) {
continueVideo();
}
void ZVision::continueVideo() {
if (_currentVideo == 0) {
warning("No video loaded. Nothing to continue");
return;
}
void RenderManager::continueVideo() {
byte bytesPerPixel = _currentVideo->getPixelFormat().bytesPerPixel;
uint16 width = _currentVideo->getWidth();
uint16 height = _currentVideo->getHeight();
@ -115,13 +110,16 @@ void ZVision::continueVideo() {
}
}
} else {
initGraphics(_width, _height, true, &_pixelFormat);
delete _currentVideo;
_currentVideo = 0;
delete[] _scaledVideoFrameBuffer;
_scaledVideoFrameBuffer = 0;
cancelVideo();
}
}
void RenderManager::cancelVideo() {
initGraphics(_width, _height, true, &_pixelFormat);
delete _currentVideo;
_currentVideo = 0;
delete[] _scaledVideoFrameBuffer;
_scaledVideoFrameBuffer = 0;
}
} // End of namespace ZVision

View File

@ -46,9 +46,6 @@ namespace ZVision {
ZVision::ZVision(OSystem *syst, const ZVisionGameDescription *gameDesc)
: Engine(syst),
_gameDescription(gameDesc),
_pixelFormat(2, 5, 5, 5, 0, 10, 5, 0, 0), // RGB555
_currentVideo(0),
_scaledVideoFrameBuffer(0),
_width(640),
_height(480) {
// Put your engine in a sane state, but do nothing big yet;
@ -104,7 +101,7 @@ void ZVision::initialize() {
SearchMan.add(name, archive);
}
initGraphics(_width, _height, true, &_pixelFormat);
_renderManager->initialize();
_scriptManager->initialize();
@ -127,19 +124,13 @@ Common::Error ZVision::run() {
uint32 deltaTime = currentTime - lastTime;
lastTime = currentTime;
if (_currentVideo != 0)
continueVideo();
else {
_scriptManager->updateNodes(deltaTime);
_scriptManager->checkPuzzleCriteria();
}
_scriptManager->updateNodes(deltaTime);
_scriptManager->checkPuzzleCriteria();
if (_needsScreenUpdate || _console->isActive()) {
_system->updateScreen();
_needsScreenUpdate = false;
}
// Render a frame
_renderManager->updateScreen(_console->isActive());
// Calculate the frame delay based off a desired frame rate
// Calculate the frame delay based off a desired frame time
int delay = desiredFrameTime - (currentTime - _system->getMillis());
// Ensure non-negative
delay = delay < 0 ? 0 : delay;

View File

@ -27,8 +27,6 @@
#include "common/random.h"
#include "common/events.h"
#include "video/video_decoder.h"
#include "engines/engine.h"
#include "zvision/detection.h"
@ -58,7 +56,6 @@ public:
private:
Console *_console;
const ZVisionGameDescription *_gameDescription;
const Graphics::PixelFormat _pixelFormat;
const int _width;
const int _height;
@ -71,10 +68,6 @@ private:
// To prevent allocation every time we process events
Common::Event _event;
bool _needsScreenUpdate;
Video::VideoDecoder *_currentVideo;
byte *_scaledVideoFrameBuffer;
public:
uint32 getFeatures() const;
Common::Language getLanguage() const;
@ -84,9 +77,6 @@ public:
Common::RandomSource *getRandomSource() const;
ZVisionGameId getGameId() const;
void startVideo(Video::VideoDecoder *videoDecoder);
void continueVideo();
private:
void initialize();