src/video/bwindow/SDL_bopengl.cc
author Nathan Heisey <nathanheisey@gmail.com>
Sat, 20 Aug 2011 15:16:13 +0000
changeset 5938 b85b6326c498
parent 5936 116b7457e73a
child 5939 d9e9c343d264
permissions -rw-r--r--
Temporary mode-setting hack works
nathanheisey@5918
     1
/*
nathanheisey@5918
     2
  Simple DirectMedia Layer
nathanheisey@5918
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
nathanheisey@5918
     4
nathanheisey@5918
     5
  This software is provided 'as-is', without any express or implied
nathanheisey@5918
     6
  warranty.  In no event will the authors be held liable for any damages
nathanheisey@5918
     7
  arising from the use of this software.
nathanheisey@5918
     8
nathanheisey@5918
     9
  Permission is granted to anyone to use this software for any purpose,
nathanheisey@5918
    10
  including commercial applications, and to alter it and redistribute it
nathanheisey@5918
    11
  freely, subject to the following restrictions:
nathanheisey@5918
    12
nathanheisey@5918
    13
  1. The origin of this software must not be misrepresented; you must not
nathanheisey@5918
    14
     claim that you wrote the original software. If you use this software
nathanheisey@5918
    15
     in a product, an acknowledgment in the product documentation would be
nathanheisey@5918
    16
     appreciated but is not required.
nathanheisey@5918
    17
  2. Altered source versions must be plainly marked as such, and must not be
nathanheisey@5918
    18
     misrepresented as being the original software.
nathanheisey@5918
    19
  3. This notice may not be removed or altered from any source distribution.
nathanheisey@5918
    20
*/
nathanheisey@5918
    21
nathanheisey@5918
    22
#include "SDL_bopengl.h"
nathanheisey@5918
    23
nathanheisey@5931
    24
#include <unistd.h>
nathanheisey@5931
    25
#include <KernelKit.h>
nathanheisey@5931
    26
#include <OpenGLKit.h>
nathanheisey@5931
    27
#include "SDL_BWin.h"
nathanheisey@5931
    28
#include "../../main/beos/SDL_BApp.h"
nathanheisey@5931
    29
nathanheisey@5919
    30
#ifdef __cplusplus
nathanheisey@5919
    31
