-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_vidsdl.c
459 lines (389 loc) · 12.8 KB
/
gl_vidsdl.c
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// gl_vidsdl.c -- sdl opengl video driver
// dynamic library access
#include <dlfcn.h>
#include <SDL.h>
#if defined(__FreeBSD__)
#include <SDL11/SDL_opengl.h>
#else
#include <SDL/SDL_opengl.h>
#endif
#include "quakedef.h"
viddef_t vid; // global video state
// The original defaults
//#define BASEWIDTH 320
//#define BASEHEIGHT 200
// Much better for high resolution displays
#define BASEWIDTH (640)
#define BASEHEIGHT (480)
static qboolean mouse_avail;
static float mouse_x, mouse_y;
static int mouse_oldbuttonstate = 0;
// No support for option menus
void (*vid_menudrawfn)(void) = NULL;
void (*vid_menukeyfn)(int key) = NULL;
static int scr_width, scr_height;
#define WARP_WIDTH 320
#define WARP_HEIGHT 200
static SDL_Surface *screen = NULL;
/*-----------------------------------------------------------------------*/
/*
=================
GL_BeginRendering
=================
*/
void GL_BeginRendering (int *x, int *y, int *width, int *height)
{
extern cvar_t gl_clear;
*x = *y = 0;
*width = scr_width;
*height = scr_height;
// if (!wglMakeCurrent( maindc, baseRC ))
// Sys_Error ("wglMakeCurrent failed");
// glViewport (*x, *y, *width, *height);
}
void GL_EndRendering (void)
{
glFlush ();
SDL_GL_SwapBuffers ();
}
static void Check_Gamma (unsigned char *pal)
{
float f, inf;
unsigned char palette[768];
int i;
if ((i = COM_CheckParm("-gamma")) == 0) {
if ((gl_renderer && strstr(gl_renderer, "Voodoo")) ||
(gl_vendor && strstr(gl_vendor, "3Dfx")))
vid_gamma = 1;
else
vid_gamma = 0.6; // default to 0.7 on non-3dfx hardware
// PENTA: lowered to make things a little brighter
} else
vid_gamma = Q_atof(com_argv[i+1]);
for (i=0 ; i<768 ; i++)
{
f = pow ( (pal[i]+1)/256.0 , vid_gamma );
inf = f*255 + 0.5;
if (inf < 0)
inf = 0;
if (inf > 255)
inf = 255;
palette[i] = inf;
}
memcpy (pal, palette, sizeof(palette));
}
void VID_Init (void)
{
int pnum, chunk;
byte *cache;
int cachesize;
Uint8 video_bpp;
Uint16 video_w, video_h;
Uint32 flags;
const SDL_VideoInfo* info = NULL;
char gldir[MAX_OSPATH];
// Load the SDL library
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_CDROM) < 0)
Sys_Error("VID: Couldn't load SDL: %s", SDL_GetError());
// Get Video info
info=SDL_GetVideoInfo ();
if( !info ) {
Sys_Error ("Video query failed: %s\n", SDL_GetError ());
}
video_bpp = 32; // 32 bpp mandatory
// Define some necessary OpenGL features
SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
// Set up display mode (width and height)
vid.width = BASEWIDTH;
vid.height = BASEHEIGHT;
vid.maxwarpwidth = WARP_WIDTH;
vid.maxwarpheight = WARP_HEIGHT;
if ((pnum=COM_CheckParm("-winsize")))
{
if (pnum >= com_argc-2)
Sys_Error("VID: -winsize <width> <height>\n");
vid.width = Q_atoi(com_argv[pnum+1]);
vid.height = Q_atoi(com_argv[pnum+2]);
if (!vid.width || !vid.height)
Sys_Error("VID: Bad window width/height\n");
}
if ((pnum=COM_CheckParm("-width")))
{
if (pnum >= com_argc-1)
Sys_Error("VID: -width <width>\n");
vid.width = Q_atoi(com_argv[pnum+1]);
if (!vid.width)
Sys_Error("VID: Bad window width\n");
}
if ((pnum=COM_CheckParm("-height")))
{
if (pnum >= com_argc-1)
Sys_Error("VID: -height <height>\n");
vid.height = Q_atoi(com_argv[pnum+1]);
if (!vid.height)
Sys_Error("VID: Bad window height\n");
}
// Set video width, height and flags
flags = SDL_OPENGL;
if ( !COM_CheckParm ("-window") )
flags |= SDL_FULLSCREEN;
// Initialize display
if (!(screen = SDL_SetVideoMode(vid.width, vid.height, video_bpp, flags)))
Sys_Error("VID: Couldn't set video mode: %s\n", SDL_GetError());
scr_width = vid.width;
scr_height = vid.height;
vid.conheight = vid.height;
vid.conwidth = vid.width;
/*
if (vid.conheight > vid.height)
vid.conheight = vid.height;
if (vid.conwidth > vid.width)
vid.conwidth = vid.width;
*/
vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
vid.numpages = 2;
// Initialize OpenGL
GL_Init ();
sprintf (gldir, "%s/glquake", com_gamedir);
Sys_mkdir (gldir);
SDL_WM_SetCaption("tenebrae-sdl","tenebrae-sdl");
// Check for 3DFX Extensions and initialize them.
//VID_Init8bitPalette();
Con_SafePrintf ("Video mode %dx%d initialized.\n", vid.width, vid.height);
vid.recalc_refdef = 1; // force a surface cache flush
// initialize the mouse
SDL_ShowCursor(0);
}
void VID_Shutdown (void)
{
R_ShutdownShaders();
SDL_Quit();
}
/*
================
D_BeginDirectRect
================
*/
void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
{
/*
Uint8 *offset;
if (!screen) return;
if ( x < 0 ) x = screen->w+x-1;
offset = (Uint8 *)screen->pixels + y*screen->pitch + x;
while ( height-- )
{
memcpy(offset, pbitmap, width);
offset += screen->pitch;
pbitmap += width;
}
*/
}
/*
================
D_EndDirectRect
================
*/
void D_EndDirectRect (int x, int y, int width, int height)
{
/*
if (!screen) return;
if (x < 0) x = screen->w+x-1;
SDL_UpdateRect(screen, x, y, width, height);
*/
}
/*
================
Sys_SendKeyEvents
================
*/
void Sys_SendKeyEvents(void)
{
SDL_Event event;
int sym, state;
int modstate;
while (SDL_PollEvent(&event))
{
switch (event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP:
sym = event.key.keysym.sym;
state = event.key.state;
modstate = SDL_GetModState();
switch(sym)
{
case SDLK_DELETE: sym = K_DEL; break;
case SDLK_BACKSPACE: sym = K_BACKSPACE; break;
case SDLK_F1: sym = K_F1; break;
case SDLK_F2: sym = K_F2; break;
case SDLK_F3: sym = K_F3; break;
case SDLK_F4: sym = K_F4; break;
case SDLK_F5: sym = K_F5; break;
case SDLK_F6: sym = K_F6; break;
case SDLK_F7: sym = K_F7; break;
case SDLK_F8: sym = K_F8; break;
case SDLK_F9: sym = K_F9; break;
case SDLK_F10: sym = K_F10; break;
case SDLK_F11: sym = K_F11; break;
case SDLK_F12: sym = K_F12; break;
case SDLK_BREAK:
case SDLK_PAUSE: sym = K_PAUSE; break;
case SDLK_UP: sym = K_UPARROW; break;
case SDLK_DOWN: sym = K_DOWNARROW; break;
case SDLK_RIGHT: sym = K_RIGHTARROW; break;
case SDLK_LEFT: sym = K_LEFTARROW; break;
case SDLK_INSERT: sym = K_INS; break;
case SDLK_HOME: sym = K_HOME; break;
case SDLK_END: sym = K_END; break;
case SDLK_PAGEUP: sym = K_PGUP; break;
case SDLK_PAGEDOWN: sym = K_PGDN; break;
case SDLK_RSHIFT:
case SDLK_LSHIFT: sym = K_SHIFT; break;
case SDLK_RCTRL:
case SDLK_LCTRL: sym = K_CTRL; break;
case SDLK_RALT:
case SDLK_LALT: sym = K_ALT; break;
case SDLK_KP0:
if(modstate & KMOD_NUM) sym = K_INS;
else sym = SDLK_0;
break;
case SDLK_KP1:
if(modstate & KMOD_NUM) sym = K_END;
else sym = SDLK_1;
break;
case SDLK_KP2:
if(modstate & KMOD_NUM) sym = K_DOWNARROW;
else sym = SDLK_2;
break;
case SDLK_KP3:
if(modstate & KMOD_NUM) sym = K_PGDN;
else sym = SDLK_3;
break;
case SDLK_KP4:
if(modstate & KMOD_NUM) sym = K_LEFTARROW;
else sym = SDLK_4;
break;
case SDLK_KP5: sym = SDLK_5; break;
case SDLK_KP6:
if(modstate & KMOD_NUM) sym = K_RIGHTARROW;
else sym = SDLK_6;
break;
case SDLK_KP7:
if(modstate & KMOD_NUM) sym = K_HOME;
else sym = SDLK_7;
break;
case SDLK_KP8:
if(modstate & KMOD_NUM) sym = K_UPARROW;
else sym = SDLK_8;
break;
case SDLK_KP9:
if(modstate & KMOD_NUM) sym = K_PGUP;
else sym = SDLK_9;
break;
case SDLK_KP_PERIOD:
if(modstate & KMOD_NUM) sym = K_DEL;
else sym = SDLK_PERIOD;
break;
case SDLK_KP_DIVIDE: sym = SDLK_SLASH; break;
case SDLK_KP_MULTIPLY: sym = SDLK_ASTERISK; break;
case SDLK_KP_MINUS: sym = SDLK_MINUS; break;
case SDLK_KP_PLUS: sym = SDLK_PLUS; break;
case SDLK_KP_ENTER: sym = SDLK_RETURN; break;
case SDLK_KP_EQUALS: sym = SDLK_EQUALS; break;
}
// If we're not directly handled and still above 255
// just force it to 0
if(sym > 255) sym = 0;
Key_Event(sym, state);
break;
case SDL_MOUSEMOTION:
if ( (event.motion.x != (vid.width/2)) ||
(event.motion.y != (vid.height/2)) ) {
mouse_x = event.motion.xrel*10;
mouse_y = event.motion.yrel*10;
if ( (event.motion.x < ((vid.width/2)-(vid.width/4))) ||
(event.motion.x > ((vid.width/2)+(vid.width/4))) ||
(event.motion.y < ((vid.height/2)-(vid.height/4))) ||
(event.motion.y > ((vid.height/2)+(vid.height/4))) ) {
SDL_WarpMouse(vid.width/2, vid.height/2);
}
}
break;
case SDL_QUIT:
CL_Disconnect ();
Host_ShutdownServer(false);
Sys_Quit ();
break;
default:
break;
}
}
}
void IN_Init (void)
{
if ( COM_CheckParm ("-nomouse") )
return;
mouse_x = mouse_y = 0.0;
mouse_avail = 1;
}
void IN_Shutdown (void)
{
mouse_avail = 0;
}
void IN_Commands (void)
{
int i;
int mouse_buttonstate;
if (!mouse_avail) return;
i = SDL_GetMouseState(NULL, NULL);
/* Quake swaps the second and third buttons */
mouse_buttonstate = (i & ~0x06) | ((i & 0x02)<<1) | ((i & 0x04)>>1);
for (i=0 ; i<3 ; i++) {
if ( (mouse_buttonstate & (1<<i)) && !(mouse_oldbuttonstate & (1<<i)) )
Key_Event (K_MOUSE1 + i, true);
if ( !(mouse_buttonstate & (1<<i)) && (mouse_oldbuttonstate & (1<<i)) )
Key_Event (K_MOUSE1 + i, false);
}
mouse_oldbuttonstate = mouse_buttonstate;
}
void IN_Move (usercmd_t *cmd)
{
if (!mouse_avail)
return;
mouse_x *= sensitivity.value;
mouse_y *= sensitivity.value;
if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
cmd->sidemove += m_side.value * mouse_x;
else
cl.viewangles[YAW] -= m_yaw.value * mouse_x;
if (in_mlook.state & 1)
V_StopPitchDrift ();
if ( (in_mlook.state & 1) && !(in_strafe.state & 1)) {
cl.viewangles[PITCH] += m_pitch.value * mouse_y;
if (cl.viewangles[PITCH] > 80)
cl.viewangles[PITCH] = 80;
if (cl.viewangles[PITCH] < -70)
cl.viewangles[PITCH] = -70;
} else {
if ((in_strafe.state & 1) && noclip_anglehack)
cmd->upmove -= m_forward.value * mouse_y;
else
cmd->forwardmove -= m_forward.value * mouse_y;
}
mouse_x = mouse_y = 0.0;
}
/*
================
Sys_ConsoleInput
================
*/
char *Sys_ConsoleInput (void)
{
return 0;
}