Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
269 lines (223 loc) · 9.27 KB

SDL_uikitwindow.m

File metadata and controls

269 lines (223 loc) · 9.27 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Feb 12, 2011
Feb 12, 2011
3
Copyright (C) 1997-2011 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
Jan 21, 2011
Jan 21, 2011
24
#include "SDL_syswm.h"
25
26
#include "SDL_video.h"
#include "SDL_mouse.h"
May 2, 2010
May 2, 2010
27
#include "SDL_assert.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "../SDL_sysvideo.h"
#include "../SDL_pixels_c.h"
#include "../../events/SDL_events_c.h"
#include "SDL_uikitvideo.h"
#include "SDL_uikitevents.h"
#include "SDL_uikitwindow.h"
#import "SDL_uikitappdelegate.h"
#import "SDL_uikitopenglview.h"
#include <Foundation/Foundation.h>
Mar 29, 2011
Mar 29, 2011
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@implementation SDL_uikitviewcontroller
- (id)initWithSDLWindow:(SDL_Window *)_window {
[self init];
self->window = _window;
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient {
return YES;
}
- (void)loadView {
// do nothing.
}
// Send a resized event when the orientation changes.
Apr 3, 2011
Apr 3, 2011
58
59
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
Mar 29, 2011
Mar 29, 2011
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
SDL_WindowData *data = self->window->driverdata;
UIWindow *uiwindow = data->uiwindow;
CGRect frame = [uiwindow frame];
const CGSize size = frame.size;
int w, h;
switch (toInterfaceOrientation) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
w = (size.width < size.height) ? size.width : size.height;
h = (size.width > size.height) ? size.width : size.height;
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
w = (size.width > size.height) ? size.width : size.height;
h = (size.width < size.height) ? size.width : size.height;
break;
default:
SDL_assert(0 && "Unexpected interface orientation!");
return;
}
Apr 3, 2011
Apr 3, 2011
83
Mar 29, 2011
Mar 29, 2011
84
85
frame.size.width = w;
frame.size.height = h;
Apr 3, 2011
Apr 3, 2011
86
87
[uiwindow setFrame:frame];
[data->view updateFrame];
Mar 29, 2011
Mar 29, 2011
88
89
90
91
92
93
94
SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
}
@end
May 2, 2010
May 2, 2010
95
96
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
{
Feb 10, 2011
Feb 10, 2011
97
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
May 2, 2010
May 2, 2010
98
UIScreen *uiscreen = (UIScreen *) display->driverdata;
99
SDL_WindowData *data;
Jan 21, 2010
Jan 21, 2010
100
101
102
103
104
105
106
107
/* Allocate the window data */
data = (SDL_WindowData *)SDL_malloc(sizeof(*data));
if (!data) {
SDL_OutOfMemory();
return -1;
}
data->uiwindow = uiwindow;
Mar 29, 2011
Mar 29, 2011
108
data->viewcontroller = nil;
Jan 21, 2010
Jan 21, 2010
109
110
data->view = nil;
111
/* Fill in the SDL window with the window data */
Jan 21, 2010
Jan 21, 2010
112
{
113
114
115
116
117
window->x = 0;
window->y = 0;
window->w = (int)uiwindow.frame.size.width;
window->h = (int)uiwindow.frame.size.height;
}
Jan 21, 2010
Jan 21, 2010
118
119
window->driverdata = data;
Mar 29, 2011
Mar 29, 2011
120
121
122
// !!! FIXME: should we force this? Shouldn't specifying FULLSCREEN
// !!! FIXME: imply BORDERLESS?
Jan 21, 2010
Jan 21, 2010
123
window->flags |= SDL_WINDOW_FULLSCREEN; /* window is always fullscreen */
Mar 29, 2011
Mar 29, 2011
124
window->flags |= SDL_WINDOW_SHOWN; /* only one window on iOS, always shown */
Jan 21, 2010
Jan 21, 2010
125
May 2, 2010
May 2, 2010
126
127
128
// SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
// This is only set if the window is on the main screen. Other screens
// just force the window to have the borderless flag.
Mar 27, 2011
Mar 27, 2011
129
130
131
if ([UIScreen mainScreen] != uiscreen) {
window->flags &= ~SDL_WINDOW_RESIZABLE; // window is NEVER resizeable
window->flags &= ~SDL_WINDOW_INPUT_FOCUS; // never has input focus
Mar 29, 2011
Mar 29, 2011
132
window->flags |= SDL_WINDOW_BORDERLESS; // never has a status bar.
Mar 27, 2011
Mar 27, 2011
133
134
135
} else {
window->flags |= SDL_WINDOW_INPUT_FOCUS; // always has input focus
May 2, 2010
May 2, 2010
136
137
138
139
140
if (window->flags & SDL_WINDOW_BORDERLESS) {
[UIApplication sharedApplication].statusBarHidden = YES;
} else {
[UIApplication sharedApplication].statusBarHidden = NO;
}
Mar 27, 2011
Mar 27, 2011
141
142
const CGSize uisize = [[uiscreen currentMode] size];
Mar 29, 2011
Mar 29, 2011
143
144
145
146
147
const UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
const BOOL landscape = (o == UIDeviceOrientationLandscapeLeft) ||
(o == UIDeviceOrientationLandscapeRight);
const BOOL rotate = ( ((window->w > window->h) && (!landscape)) ||
((window->w < window->h) && (landscape)) );
Mar 27, 2011
Mar 27, 2011
148
149
if (window->flags & SDL_WINDOW_RESIZABLE) {
Mar 29, 2011
Mar 29, 2011
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// The View Controller will handle rotating the view when the
// device orientation changes. We expose these as resize events.
SDL_uikitviewcontroller *controller;
controller = [SDL_uikitviewcontroller alloc];
data->viewcontroller = [controller initWithSDLWindow:window];
[data->viewcontroller setTitle:@"SDL App"]; // !!! FIXME: hook up SDL_SetWindowTitle()
// !!! FIXME: if (rotate), force a "resize" right at the start
} else {
// Rotate the view if we have to, but only on the main screen
// (presumably, an external display doesn't report orientation).
if (rotate) {
#define D2R(x) (M_PI * (x) / 180.0) // degrees to radians.
[uiwindow setTransform:CGAffineTransformIdentity];
[uiwindow setTransform:CGAffineTransformMakeRotation(D2R(90))];
#undef D2R
}
Mar 27, 2011
Mar 27, 2011
166
}
Jan 21, 2010
Jan 21, 2010
167
}
Mar 27, 2011
Mar 27, 2011
168
169
170
171
return 0;
}
Jan 21, 2011
Jan 21, 2011
172
int
Feb 10, 2011
Feb 10, 2011
173
174
175
UIKit_CreateWindow(_THIS, SDL_Window *window)
{
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
May 2, 2010
May 2, 2010
176
177
178
UIScreen *uiscreen = (UIScreen *) display->driverdata;
// SDL currently puts this window at the start of display's linked list. We rely on this.
Feb 11, 2011
Feb 11, 2011
179
SDL_assert(_this->windows == window);
May 2, 2010
May 2, 2010
180
Mar 29, 2011
Mar 29, 2011
181
/* We currently only handle a single window per display on iOS */
May 2, 2010
May 2, 2010
182
183
if (window->next != NULL) {
SDL_SetError("Only one window allowed per display.");
Jan 21, 2010
Jan 21, 2010
184
185
return -1;
}
May 2, 2010
May 2, 2010
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Non-mainscreen windows must be force to borderless, as there's no
// status bar there, and we want to get the right dimensions later in
// this function.
if ([UIScreen mainScreen] != uiscreen) {
window->flags |= SDL_WINDOW_BORDERLESS;
}
// If monitor has a resolution of 0x0 (hasn't been explicitly set by the
// user, so it's in standby), try to force the display to a resolution
// that most closely matches the desired window size.
if (SDL_UIKit_supports_multiple_displays) {
const CGSize origsize = [[uiscreen currentMode] size];
if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
if (display->num_display_modes == 0) {
_this->GetDisplayModes(_this, display);
}
int i;
const SDL_DisplayMode *bestmode = NULL;
for (i = display->num_display_modes; i >= 0; i--) {
const SDL_DisplayMode *mode = &display->display_modes[i];
if ((mode->w >= window->w) && (mode->h >= window->h))
bestmode = mode;
}
if (bestmode) {
UIScreenMode *uimode = (UIScreenMode *) bestmode->driverdata;
[uiscreen setCurrentMode:uimode];
display->desktop_mode = *bestmode;
display->current_mode = *bestmode;
}
}
}
Jan 21, 2010
Jan 21, 2010
221
/* ignore the size user requested, and make a fullscreen window */
May 2, 2010
May 2, 2010
222
223
224
225
226
227
228
229
230
231
232
// !!! FIXME: can we have a smaller view?
UIWindow *uiwindow = [UIWindow alloc];
if (window->flags & SDL_WINDOW_BORDERLESS)
uiwindow = [uiwindow initWithFrame:[uiscreen bounds]];
else
uiwindow = [uiwindow initWithFrame:[uiscreen applicationFrame]];
if (SDL_UIKit_supports_multiple_displays) {
[uiwindow setScreen:uiscreen];
}
Jan 21, 2010
Jan 21, 2010
233
if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
234
235
[uiwindow release];
return -1;
Jan 21, 2010
Jan 21, 2010
236
237
238
239
}
return 1;
Jan 21, 2011
Jan 21, 2011
242
243
void
UIKit_DestroyWindow(_THIS, SDL_Window * window) {
Jan 21, 2010
Jan 21, 2010
244
245
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
if (data) {
Mar 29, 2011
Mar 29, 2011
246
[data->viewcontroller release];
May 2, 2010
May 2, 2010
247
248
249
[data->uiwindow release];
SDL_free(data);
window->driverdata = NULL;
Jan 21, 2010
Jan 21, 2010
250
}
Jan 21, 2011
Jan 21, 2011
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
SDL_bool
UIKit_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
{
UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
if (info->version.major <= SDL_MAJOR_VERSION) {
info->subsystem = SDL_SYSWM_UIKIT;
info->info.uikit.window = uiwindow;
return SDL_TRUE;
} else {
SDL_SetError("Application not compiled with SDL %d.%d\n",
SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
return SDL_FALSE;
}
}
269
/* vi: set ts=4 sw=4 expandtab: */