extern "C" {
nathanheisey@5919
    32
#endif
nathanheisey@5918
    33
nathanheisey@5938
    34
nathanheisey@5938
    35
#define BGL_FLAGS BGL_RGB | BGL_DOUBLE
nathanheisey@5938
    36
nathanheisey@5931
    37
static inline SDL_BWin *_ToBeWin(SDL_Window *window) {
nathanheisey@5931
    38
	return ((SDL_BWin*)(window->driverdata));
nathanheisey@5931
    39
}
nathanheisey@5931
    40
nathanheisey@5931
    41
static inline SDL_BApp *_GetBeApp() {
nathanheisey@5931
    42
	return ((SDL_BApp*)be_app);
nathanheisey@5931
    43
}
nathanheisey@5931
    44
nathanheisey@5918
    45
/* Passing a NULL path means load pointers from the application */
nathanheisey@5918
    46
int BE_GL_LoadLibrary(_THIS, const char *path)
nathanheisey@5918
    47
{
nathanheisey@5935
    48
	image_info info;
nathanheisey@5935
    49
			int32 cookie = 0;
nathanheisey@5935
    50
	while (get_next_image_info(0, &cookie, &info) == B_OK) {
nathanheisey@5935
    51
		void *location = NULL;
nathanheisey@5935
    52
		if( get_image_symbol(info.id, "glBegin", B_SYMBOL_TYPE_ANY,
nathanheisey@5935
    53
				&location) == B_OK) {
nathanheisey@5936
    54
nathanheisey@5935
    55
			_this->gl_config.dll_handle = (void *) info.id;
nathanheisey@5935
    56
			_this->gl_config.driver_loaded = 1;
nathanheisey@5935
    57
			SDL_strlcpy(_this->gl_config.driver_path, "libGL.so",
nathanheisey@5935
    58
					SDL_arraysize(_this->gl_config.driver_path));
nathanheisey@5935
    59
		}
nathanheisey@5935
    60
	}
nathanheisey@5935
    61
#if 0
nathanheisey@5935
    62
printf("\n\nLibrary loading: %s\n\n", path);
nathanheisey@5918
    63
	if (path == NULL) {
nathanheisey@5918
    64
		if (_this->gl_config.dll_handle == NULL) {
nathanheisey@5918
    65
			image_info info;
nathanheisey@5918
    66
			int32 cookie = 0;
nathanheisey@5918
    67
			while (get_next_image_info(0, &cookie, &info) == B_OK) {
nathanheisey@5935
    68
				printf(__FILE__": %d - Inside while\n",__LINE__);
nathanheisey@5918
    69
				void *location = NULL;
nathanheisey@5918
    70
				if (get_image_symbol
nathanheisey@5918
    71
				((image_id) cookie, "glBegin",
nathanheisey@5918
    72
				B_SYMBOL_TYPE_ANY, &location) == B_OK) {
nathanheisey@5918
    73
					_this->gl_config.dll_handle = (void *) cookie;
nathanheisey@5918
    74
					_this->gl_config.driver_loaded = 1;
nathanheisey@5918
    75
					SDL_strlcpy(_this->gl_config.driver_path,
nathanheisey@5918
    76
					"libGL.so",
nathanheisey@5918
    77
					SDL_arraysize(_this->
nathanheisey@5918
    78
					gl_config.driver_path));
nathanheisey@5918
    79
				}
nathanheisey@5918
    80
			}
nathanheisey@5918
    81
		}
nathanheisey@5918
    82
	} else {
nathanheisey@5918
    83
        /*
nathanheisey@5918
    84
           FIXME None of BeOS libGL.so implementations have exported functions 
nathanheisey@5918
    85
           to load BGLView, which should be reloaded from new lib.
nathanheisey@5918
    86
           So for now just "load" linked libGL.so :(
nathanheisey@5918
    87
        */
nathanheisey@5918
    88
        if (_this->gl_config.dll_handle == NULL) {
nathanheisey@5918
    89
        	return BE_GL_LoadLibrary(_this, NULL);
nathanheisey@5918
    90
        }
nathanheisey@5918
    91
            /* Unload old first */
nathanheisey@5918
    92
            /*if (_this->gl_config.dll_handle != NULL) { */
nathanheisey@5918
    93
            /* Do not try to unload application itself (if LoadLibrary was called before with NULL ;) */
nathanheisey@5918
    94
            /*      image_info info;
nathanheisey@5918
    95
               if (get_image_info((image_id)_this->gl_config.dll_handle, &info) == B_OK) {
nathanheisey@5918
    96
               if (info.type != B_APP_IMAGE) {
nathanheisey@5918
    97
               unload_add_on((image_id)_this->gl_config.dll_handle);
nathanheisey@5918
    98
               }
nathanheisey@5918
    99
               }
nathanheisey@5918
   100
nathanheisey@5918
   101
               }
nathanheisey@5918
   102
nathanheisey@5918
   103
               if ((_this->gl_config.dll_handle = (void*)load_add_on(path)) != (void*)B_ERROR) {
nathanheisey@5918
   104
               _this->gl_config.driver_loaded = 1;
nathanheisey@5918
   105
               SDL_strlcpy(_this->gl_config.driver_path, path, SDL_arraysize(_this->gl_config.driver_path));
nathanheisey@5918
   106
               } */
nathanheisey@5918
   107
	}
nathanheisey@5918
   108
	
nathanheisey@5918
   109
	if (_this->gl_config.dll_handle != NULL) {
nathanheisey@5918
   110
		return 0;
nathanheisey@5918
   111
	} else {
nathanheisey@5935
   112
printf(__FILE__": %d- gl_config.driver_loaded = 0\n", __LINE__);
nathanheisey@5918
   113
		_this->gl_config.dll_handle = NULL;
nathanheisey@5918
   114
		_this->gl_config.driver_loaded = 0;
nathanheisey@5918
   115
		*_this->gl_config.driver_path = '\0';
nathanheisey@5918
   116
		return -1;
nathanheisey@5918
   117
	}
nathanheisey@5935
   118
#endif
nathanheisey@5918
   119
}
nathanheisey@5918
   120
nathanheisey@5918
   121
void *BE_GL_GetProcAddress(_THIS, const char *proc)
nathanheisey@5918
   122
{
nathanheisey@5918
   123
	if (_this->gl_config.dll_handle != NULL) {
nathanheisey@5918
   124
		void *location = NULL;
nathanheisey@5918
   125
		status_t err;
nathanheisey@5918
   126
		if ((err =
nathanheisey@5918
   127
			get_image_symbol((image_id) _this->gl_config.dll_handle,
nathanheisey@5918
   128
                              proc, B_SYMBOL_TYPE_ANY,
nathanheisey@5918
   129
                              &location)) == B_OK) {
nathanheisey@5918
   130
            return location;
nathanheisey@5918
   131
        } else {
nathanheisey@5918
   132
                SDL_SetError("Couldn't find OpenGL symbol");
nathanheisey@5918
   133
                return NULL;
nathanheisey@5918
   134
        }
nathanheisey@5918
   135
	} else {
nathanheisey@5918
   136
		SDL_SetError("OpenGL library not loaded");
nathanheisey@5918
   137
		return NULL;
nathanheisey@5918
   138
	}
nathanheisey@5918
   139
}
nathanheisey@5918
   140
nathanheisey@5918
   141
nathanheisey@5934
   142
nathanheisey@5934
   143
nathanheisey@5934
   144
void BE_GL_SwapWindow(_THIS, SDL_Window * window) {
nathanheisey@5934
   145
    _ToBeWin(window)->SwapBuffers();
nathanheisey@5934
   146
}
nathanheisey@5934
   147
nathanheisey@5934
   148
int BE_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) {
nathanheisey@5934
   149
	_GetBeApp()->SetCurrentContext(((SDL_BWin*)context)->GetGLView());
nathanheisey@5934
   150
	return 0;
nathanheisey@5934
   151
}
nathanheisey@5934
   152
