Skip to content

Commit

Permalink
IPHONE: Let iPhoneView and OSystem_IPHONE share the same VideoContext.
Browse files Browse the repository at this point in the history
This allows for better sharing between the current video state in the view and
the OSystem implementation.

This also gets rid of most C interface functions for calling ObjC code.
  • Loading branch information
Johannes Schickel committed Feb 23, 2012
1 parent e00fc73 commit 5ae958b
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 173 deletions.
15 changes: 4 additions & 11 deletions backends/platform/iphone/iphone_common.h
Expand Up @@ -75,20 +75,13 @@ struct VideoContext {
};

// On the ObjC side
void iPhone_setGraphicsMode(GraphicsModes mode);
void iPhone_updateScreen(int mouseX, int mouseY);
void iPhone_updateScreenRect(unsigned short *screen, int x1, int y1, int x2, int y2);
void iPhone_updateOverlayRect(unsigned short *screen, int x1, int y1, int x2, int y2);
void iPhone_initSurface(int width, int height);
void iPhone_setShakeOffset(int offset);
void iPhone_updateScreen();
void iPhone_updateScreenRect(unsigned short *screen, int x1, int y1, int x2, int y2, int width);
void iPhone_updateOverlayRect(unsigned short *screen, int x1, int y1, int x2, int y2, int width);
bool iPhone_fetchEvent(int *outEvent, int *outX, int *outY);
const char *iPhone_getDocumentsDir();
bool iPhone_isHighResDevice();
int iPhone_getScreenHeight();
int iPhone_getScreenWidth();
void iPhone_enableOverlay(bool state);
void iPhone_showCursor(int state);
void iPhone_setMouseCursor(unsigned short *buffer, int width, int height, int hotspotX, int hotspotY);
void iPhone_setMouseCursor(unsigned short *buffer);

uint getSizeNextPOT(uint size);

Expand Down
6 changes: 6 additions & 0 deletions backends/platform/iphone/iphone_video.h
Expand Up @@ -35,6 +35,8 @@
#include "iphone_common.h"

@interface iPhoneView : UIView {
VideoContext _videoContext;

NSMutableArray *_events;
SoftKeyboard *_keyboardView;

Expand All @@ -56,6 +58,8 @@

- (id)initWithFrame:(struct CGRect)frame;

- (VideoContext *)getVideoContext;

- (void)drawRect:(CGRect)frame;

- (void)initSurface;
Expand All @@ -81,4 +85,6 @@

@end

extern iPhoneView *g_iPhoneViewInstance;

#endif
76 changes: 16 additions & 60 deletions backends/platform/iphone/iphone_video.mm
Expand Up @@ -22,7 +22,7 @@

#include "iphone_video.h"

static iPhoneView *sharedInstance = nil;
iPhoneView *g_iPhoneViewInstance = nil;
static int _fullWidth;
static int _fullHeight;
static CGRect _gameScreenRect;
Expand All @@ -48,8 +48,6 @@

static int _scaledShakeOffsetY;

static VideoContext _videoContext;

#if 0
static long lastTick = 0;
static int frames = 0;
Expand All @@ -70,83 +68,36 @@ int printOglError(const char *file, int line) {
return retCode;
}

void iPhone_setGraphicsMode(GraphicsModes mode) {
_videoContext.graphicsMode = mode;

[sharedInstance performSelectorOnMainThread:@selector(setGraphicsMode) withObject:nil waitUntilDone: YES];
}

void iPhone_showCursor(int state) {
_videoContext.mouseIsVisible = state;
}

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

_videoContext.mouseWidth = width;
_videoContext.mouseHeight = height;

_videoContext.mouseHotspotX = hotspotX;
_videoContext.mouseHotspotY = hotspotY;

[sharedInstance performSelectorOnMainThread:@selector(updateMouseCursor) withObject:nil waitUntilDone: YES];
}

void iPhone_enableOverlay(bool state) {
_videoContext.overlayVisible = state;

[sharedInstance performSelectorOnMainThread:@selector(clearColorBuffer) withObject:nil waitUntilDone: YES];
}

