src/video/uikit/SDL_uikitwindow.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 21 Jan 2010 06:21:52 +0000
changeset 3685 64ce267332c6
parent 3400 4ec48602f1db
child 4446 8b03a20b320f
permissions -rw-r--r--
Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
slouken@2765
     1
/*
slouken@2765
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@3685
    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@3685
    51
    data->window = window;
slouken@2765
    52
    data->uiwindow = uiwindow;
slouken@3685
    53
    data->view = nil;
slouken@3685
    54
slouken@2765
    55
    /* Fill in the SDL window with the window data */
slouken@3685
    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@3685
    62
    
slouken@3685
    63
    window->driverdata = data;
slouken@3685
    64
    
slouken@3685
    65
    window->flags &= ~SDL_WINDOW_RESIZABLE;        /* window is NEVER resizeable */
slouken@3685
    66
    window->flags |= SDL_WINDOW_OPENGL;            /* window is always OpenGL */
slouken@3685
    67
    window->flags |= SDL_WINDOW_FULLSCREEN;        /* window is always fullscreen */
slouken@3685
    68
    window->flags |= SDL_WINDOW_SHOWN;            /* only one window on iPod touch, always shown */
slouken@3685
    69
    window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */    
slouken@2765
    70
slouken@3685
    71
    /* SDL_WINDOW_BORDERLESS controls whether status bar is hidden */
slouken@3685
    72
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@3685
    73
        [UIApplication sharedApplication].statusBarHidden = YES;
slouken@3685
    74
    }
slouken@3685
    75
    else {
slouken@3685
    76
        [UIApplication sharedApplication].statusBarHidden = NO;
slouken@3685
    77
    }
slouken@3685
    78
    
slouken@2765
    79
    return 0;
slouken@3685
    80
    
slouken@2765
    81
}
slouken@2765
    82
slouken@2765
    83
int UIKit_CreateWindow(_THIS, SDL_Window *window) {
slouken@3685
    84
        
slouken@3685
    85
    /* We currently only handle single window applications on iPhone */
slouken@3685
    86
    if (nil != [SDLUIKitDelegate sharedAppDelegate].window) {
slouken@3685
    87
        SDL_SetError("Window already exists, no multi-window support.");
slouken@3685
    88
        return -1;
slouken@3685
    89
    }
slouken@3685
    90
    
slouken@3685
    91
    /* ignore the size user requested, and make a fullscreen window */
slouken@3685
    92
    UIWindow *uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
slouken@3685
    93
    
slouken@3685
    94
    if (SetupWindowData(_this, window, uiwindow, SDL_TRUE) < 0) {
slouken@2765
    95
        [uiwindow release];
slouken@2765
    96
        return -1;
slouken@3685
    97
    }    
slouken@3685
    98
    
slouken@3685
    99
    // This saves the main window in the app delegate so event callbacks can do stuff on the window.
slouken@3685
   100
    // This assumes a single window application design and needs to be fixed for multiple windows.
slouken@3685
   101
    [SDLUIKitDelegate sharedAppDelegate].window = window;
slouken@3685
   102
    [SDLUIKitDelegate sharedAppDelegate].uiwindow = uiwindow;
slouken@3685
   103
    [uiwindow release]; /* release the window (the app delegate has retained it) */
slouken@3685
   104
    
slouken@3685
   105
    return 1;
slouken@3685
   106
    
slouken@2765
   107
}
slouken@2765
   108
slouken@2765
   109
void UIKit_DestroyWindow(_THIS, SDL_Window * window) {
slouken@3685
   110
    /* don't worry, the delegate will automatically release the window */
slouken@3685
   111
    
slouken@3685
   112
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@3685
   113
    if (data) {
slouken@3685
   114
        SDL_free( window->driverdata );
slouken@3685
   115
    }
slouken@2765
   116
slouken@3685
   117
    /* this will also destroy the window */
slouken@3685
   118
    [SDLUIKitDelegate sharedAppDelegate].window = NULL;
slouken@3685
   119
    [SDLUIKitDelegate sharedAppDelegate].uiwindow = nil;
slouken@2765
   120
}
slouken@2765
   121
slouken@2765
   122
/* vi: set ts=4 sw=4 expandtab: */