From 5c558660689e3fb6bad1fd979885a349bf77262c Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Fri, 24 Feb 2012 01:33:37 +0100 Subject: IPHONE: Cleanup mouse cursor handling slightly. Now the scaling etc. will be precalculated instead of being done on every frame. --- backends/platform/iphone/iphone_video.h | 4 ++ backends/platform/iphone/iphone_video.mm | 83 +++++++++++++++++--------------- backends/platform/iphone/osys_video.mm | 2 + 3 files changed, 51 insertions(+), 38 deletions(-) (limited to 'backends/platform') diff --git a/backends/platform/iphone/iphone_video.h b/backends/platform/iphone/iphone_video.h index bd32a582e9..135f4e64c3 100644 --- a/backends/platform/iphone/iphone_video.h +++ b/backends/platform/iphone/iphone_video.h @@ -61,6 +61,9 @@ CGRect _overlayRect; GLfloat _mouseTexCoords[4 * 2]; + GLint _mouseHotspotX, _mouseHotspotY; + GLint _mouseWidth, _mouseHeight; + GLfloat _mouseScaleX, _mouseScaleY; int _scaledShakeOffsetY; } @@ -82,6 +85,7 @@ - (void)updateMouseSurface; - (void)clearColorBuffer; +- (void)updateMouseCursorScaling; - (void)updateMouseCursor; - (id)getEvent; diff --git a/backends/platform/iphone/iphone_video.mm b/backends/platform/iphone/iphone_video.mm index 477254f46d..da954199ea 100644 --- a/backends/platform/iphone/iphone_video.mm +++ b/backends/platform/iphone/iphone_video.mm @@ -304,12 +304,50 @@ const char *iPhone_getDocumentsDir() { } +- (void)updateMouseCursorScaling { + CGRect *rect; + int maxWidth, maxHeight; + + if (!_videoContext.overlayVisible) { + rect = &_gameScreenRect; + maxWidth = _videoContext.screenWidth; + maxHeight = _videoContext.screenHeight; + } else { + rect = &_overlayRect; + maxWidth = _videoContext.overlayWidth; + maxHeight = _videoContext.overlayHeight; + } + + if (!maxWidth || !maxHeight) { + printf("WARNING: updateMouseCursorScaling called when screen was not ready (%d)!\n", _videoContext.overlayVisible); + return; + } + + _mouseScaleX = CGRectGetWidth(*rect) / (GLfloat)maxWidth; + _mouseScaleY = CGRectGetHeight(*rect) / (GLfloat)maxHeight; + + _mouseWidth = (GLint)(_videoContext.mouseWidth * _mouseScaleX); + _mouseHeight = (GLint)(_videoContext.mouseHeight * _mouseScaleY); + + _mouseHotspotX = (GLint)(_videoContext.mouseHotspotX * _mouseScaleX); + _mouseHotspotY = (GLint)(_videoContext.mouseHotspotY * _mouseScaleY); + + // We subtract the screen offset to the hotspot here to simplify the + // screen offset handling in the mouse code. Note the subtraction here + // makes sure that the offset actually gets added to the mouse position, + // since the hotspot offset is substracted from the position. + _mouseHotspotX -= (GLint)CGRectGetMinX(*rect); + _mouseHotspotY -= (GLint)CGRectGetMinY(*rect); +} + - (void)updateMouseCursor { if (_mouseCursorTexture == 0) { glGenTextures(1, &_mouseCursorTexture); printOpenGLError(); [self setFilterModeForTexture:_mouseCursorTexture]; } + [self updateMouseCursorScaling]; + _mouseTexCoords[2] = _mouseTexCoords[6] = _videoContext.mouseWidth / (GLfloat)_videoContext.mouseTexture.w; _mouseTexCoords[5] = _mouseTexCoords[7] = _videoContext.mouseHeight / (GLfloat)_videoContext.mouseTexture.h; @@ -340,53 +378,21 @@ const char *iPhone_getDocumentsDir() { } - (void)updateMouseSurface { - int width = _videoContext.mouseWidth; - int height = _videoContext.mouseHeight; - int mouseX = _videoContext.mouseX; int mouseY = _videoContext.mouseY; - int hotspotX = _videoContext.mouseHotspotX; - int hotspotY = _videoContext.mouseHotspotY; - - CGRect *rect; - int maxWidth, maxHeight; - - if (!_videoContext.overlayVisible) { - rect = &_gameScreenRect; - maxWidth = _videoContext.screenWidth; - maxHeight = _videoContext.screenHeight; - } else { - rect = &_overlayRect; - maxWidth = _videoContext.overlayWidth; - maxHeight = _videoContext.overlayHeight; - } - - const GLfloat scaleX = CGRectGetWidth(*rect) / (GLfloat)maxWidth; - const GLfloat scaleY = CGRectGetHeight(*rect) / (GLfloat)maxHeight; - - mouseX = (int)(mouseX * scaleX); - mouseY = (int)(mouseY * scaleY); - hotspotX = (int)(hotspotX * scaleX); - hotspotY = (int)(hotspotY * scaleY); - width = (int)(width * scaleX); - height = (int)(height * scaleY); - - mouseX -= hotspotX; - mouseY -= hotspotY; - - mouseX += (int)CGRectGetMinX(*rect); - mouseY += (int)CGRectGetMinY(*rect); + mouseX = (int)(mouseX * _mouseScaleX) - _mouseHotspotX; + mouseY = (int)(mouseY * _mouseScaleY) - _mouseHotspotY; GLfloat vertices[] = { // Top left - mouseX , mouseY, + mouseX , mouseY, // Top right - mouseX + width, mouseY, + mouseX + _mouseWidth, mouseY, // Bottom left - mouseX , mouseY + height, + mouseX , mouseY + _mouseHeight, // Bottom right - mouseX + width, mouseY + height + mouseX + _mouseWidth, mouseY + _mouseHeight }; //printf("Cursor: width %u height %u\n", _videoContext.mouseWidth, _videoContext.mouseHeight); @@ -531,6 +537,7 @@ const char *iPhone_getDocumentsDir() { _overlayVertCoords[5] = _overlayVertCoords[7] = CGRectGetMaxY(_overlayRect); [self setViewTransformation]; + [self updateMouseCursorScaling]; } - (void)setViewTransformation { diff --git a/backends/platform/iphone/osys_video.mm b/backends/platform/iphone/osys_video.mm index 6fa800a004..0efeb78aef 100644 --- a/backends/platform/iphone/osys_video.mm +++ b/backends/platform/iphone/osys_video.mm @@ -246,6 +246,7 @@ void OSystem_IPHONE::showOverlay() { _videoContext->overlayVisible = true; dirtyFullOverlayScreen(); updateScreen(); + [g_iPhoneViewInstance performSelectorOnMainThread:@selector(updateMouseCursorScaling) withObject:nil waitUntilDone: YES]; [g_iPhoneViewInstance performSelectorOnMainThread:@selector(clearColorBuffer) withObject:nil waitUntilDone: YES]; } @@ -254,6 +255,7 @@ void OSystem_IPHONE::hideOverlay() { _videoContext->overlayVisible = false; _dirtyOverlayRects.clear(); dirtyFullScreen(); + [g_iPhoneViewInstance performSelectorOnMainThread:@selector(updateMouseCursorScaling) withObject:nil waitUntilDone: YES]; [g_iPhoneViewInstance performSelectorOnMainThread:@selector(clearColorBuffer) withObject:nil waitUntilDone: YES]; } -- cgit v1.2.3