nathanheisey@5934
   153
nathanheisey@5934
   154
SDL_GLContext BE_GL_CreateContext(_THIS, SDL_Window * window) {
nathanheisey@5934
   155
	/* FIXME: Not sure what flags should be included here; may want to have
nathanheisey@5934
   156
	   most of them */
nathanheisey@5934
   157
	SDL_BWin *bwin = _ToBeWin(window);
nathanheisey@5938
   158
	bwin->CreateGLView(BGL_FLAGS);
nathanheisey@5934
   159
	return (SDL_GLContext)(bwin);
nathanheisey@5934
   160
}
nathanheisey@5934
   161
nathanheisey@5934
   162
void BE_GL_DeleteContext(_THIS, SDL_GLContext context) {
nathanheisey@5934
   163
	/* Currently, automatically unlocks the view */
nathanheisey@5938
   164
printf(__FILE__": %d\n", __LINE__);
nathanheisey@5934
   165
	((SDL_BWin*)context)->RemoveGLView();
nathanheisey@5934
   166
}
nathanheisey@5934
   167
nathanheisey@5934
   168
nathanheisey@5934
   169
int BE_GL_SetSwapInterval(_THIS, int interval) {
nathanheisey@5934
   170
	printf(__FILE__": %d- swap interval set\n", __LINE__);
nathanheisey@5934
   171
	return 0;
nathanheisey@5934
   172
}
nathanheisey@5934
   173
nathanheisey@5934
   174
int BE_GL_GetSwapInterval(_THIS) {
nathanheisey@5934
   175
	printf(__FILE__": %d- swap interval requested\n", __LINE__);
nathanheisey@5934
   176
	return 0;
nathanheisey@5934
   177
}
nathanheisey@5934
   178
nathanheisey@5934
   179
nathanheisey@5934
   180
void BE_GL_UnloadLibrary(_THIS) {
nathanheisey@5934
   181
	printf(__FILE__": %d- Library unloaded\n", __LINE__);
nathanheisey@5934
   182
}
nathanheisey@5934
   183
nathanheisey@5934
   184
nathanheisey@5934
   185
nathanheisey@5938
   186
void BE_GL_RebootContexts(_THIS) {
nathanheisey@5938
   187
	SDL_Window *window = _this->windows;
nathanheisey@5938
   188
	while(window) {
nathanheisey@5938
   189
		SDL_BWin *bwin = _ToBeWin(window);
nathanheisey@5938
   190
		if(bwin->GetGLView()) {
nathanheisey@5938
   191
			bwin->LockLooper();
nathanheisey@5938
   192
			bwin->RemoveGLView();
nathanheisey@5938
   193
			bwin->CreateGLView(BGL_FLAGS);
nathanheisey@5938
   194
			bwin->UnlockLooper();
nathanheisey@5938
   195
		}
nathanheisey@5938
   196
		window = window->next;
nathanheisey@5938
   197
	}
nathanheisey@5938
   198
}
nathanheisey@5934
   199
nathanheisey@5934
   200
nathanheisey@5918
   201
#if 0 /* Functions from 1.2 that do not appear to be used in 1.3 */
nathanheisey@5918
   202
nathanheisey@5918
   203
    int BE_GL_GetAttribute(_THIS, SDL_GLattr attrib, int *value)