int iPhone_getScreenHeight() {
return _videoContext.overlayHeight;
}

int iPhone_getScreenWidth() {
return _videoContext.overlayWidth;
[g_iPhoneViewInstance performSelectorOnMainThread:@selector(updateMouseCursor) withObject:nil waitUntilDone: YES];
}

bool iPhone_isHighResDevice() {
return _fullHeight > 480;
}

void iPhone_updateScreen(int mouseX, int mouseY) {
void iPhone_updateScreen() {
//printf("Mouse: (%i, %i)\n", mouseX, mouseY);

_videoContext.mouseX = mouseX;
_videoContext.mouseY = mouseY;

if (!_needsScreenUpdate) {
_needsScreenUpdate = 1;
[sharedInstance performSelectorOnMainThread:@selector(updateSurface) withObject:nil waitUntilDone: NO];
[g_iPhoneViewInstance performSelectorOnMainThread:@selector(updateSurface) withObject:nil waitUntilDone: NO];
}
}

void iPhone_updateScreenRect(unsigned short *screen, int x1, int y1, int x2, int y2) {
void iPhone_updateScreenRect(unsigned short *screen, int x1, int y1, int x2, int y2, int width) {
for (int y = y1; y < y2; ++y)
memcpy(&_gameScreenTextureBuffer[(y * _gameScreenTextureWidth + x1) * 2], &screen[y * _videoContext.screenWidth + x1], (x2 - x1) * 2);
memcpy(&_gameScreenTextureBuffer[(y * _gameScreenTextureWidth + x1) * 2], &screen[y * width + x1], (x2 - x1) * 2);
}

void iPhone_updateOverlayRect(unsigned short *screen, int x1, int y1, int x2, int y2) {
void iPhone_updateOverlayRect(unsigned short *screen, int x1, int y1, int x2, int y2, int width) {
//printf("Overlaywidth: %u, fullwidth %u\n", _videoContext.overlayWidth, _fullWidth);
for (int y = y1; y < y2; ++y)
memcpy(&_overlayTexBuffer[(y * _overlayTexWidth + x1) * 2], &screen[y * _videoContext.overlayWidth + x1], (x2 - x1) * 2);
}

void iPhone_initSurface(int width, int height) {
_videoContext.screenWidth = width;
_videoContext.screenHeight = height;
_videoContext.shakeOffsetY = 0;
[sharedInstance performSelectorOnMainThread:@selector(initSurface) withObject:nil waitUntilDone: YES];
}

void iPhone_setShakeOffset(int offset) {
_videoContext.shakeOffsetY = offset;
[sharedInstance performSelectorOnMainThread:@selector(setViewTransformation) withObject:nil waitUntilDone: YES];
memcpy(&_overlayTexBuffer[(y * _overlayTexWidth + x1) * 2], &screen[y * width + x1], (x2 - x1) * 2);
}

bool iPhone_fetchEvent(int *outEvent, int *outX, int *outY) {
id event = [sharedInstance getEvent];
id event = [g_iPhoneViewInstance getEvent];
if (event == nil) {
return false;
}
Expand Down Expand Up @@ -189,6 +140,10 @@ + (Class)layerClass {
return [CAEAGLLayer class];
}

- (VideoContext *)getVideoContext {
return &_videoContext;
}

- (void)createContext {
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;

Expand Down Expand Up @@ -263,13 +218,14 @@ - (id)initWithFrame:(struct CGRect)frame {
_fullWidth = (int)frame.size.width;
_fullHeight = (int)frame.size.height;

sharedInstance = self;
g_iPhoneViewInstance = self;

_keyboardView = nil;
_screenTexture = 0;
_overlayTexture = 0;
_mouseCursorTexture = 0;

memset(&_videoContext, 0, sizeof(_videoContext));
_videoContext.graphicsMode = kGraphicsModeLinear;
_videoContext.overlayVisible = false;

Expand Down
50 changes: 25 additions & 25 deletions backends/platform/iphone/osys_events.cpp
Expand Up @@ -122,8 +122,8 @@ bool OSystem_IPHONE::handleEvent_mouseDown(Common::Event &event, int x, int y) {

if (_mouseClickAndDragEnabled) {
event.type = Common::EVENT_LBUTTONDOWN;
event.mouse.x = _videoContext.mouseX;
event.mouse.y = _videoContext.mouseY;
event.mouse.x = _videoContext->mouseX;
event.mouse.y = _videoContext->mouseY;
return true;
} else {
_lastMouseDown = getMillis();
Expand All @@ -140,17 +140,17 @@ bool OSystem_IPHONE::handleEvent_mouseUp(Common::Event &event, int x, int y) {
return false;
} else if (_mouseClickAndDragEnabled) {
event.type = Common::EVENT_LBUTTONUP;
event.mouse.x = _videoContext.mouseX;
event.mouse.y = _videoContext.mouseY;
event.mouse.x = _videoContext->mouseX;
event.mouse.y = _videoContext->mouseY;
} else {
if (getMillis() - _lastMouseDown < 250) {
event.type = Common::EVENT_LBUTTONDOWN;
event.mouse.x = _videoContext.mouseX;
event.mouse.y = _videoContext.mouseY;
event.mouse.x = _videoContext->mouseX;
event.mouse.y = _videoContext->mouseY;

_queuedInputEvent.type = Common::EVENT_LBUTTONUP;
_queuedInputEvent.mouse.x = _videoContext.mouseX;
_queuedInputEvent.mouse.y = _videoContext.mouseY;
_queuedInputEvent.mouse.x = _videoContext->mouseX;
_queuedInputEvent.mouse.y = _videoContext->mouseY;
_lastMouseTap = getMillis();
_queuedEventTime = _lastMouseTap + kQueuedInputEventDelay;
} else
Expand All @@ -167,12 +167,12 @@ bool OSystem_IPHONE::handleEvent_secondMouseDown(Common::Event &event, int x, in

if (_mouseClickAndDragEnabled) {
event.type = Common::EVENT_LBUTTONUP;
event.mouse.x = _videoContext.mouseX;
event.mouse.y = _videoContext.mouseY;
event.mouse.x = _videoContext->mouseX;
event.mouse.y = _videoContext->mouseY;

_queuedInputEvent.type = Common::EVENT_RBUTTONDOWN;
_queuedInputEvent.mouse.x = _videoContext.mouseX;
_queuedInputEvent.mouse.y = _videoContext.mouseY;
_queuedInputEvent.mouse.x = _videoContext->mouseX;
_queuedInputEvent.mouse.y = _videoContext->mouseY;
} else
return false;

Expand All @@ -184,7 +184,7 @@ bool OSystem_IPHONE::handleEvent_secondMouseUp(Common::Event &event, int x, int

if (curTime - _lastSecondaryDown < 400) {
//printf("Right tap!\n");
if (curTime - _lastSecondaryTap < 400 && !_videoContext.overlayVisible) {
if (curTime - _lastSecondaryTap < 400 && !_videoContext->overlayVisible) {
//printf("Right escape!\n");
event.type = Common::EVENT_KEYDOWN;
_queuedInputEvent.type = Common::EVENT_KEYUP;
Expand All @@ -197,11 +197,11 @@ bool OSystem_IPHONE::handleEvent_secondMouseUp(Common::Event &event, int x, int
} else if (!_mouseClickAndDragEnabled) {
//printf("Rightclick!\n");
event.type = Common::EVENT_RBUTTONDOWN;
event.mouse.x = _videoContext.mouseX;
event.mouse.y = _videoContext.mouseY;
event.mouse.x = _videoContext->mouseX;
event.mouse.y = _videoContext->mouseY;
_queuedInputEvent.type = Common::EVENT_RBUTTONUP;
_queuedInputEvent.mouse.x = _videoContext.mouseX;
_queuedInputEvent.mouse.y = _videoContext.mouseY;
_queuedInputEvent.mouse.x = _videoContext->mouseX;
_queuedInputEvent.mouse.y = _videoContext->mouseY;
_lastSecondaryTap = curTime;
_queuedEventTime = curTime + kQueuedInputEventDelay;
} else {
Expand All @@ -211,8 +211,8 @@ bool OSystem_IPHONE::handleEvent_secondMouseUp(Common::Event &event, int x, int
}
if (_mouseClickAndDragEnabled) {
event.type = Common::EVENT_RBUTTONUP;
event.mouse.x = _videoContext.mouseX;
event.mouse.y = _videoContext.mouseY;
event.mouse.x = _videoContext->mouseX;
event.mouse.y = _videoContext->mouseY;
}

return true;
Expand All @@ -234,11 +234,11 @@ bool OSystem_IPHONE::handleEvent_mouseDragged(Common::Event &event, int x, int y
_lastPadX = x;
_lastPadY = y;

mouseNewPosX = (int)(_videoContext.mouseX - deltaX / 0.5f);
mouseNewPosY = (int)(_videoContext.mouseY - deltaY / 0.5f);
mouseNewPosX = (int)(_videoContext->mouseX - deltaX / 0.5f);
mouseNewPosY = (int)(_videoContext->mouseY - deltaY / 0.5f);

int widthCap = _videoContext.overlayVisible ? _videoContext.overlayWidth : _videoContext.screenWidth;
int heightCap = _videoContext.overlayVisible ? _videoContext.overlayHeight : _videoContext.screenHeight;
int widthCap = _videoContext->overlayVisible ? _videoContext->overlayWidth : _videoContext->screenWidth;
int heightCap = _videoContext->overlayVisible ? _videoContext->overlayHeight : _videoContext->screenHeight;

if (mouseNewPosX < 0)
mouseNewPosX = 0;
Expand Down Expand Up @@ -350,10 +350,10 @@ void OSystem_IPHONE::handleEvent_orientationChanged(int orientation) {

if (_screenOrientation != newOrientation) {
_screenOrientation = newOrientation;
iPhone_initSurface(_videoContext.screenWidth, _videoContext.screenHeight);
updateOutputSurface();

dirtyFullScreen();
if (_videoContext.overlayVisible)
if (_videoContext->overlayVisible)
dirtyFullOverlayScreen();
updateScreen();
}
Expand Down
6 changes: 1 addition & 5 deletions backends/platform/iphone/osys_main.cpp
Expand Up @@ -65,11 +65,7 @@ OSystem_IPHONE::OSystem_IPHONE() :
_queuedInputEvent.type = Common::EVENT_INVALID;
_touchpadModeEnabled = !iPhone_isHighResDevice();
_fsFactory = new POSIXFilesystemFactory();

_videoContext.mouseWidth = _videoContext.mouseHeight = 0;
_videoContext.overlayWidth = _videoContext.overlayHeight = 0;
_videoContext.overlayVisible = false;
_videoContext.graphicsMode = kGraphicsModeLinear;
initVideoContext();
}

OSystem_IPHONE::~OSystem_IPHONE() {
Expand Down
5 changes: 4 additions & 1 deletion backends/platform/iphone/osys_main.h
Expand Up @@ -61,7 +61,7 @@ class OSystem_IPHONE : public EventsBaseBackend, public PaletteManager {

Audio::MixerImpl *_mixer;

VideoContext _videoContext;
VideoContext *_videoContext;

Graphics::Surface _framebuffer;
byte *_gameScreenRaw;
Expand Down Expand Up @@ -183,6 +183,9 @@ class OSystem_IPHONE : public EventsBaseBackend, public PaletteManager {
virtual void logMessage(LogMessageType::Type type, const char *message);

protected:
void initVideoContext();
void updateOutputSurface();

void internUpdateScreen();
void dirtyFullScreen();
void dirtyFullOverlayScreen();
Expand Down

0 comments on commit 5ae958b

Please sign in to comment.