gl_vidlinux.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <termios.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/stat.h>
  18. #include <sys/vt.h>
  19. #include <stdarg.h>
  20. #include <stdio.h>
  21. #include <signal.h>
  22. #include <asm/io.h>
  23. #include <dlfcn.h>
  24. /*#include "vga.h" */
  25. #include "vgakeyboard.h"
  26. #include "vgamouse.h"
  27. #include "quakedef.h"
  28. #include "GL/fxmesa.h"
  29. #define WARP_WIDTH 320
  30. #define WARP_HEIGHT 200
  31. static fxMesaContext fc = NULL;
  32. #define stringify(m) { #m, m }
  33. unsigned short d_8to16table[256];
  34. unsigned d_8to24table[256];
  35. unsigned char d_15to8table[65536];
  36. int num_shades=32;
  37. struct
  38. {
  39. char *name;
  40. int num;
  41. } mice[] =
  42. {
  43. stringify(MOUSE_MICROSOFT),
  44. stringify(MOUSE_MOUSESYSTEMS),
  45. stringify(MOUSE_MMSERIES),
  46. stringify(MOUSE_LOGITECH),
  47. stringify(MOUSE_BUSMOUSE),
  48. stringify(MOUSE_PS2),
  49. };
  50. static unsigned char scantokey[128];
  51. int num_mice = sizeof (mice) / sizeof(mice[0]);
  52. int d_con_indirect = 0;
  53. int svgalib_inited=0;
  54. int UseMouse = 1;
  55. int UseKeyboard = 1;
  56. int mouserate = MOUSE_DEFAULTSAMPLERATE;
  57. cvar_t vid_mode = {"vid_mode","5",false};
  58. cvar_t vid_redrawfull = {"vid_redrawfull","0",false};
  59. cvar_t vid_waitforrefresh = {"vid_waitforrefresh","0",true};
  60. char *framebuffer_ptr;
  61. cvar_t mouse_button_commands[3] =
  62. {
  63. {"mouse1","+attack"},
  64. {"mouse2","+strafe"},
  65. {"mouse3","+forward"},
  66. };
  67. int mouse_buttons;
  68. int mouse_buttonstate;
  69. int mouse_oldbuttonstate;
  70. float mouse_x, mouse_y;
  71. float old_mouse_x, old_mouse_y;
  72. int mx, my;
  73. cvar_t m_filter = {"m_filter","1"};
  74. int scr_width, scr_height;
  75. /*-----------------------------------------------------------------------*/
  76. //int texture_mode = GL_NEAREST;
  77. //int texture_mode = GL_NEAREST_MIPMAP_NEAREST;
  78. //int texture_mode = GL_NEAREST_MIPMAP_LINEAR;
  79. int texture_mode = GL_LINEAR;
  80. //int texture_mode = GL_LINEAR_MIPMAP_NEAREST;
  81. //int texture_mode = GL_LINEAR_MIPMAP_LINEAR;
  82. int texture_extension_number = 1;
  83. float gldepthmin, gldepthmax;
  84. cvar_t gl_ztrick = {"gl_ztrick","1"};
  85. const char *gl_vendor;
  86. const char *gl_renderer;
  87. const char *gl_version;
  88. const char *gl_extensions;
  89. void (*qgl3DfxSetPaletteEXT) (GLuint *);
  90. void (*qglColorTableEXT) (int, int, int, int, int, const void *);
  91. static float vid_gamma = 1.0;
  92. qboolean is8bit = false;
  93. qboolean isPermedia = false;
  94. qboolean gl_mtexable = false;
  95. /*-----------------------------------------------------------------------*/
  96. void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
  97. {
  98. }
  99. void D_EndDirectRect (int x, int y, int width, int height)
  100. {
  101. }
  102. int matchmouse(int mouse, char *name)
  103. {
  104. int i;
  105. for (i=0 ; i<num_mice ; i++)
  106. if (!strcmp(mice[i].name, name))
  107. return i;
  108. return mouse;
  109. }
  110. #if 0
  111. void vtswitch(int newconsole)
  112. {
  113. int fd;
  114. struct vt_stat x;
  115. // switch consoles and wait until reactivated
  116. fd = open("/dev/console", O_RDONLY);
  117. ioctl(fd, VT_GETSTATE, &x);
  118. ioctl(fd, VT_ACTIVATE, newconsole);
  119. ioctl(fd, VT_WAITACTIVE, x.v_active);
  120. close(fd);
  121. }
  122. #endif
  123. void keyhandler(int scancode, int state)
  124. {
  125. int sc;
  126. sc = scancode & 0x7f;
  127. Key_Event(scantokey[sc], state == KEY_EVENTPRESS);
  128. }
  129. void VID_Shutdown(void)
  130. {
  131. if (!fc)
  132. return;
  133. fxMesaDestroyContext(fc);
  134. if (UseKeyboard)
  135. keyboard_close();
  136. }
  137. void signal_handler(int sig)
  138. {
  139. printf("Received signal %d, exiting...\n", sig);
  140. Sys_Quit();
  141. exit(0);
  142. }
  143. void InitSig(void)
  144. {
  145. signal(SIGHUP, signal_handler);
  146. signal(SIGINT, signal_handler);
  147. signal(SIGQUIT, signal_handler);
  148. signal(SIGILL, signal_handler);
  149. signal(SIGTRAP, signal_handler);
  150. signal(SIGIOT, signal_handler);
  151. signal(SIGBUS, signal_handler);
  152. signal(SIGFPE, signal_handler);
  153. signal(SIGSEGV, signal_handler);
  154. signal(SIGTERM, signal_handler);
  155. }
  156. void VID_ShiftPalette(unsigned char *p)
  157. {
  158. // VID_SetPalette(p);
  159. }
  160. void VID_SetPalette (unsigned char *palette)
  161. {
  162. byte *pal;
  163. unsigned r,g,b;
  164. unsigned v;
  165. int r1,g1,b1;
  166. int j,k,l,m;
  167. unsigned short i;
  168. unsigned *table;
  169. FILE *f;
  170. char s[255];
  171. int dist, bestdist;
  172. static qboolean palflag = false;
  173. //
  174. // 8 8 8 encoding
  175. //
  176. pal = palette;
  177. table = d_8to24table;
  178. for (i=0 ; i<256 ; i++)
  179. {
  180. r = pal[0];
  181. g = pal[1];
  182. b = pal[2];
  183. pal += 3;
  184. v = (255<<24) + (r<<0) + (g<<8) + (b<<16);
  185. *table++ = v;
  186. }
  187. d_8to24table[255] &= 0xffffff; // 255 is transparent
  188. // JACK: 3D distance calcs - k is last closest, l is the distance.
  189. for (i=0; i < (1<<15); i++) {
  190. /* Maps
  191. 000000000000000
  192. 000000000011111 = Red = 0x1F
  193. 000001111100000 = Blue = 0x03E0
  194. 111110000000000 = Grn = 0x7C00
  195. */
  196. r = ((i & 0x1F) << 3)+4;
  197. g = ((i & 0x03E0) >> 2)+4;
  198. b = ((i & 0x7C00) >> 7)+4;
  199. pal = (unsigned char *)d_8to24table;
  200. for (v=0,k=0,bestdist=10000*10000; v<256; v++,pal+=4) {
  201. r1 = (int)r - (int)pal[0];
  202. g1 = (int)g - (int)pal[1];
  203. b1 = (int)b - (int)pal[2];
  204. dist = (r1*r1)+(g1*g1)+(b1*b1);
  205. if (dist < bestdist) {
  206. k=v;
  207. bestdist = dist;
  208. }
  209. }
  210. d_15to8table[i]=k;
  211. }
  212. }
  213. void CheckMultiTextureExtensions(void)
  214. {
  215. void *prjobj;
  216. if (strstr(gl_extensions, "GL_SGIS_multitexture ") && !COM_CheckParm("-nomtex")) {
  217. Con_Printf("Found GL_SGIS_multitexture...\n");
  218. if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL) {
  219. Con_Printf("Unable to open symbol list for main program.\n");
  220. return;
  221. }
  222. qglMTexCoord2fSGIS = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
  223. qglSelectTextureSGIS = (void *) dlsym(prjobj, "glSelectTextureSGIS");
  224. if (qglMTexCoord2fSGIS && qglSelectTextureSGIS) {
  225. Con_Printf("Multitexture extensions found.\n");
  226. gl_mtexable = true;
  227. } else
  228. Con_Printf("Symbol not found, disabled.\n");
  229. dlclose(prjobj);
  230. }
  231. }
  232. /*
  233. ===============
  234. GL_Init
  235. ===============
  236. */
  237. void GL_Init (void)
  238. {
  239. gl_vendor = glGetString (GL_VENDOR);
  240. Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
  241. gl_renderer = glGetString (GL_RENDERER);
  242. Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
  243. gl_version = glGetString (GL_VERSION);
  244. Con_Printf ("GL_VERSION: %s\n", gl_version);
  245. gl_extensions = glGetString (GL_EXTENSIONS);
  246. Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
  247. // Con_Printf ("%s %s\n", gl_renderer, gl_version);
  248. CheckMultiTextureExtensions ();
  249. glClearColor (1,0,0,0);
  250. glCullFace(GL_FRONT);
  251. glEnable(GL_TEXTURE_2D);
  252. glEnable(GL_ALPHA_TEST);
  253. glAlphaFunc(GL_GREATER, 0.666);
  254. glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  255. glShadeModel (GL_FLAT);
  256. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  257. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  258. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  259. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  260. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  261. // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  262. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  263. }
  264. /*
  265. =================
  266. GL_BeginRendering
  267. =================
  268. */
  269. void GL_BeginRendering (int *x, int *y, int *width, int *height)
  270. {
  271. extern cvar_t gl_clear;
  272. *x = *y = 0;
  273. *width = scr_width;
  274. *height = scr_height;
  275. // if (!wglMakeCurrent( maindc, baseRC ))
  276. // Sys_Error ("wglMakeCurrent failed");
  277. // glViewport (*x, *y, *width, *height);
  278. }
  279. void GL_EndRendering (void)
  280. {
  281. glFlush();
  282. fxMesaSwapBuffers();
  283. }
  284. void Init_KBD(void)
  285. {
  286. int i;
  287. if (COM_CheckParm("-nokbd")) UseKeyboard = 0;
  288. if (UseKeyboard)
  289. {
  290. for (i=0 ; i<128 ; i++)
  291. scantokey[i] = ' ';
  292. scantokey[42] = K_SHIFT;
  293. scantokey[54] = K_SHIFT;
  294. scantokey[72] = K_UPARROW;
  295. scantokey[103] = K_UPARROW;
  296. scantokey[80] = K_DOWNARROW;
  297. scantokey[108] = K_DOWNARROW;
  298. scantokey[75] = K_LEFTARROW;
  299. scantokey[105] = K_LEFTARROW;
  300. scantokey[77] = K_RIGHTARROW;
  301. scantokey[106] = K_RIGHTARROW;
  302. scantokey[29] = K_CTRL;
  303. scantokey[97] = K_CTRL;
  304. scantokey[56] = K_ALT;
  305. scantokey[100] = K_ALT;
  306. // scantokey[58] = JK_CAPS;
  307. // scantokey[69] = JK_NUM_LOCK;
  308. scantokey[71] = K_HOME;
  309. scantokey[73] = K_PGUP;
  310. scantokey[79] = K_END;
  311. scantokey[81] = K_PGDN;
  312. scantokey[82] = K_INS;
  313. scantokey[83] = K_DEL;
  314. scantokey[1 ] = K_ESCAPE;
  315. scantokey[28] = K_ENTER;
  316. scantokey[15] = K_TAB;
  317. scantokey[14] = K_BACKSPACE;
  318. scantokey[119] = K_PAUSE;
  319. scantokey[57] = ' ';
  320. scantokey[102] = K_HOME;
  321. scantokey[104] = K_PGUP;
  322. scantokey[107] = K_END;
  323. scantokey[109] = K_PGDN;
  324. scantokey[110] = K_INS;
  325. scantokey[111] = K_DEL;
  326. scantokey[2] = '1';
  327. scantokey[3] = '2';
  328. scantokey[4] = '3';
  329. scantokey[5] = '4';
  330. scantokey[6] = '5';
  331. scantokey[7] = '6';
  332. scantokey[8] = '7';
  333. scantokey[9] = '8';
  334. scantokey[10] = '9';
  335. scantokey[11] = '0';
  336. scantokey[12] = '-';
  337. scantokey[13] = '=';
  338. scantokey[41] = '`';
  339. scantokey[26] = '[';
  340. scantokey[27] = ']';
  341. scantokey[39] = ';';
  342. scantokey[40] = '\'';
  343. scantokey[51] = ',';
  344. scantokey[52] = '.';
  345. scantokey[53] = '/';
  346. scantokey[43] = '\\';
  347. scantokey[59] = K_F1;
  348. scantokey[60] = K_F2;
  349. scantokey[61] = K_F3;
  350. scantokey[62] = K_F4;
  351. scantokey[63] = K_F5;
  352. scantokey[64] = K_F6;
  353. scantokey[65] = K_F7;
  354. scantokey[66] = K_F8;
  355. scantokey[67] = K_F9;
  356. scantokey[68] = K_F10;
  357. scantokey[87] = K_F11;
  358. scantokey[88] = K_F12;
  359. scantokey[30] = 'a';
  360. scantokey[48] = 'b';
  361. scantokey[46] = 'c';
  362. scantokey[32] = 'd';
  363. scantokey[18] = 'e';
  364. scantokey[33] = 'f';
  365. scantokey[34] = 'g';
  366. scantokey[35] = 'h';
  367. scantokey[23] = 'i';
  368. scantokey[36] = 'j';
  369. scantokey[37] = 'k';
  370. scantokey[38] = 'l';
  371. scantokey[50] = 'm';
  372. scantokey[49] = 'n';
  373. scantokey[24] = 'o';
  374. scantokey[25] = 'p';
  375. scantokey[16] = 'q';
  376. scantokey[19] = 'r';
  377. scantokey[31] = 's';
  378. scantokey[20] = 't';
  379. scantokey[22] = 'u';
  380. scantokey[47] = 'v';
  381. scantokey[17] = 'w';
  382. scantokey[45] = 'x';
  383. scantokey[21] = 'y';
  384. scantokey[44] = 'z';
  385. scantokey[78] = '+';
  386. scantokey[74] = '-';
  387. if (keyboard_init())
  388. Sys_Error("keyboard_init() failed");
  389. keyboard_seteventhandler(keyhandler);
  390. }
  391. }
  392. #define NUM_RESOLUTIONS 16
  393. static int resolutions[NUM_RESOLUTIONS][3]={
  394. 320,200, GR_RESOLUTION_320x200,
  395. 320,240, GR_RESOLUTION_320x240,
  396. 400,256, GR_RESOLUTION_400x256,
  397. 400,300, GR_RESOLUTION_400x300,
  398. 512,384, GR_RESOLUTION_512x384,
  399. 640,200, GR_RESOLUTION_640x200,
  400. 640,350, GR_RESOLUTION_640x350,
  401. 640,400, GR_RESOLUTION_640x400,
  402. 640,480, GR_RESOLUTION_640x480,
  403. 800,600, GR_RESOLUTION_800x600,
  404. 960,720, GR_RESOLUTION_960x720,
  405. 856,480, GR_RESOLUTION_856x480,
  406. 512,256, GR_RESOLUTION_512x256,
  407. 1024,768, GR_RESOLUTION_1024x768,
  408. 1280,1024,GR_RESOLUTION_1280x1024,
  409. 1600,1200,GR_RESOLUTION_1600x1200
  410. };
  411. int findres(int *width, int *height)
  412. {
  413. int i;
  414. for(i=0;i<NUM_RESOLUTIONS;i++)
  415. if((*width<=resolutions[i][0]) && (*height<=resolutions[i][1])) {
  416. *width = resolutions[i][0];
  417. *height = resolutions[i][1];
  418. return resolutions[i][2];
  419. }
  420. *width = 640;
  421. *height = 480;
  422. return GR_RESOLUTION_640x480;
  423. }
  424. qboolean VID_Is8bit(void)
  425. {
  426. return is8bit;
  427. }
  428. void VID_Init8bitPalette(void)
  429. {
  430. // Check for 8bit Extensions and initialize them.
  431. int i;
  432. void *prjobj;
  433. if (COM_CheckParm("-no8bit"))
  434. return;
  435. if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL) {
  436. Con_Printf("Unable to open symbol list for main program.\n");
  437. return;
  438. }
  439. if (strstr(gl_extensions, "3DFX_set_global_palette") &&
  440. (qgl3DfxSetPaletteEXT = dlsym(prjobj, "gl3DfxSetPaletteEXT")) != NULL) {
  441. GLubyte table[256][4];
  442. char *oldpal;
  443. Con_SafePrintf("... Using 3DFX_set_global_palette\n");
  444. glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
  445. oldpal = (char *) d_8to24table; //d_8to24table3dfx;
  446. for (i=0;i<256;i++) {
  447. table[i][2] = *oldpal++;
  448. table[i][1] = *oldpal++;
  449. table[i][0] = *oldpal++;
  450. table[i][3] = 255;
  451. oldpal++;
  452. }
  453. qgl3DfxSetPaletteEXT((GLuint *)table);
  454. is8bit = true;
  455. } else if (strstr(gl_extensions, "GL_EXT_shared_texture_palette") &&
  456. (qglColorTableEXT = dlsym(prjobj, "glColorTableEXT")) != NULL) {
  457. char thePalette[256*3];
  458. char *oldPalette, *newPalette;
  459. Con_SafePrintf("... Using GL_EXT_shared_texture_palette\n");
  460. glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
  461. oldPalette = (char *) d_8to24table; //d_8to24table3dfx;
  462. newPalette = thePalette;
  463. for (i=0;i<256;i++) {
  464. *newPalette++ = *oldPalette++;
  465. *newPalette++ = *oldPalette++;
  466. *newPalette++ = *oldPalette++;
  467. oldPalette++;
  468. }
  469. qglColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, (void *) thePalette);
  470. is8bit = true;
  471. }
  472. dlclose(prjobj);
  473. }
  474. static void Check_Gamma (unsigned char *pal)
  475. {
  476. float f, inf;
  477. unsigned char palette[768];
  478. int i;
  479. if ((i = COM_CheckParm("-gamma")) == 0) {
  480. if ((gl_renderer && strstr(gl_renderer, "Voodoo")) ||
  481. (gl_vendor && strstr(gl_vendor, "3Dfx")))
  482. vid_gamma = 1;
  483. else
  484. vid_gamma = 0.7; // default to 0.7 on non-3dfx hardware
  485. } else
  486. vid_gamma = Q_atof(com_argv[i+1]);
  487. for (i=0 ; i<768 ; i++)
  488. {
  489. f = pow ( (pal[i]+1)/256.0 , vid_gamma );
  490. inf = f*255 + 0.5;
  491. if (inf < 0)
  492. inf = 0;
  493. if (inf > 255)
  494. inf = 255;
  495. palette[i] = inf;
  496. }
  497. memcpy (pal, palette, sizeof(palette));
  498. }
  499. void VID_Init(unsigned char *palette)
  500. {
  501. int i;
  502. GLint attribs[32];
  503. char gldir[MAX_OSPATH];
  504. int width = 640, height = 480;
  505. Init_KBD();
  506. Cvar_RegisterVariable (&vid_mode);
  507. Cvar_RegisterVariable (&vid_redrawfull);
  508. Cvar_RegisterVariable (&vid_waitforrefresh);
  509. Cvar_RegisterVariable (&gl_ztrick);
  510. vid.maxwarpwidth = WARP_WIDTH;
  511. vid.maxwarpheight = WARP_HEIGHT;
  512. vid.colormap = host_colormap;
  513. vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
  514. // interpret command-line params
  515. // set vid parameters
  516. attribs[0] = FXMESA_DOUBLEBUFFER;
  517. attribs[1] = FXMESA_ALPHA_SIZE;
  518. attribs[2] = 1;
  519. attribs[3] = FXMESA_DEPTH_SIZE;
  520. attribs[4] = 1;
  521. attribs[5] = FXMESA_NONE;
  522. if ((i = COM_CheckParm("-width")) != 0)
  523. width = atoi(com_argv[i+1]);
  524. if ((i = COM_CheckParm("-height")) != 0)
  525. height = atoi(com_argv[i+1]);
  526. if ((i = COM_CheckParm("-conwidth")) != 0)
  527. vid.conwidth = Q_atoi(com_argv[i+1]);
  528. else
  529. vid.conwidth = 640;
  530. vid.conwidth &= 0xfff8; // make it a multiple of eight
  531. if (vid.conwidth < 320)
  532. vid.conwidth = 320;
  533. // pick a conheight that matches with correct aspect
  534. vid.conheight = vid.conwidth*3 / 4;
  535. if ((i = COM_CheckParm("-conheight")) != 0)
  536. vid.conheight = Q_atoi(com_argv[i+1]);
  537. if (vid.conheight < 200)
  538. vid.conheight = 200;
  539. fc = fxMesaCreateContext(0, findres(&width, &height), GR_REFRESH_75Hz,
  540. attribs);
  541. if (!fc)
  542. Sys_Error("Unable to create 3DFX context.\n");
  543. InitSig(); // trap evil signals
  544. scr_width = width;
  545. scr_height = height;
  546. fxMesaMakeCurrent(fc);
  547. if (vid.conheight > height)
  548. vid.conheight = height;
  549. if (vid.conwidth > width)
  550. vid.conwidth = width;
  551. vid.width = vid.conwidth;
  552. vid.height = vid.conheight;
  553. vid.aspect = ((float)vid.height / (float)vid.width) *
  554. (320.0 / 240.0);
  555. vid.numpages = 2;
  556. GL_Init();
  557. sprintf (gldir, "%s/glquake", com_gamedir);
  558. Sys_mkdir (gldir);
  559. Check_Gamma(palette);
  560. VID_SetPalette(palette);
  561. // Check for 3DFX Extensions and initialize them.
  562. VID_Init8bitPalette();
  563. Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
  564. vid.recalc_refdef = 1; // force a surface cache flush
  565. }
  566. void Sys_SendKeyEvents(void)
  567. {
  568. if (UseKeyboard)
  569. while (keyboard_update());
  570. }
  571. void Force_CenterView_f (void)
  572. {
  573. cl.viewangles[PITCH] = 0;
  574. }
  575. void mousehandler(int buttonstate, int dx, int dy)
  576. {
  577. mouse_buttonstate = buttonstate;
  578. mx += dx;
  579. my += dy;
  580. }
  581. void IN_Init(void)
  582. {
  583. int mtype;
  584. char *mousedev;
  585. int mouserate;
  586. if (UseMouse)
  587. {
  588. Cvar_RegisterVariable (&mouse_button_commands[0]);
  589. Cvar_RegisterVariable (&mouse_button_commands[1]);
  590. Cvar_RegisterVariable (&mouse_button_commands[2]);
  591. Cmd_AddCommand ("force_centerview", Force_CenterView_f);
  592. mouse_buttons = 3;
  593. mtype = vga_getmousetype();
  594. mousedev = "/dev/mouse";
  595. if (getenv("MOUSEDEV")) mousedev = getenv("MOUSEDEV");
  596. if (COM_CheckParm("-mdev"))
  597. mousedev = com_argv[COM_CheckParm("-mdev")+1];
  598. mouserate = 1200;
  599. if (getenv("MOUSERATE")) mouserate = atoi(getenv("MOUSERATE"));
  600. if (COM_CheckParm("-mrate"))
  601. mouserate = atoi(com_argv[COM_CheckParm("-mrate")+1]);
  602. if (mouse_init(mousedev, mtype, mouserate))
  603. {
  604. Con_Printf("No mouse found\n");
  605. UseMouse = 0;
  606. }
  607. else
  608. mouse_seteventhandler(mousehandler);
  609. }
  610. }
  611. void IN_Shutdown(void)
  612. {
  613. if (UseMouse)
  614. mouse_close();
  615. }
  616. /*
  617. ===========
  618. IN_Commands
  619. ===========
  620. */
  621. void IN_Commands (void)
  622. {
  623. if (UseMouse && cls.state != ca_dedicated)
  624. {
  625. // poll mouse values
  626. while (mouse_update())
  627. ;
  628. // perform button actions
  629. if ((mouse_buttonstate & MOUSE_LEFTBUTTON) &&
  630. !(mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
  631. Key_Event (K_MOUSE1, true);
  632. else if (!(mouse_buttonstate & MOUSE_LEFTBUTTON) &&
  633. (mouse_oldbuttonstate & MOUSE_LEFTBUTTON))
  634. Key_Event (K_MOUSE1, false);
  635. if ((mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
  636. !(mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
  637. Key_Event (K_MOUSE2, true);
  638. else if (!(mouse_buttonstate & MOUSE_RIGHTBUTTON) &&
  639. (mouse_oldbuttonstate & MOUSE_RIGHTBUTTON))
  640. Key_Event (K_MOUSE2, false);
  641. if ((mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
  642. !(mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
  643. Key_Event (K_MOUSE3, true);
  644. else if (!(mouse_buttonstate & MOUSE_MIDDLEBUTTON) &&
  645. (mouse_oldbuttonstate & MOUSE_MIDDLEBUTTON))
  646. Key_Event (K_MOUSE3, false);
  647. mouse_oldbuttonstate = mouse_buttonstate;
  648. }
  649. }
  650. /*
  651. ===========
  652. IN_Move
  653. ===========
  654. */
  655. void IN_MouseMove (usercmd_t *cmd)
  656. {
  657. if (!UseMouse)
  658. return;
  659. // poll mouse values
  660. while (mouse_update())
  661. ;
  662. if (m_filter.value)
  663. {
  664. mouse_x = (mx + old_mouse_x) * 0.5;
  665. mouse_y = (my + old_mouse_y) * 0.5;
  666. }
  667. else
  668. {
  669. mouse_x = mx;
  670. mouse_y = my;
  671. }
  672. old_mouse_x = mx;
  673. old_mouse_y = my;
  674. mx = my = 0; // clear for next update
  675. mouse_x *= sensitivity.value;
  676. mouse_y *= sensitivity.value;
  677. // add mouse X/Y movement to cmd
  678. if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
  679. cmd->sidemove += m_side.value * mouse_x;
  680. else
  681. cl.viewangles[YAW] -= m_yaw.value * mouse_x;
  682. if (in_mlook.state & 1)
  683. V_StopPitchDrift ();
  684. if ( (in_mlook.state & 1) && !(in_strafe.state & 1))
  685. {
  686. cl.viewangles[PITCH] += m_pitch.value * mouse_y;
  687. if (cl.viewangles[PITCH] > 80)
  688. cl.viewangles[PITCH] = 80;
  689. if (cl.viewangles[PITCH] < -70)
  690. cl.viewangles[PITCH] = -70;
  691. }
  692. else
  693. {
  694. if ((in_strafe.state & 1) && noclip_anglehack)
  695. cmd->upmove -= m_forward.value * mouse_y;
  696. else
  697. cmd->forwardmove -= m_forward.value * mouse_y;
  698. }
  699. }
  700. void IN_Move (usercmd_t *cmd)
  701. {
  702. IN_MouseMove(cmd);
  703. }