Skip to content

Latest commit

 

History

History
331 lines (279 loc) · 6.87 KB

SDL_atarigl.c

File metadata and controls

331 lines (279 loc) · 6.87 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2004 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@libsdl.org
*/
/* Atari OSMesa.ldg implementation of SDL OpenGL support */
/*--- Includes ---*/
#ifdef HAVE_OPENGL
#include <GL/osmesa.h>
#endif
#include "SDL_video.h"
#include "SDL_error.h"
#include "SDL_endian.h"
#include "SDL_atarigl_c.h"
/*--- Variables ---*/
/*--- Functions prototypes ---*/
static void ConvertNull(SDL_Surface *surface);
static void Convert565To555be(SDL_Surface *surface);
static void Convert565To555le(SDL_Surface *surface);
static void Convert565le(SDL_Surface *surface);
static void ConvertBGRAToABGR(SDL_Surface *surface);
/*--- Public functions ---*/
int SDL_AtariGL_Init(_THIS, SDL_Surface *current)
{
#ifdef HAVE_OPENGL
GLenum osmesa_format;
SDL_PixelFormat *pixel_format;
Uint32 redmask;
SDL_AtariGL_Quit(this); /* Destroy previous context if exist */
/* Init OpenGL context using OSMesa */
gl_convert = ConvertNull;
pixel_format = current->format;
redmask = pixel_format->Rmask;
switch (pixel_format->BitsPerPixel) {
case 15:
/* 1555, big and little endian, unsupported */
osmesa_format = OSMESA_RGB_565;
if (redmask == 31<<10) {
gl_convert = Convert565To555be;
} else {
gl_convert = Convert565To555le;
}
break;
case 16:
if (redmask == 31<<11) {
osmesa_format = OSMESA_RGB_565;
} else {
/* 565, little endian, unsupported */
osmesa_format = OSMESA_RGB_565;
gl_convert = Convert565le;
}
break;
case 24:
if (redmask == 255<<16) {
osmesa_format = OSMESA_RGB;
} else {
osmesa_format = OSMESA_BGR;
}
break;
case 32:
if (redmask == 255<<16) {
osmesa_format = OSMESA_ARGB;
} else if (redmask == 255<<8) {
osmesa_format = OSMESA_BGRA;
} else if (redmask == 255<<24) {
osmesa_format = OSMESA_RGBA;
} else {
/* ABGR format unsupported */
osmesa_format = OSMESA_BGRA;
gl_convert = ConvertBGRAToABGR;
}
break;
default:
osmesa_format = OSMESA_COLOR_INDEX;
break;
}
gl_ctx = OSMesaCreateContextExt( osmesa_format, this->gl_config.depth_size,
this->gl_config.stencil_size, this->gl_config.accum_red_size +
this->gl_config.accum_green_size + this->gl_config.accum_blue_size +
this->gl_config.accum_alpha_size, NULL );
gl_active = (gl_ctx != NULL);
return (gl_active);
#else
return 0;
#endif
}
void SDL_AtariGL_Quit(_THIS)
{
#ifdef HAVE_OPENGL
/* Shutdown OpenGL context */
if (gl_ctx) {
OSMesaDestroyContext(gl_ctx);
gl_ctx = NULL;
}
#endif
gl_active = 0;
}
int SDL_AtariGL_LoadLibrary(_THIS, const char *path)
{
#ifdef HAVE_OPENGL
/* Library is always opened */
this->gl_config.driver_loaded = 1;
#endif
return 0;
}
void *SDL_AtariGL_GetProcAddress(_THIS, const char *proc)
{
void *func = NULL;
#ifdef HAVE_OPENGL
if (gl_ctx != NULL) {
func = OSMesaGetProcAddress(proc);
}
#endif
return func;
}
int SDL_AtariGL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
{
#ifdef HAVE_OPENGL
GLenum mesa_attrib;
SDL_Surface *surface;
if (gl_ctx == NULL) {
return -1;
}
switch(attrib) {
case SDL_GL_RED_SIZE:
mesa_attrib = GL_RED_BITS;
break;
case SDL_GL_GREEN_SIZE:
mesa_attrib = GL_GREEN_BITS;
break;
case SDL_GL_BLUE_SIZE:
mesa_attrib = GL_BLUE_BITS;
break;
case SDL_GL_ALPHA_SIZE:
mesa_attrib = GL_ALPHA_BITS;
break;
case SDL_GL_DOUBLEBUFFER:
surface = this->screen;
*value = ((surface->flags & SDL_DOUBLEBUF)==SDL_DOUBLEBUF);
return 0;
case SDL_GL_DEPTH_SIZE:
mesa_attrib = GL_DEPTH_BITS;
break;
case SDL_GL_STENCIL_SIZE:
mesa_attrib = GL_STENCIL_BITS;
break;
case SDL_GL_ACCUM_RED_SIZE:
mesa_attrib = GL_ACCUM_RED_BITS;
break;
case SDL_GL_ACCUM_GREEN_SIZE:
mesa_attrib = GL_ACCUM_GREEN_BITS;
break;
case SDL_GL_ACCUM_BLUE_SIZE:
mesa_attrib = GL_ACCUM_BLUE_BITS;
break;
case SDL_GL_ACCUM_ALPHA_SIZE:
mesa_attrib = GL_ACCUM_ALPHA_BITS;
break;
default :
return -1;
}
glGetIntegerv(mesa_attrib, value);
return 0;
#else
return -1;
#endif
}
int SDL_AtariGL_MakeCurrent(_THIS)
{
#ifdef HAVE_OPENGL
SDL_Surface *surface;
GLenum type;
if (gl_ctx == NULL) {
SDL_SetError("Invalid OpenGL context");
return -1;
}
surface = this->screen;
if ((surface->format->BitsPerPixel == 15) || (surface->format->BitsPerPixel == 16)) {
type = GL_UNSIGNED_SHORT_5_6_5;
} else {
type = GL_UNSIGNED_BYTE;
}
if (!OSMesaMakeCurrent(gl_ctx, surface->pixels, type, surface->w, surface->h)) {
SDL_SetError("Can not make OpenGL context current");
return -1;
}
/* OSMesa draws upside down */
OSMesaPixelStore(OSMESA_Y_UP, 0);
return 0;
#else
return -1;
#endif
}
void SDL_AtariGL_SwapBuffers(_THIS)
{
#ifdef HAVE_OPENGL
if (gl_ctx == NULL) {
return;
}
gl_convert(this->screen);
#endif
}
/*--- Private functions ---*/
static void ConvertNull(SDL_Surface *surface)
{
}
static void Convert565To555be(SDL_Surface *surface)
{
int x,y, pitch;
unsigned short *line, *pixel;
line = surface->pixels;
pitch = surface->pitch >> 1;
for (y=0; y<surface->h; y++) {
pixel = line;
for (x=0; x<surface->w; x++) {
unsigned short color = *pixel;
*pixel++ = (color & 0x1f)|((color>>1) & 0xffe0);
}
line += pitch;
}
}
static void Convert565To555le(SDL_Surface *surface)
{
int x,y, pitch;
unsigned short *line, *pixel;
line = surface->pixels;
pitch = surface->pitch >>1;
for (y=0; y<surface->h; y++) {
pixel = line;
for (x=0; x<surface->w; x++) {
unsigned short color = *pixel;
color = (color & 0x1f)|((color>>1) & 0xffe0);
*pixel++ = SDL_Swap16(color);
}
line += pitch;
}
}
static void Convert565le(SDL_Surface *surface)
{
int x,y, pitch;
unsigned short *line, *pixel;
line = surface->pixels;
pitch = surface->pitch >>1;
for (y=0; y<surface->h; y++) {
pixel = line;
for (x=0; x<surface->w; x++) {
unsigned short color = *pixel;
*pixel++ = SDL_Swap16(color);
}
line += pitch;
}
}
static void ConvertBGRAToABGR(SDL_Surface *surface)
{
int x,y, pitch;
unsigned long *line, *pixel;
line = surface->pixels;
pitch = surface->pitch >>2;
for (y=0; y<surface->h; y++) {
pixel = line;
for (x=0; x<surface->w; x++) {
unsigned long color = *pixel;
*pixel++ = (color<<24)|(color>>8);
}
line += pitch;
}
}