Skip to content

Commit

Permalink
IPHONE: Implement cursor palette support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Schickel committed Feb 20, 2012
1 parent 65cda4c commit 8102e7e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 28 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -27,6 +27,8 @@ For a more comprehensive changelog of the latest experimental code, see:

iPhone port:
- Changed "F5 (menu)" gesture to open up the global main menu instead.
- Added support for custom cursor palettes, this makes the moderm theme use
the red pointer cursor for example.

Windows port:
- Changed default savegames location for Windows NT4/2000/XP/Vista/7.
Expand Down
2 changes: 1 addition & 1 deletion backends/platform/iphone/iphone_common.h
Expand Up @@ -76,7 +76,7 @@ int iPhone_getScreenHeight();
int iPhone_getScreenWidth();
void iPhone_enableOverlay(int state);
void iPhone_showCursor(int state);
void iPhone_setMouseCursor(short *buffer, int width, int height, int hotspotX, int hotspotY);
void iPhone_setMouseCursor(unsigned short *buffer, int width, int height, int hotspotX, int hotspotY);

uint getSizeNextPOT(uint size);

Expand Down
8 changes: 3 additions & 5 deletions backends/platform/iphone/iphone_video.m
Expand Up @@ -48,7 +48,7 @@
static UITouch *_firstTouch = NULL;
static UITouch *_secondTouch = NULL;

static short *_mouseCursor = NULL;
static unsigned short *_mouseCursor = NULL;
static int _mouseCursorHeight = 0;
static int _mouseCursorWidth = 0;
static int _mouseCursorHotspotX = 0;
Expand Down Expand Up @@ -79,7 +79,7 @@ void iPhone_showCursor(int state) {
_mouseCursorEnabled = state;
}

void iPhone_setMouseCursor(short *buffer, int width, int height, int hotspotX, int hotspotY) {
void iPhone_setMouseCursor(unsigned short *buffer, int width, int height, int hotspotX, int hotspotY) {
_mouseCursor = buffer;

_mouseCursorWidth = width;
Expand Down Expand Up @@ -326,7 +326,6 @@ - (void)updateMainSurface {
// due to the iPhone internals having to convert the whole texture back from its internal format when used.
// In the future we could use several tiled textures instead.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _textureWidth, _textureHeight, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, _textureBuffer); printOpenGLError();
glDisable(GL_BLEND);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); printOpenGLError();
}

Expand All @@ -353,7 +352,6 @@ - (void)updateOverlaySurface {

glBindTexture(GL_TEXTURE_2D, _overlayTexture); printOpenGLError();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _overlayTexWidth, _overlayTexHeight, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, _overlayTexBuffer); printOpenGLError();
glEnable(GL_BLEND);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); printOpenGLError();
}

Expand Down Expand Up @@ -413,7 +411,6 @@ - (void)updateMouseSurface {
glTexCoordPointer(2, GL_FLOAT, 0, texCoords); printOpenGLError();

glBindTexture(GL_TEXTURE_2D, _mouseCursorTexture); printOpenGLError();
glEnable(GL_BLEND);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); printOpenGLError();
}