nathanheisey@5918
   204
    {
nathanheisey@5918
   205
        /*
nathanheisey@5918
   206
           FIXME? Right now BE_GL_GetAttribute shouldn't be called between glBegin() and glEnd() - it doesn't use "cached" values
nathanheisey@5918
   207
         */
nathanheisey@5918
   208
        switch (attrib) {
nathanheisey@5918
   209
        case SDL_GL_RED_SIZE:
nathanheisey@5918
   210
            glGetIntegerv(GL_RED_BITS, (GLint *) value);
nathanheisey@5918
   211
            break;
nathanheisey@5918
   212
        case SDL_GL_GREEN_SIZE:
nathanheisey@5918
   213
            glGetIntegerv(GL_GREEN_BITS, (GLint *) value);
nathanheisey@5918
   214
            break;
nathanheisey@5918
   215
        case SDL_GL_BLUE_SIZE:
nathanheisey@5918
   216
            glGetIntegerv(GL_BLUE_BITS, (GLint *) value);
nathanheisey@5918
   217
            break;
nathanheisey@5918
   218
        case SDL_GL_ALPHA_SIZE:
nathanheisey@5918
   219
            glGetIntegerv(GL_ALPHA_BITS, (GLint *) value);
nathanheisey@5918
   220
            break;
nathanheisey@5918
   221
        case SDL_GL_DOUBLEBUFFER:
nathanheisey@5918
   222
            glGetBooleanv(GL_DOUBLEBUFFER, (GLboolean *) value);
nathanheisey@5918
   223
            break;
nathanheisey@5918
   224
        case SDL_GL_BUFFER_SIZE:
nathanheisey@5918
   225
            int v;
nathanheisey@5918
   226
            glGetIntegerv(GL_RED_BITS, (GLint *) & v);
nathanheisey@5918
   227
            *value = v;
nathanheisey@5918
   228
            glGetIntegerv(GL_GREEN_BITS, (GLint *) & v);
nathanheisey@5918
   229
            *value += v;
nathanheisey@5918
   230
            glGetIntegerv(GL_BLUE_BITS, (GLint *) & v);
nathanheisey@5918
   231
            *value += v;
nathanheisey@5918
   232
            glGetIntegerv(GL_ALPHA_BITS, (GLint *) & v);
nathanheisey@5918
   233
            *value += v;
nathanheisey@5918
   234
            break;
nathanheisey@5918
   235
        case SDL_GL_DEPTH_SIZE:
nathanheisey@5918
   236
            glGetIntegerv(GL_DEPTH_BITS, (GLint *) value);      /* Mesa creates 16 only? r5 always 32 */
nathanheisey@5918
   237
            break;
nathanheisey@5918
   238
        case SDL_GL_STENCIL_SIZE:
nathanheisey@5918
   239
            glGetIntegerv(GL_STENCIL_BITS, (GLint *) value);
nathanheisey@5918
   240
            break;
nathanheisey@5918
   241
        case SDL_GL_ACCUM_RED_SIZE:
nathanheisey@5918
   242
            glGetIntegerv(GL_ACCUM_RED_BITS, (GLint *) value);
nathanheisey@5918
   243
            break;
nathanheisey@5918
   244
        case SDL_GL_ACCUM_GREEN_SIZE:
nathanheisey@5918
   245
            glGetIntegerv(GL_ACCUM_GREEN_BITS, (GLint *) value);
nathanheisey@5918
   246
            break;
nathanheisey@5918
   247
        case SDL_GL_ACCUM_BLUE_SIZE:
nathanheisey@5918
   248
            glGetIntegerv(GL_ACCUM_BLUE_BITS, (GLint *) value);
nathanheisey@5918
   249
            break;
nathanheisey@5918
   250
        case SDL_GL_ACCUM_ALPHA_SIZE:
nathanheisey@5918
   251
            glGetIntegerv(GL_ACCUM_ALPHA_BITS, (GLint *) value);
nathanheisey@5918
   252
            break;
nathanheisey@5918
   253
        case SDL_GL_STEREO:
nathanheisey@5918
   254
        case SDL_GL_MULTISAMPLEBUFFERS:
nathanheisey@5918
   255
        case SDL_GL_MULTISAMPLESAMPLES:
nathanheisey@5918
   256
        default:
nathanheisey@5918
   257
            *value = 0;
nathanheisey@5918
   258
            return (-1);
nathanheisey@5918
   259
        }
nathanheisey@5918
   260
        return 0;
nathanheisey@5918
   261
    }
nathanheisey@5918
   262
nathanheisey@5931
   263
#endif
nathanheisey@5931
   264
nathanheisey@5931
   265
nathanheisey@5931
   266
nathanheisey@5931
   267
nathanheisey@5931
   268
nathanheisey@5931
   269
nathanheisey@5931
   270
nathanheisey@5919
   271
nathanheisey@5919
   272
#ifdef __cplusplus
nathanheisey@5919
   273
}
nathanheisey@5919
   274
#endif