aboutsummaryrefslogtreecommitdiff
path: root/sword2/driver/_mouse.cpp
diff options
context:
space:
mode:
authorTorbjörn Andersson2005-10-29 21:24:54 +0000
committerTorbjörn Andersson2005-10-29 21:24:54 +0000
commit7f4cda6622990b7aa26e433ecdc8f17b8a3b237d (patch)
tree5a7e7f9613ff2601a7bf70477b34b30b8c0a21dc /sword2/driver/_mouse.cpp
parentbcccd3f558bbbdab07d355dabfb072298e4f5cf2 (diff)
downloadscummvm-rg350-7f4cda6622990b7aa26e433ecdc8f17b8a3b237d.tar.gz
scummvm-rg350-7f4cda6622990b7aa26e433ecdc8f17b8a3b237d.tar.bz2
scummvm-rg350-7f4cda6622990b7aa26e433ecdc8f17b8a3b237d.zip
Applied my own patch #1341495, in an attempt to fix alignment issues
reported by Crilith. To elaborate a bit, the engine no longer accesses resource data through packed structs. Instead it uses memory streams and the READ/WRITE functions. If data is mainly read, not written, I have replaced the old struct with a new one with a read() function to read the whole thing from memory into the struct's variables, and a write() function to dump the struct's variables to memory. In fact, most of these write() functions remain unused. If data is both read and written, I have replaced the struct with a class with individual get/set functions to replace the old variables. This manipulates memory directly. Since I'm fairly sure that these structs are frequently stored as local variables for a script, all script variables (both local and global) are stored as little-endian and accessed through the READ/WRITE functions, rather than being treated as arrays of 32-bit integers. On a positive note, the functions for doing endian conversion of resources and save games have been removed, and some general cleanups have been made to assist in the rewrite. Initial reports indicate that this patch indeed fixes alignment issues, and that I have not - surprisingly - broken the game on big-endian platforms. At least not in any immediately obvious way. And there's still plenty of time to fix regressions before 0.9.0, too. svn-id: r19366
Diffstat (limited to 'sword2/driver/_mouse.cpp')
-rw-r--r--sword2/driver/_mouse.cpp107
1 files changed, 61 insertions, 46 deletions
diff --git a/sword2/driver/_mouse.cpp b/sword2/driver/_mouse.cpp
index 9d153f0c56..80af551f5d 100644
--- a/sword2/driver/_mouse.cpp
+++ b/sword2/driver/_mouse.cpp
@@ -20,6 +20,7 @@
#include "common/stdafx.h"
#include "common/system.h"
+#include "common/stream.h"
#include "sword2/sword2.h"
#include "sword2/defs.h"
@@ -33,12 +34,14 @@ namespace Sword2 {
#define MOUSEFLASHFRAME 6
-void Mouse::decompressMouse(byte *decomp, byte *comp, int width, int height, int pitch, int xOff, int yOff) {
+void Mouse::decompressMouse(byte *decomp, byte *comp, uint8 frame, int width, int height, int pitch, int xOff, int yOff) {
int32 size = width * height;
int32 i = 0;
int x = 0;
int y = 0;
+ comp = comp + READ_LE_UINT32(comp + frame * 4) - MOUSE_ANIM_HEADER_SIZE;
+
while (i < size) {
if (*comp > 183) {
decomp[(y + yOff) * pitch + x + xOff] = *comp++;
@@ -61,7 +64,7 @@ void Mouse::decompressMouse(byte *decomp, byte *comp, int width, int height, int
void Mouse::drawMouse() {
byte mouseData[MAX_MOUSE_W * MAX_MOUSE_H];
- if (!_mouseAnim && !_luggageAnim)
+ if (!_mouseAnim.data && !_luggageAnim.data)
return;
// When an object is used in the game, the mouse cursor should be a
@@ -77,27 +80,27 @@ void Mouse::drawMouse() {
int deltaX = 0;
int deltaY = 0;
- if (_mouseAnim) {
- hotspot_x = _mouseAnim->xHotSpot;
- hotspot_y = _mouseAnim->yHotSpot;
- mouse_width = _mouseAnim->mousew;
- mouse_height = _mouseAnim->mouseh;
+ if (_mouseAnim.data) {
+ hotspot_x = _mouseAnim.xHotSpot;
+ hotspot_y = _mouseAnim.yHotSpot;
+ mouse_width = _mouseAnim.mousew;
+ mouse_height = _mouseAnim.mouseh;
}
- if (_luggageAnim) {
- if (!_mouseAnim) {
- hotspot_x = _luggageAnim->xHotSpot;
- hotspot_y = _luggageAnim->yHotSpot;
+ if (_luggageAnim.data) {
+ if (!_mouseAnim.data) {
+ hotspot_x = _luggageAnim.xHotSpot;
+ hotspot_y = _luggageAnim.yHotSpot;
}
- if (_luggageAnim->mousew > mouse_width)
- mouse_width = _luggageAnim->mousew;
- if (_luggageAnim->mouseh > mouse_height)
- mouse_height = _luggageAnim->mouseh;
+ if (_luggageAnim.mousew > mouse_width)
+ mouse_width = _luggageAnim.mousew;
+ if (_luggageAnim.mouseh > mouse_height)
+ mouse_height = _luggageAnim.mouseh;
}
- if (_mouseAnim && _luggageAnim) {
- deltaX = _mouseAnim->xHotSpot - _luggageAnim->xHotSpot;
- deltaY = _mouseAnim->yHotSpot - _luggageAnim->yHotSpot;
+ if (_mouseAnim.data && _luggageAnim.data) {
+ deltaX = _mouseAnim.xHotSpot - _luggageAnim.xHotSpot;
+ deltaY = _mouseAnim.yHotSpot - _luggageAnim.yHotSpot;
}
assert(deltaX >= 0);
@@ -114,19 +117,21 @@ void Mouse::drawMouse() {
mouse_width += deltaX;
mouse_height += deltaY;
- if ((uint32) (mouse_width * mouse_height) > sizeof(mouseData)) {
+ if ((uint32)(mouse_width * mouse_height) > sizeof(mouseData)) {
warning("Mouse cursor too large");
return;
}
memset(mouseData, 0, mouse_width * mouse_height);
- if (_luggageAnim)
- decompressMouse(mouseData, (byte *)_luggageAnim + READ_LE_UINT32(_luggageOffset), _luggageAnim->mousew,
- _luggageAnim->mouseh, mouse_width, deltaX, deltaY);
+ if (_luggageAnim.data)
+ decompressMouse(mouseData, _luggageAnim.data, 0,
+ _luggageAnim.mousew, _luggageAnim.mouseh,
+ mouse_width, deltaX, deltaY);
- if (_mouseAnim)
- decompressMouse(mouseData, _mouseSprite, _mouseAnim->mousew, _mouseAnim->mouseh, mouse_width);
+ if (_mouseAnim.data)
+ decompressMouse(mouseData, _mouseAnim.data, _mouseFrame,
+ _mouseAnim.mousew, _mouseAnim.mouseh, mouse_width);
_vm->_system->setMouseCursor(mouseData, mouse_width, mouse_height, hotspot_x, hotspot_y, 0);
}
@@ -138,14 +143,12 @@ void Mouse::drawMouse() {
int32 Mouse::animateMouse() {
uint8 prevMouseFrame = _mouseFrame;
- if (!_mouseAnim)
+ if (!_mouseAnim.data)
return RDERR_UNKNOWN;
- if (++_mouseFrame == _mouseAnim->noAnimFrames)
+ if (++_mouseFrame == _mouseAnim.noAnimFrames)
_mouseFrame = MOUSEFLASHFRAME;
- _mouseSprite = (byte *)_mouseAnim + READ_LE_UINT32(_mouseOffsets + _mouseFrame);
-
if (_mouseFrame != prevMouseFrame)
drawMouse();
@@ -161,10 +164,8 @@ int32 Mouse::animateMouse() {
*/
int32 Mouse::setMouseAnim(byte *ma, int32 size, int32 mouseFlash) {
- if (_mouseAnim) {
- free(_mouseAnim);
- _mouseAnim = NULL;
- }
+ free(_mouseAnim.data);
+ _mouseAnim.data = NULL;
if (ma) {
if (mouseFlash == RDMOUSE_FLASH)
@@ -172,19 +173,27 @@ int32 Mouse::setMouseAnim(byte *ma, int32 size, int32 mouseFlash) {
else
_mouseFrame = MOUSEFLASHFRAME;
- _mouseAnim = (MouseAnim *)malloc(size);
- if (!_mouseAnim)
+ Common::MemoryReadStream readS(ma, size);
+
+ _mouseAnim.runTimeComp = readS.readByte();
+ _mouseAnim.noAnimFrames = readS.readByte();
+ _mouseAnim.xHotSpot = readS.readSByte();
+ _mouseAnim.yHotSpot = readS.readSByte();
+ _mouseAnim.mousew = readS.readByte();
+ _mouseAnim.mouseh = readS.readByte();
+
+ _mouseAnim.data = (byte *)malloc(size - MOUSE_ANIM_HEADER_SIZE);
+ if (!_mouseAnim.data)
return RDERR_OUTOFMEMORY;
- memcpy((byte *)_mouseAnim, ma, size);
- _mouseOffsets = (int32 *)((byte *)_mouseAnim + sizeof(MouseAnim));
+ readS.read(_mouseAnim.data, size - MOUSE_ANIM_HEADER_SIZE);
animateMouse();
drawMouse();
_vm->_system->showMouse(true);
} else {
- if (_luggageAnim)
+ if (_luggageAnim.data)
drawMouse();
else
_vm->_system->showMouse(false);
@@ -201,25 +210,31 @@ int32 Mouse::setMouseAnim(byte *ma, int32 size, int32 mouseFlash) {
*/
int32 Mouse::setLuggageAnim(byte *ma, int32 size) {
- if (_luggageAnim) {
- free(_luggageAnim);
- _luggageAnim = NULL;
- }
+ free(_luggageAnim.data);
+ _luggageAnim.data = NULL;
if (ma) {
- _luggageAnim = (MouseAnim *)malloc(size);
- if (!_luggageAnim)
+ Common::MemoryReadStream readS(ma, size);
+
+ _luggageAnim.runTimeComp = readS.readByte();
+ _luggageAnim.noAnimFrames = readS.readByte();
+ _luggageAnim.xHotSpot = readS.readSByte();
+ _luggageAnim.yHotSpot = readS.readSByte();
+ _luggageAnim.mousew = readS.readByte();
+ _luggageAnim.mouseh = readS.readByte();
+
+ _luggageAnim.data = (byte *)malloc(size - MOUSE_ANIM_HEADER_SIZE);
+ if (!_luggageAnim.data)
return RDERR_OUTOFMEMORY;
- memcpy((byte *)_luggageAnim, ma, size);
- _luggageOffset = (int32 *)((byte *)_luggageAnim + sizeof(MouseAnim));
+ readS.read(_luggageAnim.data, size - MOUSE_ANIM_HEADER_SIZE);
animateMouse();
drawMouse();
_vm->_system->showMouse(true);
} else {
- if (_mouseAnim)
+ if (_mouseAnim.data)
drawMouse();
else
_vm->_system->showMouse(false);