gl_vidnt.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  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. // gl_vidnt.c -- NT GL vid component
  16. #include "quakedef.h"
  17. #include "winquake.h"
  18. #include "resource.h"
  19. #include <commctrl.h>
  20. #define MAX_MODE_LIST 30
  21. #define VID_ROW_SIZE 3
  22. #define WARP_WIDTH 320
  23. #define WARP_HEIGHT 200
  24. #define MAXWIDTH 10000
  25. #define MAXHEIGHT 10000
  26. #define BASEWIDTH 320
  27. #define BASEHEIGHT 200
  28. #define MODE_WINDOWED 0
  29. #define NO_MODE (MODE_WINDOWED - 1)
  30. #define MODE_FULLSCREEN_DEFAULT (MODE_WINDOWED + 1)
  31. typedef struct {
  32. modestate_t type;
  33. int width;
  34. int height;
  35. int modenum;
  36. int dib;
  37. int fullscreen;
  38. int bpp;
  39. int halfscreen;
  40. char modedesc[17];
  41. } vmode_t;
  42. typedef struct {
  43. int width;
  44. int height;
  45. } lmode_t;
  46. lmode_t lowresmodes[] = {
  47. {320, 200},
  48. {320, 240},
  49. {400, 300},
  50. {512, 384},
  51. };
  52. const char *gl_vendor;
  53. const char *gl_renderer;
  54. const char *gl_version;
  55. const char *gl_extensions;
  56. qboolean DDActive;
  57. qboolean scr_skipupdate;
  58. static vmode_t modelist[MAX_MODE_LIST];
  59. static int nummodes;
  60. static vmode_t *pcurrentmode;
  61. static vmode_t badmode;
  62. static DEVMODE gdevmode;
  63. static qboolean vid_initialized = false;
  64. static qboolean windowed, leavecurrentmode;
  65. static qboolean vid_canalttab = false;
  66. static qboolean vid_wassuspended = false;
  67. static int windowed_mouse;
  68. extern qboolean mouseactive; // from in_win.c
  69. static HICON hIcon;
  70. int DIBWidth, DIBHeight;
  71. RECT WindowRect;
  72. DWORD WindowStyle, ExWindowStyle;
  73. HWND mainwindow, dibwindow;
  74. int vid_modenum = NO_MODE;
  75. int vid_realmode;
  76. int vid_default = MODE_WINDOWED;
  77. static int windowed_default;
  78. unsigned char vid_curpal[256*3];
  79. static qboolean fullsbardraw = false;
  80. static float vid_gamma = 1.0;
  81. HGLRC baseRC;
  82. HDC maindc;
  83. glvert_t glv;
  84. cvar_t gl_ztrick = {"gl_ztrick","1"};
  85. HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
  86. viddef_t vid; // global video state
  87. unsigned short d_8to16table[256];
  88. unsigned d_8to24table[256];
  89. unsigned char d_15to8table[65536];
  90. float gldepthmin, gldepthmax;
  91. modestate_t modestate = MS_UNINIT;
  92. void VID_MenuDraw (void);
  93. void VID_MenuKey (int key);
  94. LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  95. void AppActivate(BOOL fActive, BOOL minimize);
  96. char *VID_GetModeDescription (int mode);
  97. void ClearAllStates (void);
  98. void VID_UpdateWindowStatus (void);
  99. void GL_Init (void);
  100. PROC glArrayElementEXT;
  101. PROC glColorPointerEXT;
  102. PROC glTexCoordPointerEXT;
  103. PROC glVertexPointerEXT;
  104. typedef void (APIENTRY *lp3DFXFUNC) (int, int, int, int, int, const void*);
  105. lp3DFXFUNC glColorTableEXT;
  106. qboolean is8bit = false;
  107. qboolean isPermedia = false;
  108. qboolean gl_mtexable = false;
  109. //====================================
  110. cvar_t vid_mode = {"vid_mode","0", false};
  111. // Note that 0 is MODE_WINDOWED
  112. cvar_t _vid_default_mode = {"_vid_default_mode","0", true};
  113. // Note that 3 is MODE_FULLSCREEN_DEFAULT
  114. cvar_t _vid_default_mode_win = {"_vid_default_mode_win","3", true};
  115. cvar_t vid_wait = {"vid_wait","0"};
  116. cvar_t vid_nopageflip = {"vid_nopageflip","0", true};
  117. cvar_t _vid_wait_override = {"_vid_wait_override", "0", true};
  118. cvar_t vid_config_x = {"vid_config_x","800", true};
  119. cvar_t vid_config_y = {"vid_config_y","600", true};
  120. cvar_t vid_stretch_by_2 = {"vid_stretch_by_2","1", true};
  121. cvar_t _windowed_mouse = {"_windowed_mouse","1", true};
  122. int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
  123. RECT window_rect;
  124. // direct draw software compatability stuff
  125. void VID_HandlePause (qboolean pause)
  126. {
  127. }
  128. void VID_ForceLockState (int lk)
  129. {
  130. }
  131. void VID_LockBuffer (void)
  132. {
  133. }
  134. void VID_UnlockBuffer (void)
  135. {
  136. }
  137. int VID_ForceUnlockedAndReturnState (void)
  138. {
  139. return 0;
  140. }
  141. void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
  142. {
  143. }
  144. void D_EndDirectRect (int x, int y, int width, int height)
  145. {
  146. }
  147. void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
  148. {
  149. RECT rect;
  150. int CenterX, CenterY;
  151. CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
  152. CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
  153. if (CenterX > CenterY*2)
  154. CenterX >>= 1; // dual screens
  155. CenterX = (CenterX < 0) ? 0: CenterX;
  156. CenterY = (CenterY < 0) ? 0: CenterY;
  157. SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
  158. SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
  159. }
  160. qboolean VID_SetWindowedMode (int modenum)
  161. {
  162. HDC hdc;
  163. int lastmodestate, width, height;
  164. RECT rect;
  165. lastmodestate = modestate;
  166. WindowRect.top = WindowRect.left = 0;
  167. WindowRect.right = modelist[modenum].width;
  168. WindowRect.bottom = modelist[modenum].height;
  169. DIBWidth = modelist[modenum].width;
  170. DIBHeight = modelist[modenum].height;
  171. WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU |
  172. WS_MINIMIZEBOX;
  173. ExWindowStyle = 0;
  174. rect = WindowRect;
  175. AdjustWindowRectEx(&rect, WindowStyle, FALSE, 0);
  176. width = rect.right - rect.left;
  177. height = rect.bottom - rect.top;
  178. // Create the DIB window
  179. dibwindow = CreateWindowEx (
  180. ExWindowStyle,
  181. "WinQuake",
  182. "GLQuake",
  183. WindowStyle,
  184. rect.left, rect.top,
  185. width,
  186. height,
  187. NULL,
  188. NULL,
  189. global_hInstance,
  190. NULL);
  191. if (!dibwindow)
  192. Sys_Error ("Couldn't create DIB window");
  193. // Center and show the DIB window
  194. CenterWindow(dibwindow, WindowRect.right - WindowRect.left,
  195. WindowRect.bottom - WindowRect.top, false);
  196. ShowWindow (dibwindow, SW_SHOWDEFAULT);
  197. UpdateWindow (dibwindow);
  198. modestate = MS_WINDOWED;
  199. // because we have set the background brush for the window to NULL
  200. // (to avoid flickering when re-sizing the window on the desktop),
  201. // we clear the window to black when created, otherwise it will be
  202. // empty while Quake starts up.
  203. hdc = GetDC(dibwindow);
  204. PatBlt(hdc,0,0,WindowRect.right,WindowRect.bottom,BLACKNESS);
  205. ReleaseDC(dibwindow, hdc);
  206. if (vid.conheight > modelist[modenum].height)
  207. vid.conheight = modelist[modenum].height;
  208. if (vid.conwidth > modelist[modenum].width)
  209. vid.conwidth = modelist[modenum].width;
  210. vid.width = vid.conwidth;
  211. vid.height = vid.conheight;
  212. vid.numpages = 2;
  213. mainwindow = dibwindow;
  214. SendMessage (mainwindow, WM_SETICON, (WPARAM)TRUE, (LPARAM)hIcon);
  215. SendMessage (mainwindow, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon);
  216. return true;
  217. }
  218. qboolean VID_SetFullDIBMode (int modenum)
  219. {
  220. HDC hdc;
  221. int lastmodestate, width, height;
  222. RECT rect;
  223. if (!leavecurrentmode)
  224. {
  225. gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  226. gdevmode.dmBitsPerPel = modelist[modenum].bpp;
  227. gdevmode.dmPelsWidth = modelist[modenum].width <<
  228. modelist[modenum].halfscreen;
  229. gdevmode.dmPelsHeight = modelist[modenum].height;
  230. gdevmode.dmSize = sizeof (gdevmode);
  231. if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  232. Sys_Error ("Couldn't set fullscreen DIB mode");
  233. }
  234. lastmodestate = modestate;
  235. modestate = MS_FULLDIB;
  236. WindowRect.top = WindowRect.left = 0;
  237. WindowRect.right = modelist[modenum].width;
  238. WindowRect.bottom = modelist[modenum].height;
  239. DIBWidth = modelist[modenum].width;
  240. DIBHeight = modelist[modenum].height;
  241. WindowStyle = WS_POPUP;
  242. ExWindowStyle = 0;
  243. rect = WindowRect;
  244. AdjustWindowRectEx(&rect, WindowStyle, FALSE, 0);
  245. width = rect.right - rect.left;
  246. height = rect.bottom - rect.top;
  247. // Create the DIB window
  248. dibwindow = CreateWindowEx (
  249. ExWindowStyle,
  250. "WinQuake",
  251. "GLQuake",
  252. WindowStyle,
  253. rect.left, rect.top,
  254. width,
  255. height,
  256. NULL,
  257. NULL,
  258. global_hInstance,
  259. NULL);
  260. if (!dibwindow)
  261. Sys_Error ("Couldn't create DIB window");
  262. ShowWindow (dibwindow, SW_SHOWDEFAULT);
  263. UpdateWindow (dibwindow);
  264. // Because we have set the background brush for the window to NULL
  265. // (to avoid flickering when re-sizing the window on the desktop), we
  266. // clear the window to black when created, otherwise it will be
  267. // empty while Quake starts up.
  268. hdc = GetDC(dibwindow);
  269. PatBlt(hdc,0,0,WindowRect.right,WindowRect.bottom,BLACKNESS);
  270. ReleaseDC(dibwindow, hdc);
  271. if (vid.conheight > modelist[modenum].height)
  272. vid.conheight = modelist[modenum].height;
  273. if (vid.conwidth > modelist[modenum].width)
  274. vid.conwidth = modelist[modenum].width;
  275. vid.width = vid.conwidth;
  276. vid.height = vid.conheight;
  277. vid.numpages = 2;
  278. // needed because we're not getting WM_MOVE messages fullscreen on NT
  279. window_x = 0;
  280. window_y = 0;
  281. mainwindow = dibwindow;
  282. SendMessage (mainwindow, WM_SETICON, (WPARAM)TRUE, (LPARAM)hIcon);
  283. SendMessage (mainwindow, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon);
  284. return true;
  285. }
  286. int VID_SetMode (int modenum, unsigned char *palette)
  287. {
  288. int original_mode, temp;
  289. qboolean stat;
  290. MSG msg;
  291. HDC hdc;
  292. if ((windowed && (modenum != 0)) ||
  293. (!windowed && (modenum < 1)) ||
  294. (!windowed && (modenum >= nummodes)))
  295. {
  296. Sys_Error ("Bad video mode\n");
  297. }
  298. // so Con_Printfs don't mess us up by forcing vid and snd updates
  299. temp = scr_disabled_for_loading;
  300. scr_disabled_for_loading = true;
  301. CDAudio_Pause ();
  302. if (vid_modenum == NO_MODE)
  303. original_mode = windowed_default;
  304. else
  305. original_mode = vid_modenum;
  306. // Set either the fullscreen or windowed mode
  307. if (modelist[modenum].type == MS_WINDOWED)
  308. {
  309. if (_windowed_mouse.value && key_dest == key_game)
  310. {
  311. stat = VID_SetWindowedMode(modenum);
  312. IN_ActivateMouse ();
  313. IN_HideMouse ();
  314. }
  315. else
  316. {
  317. IN_DeactivateMouse ();
  318. IN_ShowMouse ();
  319. stat = VID_SetWindowedMode(modenum);
  320. }
  321. }
  322. else if (modelist[modenum].type == MS_FULLDIB)
  323. {
  324. stat = VID_SetFullDIBMode(modenum);
  325. IN_ActivateMouse ();
  326. IN_HideMouse ();
  327. }
  328. else
  329. {
  330. Sys_Error ("VID_SetMode: Bad mode type in modelist");
  331. }
  332. window_width = DIBWidth;
  333. window_height = DIBHeight;
  334. VID_UpdateWindowStatus ();
  335. CDAudio_Resume ();
  336. scr_disabled_for_loading = temp;
  337. if (!stat)
  338. {
  339. Sys_Error ("Couldn't set video mode");
  340. }
  341. // now we try to make sure we get the focus on the mode switch, because
  342. // sometimes in some systems we don't. We grab the foreground, then
  343. // finish setting up, pump all our messages, and sleep for a little while
  344. // to let messages finish bouncing around the system, then we put
  345. // ourselves at the top of the z order, then grab the foreground again,
  346. // Who knows if it helps, but it probably doesn't hurt
  347. SetForegroundWindow (mainwindow);
  348. VID_SetPalette (palette);
  349. vid_modenum = modenum;
  350. Cvar_SetValue ("vid_mode", (float)vid_modenum);
  351. while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  352. {
  353. TranslateMessage (&msg);
  354. DispatchMessage (&msg);
  355. }
  356. Sleep (100);
  357. SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0,
  358. SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW |
  359. SWP_NOCOPYBITS);
  360. SetForegroundWindow (mainwindow);
  361. // fix the leftover Alt from any Alt-Tab or the like that switched us away
  362. ClearAllStates ();
  363. if (!msg_suppress_1)
  364. Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
  365. VID_SetPalette (palette);
  366. vid.recalc_refdef = 1;
  367. return true;
  368. }
  369. /*
  370. ================
  371. VID_UpdateWindowStatus
  372. ================
  373. */
  374. void VID_UpdateWindowStatus (void)
  375. {
  376. window_rect.left = window_x;
  377. window_rect.top = window_y;
  378. window_rect.right = window_x + window_width;
  379. window_rect.bottom = window_y + window_height;
  380. window_center_x = (window_rect.left + window_rect.right) / 2;
  381. window_center_y = (window_rect.top + window_rect.bottom) / 2;
  382. IN_UpdateClipCursor ();
  383. }
  384. //====================================
  385. BINDTEXFUNCPTR bindTexFunc;
  386. #define TEXTURE_EXT_STRING "GL_EXT_texture_object"
  387. void CheckTextureExtensions (void)
  388. {
  389. char *tmp;
  390. qboolean texture_ext;
  391. HINSTANCE hInstGL;
  392. texture_ext = FALSE;
  393. /* check for texture extension */
  394. tmp = (unsigned char *)glGetString(GL_EXTENSIONS);
  395. while (*tmp)
  396. {
  397. if (strncmp((const char*)tmp, TEXTURE_EXT_STRING, strlen(TEXTURE_EXT_STRING)) == 0)
  398. texture_ext = TRUE;
  399. tmp++;
  400. }
  401. if (!texture_ext || COM_CheckParm ("-gl11") )
  402. {
  403. hInstGL = LoadLibrary("opengl32.dll");
  404. if (hInstGL == NULL)
  405. Sys_Error ("Couldn't load opengl32.dll\n");
  406. bindTexFunc = (void *)GetProcAddress(hInstGL,"glBindTexture");
  407. if (!bindTexFunc)
  408. Sys_Error ("No texture objects!");
  409. return;
  410. }
  411. /* load library and get procedure adresses for texture extension API */
  412. if ((bindTexFunc = (BINDTEXFUNCPTR)
  413. wglGetProcAddress((LPCSTR) "glBindTextureEXT")) == NULL)
  414. {
  415. Sys_Error ("GetProcAddress for BindTextureEXT failed");
  416. return;
  417. }
  418. }
  419. void CheckArrayExtensions (void)
  420. {
  421. char *tmp;
  422. /* check for texture extension */
  423. tmp = (unsigned char *)glGetString(GL_EXTENSIONS);
  424. while (*tmp)
  425. {
  426. if (strncmp((const char*)tmp, "GL_EXT_vertex_array", strlen("GL_EXT_vertex_array")) == 0)
  427. {
  428. if (
  429. ((glArrayElementEXT = wglGetProcAddress("glArrayElementEXT")) == NULL) ||
  430. ((glColorPointerEXT = wglGetProcAddress("glColorPointerEXT")) == NULL) ||
  431. ((glTexCoordPointerEXT = wglGetProcAddress("glTexCoordPointerEXT")) == NULL) ||
  432. ((glVertexPointerEXT = wglGetProcAddress("glVertexPointerEXT")) == NULL) )
  433. {
  434. Sys_Error ("GetProcAddress for vertex extension failed");
  435. return;
  436. }
  437. return;
  438. }
  439. tmp++;
  440. }
  441. Sys_Error ("Vertex array extension not present");
  442. }
  443. //int texture_mode = GL_NEAREST;
  444. //int texture_mode = GL_NEAREST_MIPMAP_NEAREST;
  445. //int texture_mode = GL_NEAREST_MIPMAP_LINEAR;
  446. int texture_mode = GL_LINEAR;
  447. //int texture_mode = GL_LINEAR_MIPMAP_NEAREST;
  448. //int texture_mode = GL_LINEAR_MIPMAP_LINEAR;
  449. int texture_extension_number = 1;
  450. #ifdef _WIN32
  451. void CheckMultiTextureExtensions(void)
  452. {
  453. if (strstr(gl_extensions, "GL_SGIS_multitexture ") && !COM_CheckParm("-nomtex")) {
  454. Con_Printf("Multitexture extensions found.\n");
  455. qglMTexCoord2fSGIS = (void *) wglGetProcAddress("glMTexCoord2fSGIS");
  456. qglSelectTextureSGIS = (void *) wglGetProcAddress("glSelectTextureSGIS");
  457. gl_mtexable = true;
  458. }
  459. }
  460. #else
  461. void CheckMultiTextureExtensions(void)
  462. {
  463. gl_mtexable = true;
  464. }
  465. #endif
  466. /*
  467. ===============
  468. GL_Init
  469. ===============
  470. */
  471. void GL_Init (void)
  472. {
  473. gl_vendor = glGetString (GL_VENDOR);
  474. Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
  475. gl_renderer = glGetString (GL_RENDERER);
  476. Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
  477. gl_version = glGetString (GL_VERSION);
  478. Con_Printf ("GL_VERSION: %s\n", gl_version);
  479. gl_extensions = glGetString (GL_EXTENSIONS);
  480. Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
  481. // Con_Printf ("%s %s\n", gl_renderer, gl_version);
  482. if (strnicmp(gl_renderer,"PowerVR",7)==0)
  483. fullsbardraw = true;
  484. if (strnicmp(gl_renderer,"Permedia",8)==0)
  485. isPermedia = true;
  486. CheckTextureExtensions ();
  487. CheckMultiTextureExtensions ();
  488. glClearColor (1,0,0,0);
  489. glCullFace(GL_FRONT);
  490. glEnable(GL_TEXTURE_2D);
  491. glEnable(GL_ALPHA_TEST);
  492. glAlphaFunc(GL_GREATER, 0.666);
  493. glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  494. glShadeModel (GL_FLAT);
  495. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  496. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  497. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  498. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  499. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  500. // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  501. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  502. #if 0
  503. CheckArrayExtensions ();
  504. glEnable (GL_VERTEX_ARRAY_EXT);
  505. glEnable (GL_TEXTURE_COORD_ARRAY_EXT);
  506. glVertexPointerEXT (3, GL_FLOAT, 0, 0, &glv.x);
  507. glTexCoordPointerEXT (2, GL_FLOAT, 0, 0, &glv.s);
  508. glColorPointerEXT (3, GL_FLOAT, 0, 0, &glv.r);
  509. #endif
  510. }
  511. /*
  512. =================
  513. GL_BeginRendering
  514. =================
  515. */
  516. void GL_BeginRendering (int *x, int *y, int *width, int *height)
  517. {
  518. extern cvar_t gl_clear;
  519. *x = *y = 0;
  520. *width = WindowRect.right - WindowRect.left;
  521. *height = WindowRect.bottom - WindowRect.top;
  522. // if (!wglMakeCurrent( maindc, baseRC ))
  523. // Sys_Error ("wglMakeCurrent failed");
  524. // glViewport (*x, *y, *width, *height);
  525. }
  526. void GL_EndRendering (void)
  527. {
  528. if (!scr_skipupdate || block_drawing)
  529. SwapBuffers(maindc);
  530. // handle the mouse state when windowed if that's changed
  531. if (modestate == MS_WINDOWED)
  532. {
  533. if (!_windowed_mouse.value) {
  534. if (windowed_mouse) {
  535. IN_DeactivateMouse ();
  536. IN_ShowMouse ();
  537. windowed_mouse = false;
  538. }
  539. } else {
  540. windowed_mouse = true;
  541. if (key_dest == key_game && !mouseactive && ActiveApp) {
  542. IN_ActivateMouse ();
  543. IN_HideMouse ();
  544. } else if (mouseactive && key_dest != key_game) {
  545. IN_DeactivateMouse ();
  546. IN_ShowMouse ();
  547. }
  548. }
  549. }
  550. if (fullsbardraw)
  551. Sbar_Changed();
  552. }
  553. void VID_SetPalette (unsigned char *palette)
  554. {
  555. byte *pal;
  556. unsigned r,g,b;
  557. unsigned v;
  558. int r1,g1,b1;
  559. int j,k,l,m;
  560. unsigned short i;
  561. unsigned *table;
  562. FILE *f;
  563. char s[255];
  564. HWND hDlg, hProgress;
  565. float gamma;
  566. //
  567. // 8 8 8 encoding
  568. //
  569. pal = palette;
  570. table = d_8to24table;
  571. for (i=0 ; i<256 ; i++)
  572. {
  573. r = pal[0];
  574. g = pal[1];
  575. b = pal[2];
  576. pal += 3;
  577. // v = (255<<24) + (r<<16) + (g<<8) + (b<<0);
  578. // v = (255<<0) + (r<<8) + (g<<16) + (b<<24);
  579. v = (255<<24) + (r<<0) + (g<<8) + (b<<16);
  580. *table++ = v;
  581. }
  582. d_8to24table[255] &= 0xffffff; // 255 is transparent
  583. // JACK: 3D distance calcs - k is last closest, l is the distance.
  584. // FIXME: Precalculate this and cache to disk.
  585. for (i=0; i < (1<<15); i++) {
  586. /* Maps
  587. 000000000000000
  588. 000000000011111 = Red = 0x1F
  589. 000001111100000 = Blue = 0x03E0
  590. 111110000000000 = Grn = 0x7C00
  591. */
  592. r = ((i & 0x1F) << 3)+4;
  593. g = ((i & 0x03E0) >> 2)+4;
  594. b = ((i & 0x7C00) >> 7)+4;
  595. pal = (unsigned char *)d_8to24table;
  596. for (v=0,k=0,l=10000*10000; v<256; v++,pal+=4) {
  597. r1 = r-pal[0];
  598. g1 = g-pal[1];
  599. b1 = b-pal[2];
  600. j = (r1*r1)+(g1*g1)+(b1*b1);
  601. if (j<l) {
  602. k=v;
  603. l=j;
  604. }
  605. }
  606. d_15to8table[i]=k;
  607. }
  608. }
  609. BOOL gammaworks;
  610. void VID_ShiftPalette (unsigned char *palette)
  611. {
  612. extern byte ramps[3][256];
  613. // VID_SetPalette (palette);
  614. // gammaworks = SetDeviceGammaRamp (maindc, ramps);
  615. }
  616. void VID_SetDefaultMode (void)
  617. {
  618. IN_DeactivateMouse ();
  619. }
  620. void VID_Shutdown (void)
  621. {
  622. HGLRC hRC;
  623. HDC hDC;
  624. if (vid_initialized)
  625. {
  626. vid_canalttab = false;
  627. hRC = wglGetCurrentContext();
  628. hDC = wglGetCurrentDC();
  629. wglMakeCurrent(NULL, NULL);
  630. if (hRC)
  631. wglDeleteContext(hRC);
  632. if (hDC && dibwindow)
  633. ReleaseDC(dibwindow, hDC);
  634. if (modestate == MS_FULLDIB)
  635. ChangeDisplaySettings (NULL, 0);
  636. if (maindc && dibwindow)
  637. ReleaseDC (dibwindow, maindc);
  638. AppActivate(false, false);
  639. }
  640. }
  641. //==========================================================================
  642. BOOL bSetupPixelFormat(HDC hDC)
  643. {
  644. static PIXELFORMATDESCRIPTOR pfd = {
  645. sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
  646. 1, // version number
  647. PFD_DRAW_TO_WINDOW // support window
  648. | PFD_SUPPORT_OPENGL // support OpenGL
  649. | PFD_DOUBLEBUFFER , // double buffered
  650. PFD_TYPE_RGBA, // RGBA type
  651. 24, // 24-bit color depth
  652. 0, 0, 0, 0, 0, 0, // color bits ignored
  653. 0, // no alpha buffer
  654. 0, // shift bit ignored
  655. 0, // no accumulation buffer
  656. 0, 0, 0, 0, // accum bits ignored
  657. 32, // 32-bit z-buffer
  658. 0, // no stencil buffer
  659. 0, // no auxiliary buffer
  660. PFD_MAIN_PLANE, // main layer
  661. 0, // reserved
  662. 0, 0, 0 // layer masks ignored
  663. };
  664. int pixelformat;
  665. if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
  666. {
  667. MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
  668. return FALSE;
  669. }
  670. if (SetPixelFormat(hDC, pixelformat, &pfd) == FALSE)
  671. {
  672. MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
  673. return FALSE;
  674. }
  675. return TRUE;
  676. }
  677. byte scantokey[128] =
  678. {
  679. // 0 1 2 3 4 5 6 7
  680. // 8 9 A B C D E F
  681. 0 , 27, '1', '2', '3', '4', '5', '6',
  682. '7', '8', '9', '0', '-', '=', K_BACKSPACE, 9, // 0
  683. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  684. 'o', 'p', '[', ']', 13 , K_CTRL,'a', 's', // 1
  685. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  686. '\'' , '`', K_SHIFT,'\\', 'z', 'x', 'c', 'v', // 2
  687. 'b', 'n', 'm', ',', '.', '/', K_SHIFT,'*',
  688. K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
  689. K_F6, K_F7, K_F8, K_F9, K_F10, K_PAUSE , 0 , K_HOME,
  690. K_UPARROW,K_PGUP,'-',K_LEFTARROW,'5',K_RIGHTARROW,'+',K_END, //4
  691. K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
  692. K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
  693. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  694. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
  695. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  696. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
  697. };
  698. byte shiftscantokey[128] =
  699. {
  700. // 0 1 2 3 4 5 6 7
  701. // 8 9 A B C D E F
  702. 0 , 27, '!', '@', '#', '$', '%', '^',
  703. '&', '*', '(', ')', '_', '+', K_BACKSPACE, 9, // 0
  704. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  705. 'O', 'P', '{', '}', 13 , K_CTRL,'A', 'S', // 1
  706. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  707. '"' , '~', K_SHIFT,'|', 'Z', 'X', 'C', 'V', // 2
  708. 'B', 'N', 'M', '<', '>', '?', K_SHIFT,'*',
  709. K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
  710. K_F6, K_F7, K_F8, K_F9, K_F10, K_PAUSE , 0 , K_HOME,
  711. K_UPARROW,K_PGUP,'_',K_LEFTARROW,'%',K_RIGHTARROW,'+',K_END, //4
  712. K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
  713. K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
  714. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  715. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
  716. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  717. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
  718. };
  719. /*
  720. =======
  721. MapKey
  722. Map from windows to quake keynums
  723. =======
  724. */
  725. int MapKey (int key)
  726. {
  727. key = (key>>16)&255;
  728. if (key > 127)
  729. return 0;
  730. if (scantokey[key] == 0)
  731. Con_DPrintf("key 0x%02x has no translation\n", key);
  732. return scantokey[key];
  733. }
  734. /*
  735. ===================================================================
  736. MAIN WINDOW
  737. ===================================================================
  738. */
  739. /*
  740. ================
  741. ClearAllStates
  742. ================
  743. */
  744. void ClearAllStates (void)
  745. {
  746. int i;
  747. // send an up event for each key, to make sure the server clears them all
  748. for (i=0 ; i<256 ; i++)
  749. {
  750. Key_Event (i, false);
  751. }
  752. Key_ClearStates ();
  753. IN_ClearStates ();
  754. }
  755. void AppActivate(BOOL fActive, BOOL minimize)
  756. /****************************************************************************
  757. *
  758. * Function: AppActivate
  759. * Parameters: fActive - True if app is activating
  760. *
  761. * Description: If the application is activating, then swap the system
  762. * into SYSPAL_NOSTATIC mode so that our palettes will display
  763. * correctly.
  764. *
  765. ****************************************************************************/
  766. {
  767. MSG msg;
  768. HDC hdc;
  769. int i, t;
  770. static BOOL sound_active;
  771. ActiveApp = fActive;
  772. Minimized = minimize;
  773. // enable/disable sound on focus gain/loss
  774. if (!ActiveApp && sound_active)
  775. {
  776. S_BlockSound ();
  777. sound_active = false;
  778. }
  779. else if (ActiveApp && !sound_active)
  780. {
  781. S_UnblockSound ();
  782. sound_active = true;
  783. }
  784. if (fActive)
  785. {
  786. if (modestate == MS_FULLDIB)
  787. {
  788. IN_ActivateMouse ();
  789. IN_HideMouse ();
  790. if (vid_canalttab && vid_wassuspended) {
  791. vid_wassuspended = false;
  792. ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
  793. ShowWindow(mainwindow, SW_SHOWNORMAL);
  794. }
  795. }
  796. else if ((modestate == MS_WINDOWED) && _windowed_mouse.value && key_dest == key_game)
  797. {
  798. IN_ActivateMouse ();
  799. IN_HideMouse ();
  800. }
  801. }
  802. if (!fActive)
  803. {
  804. if (modestate == MS_FULLDIB)
  805. {
  806. IN_DeactivateMouse ();
  807. IN_ShowMouse ();
  808. if (vid_canalttab) {
  809. ChangeDisplaySettings (NULL, 0);
  810. vid_wassuspended = true;
  811. }
  812. }
  813. else if ((modestate == MS_WINDOWED) && _windowed_mouse.value)
  814. {
  815. IN_DeactivateMouse ();
  816. IN_ShowMouse ();
  817. }
  818. }
  819. }
  820. /* main window procedure */
  821. LONG WINAPI MainWndProc (
  822. HWND hWnd,
  823. UINT uMsg,
  824. WPARAM wParam,
  825. LPARAM lParam)
  826. {
  827. LONG lRet = 1;
  828. int fwKeys, xPos, yPos, fActive, fMinimized, temp;
  829. extern unsigned int uiWheelMessage;
  830. if ( uMsg == uiWheelMessage )
  831. uMsg = WM_MOUSEWHEEL;
  832. switch (uMsg)
  833. {
  834. case WM_KILLFOCUS:
  835. if (modestate == MS_FULLDIB)
  836. ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
  837. break;
  838. case WM_CREATE:
  839. break;
  840. case WM_MOVE:
  841. window_x = (int) LOWORD(lParam);
  842. window_y = (int) HIWORD(lParam);
  843. VID_UpdateWindowStatus ();
  844. break;
  845. case WM_KEYDOWN:
  846. case WM_SYSKEYDOWN:
  847. Key_Event (MapKey(lParam), true);
  848. break;
  849. case WM_KEYUP:
  850. case WM_SYSKEYUP:
  851. Key_Event (MapKey(lParam), false);
  852. break;
  853. case WM_SYSCHAR:
  854. // keep Alt-Space from happening
  855. break;
  856. // this is complicated because Win32 seems to pack multiple mouse events into
  857. // one update sometimes, so we always check all states and look for events
  858. case WM_LBUTTONDOWN:
  859. case WM_LBUTTONUP:
  860. case WM_RBUTTONDOWN:
  861. case WM_RBUTTONUP:
  862. case WM_MBUTTONDOWN:
  863. case WM_MBUTTONUP:
  864. case WM_MOUSEMOVE:
  865. temp = 0;
  866. if (wParam & MK_LBUTTON)
  867. temp |= 1;
  868. if (wParam & MK_RBUTTON)
  869. temp |= 2;
  870. if (wParam & MK_MBUTTON)
  871. temp |= 4;
  872. IN_MouseEvent (temp);
  873. break;
  874. // JACK: This is the mouse wheel with the Intellimouse
  875. // Its delta is either positive or neg, and we generate the proper
  876. // Event.
  877. case WM_MOUSEWHEEL:
  878. if ((short) HIWORD(wParam) > 0) {
  879. Key_Event(K_MWHEELUP, true);
  880. Key_Event(K_MWHEELUP, false);
  881. } else {
  882. Key_Event(K_MWHEELDOWN, true);
  883. Key_Event(K_MWHEELDOWN, false);
  884. }
  885. break;
  886. case WM_SIZE:
  887. break;
  888. case WM_CLOSE:
  889. if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit",
  890. MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
  891. {
  892. Sys_Quit ();
  893. }
  894. break;
  895. case WM_ACTIVATE:
  896. fActive = LOWORD(wParam);
  897. fMinimized = (BOOL) HIWORD(wParam);
  898. AppActivate(!(fActive == WA_INACTIVE), fMinimized);
  899. // fix the leftover Alt from any Alt-Tab or the like that switched us away
  900. ClearAllStates ();
  901. break;
  902. case WM_DESTROY:
  903. {
  904. if (dibwindow)
  905. DestroyWindow (dibwindow);
  906. PostQuitMessage (0);
  907. }
  908. break;
  909. case MM_MCINOTIFY:
  910. lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
  911. break;
  912. default:
  913. /* pass all unhandled messages to DefWindowProc */
  914. lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  915. break;
  916. }
  917. /* return 1 if handled message, 0 if not */
  918. return lRet;
  919. }
  920. /*
  921. =================
  922. VID_NumModes
  923. =================
  924. */
  925. int VID_NumModes (void)
  926. {
  927. return nummodes;
  928. }
  929. /*
  930. =================
  931. VID_GetModePtr
  932. =================
  933. */
  934. vmode_t *VID_GetModePtr (int modenum)
  935. {
  936. if ((modenum >= 0) && (modenum < nummodes))
  937. return &modelist[modenum];
  938. else
  939. return &badmode;
  940. }
  941. /*
  942. =================
  943. VID_GetModeDescription
  944. =================
  945. */
  946. char *VID_GetModeDescription (int mode)
  947. {
  948. char *pinfo;
  949. vmode_t *pv;
  950. static char temp[100];
  951. if ((mode < 0) || (mode >= nummodes))
  952. return NULL;
  953. if (!leavecurrentmode)
  954. {
  955. pv = VID_GetModePtr (mode);
  956. pinfo = pv->modedesc;
  957. }
  958. else
  959. {
  960. sprintf (temp, "Desktop resolution (%dx%d)",
  961. modelist[MODE_FULLSCREEN_DEFAULT].width,
  962. modelist[MODE_FULLSCREEN_DEFAULT].height);
  963. pinfo = temp;
  964. }
  965. return pinfo;
  966. }
  967. // KJB: Added this to return the mode driver name in description for console
  968. char *VID_GetExtModeDescription (int mode)
  969. {
  970. static char pinfo[40];
  971. vmode_t *pv;
  972. if ((mode < 0) || (mode >= nummodes))
  973. return NULL;
  974. pv = VID_GetModePtr (mode);
  975. if (modelist[mode].type == MS_FULLDIB)
  976. {
  977. if (!leavecurrentmode)
  978. {
  979. sprintf(pinfo,"%s fullscreen", pv->modedesc);
  980. }
  981. else
  982. {
  983. sprintf (pinfo, "Desktop resolution (%dx%d)",
  984. modelist[MODE_FULLSCREEN_DEFAULT].width,
  985. modelist[MODE_FULLSCREEN_DEFAULT].height);
  986. }
  987. }
  988. else
  989. {
  990. if (modestate == MS_WINDOWED)
  991. sprintf(pinfo, "%s windowed", pv->modedesc);
  992. else
  993. sprintf(pinfo, "windowed");
  994. }
  995. return pinfo;
  996. }
  997. /*
  998. =================
  999. VID_DescribeCurrentMode_f
  1000. =================
  1001. */
  1002. void VID_DescribeCurrentMode_f (void)
  1003. {
  1004. Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
  1005. }
  1006. /*
  1007. =================
  1008. VID_NumModes_f
  1009. =================
  1010. */
  1011. void VID_NumModes_f (void)
  1012. {
  1013. if (nummodes == 1)
  1014. Con_Printf ("%d video mode is available\n", nummodes);
  1015. else
  1016. Con_Printf ("%d video modes are available\n", nummodes);
  1017. }
  1018. /*
  1019. =================
  1020. VID_DescribeMode_f
  1021. =================
  1022. */
  1023. void VID_DescribeMode_f (void)
  1024. {
  1025. int t, modenum;
  1026. modenum = Q_atoi (Cmd_Argv(1));
  1027. t = leavecurrentmode;
  1028. leavecurrentmode = 0;
  1029. Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
  1030. leavecurrentmode = t;
  1031. }
  1032. /*
  1033. =================
  1034. VID_DescribeModes_f
  1035. =================
  1036. */
  1037. void VID_DescribeModes_f (void)
  1038. {
  1039. int i, lnummodes, t;
  1040. char *pinfo;
  1041. vmode_t *pv;
  1042. lnummodes = VID_NumModes ();
  1043. t = leavecurrentmode;
  1044. leavecurrentmode = 0;
  1045. for (i=1 ; i<lnummodes ; i++)
  1046. {
  1047. pv = VID_GetModePtr (i);
  1048. pinfo = VID_GetExtModeDescription (i);
  1049. Con_Printf ("%2d: %s\n", i, pinfo);
  1050. }
  1051. leavecurrentmode = t;
  1052. }
  1053. void VID_InitDIB (HINSTANCE hInstance)
  1054. {
  1055. WNDCLASS wc;
  1056. HDC hdc;
  1057. int i;
  1058. /* Register the frame class */
  1059. wc.style = 0;
  1060. wc.lpfnWndProc = (WNDPROC)MainWndProc;
  1061. wc.cbClsExtra = 0;
  1062. wc.cbWndExtra = 0;
  1063. wc.hInstance = hInstance;
  1064. wc.hIcon = 0;
  1065. wc.hCursor = LoadCursor (NULL,IDC_ARROW);
  1066. wc.hbrBackground = NULL;
  1067. wc.lpszMenuName = 0;
  1068. wc.lpszClassName = "WinQuake";
  1069. if (!RegisterClass (&wc) )
  1070. Sys_Error ("Couldn't register window class");
  1071. modelist[0].type = MS_WINDOWED;
  1072. if (COM_CheckParm("-width"))
  1073. modelist[0].width = Q_atoi(com_argv[COM_CheckParm("-width")+1]);
  1074. else
  1075. modelist[0].width = 640;
  1076. if (modelist[0].width < 320)
  1077. modelist[0].width = 320;
  1078. if (COM_CheckParm("-height"))
  1079. modelist[0].height= Q_atoi(com_argv[COM_CheckParm("-height")+1]);
  1080. else
  1081. modelist[0].height = modelist[0].width * 240/320;
  1082. if (modelist[0].height < 240)
  1083. modelist[0].height = 240;
  1084. sprintf (modelist[0].modedesc, "%dx%d",
  1085. modelist[0].width, modelist[0].height);
  1086. modelist[0].modenum = MODE_WINDOWED;
  1087. modelist[0].dib = 1;
  1088. modelist[0].fullscreen = 0;
  1089. modelist[0].halfscreen = 0;
  1090. modelist[0].bpp = 0;
  1091. nummodes = 1;
  1092. }
  1093. /*
  1094. =================
  1095. VID_InitFullDIB
  1096. =================
  1097. */
  1098. void VID_InitFullDIB (HINSTANCE hInstance)
  1099. {
  1100. DEVMODE devmode;
  1101. int i, modenum, cmodes, originalnummodes, existingmode, numlowresmodes;
  1102. int j, bpp, done;
  1103. BOOL stat;
  1104. // enumerate >8 bpp modes
  1105. originalnummodes = nummodes;
  1106. modenum = 0;
  1107. do
  1108. {
  1109. stat = EnumDisplaySettings (NULL, modenum, &devmode);
  1110. if ((devmode.dmBitsPerPel >= 15) &&
  1111. (devmode.dmPelsWidth <= MAXWIDTH) &&
  1112. (devmode.dmPelsHeight <= MAXHEIGHT) &&
  1113. (nummodes < MAX_MODE_LIST))
  1114. {
  1115. devmode.dmFields = DM_BITSPERPEL |
  1116. DM_PELSWIDTH |
  1117. DM_PELSHEIGHT;
  1118. if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) ==
  1119. DISP_CHANGE_SUCCESSFUL)
  1120. {
  1121. modelist[nummodes].type = MS_FULLDIB;
  1122. modelist[nummodes].width = devmode.dmPelsWidth;
  1123. modelist[nummodes].height = devmode.dmPelsHeight;
  1124. modelist[nummodes].modenum = 0;
  1125. modelist[nummodes].halfscreen = 0;
  1126. modelist[nummodes].dib = 1;
  1127. modelist[nummodes].fullscreen = 1;
  1128. modelist[nummodes].bpp = devmode.dmBitsPerPel;
  1129. sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
  1130. devmode.dmPelsWidth, devmode.dmPelsHeight,
  1131. devmode.dmBitsPerPel);
  1132. // if the width is more than twice the height, reduce it by half because this
  1133. // is probably a dual-screen monitor
  1134. if (!COM_CheckParm("-noadjustaspect"))
  1135. {
  1136. if (modelist[nummodes].width > (modelist[nummodes].height << 1))
  1137. {
  1138. modelist[nummodes].width >>= 1;
  1139. modelist[nummodes].halfscreen = 1;
  1140. sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
  1141. modelist[nummodes].width,
  1142. modelist[nummodes].height,
  1143. modelist[nummodes].bpp);
  1144. }
  1145. }
  1146. for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
  1147. {
  1148. if ((modelist[nummodes].width == modelist[i].width) &&
  1149. (modelist[nummodes].height == modelist[i].height) &&
  1150. (modelist[nummodes].bpp == modelist[i].bpp))
  1151. {
  1152. existingmode = 1;
  1153. break;
  1154. }
  1155. }
  1156. if (!existingmode)
  1157. {
  1158. nummodes++;
  1159. }
  1160. }
  1161. }
  1162. modenum++;
  1163. } while (stat);
  1164. // see if there are any low-res modes that aren't being reported
  1165. numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
  1166. bpp = 16;
  1167. done = 0;
  1168. do
  1169. {
  1170. for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
  1171. {
  1172. devmode.dmBitsPerPel = bpp;
  1173. devmode.dmPelsWidth = lowresmodes[j].width;
  1174. devmode.dmPelsHeight = lowresmodes[j].height;
  1175. devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  1176. if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) ==
  1177. DISP_CHANGE_SUCCESSFUL)
  1178. {
  1179. modelist[nummodes].type = MS_FULLDIB;
  1180. modelist[nummodes].width = devmode.dmPelsWidth;
  1181. modelist[nummodes].height = devmode.dmPelsHeight;
  1182. modelist[nummodes].modenum = 0;
  1183. modelist[nummodes].halfscreen = 0;
  1184. modelist[nummodes].dib = 1;
  1185. modelist[nummodes].fullscreen = 1;
  1186. modelist[nummodes].bpp = devmode.dmBitsPerPel;
  1187. sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
  1188. devmode.dmPelsWidth, devmode.dmPelsHeight,
  1189. devmode.dmBitsPerPel);
  1190. for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
  1191. {
  1192. if ((modelist[nummodes].width == modelist[i].width) &&
  1193. (modelist[nummodes].height == modelist[i].height) &&
  1194. (modelist[nummodes].bpp == modelist[i].bpp))
  1195. {
  1196. existingmode = 1;
  1197. break;
  1198. }
  1199. }
  1200. if (!existingmode)
  1201. {
  1202. nummodes++;
  1203. }
  1204. }
  1205. }
  1206. switch (bpp)
  1207. {
  1208. case 16:
  1209. bpp = 32;
  1210. break;
  1211. case 32:
  1212. bpp = 24;
  1213. break;
  1214. case 24:
  1215. done = 1;
  1216. break;
  1217. }
  1218. } while (!done);
  1219. if (nummodes == originalnummodes)
  1220. Con_SafePrintf ("No fullscreen DIB modes found\n");
  1221. }
  1222. qboolean VID_Is8bit() {
  1223. return is8bit;
  1224. }
  1225. #define GL_SHARED_TEXTURE_PALETTE_EXT 0x81FB
  1226. void VID_Init8bitPalette()
  1227. {
  1228. // Check for 8bit Extensions and initialize them.
  1229. int i;
  1230. char thePalette[256*3];
  1231. char *oldPalette, *newPalette;
  1232. glColorTableEXT = (void *)wglGetProcAddress("glColorTableEXT");
  1233. if (!glColorTableEXT || strstr(gl_extensions, "GL_EXT_shared_texture_palette") ||
  1234. COM_CheckParm("-no8bit"))
  1235. return;
  1236. Con_SafePrintf("8-bit GL extensions enabled.\n");
  1237. glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
  1238. oldPalette = (char *) d_8to24table; //d_8to24table3dfx;
  1239. newPalette = thePalette;
  1240. for (i=0;i<256;i++) {
  1241. *newPalette++ = *oldPalette++;
  1242. *newPalette++ = *oldPalette++;
  1243. *newPalette++ = *oldPalette++;
  1244. oldPalette++;
  1245. }
  1246. glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE,
  1247. (void *) thePalette);
  1248. is8bit = TRUE;
  1249. }
  1250. static void Check_Gamma (unsigned char *pal)
  1251. {
  1252. float f, inf;
  1253. unsigned char palette[768];
  1254. int i;
  1255. if ((i = COM_CheckParm("-gamma")) == 0) {
  1256. if ((gl_renderer && strstr(gl_renderer, "Voodoo")) ||
  1257. (gl_vendor && strstr(gl_vendor, "3Dfx")))
  1258. vid_gamma = 1;
  1259. else
  1260. vid_gamma = 0.7; // default to 0.7 on non-3dfx hardware
  1261. } else
  1262. vid_gamma = Q_atof(com_argv[i+1]);
  1263. for (i=0 ; i<768 ; i++)
  1264. {
  1265. f = pow ( (pal[i]+1)/256.0 , vid_gamma );
  1266. inf = f*255 + 0.5;
  1267. if (inf < 0)
  1268. inf = 0;
  1269. if (inf > 255)
  1270. inf = 255;
  1271. palette[i] = inf;
  1272. }
  1273. memcpy (pal, palette, sizeof(palette));
  1274. }
  1275. /*
  1276. ===================
  1277. VID_Init
  1278. ===================
  1279. */
  1280. void VID_Init (unsigned char *palette)
  1281. {
  1282. int i, existingmode;
  1283. int basenummodes, width, height, bpp, findbpp, done;
  1284. byte *ptmp;
  1285. char gldir[MAX_OSPATH];
  1286. HDC hdc;
  1287. DEVMODE devmode;
  1288. memset(&devmode, 0, sizeof(devmode));
  1289. Cvar_RegisterVariable (&vid_mode);
  1290. Cvar_RegisterVariable (&vid_wait);
  1291. Cvar_RegisterVariable (&vid_nopageflip);
  1292. Cvar_RegisterVariable (&_vid_wait_override);
  1293. Cvar_RegisterVariable (&_vid_default_mode);
  1294. Cvar_RegisterVariable (&_vid_default_mode_win);
  1295. Cvar_RegisterVariable (&vid_config_x);
  1296. Cvar_RegisterVariable (&vid_config_y);
  1297. Cvar_RegisterVariable (&vid_stretch_by_2);
  1298. Cvar_RegisterVariable (&_windowed_mouse);
  1299. Cvar_RegisterVariable (&gl_ztrick);
  1300. Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
  1301. Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
  1302. Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
  1303. Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
  1304. hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
  1305. InitCommonControls();
  1306. VID_InitDIB (global_hInstance);
  1307. basenummodes = nummodes = 1;
  1308. VID_InitFullDIB (global_hInstance);
  1309. if (COM_CheckParm("-window"))
  1310. {
  1311. hdc = GetDC (NULL);
  1312. if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
  1313. {
  1314. Sys_Error ("Can't run in non-RGB mode");
  1315. }
  1316. ReleaseDC (NULL, hdc);
  1317. windowed = true;
  1318. vid_default = MODE_WINDOWED;
  1319. }
  1320. else
  1321. {
  1322. if (nummodes == 1)
  1323. Sys_Error ("No RGB fullscreen modes available");
  1324. windowed = false;
  1325. if (COM_CheckParm("-mode"))
  1326. {
  1327. vid_default = Q_atoi(com_argv[COM_CheckParm("-mode")+1]);
  1328. }
  1329. else
  1330. {
  1331. if (COM_CheckParm("-current"))
  1332. {
  1333. modelist[MODE_FULLSCREEN_DEFAULT].width =
  1334. GetSystemMetrics (SM_CXSCREEN);
  1335. modelist[MODE_FULLSCREEN_DEFAULT].height =
  1336. GetSystemMetrics (SM_CYSCREEN);
  1337. vid_default = MODE_FULLSCREEN_DEFAULT;
  1338. leavecurrentmode = 1;
  1339. }
  1340. else
  1341. {
  1342. if (COM_CheckParm("-width"))
  1343. {
  1344. width = Q_atoi(com_argv[COM_CheckParm("-width")+1]);
  1345. }
  1346. else
  1347. {
  1348. width = 640;
  1349. }
  1350. if (COM_CheckParm("-bpp"))
  1351. {
  1352. bpp = Q_atoi(com_argv[COM_CheckParm("-bpp")+1]);
  1353. findbpp = 0;
  1354. }
  1355. else
  1356. {
  1357. bpp = 15;
  1358. findbpp = 1;
  1359. }
  1360. if (COM_CheckParm("-height"))
  1361. height = Q_atoi(com_argv[COM_CheckParm("-height")+1]);
  1362. // if they want to force it, add the specified mode to the list
  1363. if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
  1364. {
  1365. modelist[nummodes].type = MS_FULLDIB;
  1366. modelist[nummodes].width = width;
  1367. modelist[nummodes].height = height;
  1368. modelist[nummodes].modenum = 0;
  1369. modelist[nummodes].halfscreen = 0;
  1370. modelist[nummodes].dib = 1;
  1371. modelist[nummodes].fullscreen = 1;
  1372. modelist[nummodes].bpp = bpp;
  1373. sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
  1374. devmode.dmPelsWidth, devmode.dmPelsHeight,
  1375. devmode.dmBitsPerPel);
  1376. for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
  1377. {
  1378. if ((modelist[nummodes].width == modelist[i].width) &&
  1379. (modelist[nummodes].height == modelist[i].height) &&
  1380. (modelist[nummodes].bpp == modelist[i].bpp))
  1381. {
  1382. existingmode = 1;
  1383. break;
  1384. }
  1385. }
  1386. if (!existingmode)
  1387. {
  1388. nummodes++;
  1389. }
  1390. }
  1391. done = 0;
  1392. do
  1393. {
  1394. if (COM_CheckParm("-height"))
  1395. {
  1396. height = Q_atoi(com_argv[COM_CheckParm("-height")+1]);
  1397. for (i=1, vid_default=0 ; i<nummodes ; i++)
  1398. {
  1399. if ((modelist[i].width == width) &&
  1400. (modelist[i].height == height) &&
  1401. (modelist[i].bpp == bpp))
  1402. {
  1403. vid_default = i;
  1404. done = 1;
  1405. break;
  1406. }
  1407. }
  1408. }
  1409. else
  1410. {
  1411. for (i=1, vid_default=0 ; i<nummodes ; i++)
  1412. {
  1413. if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
  1414. {
  1415. vid_default = i;
  1416. done = 1;
  1417. break;
  1418. }
  1419. }
  1420. }
  1421. if (!done)
  1422. {
  1423. if (findbpp)
  1424. {
  1425. switch (bpp)
  1426. {
  1427. case 15:
  1428. bpp = 16;
  1429. break;
  1430. case 16:
  1431. bpp = 32;
  1432. break;
  1433. case 32:
  1434. bpp = 24;
  1435. break;
  1436. case 24:
  1437. done = 1;
  1438. break;
  1439. }
  1440. }
  1441. else
  1442. {
  1443. done = 1;
  1444. }
  1445. }
  1446. } while (!done);
  1447. if (!vid_default)
  1448. {
  1449. Sys_Error ("Specified video mode not available");
  1450. }
  1451. }
  1452. }
  1453. }
  1454. vid_initialized = true;
  1455. if ((i = COM_CheckParm("-conwidth")) != 0)
  1456. vid.conwidth = Q_atoi(com_argv[i+1]);
  1457. else
  1458. vid.conwidth = 640;
  1459. vid.conwidth &= 0xfff8; // make it a multiple of eight
  1460. if (vid.conwidth < 320)
  1461. vid.conwidth = 320;
  1462. // pick a conheight that matches with correct aspect
  1463. vid.conheight = vid.conwidth*3 / 4;
  1464. if ((i = COM_CheckParm("-conheight")) != 0)
  1465. vid.conheight = Q_atoi(com_argv[i+1]);
  1466. if (vid.conheight < 200)
  1467. vid.conheight = 200;
  1468. vid.maxwarpwidth = WARP_WIDTH;
  1469. vid.maxwarpheight = WARP_HEIGHT;
  1470. vid.colormap = host_colormap;
  1471. vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
  1472. DestroyWindow (hwnd_dialog);
  1473. Check_Gamma(palette);
  1474. VID_SetPalette (palette);
  1475. VID_SetMode (vid_default, palette);
  1476. maindc = GetDC(mainwindow);
  1477. bSetupPixelFormat(maindc);
  1478. baseRC = wglCreateContext( maindc );
  1479. if (!baseRC)
  1480. Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you in are 65535 color mode, and try running -window.");
  1481. if (!wglMakeCurrent( maindc, baseRC ))
  1482. Sys_Error ("wglMakeCurrent failed");
  1483. GL_Init ();
  1484. sprintf (gldir, "%s/glquake", com_gamedir);
  1485. Sys_mkdir (gldir);
  1486. vid_realmode = vid_modenum;
  1487. // Check for 3DFX Extensions and initialize them.
  1488. VID_Init8bitPalette();
  1489. vid_menudrawfn = VID_MenuDraw;
  1490. vid_menukeyfn = VID_MenuKey;
  1491. strcpy (badmode.modedesc, "Bad mode");
  1492. vid_canalttab = true;
  1493. if (COM_CheckParm("-fullsbar"))
  1494. fullsbardraw = true;
  1495. }
  1496. //========================================================
  1497. // Video menu stuff
  1498. //========================================================
  1499. extern void M_Menu_Options_f (void);
  1500. extern void M_Print (int cx, int cy, char *str);
  1501. extern void M_PrintWhite (int cx, int cy, char *str);
  1502. extern void M_DrawCharacter (int cx, int line, int num);
  1503. extern void M_DrawTransPic (int x, int y, qpic_t *pic);
  1504. extern void M_DrawPic (int x, int y, qpic_t *pic);
  1505. static int vid_line, vid_wmodes;
  1506. typedef struct
  1507. {
  1508. int modenum;
  1509. char *desc;
  1510. int iscur;
  1511. } modedesc_t;
  1512. #define MAX_COLUMN_SIZE 9
  1513. #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
  1514. #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
  1515. static modedesc_t modedescs[MAX_MODEDESCS];
  1516. /*
  1517. ================
  1518. VID_MenuDraw
  1519. ================
  1520. */
  1521. void VID_MenuDraw (void)
  1522. {
  1523. qpic_t *p;
  1524. char *ptr;
  1525. int lnummodes, i, j, k, column, row, dup, dupmode;
  1526. char temp[100];
  1527. vmode_t *pv;
  1528. p = Draw_CachePic ("gfx/vidmodes.lmp");
  1529. M_DrawPic ( (320-p->width)/2, 4, p);
  1530. vid_wmodes = 0;
  1531. lnummodes = VID_NumModes ();
  1532. for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
  1533. {
  1534. ptr = VID_GetModeDescription (i);
  1535. pv = VID_GetModePtr (i);
  1536. k = vid_wmodes;
  1537. modedescs[k].modenum = i;
  1538. modedescs[k].desc = ptr;
  1539. modedescs[k].iscur = 0;
  1540. if (i == vid_modenum)
  1541. modedescs[k].iscur = 1;
  1542. vid_wmodes++;
  1543. }
  1544. if (vid_wmodes > 0)
  1545. {
  1546. M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
  1547. column = 8;
  1548. row = 36+2*8;
  1549. for (i=0 ; i<vid_wmodes ; i++)
  1550. {
  1551. if (modedescs[i].iscur)
  1552. M_PrintWhite (column, row, modedescs[i].desc);
  1553. else
  1554. M_Print (column, row, modedescs[i].desc);
  1555. column += 13*8;
  1556. if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
  1557. {
  1558. column = 8;
  1559. row += 8;
  1560. }
  1561. }
  1562. }
  1563. M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2,
  1564. "Video modes must be set from the");
  1565. M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3,
  1566. "command line with -width <width>");
  1567. M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4,
  1568. "and -bpp <bits-per-pixel>");
  1569. M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6,
  1570. "Select windowed mode with -window");
  1571. }
  1572. /*
  1573. ================
  1574. VID_MenuKey
  1575. ================
  1576. */
  1577. void VID_MenuKey (int key)
  1578. {
  1579. switch (key)
  1580. {
  1581. case K_ESCAPE:
  1582. S_LocalSound ("misc/menu1.wav");
  1583. M_Menu_Options_f ();
  1584. break;
  1585. default:
  1586. break;
  1587. }
  1588. }