Expand Down Expand Up @@ -463,6 +460,7 @@ - (void)initSurface {
glViewport(0, 0, _backingWidth, _backingHeight); printOpenGLError();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); printOpenGLError();

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_TEXTURE_2D); printOpenGLError();
Expand Down
32 changes: 28 additions & 4 deletions backends/platform/iphone/osys_main.cpp
Expand Up @@ -56,11 +56,11 @@ OSystem_IPHONE::OSystem_IPHONE() :
_mixer(NULL), _gameScreenRaw(NULL),
_overlayVisible(false), _gameScreenConverted(NULL),
_mouseHeight(0), _mouseWidth(0), _mouseBuf(NULL), _lastMouseTap(0), _queuedEventTime(0),
_secondaryTapped(false), _lastSecondaryTap(0),
_mouseNeedTextureUpdate(false), _secondaryTapped(false), _lastSecondaryTap(0),
_screenOrientation(kScreenOrientationFlippedLandscape), _mouseClickAndDragEnabled(false),
_gestureStartX(-1), _gestureStartY(-1), _fullScreenIsDirty(false), _fullScreenOverlayIsDirty(false),
_mouseDirty(false), _timeSuspended(0), _lastDragPosX(-1), _lastDragPosY(-1), _screenChangeCount(0),
_overlayHeight(0), _overlayWidth(0), _overlayBuffer(0) {
_overlayHeight(0), _overlayWidth(0), _overlayBuffer(0), _mouseCursorPaletteEnabled(false) {
_queuedInputEvent.type = Common::EVENT_INVALID;
_touchpadModeEnabled = !iPhone_isHighResDevice();
_fsFactory = new POSIXFilesystemFactory();
Expand Down Expand Up @@ -99,14 +99,38 @@ void OSystem_IPHONE::initBackend() {
}

bool OSystem_IPHONE::hasFeature(Feature f) {
return false;
switch (f) {
case kFeatureCursorPalette:
return true;

default:
return false;
}
}

void OSystem_IPHONE::setFeatureState(Feature f, bool enable) {
switch (f) {
case kFeatureCursorPalette:
if (_mouseCursorPaletteEnabled != enable) {
_mouseNeedTextureUpdate = true;
_mouseDirty = true;
_mouseCursorPaletteEnabled = enable;
}
break;

default:
break;
}
}

bool OSystem_IPHONE::getFeatureState(Feature f) {
return false;
switch (f) {
case kFeatureCursorPalette:
return _mouseCursorPaletteEnabled;

default:
return false;
}
}

void OSystem_IPHONE::suspendLoop() {
Expand Down
5 changes: 5 additions & 0 deletions backends/platform/iphone/osys_main.h
Expand Up @@ -82,12 +82,15 @@ class OSystem_IPHONE : public EventsBaseBackend, public PaletteManager {
uint32 _timeSuspended;

bool _mouseVisible;
bool _mouseCursorPaletteEnabled;
uint16 _mouseCursorPalette[256];
byte *_mouseBuf;
byte _mouseKeyColor;
uint _mouseWidth, _mouseHeight;
uint _mouseX, _mouseY;
int _mouseHotspotX, _mouseHotspotY;
bool _mouseDirty;
bool _mouseNeedTextureUpdate;
long _lastMouseDown;
long _lastMouseTap;
long _queuedEventTime;
Expand Down Expand Up @@ -159,6 +162,7 @@ class OSystem_IPHONE : public EventsBaseBackend, public PaletteManager {

virtual void warpMouse(int x, int y);
virtual void setMouseCursor(const byte *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor = 255, int cursorTargetScale = 1, const Graphics::PixelFormat *format = NULL);
virtual void setCursorPalette(const byte *colors, uint start, uint num);

virtual bool pollEvent(Common::Event &event);
virtual uint32 getMillis();
Expand Down Expand Up @@ -195,6 +199,7 @@ class OSystem_IPHONE : public EventsBaseBackend, public PaletteManager {
void drawDirtyRect(const Common::Rect &dirtyRect);
void drawDirtyOverlayRect(const Common::Rect &dirtyRect);
void updateHardwareSurfaceForRect(const Common::Rect &updatedRect);
void updateMouseTexture();
static void AQBufferCallback(void *in, AudioQueueRef inQ, AudioQueueBufferRef outQB);
static int timerHandler(int t);

Expand Down
65 changes: 47 additions & 18 deletions backends/platform/iphone/osys_video.cpp
Expand Up @@ -81,6 +81,7 @@ void OSystem_IPHONE::initSize(uint width, uint height, const Graphics::PixelForm
_fullScreenIsDirty = false;
dirtyFullScreen();
_mouseVisible = false;
_mouseCursorPaletteEnabled = false;
_screenChangeCount++;
updateScreen();
}
Expand Down Expand Up @@ -174,6 +175,11 @@ void OSystem_IPHONE::updateScreen() {
}

void OSystem_IPHONE::internUpdateScreen() {
if (_mouseNeedTextureUpdate) {
updateMouseTexture();
_mouseNeedTextureUpdate = false;
}

while (_dirtyRects.size()) {
Common::Rect dirtyRect = _dirtyRects.remove_at(_dirtyRects.size() - 1);

Expand Down Expand Up @@ -355,24 +361,6 @@ void OSystem_IPHONE::dirtyFullOverlayScreen() {
void OSystem_IPHONE::setMouseCursor(const byte *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, int cursorTargetScale, const Graphics::PixelFormat *format) {
//printf("setMouseCursor(%i, %i, scale %u)\n", hotspotX, hotspotY, cursorTargetScale);

int texWidth = getSizeNextPOT(w);
int texHeight = getSizeNextPOT(h);
int bufferSize = texWidth * texHeight * sizeof(int16);
int16 *mouseBuf = (int16 *)malloc(bufferSize);
memset(mouseBuf, 0, bufferSize);

for (uint x = 0; x < w; ++x) {
for (uint y = 0; y < h; ++y) {
byte color = buf[y * w + x];
if (color != keycolor)
mouseBuf[y * texWidth + x] = _gamePaletteRGBA5551[color] | 0x1;
else
mouseBuf[y * texWidth + x] = 0x0;
}
}

iPhone_setMouseCursor(mouseBuf, w, h, hotspotX, hotspotY);

if (_mouseBuf != NULL && (_mouseWidth != w || _mouseHeight != h)) {
free(_mouseBuf);
_mouseBuf = NULL;
Expand All @@ -392,4 +380,45 @@ void OSystem_IPHONE::setMouseCursor(const byte *buf, uint w, uint h, int hotspot
memcpy(_mouseBuf, buf, w * h);

_mouseDirty = true;
_mouseNeedTextureUpdate = true;
}

void OSystem_IPHONE::setCursorPalette(const byte *colors, uint start, uint num) {
assert(start + num <= 256);

for (uint i = start; i < start + num; ++i, colors += 3)
_mouseCursorPalette[i] = Graphics::RGBToColor<Graphics::ColorMasks<5551> >(colors[0], colors[1], colors[2]);

// FIXME: This is just stupid, our client code seems to assume that this
// automatically enables the cursor palette.
_mouseCursorPaletteEnabled = true;

if (_mouseCursorPaletteEnabled)
_mouseDirty = _mouseNeedTextureUpdate = true;
}

void OSystem_IPHONE::updateMouseTexture() {
int texWidth = getSizeNextPOT(_mouseWidth);
int texHeight = getSizeNextPOT(_mouseHeight);
int bufferSize = texWidth * texHeight * sizeof(int16);
uint16 *mouseBuf = (uint16 *)malloc(bufferSize);
memset(mouseBuf, 0, bufferSize);

const uint16 *palette;
if (_mouseCursorPaletteEnabled)
palette = _mouseCursorPalette;
else
palette = _gamePaletteRGBA5551;

for (uint x = 0; x < _mouseWidth; ++x) {
for (uint y = 0; y < _mouseHeight; ++y) {
const byte color = _mouseBuf[y * _mouseWidth + x];
if (color != _mouseKeyColor)
mouseBuf[y * texWidth + x] = palette[color] | 0x1;
else
mouseBuf[y * texWidth + x] = 0x0;
}
}

iPhone_setMouseCursor(mouseBuf, _mouseWidth, _mouseHeight, _mouseHotspotX, _mouseHotspotY);
}

0 comments on commit 8102e7e

Please sign in to comment.