src/video/uikit/SDL_uikitwindow.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Oct 2008 06:46:59 +0000
changeset 2765 f55c87ae336b
child 2859 99210400e8b9
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

Bring SDL to iPhone and iPod Touch
by Holmes Futrell, mentored by Sam Lantinga
slouken@2765
     1
/*
slouken@2765
     2
    SDL - Simple DirectMedia Layer
slouken@2765
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@2765
     4
slouken@2765
     5
    This library is free software; you can redistribute it and/or
slouken@2765
     6
    modify it under the terms of the GNU Lesser General Public
slouken@2765
     7
    License as published by the Free Software Foundation; either
slouken@2765
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@2765
     9
slouken@2765
    10
    This library is distributed in the hope that it will be useful,
slouken@2765
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2765
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2765
    13
    Lesser General Public License for more details.
slouken@2765
    14
slouken@2765
    15
    You should have received a copy of the GNU Lesser General Public
slouken@2765
    16
    License along with this library; if not, write to the Free Software
slouken@2765
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2765
    18
slouken@2765
    19
    Sam Lantinga
slouken@2765
    20
    slouken@libsdl.org
slouken@2765
    21
*/
slouken@2765
    22
#include "SDL_config.h"
slouken@2765
    23
slouken@2765
    24
#include "SDL_video.h"
slouken@2765
    25
#include "SDL_mouse.h"
slouken@2765
    26
#include "../SDL_sysvideo.h"
slouken@2765
    27
#include "../SDL_pixels_c.h"
slouken@2765
    28
#include "../../events/SDL_events_c.h"
slouken@2765
    29
slouken@2765
    30
#include "SDL_uikitvideo.h"
slouken@2765
    31
#include "SDL_uikitevents.h"
slouken@2765
    32
#include "SDL_uikitwindow.h"
slouken@2765
    33
#import "SDL_uikitappdelegate.h"
slouken@2765
    34
slouken@2765
    35
#import "SDL_uikitopenglview.h"
slouken@2765
    36
#import "SDL_renderer_sw.h"
slouken@2765
    37
slouken@2765
    38
#include <UIKit/UIKit.h>
slouken@2765
    39
#include <Foundation/Foundation.h>
slouken@2765
    40
slouken@2765
    41
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created) {
slouken@2765
    42
slouken@2765
    43
    SDL_WindowData *data;
slouken@2765
    44
		
slouken@2765
    45
    /* Allocate the window data */
slouken@2765
    46
    data = (SDL_WindowData *)SDL_malloc(sizeof(*data));
slouken@2765
    47
    if (!data) {
slouken@2765
    48
        SDL_OutOfMemory();
slouken@2765
    49
        return -1;
slouken@2765
    50
    }
slouken@2765
    51
    data->windowID = window->id;
slouken@2765
    52
    data->uiwindow = uiwindow;
slouken@2765
    53
	data->view = nil;
slouken@2765
    54
		
slouken@2765
    55
    /* Fill in the SDL window with the window data */
slouken@2765
    56
	{
slouken@2765
    57
        window->x = 0;
slouken@2765
    58
        window->y = 0;
slouken@2765
    59
        window->w = (int)uiwindow.frame.size.width;
slouken@2765
    60
        window->h = (int)uiwindow.frame.size.height;
slouken@2765
    61
    }
slouken@2765
    62
	
slouken@2765
    63
	window->driverdata = data;
slouken@2765
    64
	
slouken@2765
    65
	window->flags &= ~SDL_WINDOW_RESIZABLE;		/* window is NEVER resizeable */
slouken@2765
    66
	window->flags |= SDL_WINDOW_OPENGL;			/* window is always OpenGL */
slouken@2765
    67
	window->flags |= SDL_WINDOW_FULLSCREEN;		/* window is always fullscreen */
slouken@2765
    68
	window->flags |= SDL_WINDOW_SHOWN;			/* only one window on iPod touch, always shown */
slouken@2765
    69
	window->flags |= SDL_WINDOW_INPUT_FOCUS;	/* always has input focus */	
slouken@2765
    70
slouken@2765
    71
	/* SDL_WINDOW_BORDERLESS controls whether status bar is hidden */
slouken@2765
    72
	if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@2765
    73
		[UIApplication sharedApplication].statusBarHidden = YES;
slouken@2765
    74
	}
slouken@2765
    75
	else {
slouken@2765
    76
		[UIApplication sharedApplication].statusBarHidden = NO;
slouken@2765
    77
	}
slouken@2765
    78
	
slouken@2765
    79
    return 0;
slouken@2765
    80
	
slouken@2765
    81
}
slouken@2765
    82
slouken@2765
    83
int UIKit_CreateWindow(_THIS, SDL_Window *window) {
slouken@2765
    84
		
slouken@2765
    85
	/* iPhone applications are single window only */
slouken@2765
    86
	if (nil != [SDLUIKitDelegate sharedAppDelegate].window) {
slouken@2765
    87
		SDL_SetError("Window already exists, no multi-window support.");
slouken@2765
    88
		return -1;
slouken@2765
    89
	}
slouken@2765
    90
	
slouken@2765
    91
	/* ignore the size user requested, and make a fullscreen window */
slouken@2765
    92
	UIWindow *uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
slouken@2765
    93
	
slouken@2765
    94
	if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
slouken@2765
    95
        [uiwindow release];
slouken@2765
    96
        return -1;
slouken@2765
    97
    }	
slouken@2765
    98
	
slouken@2765
    99
	[SDLUIKitDelegate sharedAppDelegate].window = uiwindow;
slouken@2765
   100
	[uiwindow release]; /* release the window (the app delegate has retained it) */
slouken@2765
   101
	
slouken@2765
   102
	return 1;
slouken@2765
   103
	
slouken@2765
   104
}
slouken@2765
   105
slouken@2765
   106
void UIKit_DestroyWindow(_THIS, SDL_Window * window) {
slouken@2765
   107
	/* don't worry, the delegate will automatically release the window */
slouken@2765
   108
	
slouken@2765
   109
	SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@2765
   110
	if (data) {
slouken@2765
   111
		SDL_free( window->driverdata );
slouken@2765
   112
	}
slouken@2765
   113
slouken@2765
   114
	/* this will also destroy the window */
slouken@2765
   115
	[SDLUIKitDelegate sharedAppDelegate].window = nil;
slouken@2765
   116
slouken@2765
   117
}
slouken@2765
   118
slouken@2765
   119
/* vi: set ts=4 sw=4 expandtab: */