cocoaTouch/SDLOverrides/SDL_uikitopengles.m
author koda
Sat, 16 Jan 2010 06:48:56 +0000
changeset 2697 75880595a9f1
parent 2692 ce9992075118
permissions -rw-r--r--
code cleanup and opengles optimizations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2692
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
     1
/*
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
     2
 SDL - Simple DirectMedia Layer
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
     3
 Copyright (C) 1997-2009 Sam Lantinga
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
     4
 
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
     5
 This library is free software; you can redistribute it and/or
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
     6
 modify it under the terms of the GNU Lesser General Public
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
     7
 License as published by the Free Software Foundation; either
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
     8
 version 2.1 of the License, or (at your option) any later version.
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
     9
 
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    10
 This library is distributed in the hope that it will be useful,
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    13
 Lesser General Public License for more details.
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    14
 
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    15
 You should have received a copy of the GNU Lesser General Public
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    16
 License along with this library; if not, write to the Free Software
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    17
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    18
 
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    19
 Sam Lantinga
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    20
 slouken@libsdl.org
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    21
 */
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    22
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    23
#include "SDL_uikitopengles.h"
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    24
#include "SDL_uikitopenglview.h"
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    25
#include "SDL_uikitappdelegate.h"
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    26
#include "SDL_uikitwindow.h"
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    27
#include "jumphack.h"
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    28
#include "SDL_sysvideo.h"
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    29
#include "SDL_loadso.h"
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    30
#include <dlfcn.h>
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    31
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    32
static int UIKit_GL_Initialize(_THIS);
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    33
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    34
void *
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    35
UIKit_GL_GetProcAddress(_THIS, const char *proc)
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    36
{	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    37
	/* Look through all SO's for the proc symbol.  Here's why:
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    38
	   -Looking for the path to the OpenGL Library seems not to work in the iPhone Simulator.
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    39
	   -We don't know that the path won't change in the future.
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    40
	*/
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    41
    return SDL_LoadFunction(RTLD_DEFAULT, proc);
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    42
}
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    43
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    44
/*
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    45
	note that SDL_GL_Delete context makes it current without passing the window
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    46
*/
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    47
int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    48
{
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    49
	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    50
	if (context) {
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    51
		SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    52
		[data->view setCurrentContext];
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    53
	}
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    54
	else {
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    55
		[EAGLContext setCurrentContext: nil];
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    56
	}
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    57
		
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    58
    return 0;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    59
}
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    60
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    61
int
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    62
UIKit_GL_LoadLibrary(_THIS, const char *path)
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    63
{
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    64
	/* 
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    65
		shouldn't be passing a path into this function 
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    66
		why?  Because we've already loaded the library
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    67
		and because the SDK forbids loading an external SO
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    68
	*/
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    69
    if (path != NULL) {
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    70
		SDL_SetError("iPhone GL Load Library just here for compatibility");
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    71
		return -1;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    72
    }
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    73
    return 0;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    74
}
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    75
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    76
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    77
void UIKit_GL_SwapWindow(_THIS, SDL_Window * window)
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    78
{
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    79
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    80
	SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    81
	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    82
	if (nil == data->view) {
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    83
		return;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    84
	}
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    85
	[data->view swapBuffers];
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    86
	/* since now we've got something to draw
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    87
	   make the window visible */
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    88
	[data->uiwindow makeKeyAndVisible];
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    89
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    90
	/* we need to let the event cycle run, or the OS won't update the OpenGL view! */
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    91
	SDL_PumpEvents();
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    92
	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    93
}
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    94
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    95
SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window)
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    96
{
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    97
	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    98
	SDL_uikitopenglview *view;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
    99
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   100
	SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   101
	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   102
	/* construct our view, passing in SDL's OpenGL configuration data */
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   103
	view = [[SDL_uikitopenglview alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame] \
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   104
									retainBacking: _this->gl_config.retained_backing \
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   105
									rBits: _this->gl_config.red_size \
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   106
									gBits: _this->gl_config.green_size \
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   107
									bBits: _this->gl_config.blue_size \
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   108
									aBits: _this->gl_config.alpha_size \
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   109
									depthBits: _this->gl_config.depth_size];
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   110
	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   111
	// add a tag to be able to remove from superview once finished
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   112
	view.tag = 54867;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   113
	data->view = view;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   114
	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   115
	/* add the view to our window */
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   116
	[data->uiwindow addSubview: view ];
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   117
	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   118
	/* Don't worry, the window retained the view */
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   119
	[view release];
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   120
	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   121
	if ( UIKit_GL_MakeCurrent(_this, window, view) < 0 ) {
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   122
        UIKit_GL_DeleteContext(_this, view);
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   123
        return NULL;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   124
    }
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   125
		
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   126
	return view;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   127
}
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   128
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   129
void UIKit_GL_DeleteContext(_THIS, SDL_GLContext context)
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   130
{
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   131
	/* the delegate has retained the view, this will release him */
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   132
	SDL_uikitopenglview *view = (SDL_uikitopenglview *)context;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   133
	/* this will also delete it */
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   134
	[view removeFromSuperview];
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   135
	
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   136
	return;
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   137
}
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   138
ce9992075118 better network support + initial work for returning to frontend
koda
parents:
diff changeset
   139