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

Latest commit

 

History

History
296 lines (248 loc) · 8.82 KB

SDL_uikitvideo.m

File metadata and controls

296 lines (248 loc) · 8.82 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
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
*/
Apr 30, 2010
Apr 30, 2010
22
23
24
#import <UIKit/UIKit.h>
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "SDL_config.h"
#include "SDL_video.h"
#include "SDL_mouse.h"
#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"
#include "SDL_uikitopengles.h"
Apr 30, 2010
Apr 30, 2010
38
39
#include "SDL_assert.h"
40
41
42
43
#define UIKITVID_DRIVER_NAME "uikit"
/* Initialization/Query functions */
static int UIKit_VideoInit(_THIS);
Apr 30, 2010
Apr 30, 2010
44
static void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display);
Jan 13, 2010
Jan 13, 2010
45
46
static int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display,
SDL_DisplayMode * mode);
47
48
static void UIKit_VideoQuit(_THIS);
May 2, 2010
May 2, 2010
49
BOOL SDL_UIKit_supports_multiple_displays = NO;
Apr 30, 2010
Apr 30, 2010
50
51
52
53
54
55
/* DUMMY driver bootstrap functions */
static int
UIKit_Available(void)
{
Feb 1, 2011
Feb 1, 2011
56
return (1);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
}
static void UIKit_DeleteDevice(SDL_VideoDevice * device)
{
SDL_free(device);
}
static SDL_VideoDevice *
UIKit_CreateDevice(int devindex)
{
SDL_VideoDevice *device;
/* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
if (!device) {
SDL_OutOfMemory();
if (device) {
SDL_free(device);
}
return (0);
}
/* Set the function pointers */
device->VideoInit = UIKit_VideoInit;
device->VideoQuit = UIKit_VideoQuit;
Apr 30, 2010
Apr 30, 2010
82
device->GetDisplayModes = UIKit_GetDisplayModes;
83
84
device->SetDisplayMode = UIKit_SetDisplayMode;
device->PumpEvents = UIKit_PumpEvents;
Feb 1, 2011
Feb 1, 2011
85
86
device->CreateWindow = UIKit_CreateWindow;
device->DestroyWindow = UIKit_DestroyWindow;
Jan 21, 2011
Jan 21, 2011
87
device->GetWindowWMInfo = UIKit_GetWindowWMInfo;
Feb 1, 2011
Feb 1, 2011
88
89
90
91
92
93
94
95
96
97
98
99
100
/* OpenGL (ES) functions */
device->GL_MakeCurrent = UIKit_GL_MakeCurrent;
device->GL_SwapWindow = UIKit_GL_SwapWindow;
device->GL_CreateContext = UIKit_GL_CreateContext;
device->GL_DeleteContext = UIKit_GL_DeleteContext;
device->GL_GetProcAddress = UIKit_GL_GetProcAddress;
device->GL_LoadLibrary = UIKit_GL_LoadLibrary;
device->free = UIKit_DeleteDevice;
device->gl_config.accelerated = 1;
101
102
103
104
105
106
107
108
109
return device;
}
VideoBootStrap UIKIT_bootstrap = {
UIKITVID_DRIVER_NAME, "SDL UIKit video driver",
UIKit_Available, UIKit_CreateDevice
};
Apr 30, 2010
Apr 30, 2010
110
111
112
113
114
115
116
117
118
119
120
121
/*
!!! FIXME:
The main screen should list a AxB mode for portrait orientation, and then
also list BxA for landscape mode. When setting a given resolution, we should
rotate the view's transform appropriately (extra credit if you check the
accelerometer and rotate the display so it's never upside down).
http://iphonedevelopment.blogspot.com/2008/10/starting-in-landscape-mode-without.html
*/
Mar 27, 2011
Mar 27, 2011
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
static CGSize
UIKit_ForcePortrait(const CGSize size)
{
CGSize retval;
if (size.width < size.height) { // portrait
retval = size;
} else { // landscape
retval.width = size.height;
retval.height = size.width;
}
return retval;
}
static CGSize
UIKit_ForceLandscape(const CGSize size)
{
CGSize retval;
if (size.width > size.height) { // landscape
retval = size;
} else { // portrait
retval.width = size.height;
retval.height = size.width;
}
return retval;
}
Apr 30, 2010
Apr 30, 2010
148
149
static void
UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
May 2, 2010
May 2, 2010
151
UIScreen *uiscreen = (UIScreen *) display->driverdata;
152
SDL_DisplayMode mode;
Apr 30, 2010
Apr 30, 2010
153
154
155
156
SDL_zero(mode);
// availableModes showed up in 3.2 (the iPad and later). We should only
// land here for at least that version of the OS.
May 2, 2010
May 2, 2010
157
158
if (!SDL_UIKit_supports_multiple_displays) {
const CGRect rect = [uiscreen bounds];
Apr 30, 2010
Apr 30, 2010
159
160
161
162
163
164
mode.format = SDL_PIXELFORMAT_ABGR8888;
mode.w = (int) rect.size.width;
mode.h = (int) rect.size.height;
mode.refresh_rate = 0;
mode.driverdata = NULL;
SDL_AddDisplayMode(display, &mode);
Mar 27, 2011
Mar 27, 2011
165
166
167
mode.w = (int) rect.size.height; // swap the orientation, add again.
mode.h = (int) rect.size.width;
SDL_AddDisplayMode(display, &mode);
Apr 30, 2010
Apr 30, 2010
168
169
170
return;
}
Mar 27, 2011
Mar 27, 2011
171
const int ismain = (uiscreen == [UIScreen mainScreen]);
May 2, 2010
May 2, 2010
172
const NSArray *modes = [uiscreen availableModes];
Apr 30, 2010
Apr 30, 2010
173
174
175
176
const NSUInteger mode_count = [modes count];
NSUInteger i;
for (i = 0; i < mode_count; i++) {
UIScreenMode *uimode = (UIScreenMode *) [modes objectAtIndex:i];
Mar 27, 2011
Mar 27, 2011
177
CGSize size = [uimode size];
Apr 30, 2010
Apr 30, 2010
178
179
180
mode.format = SDL_PIXELFORMAT_ABGR8888;
mode.refresh_rate = 0;
mode.driverdata = uimode;
Mar 27, 2011
Mar 27, 2011
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
mode.w = (int) size.width;
mode.h = (int) size.height;
if (SDL_AddDisplayMode(display, &mode))
[uimode retain];
if (ismain) {
// Add the mode twice, flipped to portrait and landscape.
// SDL_AddDisplayMode() will ignore duplicates.
size = UIKit_ForcePortrait([uimode size]);
mode.w = (int) size.width;
mode.h = (int) size.height;
if (SDL_AddDisplayMode(display, &mode))
[uimode retain];
size = UIKit_ForceLandscape(size);
mode.w = (int) size.width;
mode.h = (int) size.height;
if (SDL_AddDisplayMode(display, &mode))
[uimode retain];
}
Apr 30, 2010
Apr 30, 2010
201
202
}
}
Apr 30, 2010
Apr 30, 2010
205
static void
May 2, 2010
May 2, 2010
206
UIKit_AddDisplay(UIScreen *uiscreen, int w, int h)
Apr 30, 2010
Apr 30, 2010
207
208
209
{
SDL_VideoDisplay display;
SDL_DisplayMode mode;
210
SDL_zero(mode);
Apr 30, 2010
Apr 30, 2010
211
212
213
mode.format = SDL_PIXELFORMAT_ABGR8888;
mode.w = w;
mode.h = h;
214
mode.refresh_rate = 0;
Apr 30, 2010
Apr 30, 2010
215
216
217
218
SDL_zero(display);
display.desktop_mode = mode;
display.current_mode = mode;
May 2, 2010
May 2, 2010
219
220
221
[uiscreen retain];
display.driverdata = uiscreen;
Apr 30, 2010
Apr 30, 2010
222
223
224
225
226
227
228
229
230
SDL_AddVideoDisplay(&display);
}
int
UIKit_VideoInit(_THIS)
{
_this->gl_config.driver_loaded = 1;
May 1, 2010
May 1, 2010
231
232
233
NSString *reqSysVer = @"3.2";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
May 2, 2010
May 2, 2010
234
SDL_UIKit_supports_multiple_displays = YES;
Apr 30, 2010
Apr 30, 2010
235
236
237
238
// If this is iPhoneOS < 3.2, all devices are one screen, 320x480 pixels.
// The iPad added both a larger main screen and the ability to use
// external displays.
May 2, 2010
May 2, 2010
239
if (!SDL_UIKit_supports_multiple_displays) {
Apr 30, 2010
Apr 30, 2010
240
// Just give 'em the whole main screen.
May 2, 2010
May 2, 2010
241
242
243
UIScreen *uiscreen = [UIScreen mainScreen];
const CGRect rect = [uiscreen bounds];
UIKit_AddDisplay(uiscreen, (int)rect.size.width, (int)rect.size.height);
Apr 30, 2010
Apr 30, 2010
244
245
246
247
248
249
} else {
const NSArray *screens = [UIScreen screens];
const NSUInteger screen_count = [screens count];
NSUInteger i;
for (i = 0; i < screen_count; i++) {
// the main screen is the first element in the array.
May 2, 2010
May 2, 2010
250
251
252
UIScreen *uiscreen = (UIScreen *) [screens objectAtIndex:i];
const CGSize size = [[uiscreen currentMode] size];
UIKit_AddDisplay(uiscreen, (int) size.width, (int) size.height);
Apr 30, 2010
Apr 30, 2010
253
254
}
}
255
256
257
258
259
260
/* We're done! */
return 0;
}
static int
Jan 13, 2010
Jan 13, 2010
261
UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
May 2, 2010
May 2, 2010
263
264
UIScreen *uiscreen = (UIScreen *) display->driverdata;
if (!SDL_UIKit_supports_multiple_displays) {
Apr 30, 2010
Apr 30, 2010
265
266
267
268
// Not on at least iPhoneOS 3.2 (versions prior to iPad).
SDL_assert(mode->driverdata == NULL);
} else {
UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
May 2, 2010
May 2, 2010
269
[uiscreen setCurrentMode:uimode];
Apr 30, 2010
Apr 30, 2010
270
271
}
272
273
274
275
276
277
return 0;
}
void
UIKit_VideoQuit(_THIS)
{
Apr 30, 2010
Apr 30, 2010
278
279
280
281
// Release Objective-C objects, so higher level doesn't free() them.
int i, j;
for (i = 0; i < _this->num_displays; i++) {
SDL_VideoDisplay *display = &_this->displays[i];
May 2, 2010
May 2, 2010
282
283
UIScreen *uiscreen = (UIScreen *) display->driverdata;
[uiscreen release];
Apr 30, 2010
Apr 30, 2010
284
285
286
287
288
289
290
291
292
293
display->driverdata = NULL;
for (j = 0; j < display->num_display_modes; j++) {
SDL_DisplayMode *mode = &display->display_modes[j];
UIScreenMode *uimode = (UIScreenMode *) mode->driverdata;
if (uimode) {
[uimode release];
mode->driverdata = NULL;
}
}
}
294
295
296
}
/* vi: set ts=4 sw=4 expandtab: */