vid_win.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392
  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. // vid_win.c -- Win32 video driver
  16. #include "quakedef.h"
  17. #include "winquake.h"
  18. #include "d_local.h"
  19. #include "resource.h"
  20. #define MINIMUM_MEMORY 0x550000
  21. #define MAX_MODE_LIST 30
  22. #define VID_ROW_SIZE 3
  23. qboolean dibonly;
  24. extern int Minimized;
  25. HWND mainwindow;
  26. HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
  27. int DIBWidth, DIBHeight;
  28. qboolean DDActive;
  29. RECT WindowRect;
  30. DWORD WindowStyle, ExWindowStyle;
  31. int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
  32. RECT window_rect;
  33. static DEVMODE gdevmode;
  34. static qboolean startwindowed = 0, windowed_mode_set;
  35. static int firstupdate = 1;
  36. static qboolean vid_initialized = false, vid_palettized;
  37. static int lockcount;
  38. static int vid_fulldib_on_focus_mode;
  39. static qboolean force_minimized, in_mode_set, is_mode0x13, force_mode_set;
  40. static int vid_stretched, windowed_mouse;
  41. static qboolean palette_changed, syscolchg, vid_mode_set, hide_window, pal_is_nostatic;
  42. static HICON hIcon;
  43. qboolean mouseactive; // from in_win.c
  44. viddef_t vid; // global video state
  45. #define MODE_WINDOWED 0
  46. #define MODE_SETTABLE_WINDOW 2
  47. #define NO_MODE (MODE_WINDOWED - 1)
  48. #define MODE_FULLSCREEN_DEFAULT (MODE_WINDOWED + 3)
  49. // Note that 0 is MODE_WINDOWED
  50. cvar_t vid_mode = {"vid_mode","0", false};
  51. // Note that 0 is MODE_WINDOWED
  52. cvar_t _vid_default_mode = {"_vid_default_mode","0", true};
  53. // Note that 3 is MODE_FULLSCREEN_DEFAULT
  54. cvar_t _vid_default_mode_win = {"_vid_default_mode_win","3", true};
  55. cvar_t vid_wait = {"vid_wait","0"};
  56. cvar_t vid_nopageflip = {"vid_nopageflip","0", true};
  57. cvar_t _vid_wait_override = {"_vid_wait_override", "0", true};
  58. cvar_t vid_config_x = {"vid_config_x","800", true};
  59. cvar_t vid_config_y = {"vid_config_y","600", true};
  60. cvar_t vid_stretch_by_2 = {"vid_stretch_by_2","1", true};
  61. cvar_t _windowed_mouse = {"_windowed_mouse","0", true};
  62. cvar_t vid_fullscreen_mode = {"vid_fullscreen_mode","3", true};
  63. cvar_t vid_windowed_mode = {"vid_windowed_mode","0", true};
  64. cvar_t block_switch = {"block_switch","0", true};
  65. cvar_t vid_window_x = {"vid_window_x", "0", true};
  66. cvar_t vid_window_y = {"vid_window_y", "0", true};
  67. typedef struct {
  68. int width;
  69. int height;
  70. } lmode_t;
  71. lmode_t lowresmodes[] = {
  72. {320, 200},
  73. {320, 240},
  74. {400, 300},
  75. {512, 384},
  76. };
  77. int vid_modenum = NO_MODE;
  78. int vid_testingmode, vid_realmode;
  79. double vid_testendtime;
  80. int vid_default = MODE_WINDOWED;
  81. static int windowed_default;
  82. modestate_t modestate = MS_UNINIT;
  83. static byte *vid_surfcache;
  84. static int vid_surfcachesize;
  85. static int VID_highhunkmark;
  86. unsigned char vid_curpal[256*3];
  87. unsigned short d_8to16table[256];
  88. unsigned d_8to24table[256];
  89. int driver = grDETECT,mode;
  90. bool useWinDirect = true, useDirectDraw = true;
  91. MGLDC *mgldc = NULL,*memdc = NULL,*dibdc = NULL,*windc = NULL;
  92. typedef struct {
  93. modestate_t type;
  94. int width;
  95. int height;
  96. int modenum;
  97. int mode13;
  98. int stretched;
  99. int dib;
  100. int fullscreen;
  101. int bpp;
  102. int halfscreen;
  103. char modedesc[13];
  104. } vmode_t;
  105. static vmode_t modelist[MAX_MODE_LIST];
  106. static int nummodes;
  107. static vmode_t *pcurrentmode;
  108. int aPage; // Current active display page
  109. int vPage; // Current visible display page
  110. int waitVRT = true; // True to wait for retrace on flip
  111. static vmode_t badmode;
  112. static byte backingbuf[48*24];
  113. void VID_MenuDraw (void);
  114. void VID_MenuKey (int key);
  115. LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  116. void AppActivate(BOOL fActive, BOOL minimize);
  117. /*
  118. ================
  119. VID_RememberWindowPos
  120. ================
  121. */
  122. void VID_RememberWindowPos (void)
  123. {
  124. RECT rect;
  125. if (GetWindowRect (mainwindow, &rect))
  126. {
  127. if ((rect.left < GetSystemMetrics (SM_CXSCREEN)) &&
  128. (rect.top < GetSystemMetrics (SM_CYSCREEN)) &&
  129. (rect.right > 0) &&
  130. (rect.bottom > 0))
  131. {
  132. Cvar_SetValue ("vid_window_x", (float)rect.left);
  133. Cvar_SetValue ("vid_window_y", (float)rect.top);
  134. }
  135. }
  136. }
  137. /*
  138. ================
  139. VID_CheckWindowXY
  140. ================
  141. */
  142. void VID_CheckWindowXY (void)
  143. {
  144. if (((int)vid_window_x.value > (GetSystemMetrics (SM_CXSCREEN) - 160)) ||
  145. ((int)vid_window_y.value > (GetSystemMetrics (SM_CYSCREEN) - 120)) ||
  146. ((int)vid_window_x.value < 0) ||
  147. ((int)vid_window_y.value < 0))
  148. {
  149. Cvar_SetValue ("vid_window_x", 0.0);
  150. Cvar_SetValue ("vid_window_y", 0.0 );
  151. }
  152. }
  153. /*
  154. ================
  155. VID_UpdateWindowStatus
  156. ================
  157. */
  158. void VID_UpdateWindowStatus (void)
  159. {
  160. window_rect.left = window_x;
  161. window_rect.top = window_y;
  162. window_rect.right = window_x + window_width;
  163. window_rect.bottom = window_y + window_height;
  164. window_center_x = (window_rect.left + window_rect.right) / 2;
  165. window_center_y = (window_rect.top + window_rect.bottom) / 2;
  166. IN_UpdateClipCursor ();
  167. }
  168. /*
  169. ================
  170. ClearAllStates
  171. ================
  172. */
  173. void ClearAllStates (void)
  174. {
  175. int i;
  176. // send an up event for each key, to make sure the server clears them all
  177. for (i=0 ; i<256 ; i++)
  178. {
  179. Key_Event (i, false);
  180. }
  181. Key_ClearStates ();
  182. IN_ClearStates ();
  183. }
  184. /*
  185. ================
  186. VID_CheckAdequateMem
  187. ================
  188. */
  189. qboolean VID_CheckAdequateMem (int width, int height)
  190. {
  191. int tbuffersize;
  192. tbuffersize = width * height * sizeof (*d_pzbuffer);
  193. tbuffersize += D_SurfaceCacheForRes (width, height);
  194. // see if there's enough memory, allowing for the normal mode 0x13 pixel,
  195. // z, and surface buffers
  196. if ((host_parms.memsize - tbuffersize + SURFCACHE_SIZE_AT_320X200 +
  197. 0x10000 * 3) < MINIMUM_MEMORY)
  198. {
  199. return false; // not enough memory for mode
  200. }
  201. return true;
  202. }
  203. /*
  204. ================
  205. VID_AllocBuffers
  206. ================
  207. */
  208. qboolean VID_AllocBuffers (int width, int height)
  209. {
  210. int tsize, tbuffersize;
  211. tbuffersize = width * height * sizeof (*d_pzbuffer);
  212. tsize = D_SurfaceCacheForRes (width, height);
  213. tbuffersize += tsize;
  214. // see if there's enough memory, allowing for the normal mode 0x13 pixel,
  215. // z, and surface buffers
  216. if ((host_parms.memsize - tbuffersize + SURFCACHE_SIZE_AT_320X200 +
  217. 0x10000 * 3) < MINIMUM_MEMORY)
  218. {
  219. Con_SafePrintf ("Not enough memory for video mode\n");
  220. return false; // not enough memory for mode
  221. }
  222. vid_surfcachesize = tsize;
  223. if (d_pzbuffer)
  224. {
  225. D_FlushCaches ();
  226. Hunk_FreeToHighMark (VID_highhunkmark);
  227. d_pzbuffer = NULL;
  228. }
  229. VID_highhunkmark = Hunk_HighMark ();
  230. d_pzbuffer = Hunk_HighAllocName (tbuffersize, "video");
  231. vid_surfcache = (byte *)d_pzbuffer +
  232. width * height * sizeof (*d_pzbuffer);
  233. return true;
  234. }
  235. void initFatalError(void)
  236. {
  237. MGL_exit();
  238. MGL_fatalError(MGL_errorMsg(MGL_result()));
  239. exit(EXIT_FAILURE);
  240. }
  241. #if 0 //def NEW_SUSPEND
  242. int VID_Suspend (MGLDC *dc, int flags)
  243. {
  244. int i;
  245. if (flags & MGL_DEACTIVATE)
  246. {
  247. IN_RestoreOriginalMouseState ();
  248. CDAudio_Pause ();
  249. // keep WM_PAINT from trying to redraw
  250. in_mode_set = true;
  251. block_drawing = true;
  252. }
  253. else if (flags & MGL_REACTIVATE)
  254. {
  255. IN_SetQuakeMouseState ();
  256. // fix the leftover Alt from any Alt-Tab or the like that switched us away
  257. ClearAllStates ();
  258. CDAudio_Resume ();
  259. in_mode_set = false;
  260. block_drawing = false;
  261. // vid.recalc_refdef = 1;
  262. force_mode_set = 1;
  263. i = msg_suppress_1;
  264. msg_suppress_1 = 1;
  265. VID_Fullscreen_f();
  266. msg_suppress_1 = i;
  267. force_mode_set = 0;
  268. }
  269. return 1;
  270. }
  271. #else
  272. int VID_Suspend (MGLDC *dc, int flags)
  273. {
  274. if (flags & MGL_DEACTIVATE)
  275. {
  276. // FIXME: this doesn't currently work on NT
  277. if (block_switch.value && !WinNT)
  278. {
  279. return MGL_NO_DEACTIVATE;
  280. }
  281. S_BlockSound ();
  282. S_ClearBuffer ();
  283. IN_RestoreOriginalMouseState ();
  284. CDAudio_Pause ();
  285. // keep WM_PAINT from trying to redraw
  286. in_mode_set = true;
  287. block_drawing = true; // so we don't try to draw while switched away
  288. return MGL_NO_SUSPEND_APP;
  289. }
  290. else if (flags & MGL_REACTIVATE)
  291. {
  292. IN_SetQuakeMouseState ();
  293. // fix the leftover Alt from any Alt-Tab or the like that switched us away
  294. ClearAllStates ();
  295. CDAudio_Resume ();
  296. S_UnblockSound ();
  297. in_mode_set = false;
  298. vid.recalc_refdef = 1;
  299. block_drawing = false;
  300. return MGL_NO_SUSPEND_APP;
  301. }
  302. }
  303. #endif
  304. void registerAllDispDrivers(void)
  305. {
  306. /* Event though these driver require WinDirect, we register
  307. * them so that they will still be available even if DirectDraw
  308. * is present and the user has disable the high performance
  309. * WinDirect modes.
  310. */
  311. MGL_registerDriver(MGL_VGA8NAME,VGA8_driver);
  312. // MGL_registerDriver(MGL_VGAXNAME,VGAX_driver);
  313. /* Register display drivers */
  314. if (useWinDirect)
  315. {
  316. //we don't want VESA 1.X drivers MGL_registerDriver(MGL_SVGA8NAME,SVGA8_driver);
  317. MGL_registerDriver(MGL_LINEAR8NAME,LINEAR8_driver);
  318. if (!COM_CheckParm ("-novbeaf"))
  319. MGL_registerDriver(MGL_ACCEL8NAME,ACCEL8_driver);
  320. }
  321. if (useDirectDraw)
  322. {
  323. MGL_registerDriver(MGL_DDRAW8NAME,DDRAW8_driver);
  324. }
  325. }
  326. void registerAllMemDrivers(void)
  327. {
  328. /* Register memory context drivers */
  329. MGL_registerDriver(MGL_PACKED8NAME,PACKED8_driver);
  330. }
  331. void VID_InitMGLFull (HINSTANCE hInstance)
  332. {
  333. int i, xRes, yRes, bits, vMode, lowres, curmode, temp;
  334. int lowstretchedres, stretchedmode, lowstretched;
  335. uchar *m;
  336. // FIXME: NT is checked for because MGL currently has a bug that causes it
  337. // to try to use WinDirect modes even on NT
  338. if (COM_CheckParm("-nowindirect") ||
  339. COM_CheckParm("-nowd") ||
  340. COM_CheckParm("-novesa") ||
  341. WinNT)
  342. {
  343. useWinDirect = false;
  344. }
  345. if (COM_CheckParm("-nodirectdraw") || COM_CheckParm("-noddraw") || COM_CheckParm("-nodd"))
  346. useDirectDraw = false;
  347. // Initialise the MGL
  348. MGL_unregisterAllDrivers();
  349. registerAllDispDrivers();
  350. registerAllMemDrivers();
  351. MGL_detectGraph(&driver,&mode);
  352. m = MGL_availableModes();
  353. if (m[0] != 0xFF)
  354. {
  355. lowres = lowstretchedres = 99999;
  356. lowstretched = 0;
  357. curmode = 0;
  358. // find the lowest-res mode, or a mode we can stretch up to and get
  359. // lowest-res that way
  360. for (i = 0; m[i] != 0xFF; i++)
  361. {
  362. MGL_modeResolution(m[i], &xRes, &yRes,&bits);
  363. if ((bits == 8) &&
  364. (xRes <= MAXWIDTH) &&
  365. (yRes <= MAXHEIGHT) &&
  366. (curmode < MAX_MODE_LIST))
  367. {
  368. if (m[i] == grVGA_320x200x256)
  369. is_mode0x13 = true;
  370. if (!COM_CheckParm("-noforcevga"))
  371. {
  372. if (m[i] == grVGA_320x200x256)
  373. {
  374. mode = i;
  375. break;
  376. }
  377. }
  378. if (xRes < lowres)
  379. {
  380. lowres = xRes;
  381. mode = i;
  382. }
  383. if ((xRes < lowstretchedres) && ((xRes >> 1) >= 320))
  384. {
  385. lowstretchedres = xRes >> 1;
  386. stretchedmode = i;
  387. }
  388. }
  389. curmode++;
  390. }
  391. // if there's a mode we can stretch by 2 up to, thereby effectively getting
  392. // a lower-res mode than the lowest-res real but still at least 320x200, that
  393. // will be our default mode
  394. if (lowstretchedres < lowres)
  395. {
  396. mode = stretchedmode;
  397. lowres = lowstretchedres;
  398. lowstretched = 1;
  399. }
  400. // build the mode list, leaving room for the low-res stretched mode, if any
  401. nummodes++; // leave room for default mode
  402. for (i = 0; m[i] != 0xFF; i++)
  403. {
  404. MGL_modeResolution(m[i], &xRes, &yRes,&bits);
  405. if ((bits == 8) &&
  406. (xRes <= MAXWIDTH) &&
  407. (yRes <= MAXHEIGHT) &&
  408. (nummodes < MAX_MODE_LIST))
  409. {
  410. if (i == mode)
  411. {
  412. if (lowstretched)
  413. {
  414. stretchedmode = nummodes;
  415. curmode = nummodes++;
  416. }
  417. else
  418. {
  419. curmode = MODE_FULLSCREEN_DEFAULT;
  420. }
  421. }
  422. else
  423. {
  424. curmode = nummodes++;
  425. }
  426. modelist[curmode].type = MS_FULLSCREEN;
  427. modelist[curmode].width = xRes;
  428. modelist[curmode].height = yRes;
  429. sprintf (modelist[curmode].modedesc, "%dx%d", xRes, yRes);
  430. if (m[i] == grVGA_320x200x256)
  431. modelist[curmode].mode13 = 1;
  432. else
  433. modelist[curmode].mode13 = 0;
  434. modelist[curmode].modenum = m[i];
  435. modelist[curmode].stretched = 0;
  436. modelist[curmode].dib = 0;
  437. modelist[curmode].fullscreen = 1;
  438. modelist[curmode].halfscreen = 0;
  439. modelist[curmode].bpp = 8;
  440. }
  441. }
  442. if (lowstretched)
  443. {
  444. modelist[MODE_FULLSCREEN_DEFAULT] = modelist[stretchedmode];
  445. modelist[MODE_FULLSCREEN_DEFAULT].stretched = 1;
  446. modelist[MODE_FULLSCREEN_DEFAULT].width >>= 1;
  447. modelist[MODE_FULLSCREEN_DEFAULT].height >>= 1;
  448. sprintf (modelist[MODE_FULLSCREEN_DEFAULT].modedesc, "%dx%d",
  449. modelist[MODE_FULLSCREEN_DEFAULT].width,
  450. modelist[MODE_FULLSCREEN_DEFAULT].height);
  451. }
  452. vid_default = MODE_FULLSCREEN_DEFAULT;
  453. temp = m[0];
  454. if (!MGL_init(&driver, &temp, ""))
  455. {
  456. initFatalError();
  457. }
  458. }
  459. MGL_setSuspendAppCallback(VID_Suspend);
  460. }
  461. MGLDC *createDisplayDC(int forcemem)
  462. /****************************************************************************
  463. *
  464. * Function: createDisplayDC
  465. * Returns: Pointer to the MGL device context to use for the application
  466. *
  467. * Description: Initialises the MGL and creates an appropriate display
  468. * device context to be used by the GUI. This creates and
  469. * apropriate device context depending on the system being
  470. * compile for, and should be the only place where system
  471. * specific code is required.
  472. *
  473. ****************************************************************************/
  474. {
  475. MGLDC *dc;
  476. pixel_format_t pf;
  477. int npages;
  478. // Start the specified video mode
  479. if (!MGL_changeDisplayMode(mode))
  480. initFatalError();
  481. npages = MGL_availablePages(mode);
  482. if (npages > 3)
  483. npages = 3;
  484. if (!COM_CheckParm ("-notriplebuf"))
  485. {
  486. if (npages > 2)
  487. {
  488. npages = 2;
  489. }
  490. }
  491. if ((dc = MGL_createDisplayDC(npages)) == NULL)
  492. return NULL;
  493. if (!forcemem && (MGL_surfaceAccessType(dc)) == MGL_LINEAR_ACCESS && (dc->mi.maxPage > 0))
  494. {
  495. MGL_makeCurrentDC(dc);
  496. memdc = NULL;
  497. }
  498. else
  499. {
  500. // Set up for blitting from a memory buffer
  501. memdc = MGL_createMemoryDC(MGL_sizex(dc)+1,MGL_sizey(dc)+1,8,&pf);
  502. MGL_makeCurrentDC(memdc);
  503. }
  504. // Enable page flipping even for even for blitted surfaces
  505. if (forcemem)
  506. {
  507. vid.numpages = 1;
  508. }
  509. else
  510. {
  511. vid.numpages = dc->mi.maxPage + 1;
  512. if (vid.numpages > 1)
  513. {
  514. // Set up for page flipping
  515. MGL_setActivePage(dc, aPage = 1);
  516. MGL_setVisualPage(dc, vPage = 0, false);
  517. }
  518. if (vid.numpages > 3)
  519. vid.numpages = 3;
  520. }
  521. if (vid.numpages == 2)
  522. waitVRT = true;
  523. else
  524. waitVRT = false;
  525. return dc;
  526. }
  527. void VID_InitMGLDIB (HINSTANCE hInstance)
  528. {
  529. WNDCLASS wc;
  530. HDC hdc;
  531. int i;
  532. hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON2));
  533. /* Register the frame class */
  534. wc.style = 0;
  535. wc.lpfnWndProc = (WNDPROC)MainWndProc;
  536. wc.cbClsExtra = 0;
  537. wc.cbWndExtra = 0;
  538. wc.hInstance = hInstance;
  539. wc.hIcon = 0;
  540. wc.hCursor = LoadCursor (NULL,IDC_ARROW);
  541. wc.hbrBackground = NULL;
  542. wc.lpszMenuName = 0;
  543. wc.lpszClassName = "WinQuake";
  544. if (!RegisterClass (&wc) )
  545. Sys_Error ("Couldn't register window class");
  546. /* Find the size for the DIB window */
  547. /* Initialise the MGL for windowed operation */
  548. MGL_setAppInstance(hInstance);
  549. registerAllMemDrivers();
  550. MGL_initWindowed("");
  551. modelist[0].type = MS_WINDOWED;
  552. modelist[0].width = 320;
  553. modelist[0].height = 240;
  554. strcpy (modelist[0].modedesc, "320x240");
  555. modelist[0].mode13 = 0;
  556. modelist[0].modenum = MODE_WINDOWED;
  557. modelist[0].stretched = 0;
  558. modelist[0].dib = 1;
  559. modelist[0].fullscreen = 0;
  560. modelist[0].halfscreen = 0;
  561. modelist[0].bpp = 8;
  562. modelist[1].type = MS_WINDOWED;
  563. modelist[1].width = 640;
  564. modelist[1].height = 480;
  565. strcpy (modelist[1].modedesc, "640x480");
  566. modelist[1].mode13 = 0;
  567. modelist[1].modenum = MODE_WINDOWED + 1;
  568. modelist[1].stretched = 1;
  569. modelist[1].dib = 1;
  570. modelist[1].fullscreen = 0;
  571. modelist[1].halfscreen = 0;
  572. modelist[1].bpp = 8;
  573. modelist[2].type = MS_WINDOWED;
  574. modelist[2].width = 800;
  575. modelist[2].height = 600;
  576. strcpy (modelist[2].modedesc, "800x600");
  577. modelist[2].mode13 = 0;
  578. modelist[2].modenum = MODE_WINDOWED + 2;
  579. modelist[2].stretched = 1;
  580. modelist[2].dib = 1;
  581. modelist[2].fullscreen = 0;
  582. modelist[2].halfscreen = 0;
  583. modelist[2].bpp = 8;
  584. // automatically stretch the default mode up if > 640x480 desktop resolution
  585. hdc = GetDC(NULL);
  586. if ((GetDeviceCaps(hdc, HORZRES) > 640) && !COM_CheckParm("-noautostretch"))
  587. {
  588. vid_default = MODE_WINDOWED + 1;
  589. }
  590. else
  591. {
  592. vid_default = MODE_WINDOWED;
  593. }
  594. windowed_default = vid_default;
  595. ReleaseDC(NULL,hdc);
  596. nummodes = 3; // reserve space for windowed mode
  597. DDActive = 0;
  598. }
  599. /*
  600. =================
  601. VID_InitFullDIB
  602. =================
  603. */
  604. void VID_InitFullDIB (HINSTANCE hInstance)
  605. {
  606. DEVMODE devmode;
  607. int i, j, modenum, cmodes, existingmode, originalnummodes, lowestres;
  608. int numlowresmodes, bpp, done;
  609. int cstretch, istretch, mstretch;
  610. BOOL stat;
  611. // enumerate 8 bpp modes
  612. originalnummodes = nummodes;
  613. modenum = 0;
  614. lowestres = 99999;
  615. do
  616. {
  617. stat = EnumDisplaySettings (NULL, modenum, &devmode);
  618. if ((devmode.dmBitsPerPel == 8) &&
  619. (devmode.dmPelsWidth <= MAXWIDTH) &&
  620. (devmode.dmPelsHeight <= MAXHEIGHT) &&
  621. (nummodes < MAX_MODE_LIST))
  622. {
  623. devmode.dmFields = DM_BITSPERPEL |
  624. DM_PELSWIDTH |
  625. DM_PELSHEIGHT;
  626. if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) ==
  627. DISP_CHANGE_SUCCESSFUL)
  628. {
  629. modelist[nummodes].type = MS_FULLDIB;
  630. modelist[nummodes].width = devmode.dmPelsWidth;
  631. modelist[nummodes].height = devmode.dmPelsHeight;
  632. modelist[nummodes].modenum = 0;
  633. modelist[nummodes].mode13 = 0;
  634. modelist[nummodes].stretched = 0;
  635. modelist[nummodes].halfscreen = 0;
  636. modelist[nummodes].dib = 1;
  637. modelist[nummodes].fullscreen = 1;
  638. modelist[nummodes].bpp = devmode.dmBitsPerPel;
  639. sprintf (modelist[nummodes].modedesc, "%dx%d",
  640. devmode.dmPelsWidth, devmode.dmPelsHeight);
  641. // if the width is more than twice the height, reduce it by half because this
  642. // is probably a dual-screen monitor
  643. if (!COM_CheckParm("-noadjustaspect"))
  644. {
  645. if (modelist[nummodes].width > (modelist[nummodes].height << 1))
  646. {
  647. modelist[nummodes].width >>= 1;
  648. modelist[nummodes].halfscreen = 1;
  649. sprintf (modelist[nummodes].modedesc, "%dx%d",
  650. modelist[nummodes].width,
  651. modelist[nummodes].height);
  652. }
  653. }
  654. for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
  655. {
  656. if ((modelist[nummodes].width == modelist[i].width) &&
  657. (modelist[nummodes].height == modelist[i].height))
  658. {
  659. existingmode = 1;
  660. break;
  661. }
  662. }
  663. if (!existingmode)
  664. {
  665. if (modelist[nummodes].width < lowestres)
  666. lowestres = modelist[nummodes].width;
  667. nummodes++;
  668. }
  669. }
  670. }
  671. modenum++;
  672. } while (stat);
  673. // see if any of them were actually settable; if so, this is our mode list,
  674. // else enumerate all modes; our mode list is whichever ones are settable
  675. // with > 8 bpp
  676. if (nummodes == originalnummodes)
  677. {
  678. modenum = 0;
  679. lowestres = 99999;
  680. Con_SafePrintf ("No 8-bpp fullscreen DIB modes found\n");
  681. do
  682. {
  683. stat = EnumDisplaySettings (NULL, modenum, &devmode);
  684. if ((((devmode.dmPelsWidth <= MAXWIDTH) &&
  685. (devmode.dmPelsHeight <= MAXHEIGHT)) ||
  686. (!COM_CheckParm("-noadjustaspect") &&
  687. (devmode.dmPelsWidth <= (MAXWIDTH*2)) &&
  688. (devmode.dmPelsWidth > (devmode.dmPelsHeight*2)))) &&
  689. (nummodes < MAX_MODE_LIST) &&
  690. (devmode.dmBitsPerPel > 8))
  691. {
  692. devmode.dmFields = DM_BITSPERPEL |
  693. DM_PELSWIDTH |
  694. DM_PELSHEIGHT;
  695. if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) ==
  696. DISP_CHANGE_SUCCESSFUL)
  697. {
  698. modelist[nummodes].type = MS_FULLDIB;
  699. modelist[nummodes].width = devmode.dmPelsWidth;
  700. modelist[nummodes].height = devmode.dmPelsHeight;
  701. modelist[nummodes].modenum = 0;
  702. modelist[nummodes].mode13 = 0;
  703. modelist[nummodes].stretched = 0;
  704. modelist[nummodes].halfscreen = 0;
  705. modelist[nummodes].dib = 1;
  706. modelist[nummodes].fullscreen = 1;
  707. modelist[nummodes].bpp = devmode.dmBitsPerPel;
  708. sprintf (modelist[nummodes].modedesc, "%dx%d",
  709. devmode.dmPelsWidth, devmode.dmPelsHeight);
  710. // if the width is more than twice the height, reduce it by half because this
  711. // is probably a dual-screen monitor
  712. if (!COM_CheckParm("-noadjustaspect"))
  713. {
  714. if (modelist[nummodes].width > (modelist[nummodes].height*2))
  715. {
  716. modelist[nummodes].width >>= 1;
  717. modelist[nummodes].halfscreen = 1;
  718. sprintf (modelist[nummodes].modedesc, "%dx%d",
  719. modelist[nummodes].width,
  720. modelist[nummodes].height);
  721. }
  722. }
  723. for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
  724. {
  725. if ((modelist[nummodes].width == modelist[i].width) &&
  726. (modelist[nummodes].height == modelist[i].height))
  727. {
  728. // pick the lowest available bpp
  729. if (modelist[nummodes].bpp < modelist[i].bpp)
  730. modelist[i] = modelist[nummodes];
  731. existingmode = 1;
  732. break;
  733. }
  734. }
  735. if (!existingmode)
  736. {
  737. if (modelist[nummodes].width < lowestres)
  738. lowestres = modelist[nummodes].width;
  739. nummodes++;
  740. }
  741. }
  742. }
  743. modenum++;
  744. } while (stat);
  745. }
  746. // see if there are any low-res modes that aren't being reported
  747. numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
  748. bpp = 8;
  749. done = 0;
  750. // first make sure the driver doesn't just answer yes to all tests
  751. devmode.dmBitsPerPel = 8;
  752. devmode.dmPelsWidth = 42;
  753. devmode.dmPelsHeight = 37;
  754. devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  755. if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) ==
  756. DISP_CHANGE_SUCCESSFUL)
  757. {
  758. done = 1;
  759. }
  760. while (!done)
  761. {
  762. for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
  763. {
  764. devmode.dmBitsPerPel = bpp;
  765. devmode.dmPelsWidth = lowresmodes[j].width;
  766. devmode.dmPelsHeight = lowresmodes[j].height;
  767. devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  768. if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) ==
  769. DISP_CHANGE_SUCCESSFUL)
  770. {
  771. modelist[nummodes].type = MS_FULLDIB;
  772. modelist[nummodes].width = devmode.dmPelsWidth;
  773. modelist[nummodes].height = devmode.dmPelsHeight;
  774. modelist[nummodes].modenum = 0;
  775. modelist[nummodes].mode13 = 0;
  776. modelist[nummodes].stretched = 0;
  777. modelist[nummodes].halfscreen = 0;
  778. modelist[nummodes].dib = 1;
  779. modelist[nummodes].fullscreen = 1;
  780. modelist[nummodes].bpp = devmode.dmBitsPerPel;
  781. sprintf (modelist[nummodes].modedesc, "%dx%d",
  782. devmode.dmPelsWidth, devmode.dmPelsHeight);
  783. // we only want the lowest-bpp version of each mode
  784. for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
  785. {
  786. if ((modelist[nummodes].width == modelist[i].width) &&
  787. (modelist[nummodes].height == modelist[i].height) &&
  788. (modelist[nummodes].bpp >= modelist[i].bpp))
  789. {
  790. existingmode = 1;
  791. break;
  792. }
  793. }
  794. if (!existingmode)
  795. {
  796. if (modelist[nummodes].width < lowestres)
  797. lowestres = modelist[nummodes].width;
  798. nummodes++;
  799. }
  800. }
  801. }
  802. switch (bpp)
  803. {
  804. case 8:
  805. bpp = 16;
  806. break;
  807. case 16:
  808. bpp = 32;
  809. break;
  810. case 32:
  811. done = 1;
  812. break;
  813. }
  814. }
  815. // now add the lowest stretch-by-2 pseudo-modes between 320-wide
  816. // (inclusive) and lowest real res (not inclusive)
  817. // don't bother if we have a real VGA mode 0x13 mode
  818. if (!is_mode0x13)
  819. {
  820. for (i=originalnummodes, cstretch=0 ; i<nummodes ; i++)
  821. {
  822. if (((modelist[i].width >> 1) < lowestres) &&
  823. ((modelist[i].width >> 1) >= 320))
  824. {
  825. lowestres = modelist[i].width >> 1;
  826. cstretch = 1;
  827. mstretch = i;
  828. }
  829. }
  830. if ((nummodes + cstretch) > MAX_MODE_LIST)
  831. cstretch = MAX_MODE_LIST - nummodes;
  832. if (cstretch > 0)
  833. {
  834. for (i=(nummodes-1) ; i>=originalnummodes ; i--)
  835. modelist[i+cstretch] = modelist[i];
  836. nummodes += cstretch;
  837. istretch = originalnummodes;
  838. modelist[istretch] = modelist[mstretch];
  839. modelist[istretch].width >>= 1;
  840. modelist[istretch].height >>= 1;
  841. modelist[istretch].stretched = 1;
  842. sprintf (modelist[istretch].modedesc, "%dx%d",
  843. modelist[istretch].width, modelist[istretch].height);
  844. }
  845. }
  846. if (nummodes != originalnummodes)
  847. vid_default = MODE_FULLSCREEN_DEFAULT;
  848. else
  849. Con_SafePrintf ("No fullscreen DIB modes found\n");
  850. }
  851. /*
  852. =================
  853. VID_NumModes
  854. =================
  855. */
  856. int VID_NumModes (void)
  857. {
  858. return nummodes;
  859. }
  860. /*
  861. =================
  862. VID_GetModePtr
  863. =================
  864. */
  865. vmode_t *VID_GetModePtr (int modenum)
  866. {
  867. if ((modenum >= 0) && (modenum < nummodes))
  868. return &modelist[modenum];
  869. else
  870. return &badmode;
  871. }
  872. /*
  873. =================
  874. VID_CheckModedescFixup
  875. =================
  876. */
  877. void VID_CheckModedescFixup (int mode)
  878. {
  879. int x, y, stretch;
  880. if (mode == MODE_SETTABLE_WINDOW)
  881. {
  882. modelist[mode].stretched = (int)vid_stretch_by_2.value;
  883. stretch = modelist[mode].stretched;
  884. if (vid_config_x.value < (320 << stretch))
  885. vid_config_x.value = 320 << stretch;
  886. if (vid_config_y.value < (200 << stretch))
  887. vid_config_y.value = 200 << stretch;
  888. x = (int)vid_config_x.value;
  889. y = (int)vid_config_y.value;
  890. sprintf (modelist[mode].modedesc, "%dx%d", x, y);
  891. modelist[mode].width = x;
  892. modelist[mode].height = y;
  893. }
  894. }
  895. /*
  896. =================
  897. VID_GetModeDescriptionMemCheck
  898. =================
  899. */
  900. char *VID_GetModeDescriptionMemCheck (int mode)
  901. {
  902. char *pinfo;
  903. vmode_t *pv;
  904. if ((mode < 0) || (mode >= nummodes))
  905. return NULL;
  906. VID_CheckModedescFixup (mode);
  907. pv = VID_GetModePtr (mode);
  908. pinfo = pv->modedesc;
  909. if (VID_CheckAdequateMem (pv->width, pv->height))
  910. {
  911. return pinfo;
  912. }
  913. else
  914. {
  915. return NULL;
  916. }
  917. }
  918. /*
  919. =================
  920. VID_GetModeDescription
  921. =================
  922. */
  923. char *VID_GetModeDescription (int mode)
  924. {
  925. char *pinfo;
  926. vmode_t *pv;
  927. if ((mode < 0) || (mode >= nummodes))
  928. return NULL;
  929. VID_CheckModedescFixup (mode);
  930. pv = VID_GetModePtr (mode);
  931. pinfo = pv->modedesc;
  932. return pinfo;
  933. }
  934. /*
  935. =================
  936. VID_GetModeDescription2
  937. Tacks on "windowed" or "fullscreen"
  938. =================
  939. */
  940. char *VID_GetModeDescription2 (int mode)
  941. {
  942. static char pinfo[40];
  943. vmode_t *pv;
  944. if ((mode < 0) || (mode >= nummodes))
  945. return NULL;
  946. VID_CheckModedescFixup (mode);
  947. pv = VID_GetModePtr (mode);
  948. if (modelist[mode].type == MS_FULLSCREEN)
  949. {
  950. sprintf(pinfo,"%s fullscreen", pv->modedesc);
  951. }
  952. else if (modelist[mode].type == MS_FULLDIB)
  953. {
  954. sprintf(pinfo,"%s fullscreen", pv->modedesc);
  955. }
  956. else
  957. {
  958. sprintf(pinfo, "%s windowed", pv->modedesc);
  959. }
  960. return pinfo;
  961. }
  962. // KJB: Added this to return the mode driver name in description for console
  963. char *VID_GetExtModeDescription (int mode)
  964. {
  965. static char pinfo[40];
  966. vmode_t *pv;
  967. if ((mode < 0) || (mode >= nummodes))
  968. return NULL;
  969. VID_CheckModedescFixup (mode);
  970. pv = VID_GetModePtr (mode);
  971. if (modelist[mode].type == MS_FULLSCREEN)
  972. {
  973. sprintf(pinfo,"%s fullscreen %s",pv->modedesc,
  974. MGL_modeDriverName(pv->modenum));
  975. }
  976. else if (modelist[mode].type == MS_FULLDIB)
  977. {
  978. sprintf(pinfo,"%s fullscreen DIB", pv->modedesc);
  979. }
  980. else
  981. {
  982. sprintf(pinfo, "%s windowed", pv->modedesc);
  983. }
  984. return pinfo;
  985. }
  986. void DestroyDIBWindow (void)
  987. {
  988. if (modestate == MS_WINDOWED)
  989. {
  990. // destroy the associated MGL DC's; the window gets reused
  991. if (windc)
  992. MGL_destroyDC(windc);
  993. if (dibdc)
  994. MGL_destroyDC(dibdc);
  995. windc = dibdc = NULL;
  996. }
  997. }
  998. void DestroyFullscreenWindow (void)
  999. {
  1000. if (modestate == MS_FULLSCREEN)
  1001. {
  1002. // destroy the existing fullscreen mode and DC's
  1003. if (mgldc)
  1004. MGL_destroyDC (mgldc);
  1005. if (memdc)
  1006. MGL_destroyDC (memdc);
  1007. mgldc = memdc = NULL;
  1008. }
  1009. }
  1010. void DestroyFullDIBWindow (void)
  1011. {
  1012. if (modestate == MS_FULLDIB)
  1013. {
  1014. ChangeDisplaySettings (NULL, CDS_FULLSCREEN);
  1015. // Destroy the fullscreen DIB window and associated MGL DC's
  1016. if (windc)
  1017. MGL_destroyDC(windc);
  1018. if (dibdc)
  1019. MGL_destroyDC(dibdc);
  1020. windc = dibdc = NULL;
  1021. }
  1022. }
  1023. qboolean VID_SetWindowedMode (int modenum)
  1024. {
  1025. HDC hdc;
  1026. pixel_format_t pf;
  1027. qboolean stretched;
  1028. int lastmodestate;
  1029. LONG wlong;
  1030. if (!windowed_mode_set)
  1031. {
  1032. if (COM_CheckParm ("-resetwinpos"))
  1033. {
  1034. Cvar_SetValue ("vid_window_x", 0.0);
  1035. Cvar_SetValue ("vid_window_y", 0.0);
  1036. }
  1037. windowed_mode_set;
  1038. }
  1039. VID_CheckModedescFixup (modenum);
  1040. DDActive = 0;
  1041. lastmodestate = modestate;
  1042. DestroyFullscreenWindow ();
  1043. DestroyFullDIBWindow ();
  1044. if (windc)
  1045. MGL_destroyDC(windc);
  1046. if (dibdc)
  1047. MGL_destroyDC(dibdc);
  1048. windc = dibdc = NULL;
  1049. // KJB: Signal to the MGL that we are going back to windowed mode
  1050. if (!MGL_changeDisplayMode(grWINDOWED))
  1051. initFatalError();
  1052. WindowRect.top = WindowRect.left = 0;
  1053. WindowRect.right = modelist[modenum].width;
  1054. WindowRect.bottom = modelist[modenum].height;
  1055. stretched = modelist[modenum].stretched;
  1056. DIBWidth = modelist[modenum].width;
  1057. DIBHeight = modelist[modenum].height;
  1058. if (stretched)
  1059. {
  1060. DIBWidth >>= 1;
  1061. DIBHeight >>= 1;
  1062. }
  1063. WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU |
  1064. WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CLIPSIBLINGS |
  1065. WS_CLIPCHILDREN;
  1066. ExWindowStyle = 0;
  1067. AdjustWindowRectEx(&WindowRect, WindowStyle, FALSE, 0);
  1068. // the first time we're called to set the mode, create the window we'll use
  1069. // for the rest of the session
  1070. if (!vid_mode_set)
  1071. {
  1072. mainwindow = CreateWindowEx (
  1073. ExWindowStyle,
  1074. "WinQuake",
  1075. "WinQuake",
  1076. WindowStyle,
  1077. 0, 0,
  1078. WindowRect.right - WindowRect.left,
  1079. WindowRect.bottom - WindowRect.top,
  1080. NULL,
  1081. NULL,
  1082. global_hInstance,
  1083. NULL);
  1084. if (!mainwindow)
  1085. Sys_Error ("Couldn't create DIB window");
  1086. // tell MGL to use this window for fullscreen modes
  1087. MGL_registerFullScreenWindow (mainwindow);
  1088. vid_mode_set = true;
  1089. }
  1090. else
  1091. {
  1092. SetWindowLong(mainwindow, GWL_STYLE, WindowStyle | WS_VISIBLE);
  1093. SetWindowLong(mainwindow, GWL_EXSTYLE, ExWindowStyle);
  1094. }
  1095. if (!SetWindowPos (mainwindow,
  1096. NULL,
  1097. 0, 0,
  1098. WindowRect.right - WindowRect.left,
  1099. WindowRect.bottom - WindowRect.top,
  1100. SWP_NOCOPYBITS | SWP_NOZORDER |
  1101. SWP_HIDEWINDOW))
  1102. {
  1103. Sys_Error ("Couldn't resize DIB window");
  1104. }
  1105. if (hide_window)
  1106. return true;
  1107. // position and show the DIB window
  1108. VID_CheckWindowXY ();
  1109. SetWindowPos (mainwindow, NULL, (int)vid_window_x.value,
  1110. (int)vid_window_y.value, 0, 0,
  1111. SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
  1112. if (force_minimized)
  1113. ShowWindow (mainwindow, SW_MINIMIZE);
  1114. else
  1115. ShowWindow (mainwindow, SW_SHOWDEFAULT);
  1116. UpdateWindow (mainwindow);
  1117. modestate = MS_WINDOWED;
  1118. vid_fulldib_on_focus_mode = 0;
  1119. // because we have set the background brush for the window to NULL
  1120. // (to avoid flickering when re-sizing the window on the desktop),
  1121. // we clear the window to black when created, otherwise it will be
  1122. // empty while Quake starts up.
  1123. hdc = GetDC(mainwindow);
  1124. PatBlt(hdc,0,0,WindowRect.right,WindowRect.bottom,BLACKNESS);
  1125. ReleaseDC(mainwindow, hdc);
  1126. /* Create the MGL window DC and the MGL memory DC */
  1127. if ((windc = MGL_createWindowedDC(mainwindow)) == NULL)
  1128. MGL_fatalError("Unable to create Windowed DC!");
  1129. if ((dibdc = MGL_createMemoryDC(DIBWidth,DIBHeight,8,&pf)) == NULL)
  1130. MGL_fatalError("Unable to create Memory DC!");
  1131. MGL_makeCurrentDC(dibdc);
  1132. vid.buffer = vid.conbuffer = vid.direct = dibdc->surface;
  1133. vid.rowbytes = vid.conrowbytes = dibdc->mi.bytesPerLine;
  1134. vid.numpages = 1;
  1135. vid.maxwarpwidth = WARP_WIDTH;
  1136. vid.maxwarpheight = WARP_HEIGHT;
  1137. vid.height = vid.conheight = DIBHeight;
  1138. vid.width = vid.conwidth = DIBWidth;
  1139. vid.aspect = ((float)vid.height / (float)vid.width) *
  1140. (320.0 / 240.0);
  1141. vid_stretched = stretched;
  1142. SendMessage (mainwindow, WM_SETICON, (WPARAM)TRUE, (LPARAM)hIcon);
  1143. SendMessage (mainwindow, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon);
  1144. return true;
  1145. }
  1146. qboolean VID_SetFullscreenMode (int modenum)
  1147. {
  1148. DDActive = 1;
  1149. DestroyDIBWindow ();
  1150. DestroyFullDIBWindow ();
  1151. mode = modelist[modenum].modenum;
  1152. // Destroy old DC's, resetting back to fullscreen mode
  1153. if (mgldc)
  1154. MGL_destroyDC (mgldc);
  1155. if (memdc)
  1156. MGL_destroyDC (memdc);
  1157. mgldc = memdc = NULL;
  1158. if ((mgldc = createDisplayDC (modelist[modenum].stretched ||
  1159. (int)vid_nopageflip.value)) == NULL)
  1160. {
  1161. return false;
  1162. }
  1163. modestate = MS_FULLSCREEN;
  1164. vid_fulldib_on_focus_mode = 0;
  1165. vid.buffer = vid.conbuffer = vid.direct = NULL;
  1166. vid.maxwarpwidth = WARP_WIDTH;
  1167. vid.maxwarpheight = WARP_HEIGHT;
  1168. DIBHeight = vid.height = vid.conheight = modelist[modenum].height;
  1169. DIBWidth = vid.width = vid.conwidth = modelist[modenum].width;
  1170. vid.aspect = ((float)vid.height / (float)vid.width) *
  1171. (320.0 / 240.0);
  1172. vid_stretched = modelist[modenum].stretched;
  1173. // needed because we're not getting WM_MOVE messages fullscreen on NT
  1174. window_x = 0;
  1175. window_y = 0;
  1176. // set the large icon, so the Quake icon will show up in the taskbar
  1177. SendMessage (mainwindow, WM_SETICON, (WPARAM)1, (LPARAM)hIcon);
  1178. SendMessage (mainwindow, WM_SETICON, (WPARAM)0, (LPARAM)hIcon);
  1179. // shouldn't be needed, but Kendall needs to let us get the activation
  1180. // message for this not to be needed on NT
  1181. AppActivate (true, false);
  1182. return true;
  1183. }
  1184. qboolean VID_SetFullDIBMode (int modenum)
  1185. {
  1186. HDC hdc;
  1187. pixel_format_t pf;
  1188. int lastmodestate;
  1189. DDActive = 0;
  1190. DestroyFullscreenWindow ();
  1191. DestroyDIBWindow ();
  1192. if (windc)
  1193. MGL_destroyDC(windc);
  1194. if (dibdc)
  1195. MGL_destroyDC(dibdc);
  1196. windc = dibdc = NULL;
  1197. // KJB: Signal to the MGL that we are going back to windowed mode
  1198. if (!MGL_changeDisplayMode(grWINDOWED))
  1199. initFatalError();
  1200. gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  1201. gdevmode.dmBitsPerPel = modelist[modenum].bpp;
  1202. gdevmode.dmPelsWidth = modelist[modenum].width << modelist[modenum].stretched <<
  1203. modelist[modenum].halfscreen;
  1204. gdevmode.dmPelsHeight = modelist[modenum].height << modelist[modenum].stretched;
  1205. gdevmode.dmSize = sizeof (gdevmode);
  1206. if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  1207. Sys_Error ("Couldn't set fullscreen DIB mode");
  1208. lastmodestate = modestate;
  1209. modestate = MS_FULLDIB;
  1210. vid_fulldib_on_focus_mode = modenum;
  1211. WindowRect.top = WindowRect.left = 0;
  1212. hdc = GetDC(NULL);
  1213. WindowRect.right = modelist[modenum].width << modelist[modenum].stretched;
  1214. WindowRect.bottom = modelist[modenum].height << modelist[modenum].stretched;
  1215. ReleaseDC(NULL,hdc);
  1216. DIBWidth = modelist[modenum].width;
  1217. DIBHeight = modelist[modenum].height;
  1218. WindowStyle = WS_POPUP | WS_SYSMENU | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  1219. ExWindowStyle = 0;
  1220. AdjustWindowRectEx(&WindowRect, WindowStyle, FALSE, 0);
  1221. SetWindowLong(mainwindow, GWL_STYLE, WindowStyle | WS_VISIBLE);
  1222. SetWindowLong(mainwindow, GWL_EXSTYLE, ExWindowStyle);
  1223. if (!SetWindowPos (mainwindow,
  1224. NULL,
  1225. 0, 0,
  1226. WindowRect.right - WindowRect.left,
  1227. WindowRect.bottom - WindowRect.top,
  1228. SWP_NOCOPYBITS | SWP_NOZORDER))
  1229. {
  1230. Sys_Error ("Couldn't resize DIB window");
  1231. }
  1232. // position and show the DIB window
  1233. SetWindowPos (mainwindow, HWND_TOPMOST, 0, 0, 0, 0,
  1234. SWP_NOSIZE | SWP_SHOWWINDOW | SWP_DRAWFRAME);
  1235. ShowWindow (mainwindow, SW_SHOWDEFAULT);
  1236. UpdateWindow (mainwindow);
  1237. // Because we have set the background brush for the window to NULL
  1238. // (to avoid flickering when re-sizing the window on the desktop), we
  1239. // clear the window to black when created, otherwise it will be
  1240. // empty while Quake starts up.
  1241. hdc = GetDC(mainwindow);
  1242. PatBlt(hdc,0,0,WindowRect.right,WindowRect.bottom,BLACKNESS);
  1243. ReleaseDC(mainwindow, hdc);
  1244. /* Create the MGL window DC and the MGL memory DC */
  1245. if ((windc = MGL_createWindowedDC(mainwindow)) == NULL)
  1246. MGL_fatalError("Unable to create Fullscreen DIB DC!");
  1247. if ((dibdc = MGL_createMemoryDC(DIBWidth,DIBHeight,8,&pf)) == NULL)
  1248. MGL_fatalError("Unable to create Memory DC!");
  1249. MGL_makeCurrentDC(dibdc);
  1250. vid.buffer = vid.conbuffer = vid.direct = dibdc->surface;
  1251. vid.rowbytes = vid.conrowbytes = dibdc->mi.bytesPerLine;
  1252. vid.numpages = 1;
  1253. vid.maxwarpwidth = WARP_WIDTH;
  1254. vid.maxwarpheight = WARP_HEIGHT;
  1255. vid.height = vid.conheight = DIBHeight;
  1256. vid.width = vid.conwidth = DIBWidth;
  1257. vid.aspect = ((float)vid.height / (float)vid.width) *
  1258. (320.0 / 240.0);
  1259. vid_stretched = modelist[modenum].stretched;
  1260. // needed because we're not getting WM_MOVE messages fullscreen on NT
  1261. window_x = 0;
  1262. window_y = 0;
  1263. return true;
  1264. }
  1265. void VID_RestoreOldMode (int original_mode)
  1266. {
  1267. static qboolean inerror = false;
  1268. if (inerror)
  1269. return;
  1270. in_mode_set = false;
  1271. inerror = true;
  1272. // make sure mode set happens (video mode changes)
  1273. vid_modenum = original_mode - 1;
  1274. if (!VID_SetMode (original_mode, vid_curpal))
  1275. {
  1276. vid_modenum = MODE_WINDOWED - 1;
  1277. if (!VID_SetMode (windowed_default, vid_curpal))
  1278. Sys_Error ("Can't set any video mode");
  1279. }
  1280. inerror = false;
  1281. }
  1282. void VID_SetDefaultMode (void)
  1283. {
  1284. if (vid_initialized)
  1285. VID_SetMode (0, vid_curpal);
  1286. IN_DeactivateMouse ();
  1287. }
  1288. int VID_SetMode (int modenum, unsigned char *palette)
  1289. {
  1290. int original_mode, temp, dummy;
  1291. qboolean stat;
  1292. MSG msg;
  1293. HDC hdc;
  1294. while ((modenum >= nummodes) || (modenum < 0))
  1295. {
  1296. if (vid_modenum == NO_MODE)
  1297. {
  1298. if (modenum == vid_default)
  1299. {
  1300. modenum = windowed_default;
  1301. }
  1302. else
  1303. {
  1304. modenum = vid_default;
  1305. }
  1306. Cvar_SetValue ("vid_mode", (float)modenum);
  1307. }
  1308. else
  1309. {
  1310. Cvar_SetValue ("vid_mode", (float)vid_modenum);
  1311. return 0;
  1312. }
  1313. }
  1314. if (!force_mode_set && (modenum == vid_modenum))
  1315. return true;
  1316. // so Con_Printfs don't mess us up by forcing vid and snd updates
  1317. temp = scr_disabled_for_loading;
  1318. scr_disabled_for_loading = true;
  1319. in_mode_set = true;
  1320. CDAudio_Pause ();
  1321. S_ClearBuffer ();
  1322. if (vid_modenum == NO_MODE)
  1323. original_mode = windowed_default;
  1324. else
  1325. original_mode = vid_modenum;
  1326. // Set either the fullscreen or windowed mode
  1327. if (modelist[modenum].type == MS_WINDOWED)
  1328. {
  1329. if (_windowed_mouse.value && key_dest == key_game)
  1330. {
  1331. stat = VID_SetWindowedMode(modenum);
  1332. IN_ActivateMouse ();
  1333. IN_HideMouse ();
  1334. }
  1335. else
  1336. {
  1337. IN_DeactivateMouse ();
  1338. IN_ShowMouse ();
  1339. stat = VID_SetWindowedMode(modenum);
  1340. }
  1341. }
  1342. else if (modelist[modenum].type == MS_FULLDIB)
  1343. {
  1344. stat = VID_SetFullDIBMode(modenum);
  1345. IN_ActivateMouse ();
  1346. IN_HideMouse ();
  1347. }
  1348. else
  1349. {
  1350. stat = VID_SetFullscreenMode(modenum);
  1351. IN_ActivateMouse ();
  1352. IN_HideMouse ();
  1353. }
  1354. window_width = vid.width << vid_stretched;
  1355. window_height = vid.height << vid_stretched;
  1356. VID_UpdateWindowStatus ();
  1357. CDAudio_Resume ();
  1358. scr_disabled_for_loading = temp;
  1359. if (!stat)
  1360. {
  1361. VID_RestoreOldMode (original_mode);
  1362. return false;
  1363. }
  1364. if (hide_window)
  1365. return true;
  1366. // now we try to make sure we get the focus on the mode switch, because
  1367. // sometimes in some systems we don't. We grab the foreground, then
  1368. // finish setting up, pump all our messages, and sleep for a little while
  1369. // to let messages finish bouncing around the system, then we put
  1370. // ourselves at the top of the z order, then grab the foreground again,
  1371. // Who knows if it helps, but it probably doesn't hurt
  1372. if (!force_minimized)
  1373. SetForegroundWindow (mainwindow);
  1374. hdc = GetDC(NULL);
  1375. if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
  1376. vid_palettized = true;
  1377. else
  1378. vid_palettized = false;
  1379. VID_SetPalette (palette);
  1380. ReleaseDC(NULL,hdc);
  1381. vid_modenum = modenum;
  1382. Cvar_SetValue ("vid_mode", (float)vid_modenum);
  1383. if (!VID_AllocBuffers (vid.width, vid.height))
  1384. {
  1385. // couldn't get memory for this mode; try to fall back to previous mode
  1386. VID_RestoreOldMode (original_mode);
  1387. return false;
  1388. }
  1389. D_InitCaches (vid_surfcache, vid_surfcachesize);
  1390. while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  1391. {
  1392. TranslateMessage (&msg);
  1393. DispatchMessage (&msg);
  1394. }
  1395. Sleep (100);
  1396. if (!force_minimized)
  1397. {
  1398. SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0,
  1399. SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW |
  1400. SWP_NOCOPYBITS);
  1401. SetForegroundWindow (mainwindow);
  1402. }
  1403. // fix the leftover Alt from any Alt-Tab or the like that switched us away
  1404. ClearAllStates ();
  1405. if (!msg_suppress_1)
  1406. Con_SafePrintf ("Video mode %s initialized\n", VID_GetModeDescription (vid_modenum));
  1407. VID_SetPalette (palette);
  1408. in_mode_set = false;
  1409. vid.recalc_refdef = 1;
  1410. return true;
  1411. }
  1412. void VID_LockBuffer (void)
  1413. {
  1414. if (dibdc)
  1415. return;
  1416. lockcount++;
  1417. if (lockcount > 1)
  1418. return;
  1419. MGL_beginDirectAccess();
  1420. if (memdc)
  1421. {
  1422. // Update surface pointer for linear access modes
  1423. vid.buffer = vid.conbuffer = vid.direct = memdc->surface;
  1424. vid.rowbytes = vid.conrowbytes = memdc->mi.bytesPerLine;
  1425. }
  1426. else if (mgldc)
  1427. {
  1428. // Update surface pointer for linear access modes
  1429. vid.buffer = vid.conbuffer = vid.direct = mgldc->surface;
  1430. vid.rowbytes = vid.conrowbytes = mgldc->mi.bytesPerLine;
  1431. }
  1432. if (r_dowarp)
  1433. d_viewbuffer = r_warpbuffer;
  1434. else
  1435. d_viewbuffer = (void *)(byte *)vid.buffer;
  1436. if (r_dowarp)
  1437. screenwidth = WARP_WIDTH;
  1438. else
  1439. screenwidth = vid.rowbytes;
  1440. if (lcd_x.value)
  1441. screenwidth <<= 1;
  1442. }
  1443. void VID_UnlockBuffer (void)
  1444. {
  1445. if (dibdc)
  1446. return;
  1447. lockcount--;
  1448. if (lockcount > 0)
  1449. return;
  1450. if (lockcount < 0)
  1451. Sys_Error ("Unbalanced unlock");
  1452. MGL_endDirectAccess();
  1453. // to turn up any unlocked accesses
  1454. vid.buffer = vid.conbuffer = vid.direct = d_viewbuffer = NULL;
  1455. }
  1456. int VID_ForceUnlockedAndReturnState (void)
  1457. {
  1458. int lk;
  1459. if (!lockcount)
  1460. return 0;
  1461. lk = lockcount;
  1462. if (dibdc)
  1463. {
  1464. lockcount = 0;
  1465. }
  1466. else
  1467. {
  1468. lockcount = 1;
  1469. VID_UnlockBuffer ();
  1470. }
  1471. return lk;
  1472. }
  1473. void VID_ForceLockState (int lk)
  1474. {
  1475. if (!dibdc && lk)
  1476. {
  1477. lockcount = 0;
  1478. VID_LockBuffer ();
  1479. }
  1480. lockcount = lk;
  1481. }
  1482. void VID_SetPalette (unsigned char *palette)
  1483. {
  1484. INT i;
  1485. palette_t pal[256];
  1486. HDC hdc;
  1487. if (!Minimized)
  1488. {
  1489. palette_changed = true;
  1490. // make sure we have the static colors if we're the active app
  1491. hdc = GetDC(NULL);
  1492. if (vid_palettized && ActiveApp)
  1493. {
  1494. if (GetSystemPaletteUse(hdc) == SYSPAL_STATIC)
  1495. {
  1496. // switch to SYSPAL_NOSTATIC and remap the colors
  1497. SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
  1498. syscolchg = true;
  1499. pal_is_nostatic = true;
  1500. }
  1501. }
  1502. ReleaseDC(NULL,hdc);
  1503. // Translate the palette values to an MGL palette array and
  1504. // set the values.
  1505. for (i = 0; i < 256; i++)
  1506. {
  1507. pal[i].red = palette[i*3];
  1508. pal[i].green = palette[i*3+1];
  1509. pal[i].blue = palette[i*3+2];
  1510. }
  1511. if (DDActive)
  1512. {
  1513. if (!mgldc)
  1514. return;
  1515. MGL_setPalette(mgldc,pal,256,0);
  1516. MGL_realizePalette(mgldc,256,0,false);
  1517. if (memdc)
  1518. MGL_setPalette(memdc,pal,256,0);
  1519. }
  1520. else
  1521. {
  1522. if (!windc)
  1523. return;
  1524. MGL_setPalette(windc,pal,256,0);
  1525. MGL_realizePalette(windc,256,0,false);
  1526. if (dibdc)
  1527. {
  1528. MGL_setPalette(dibdc,pal,256,0);
  1529. MGL_realizePalette(dibdc,256,0,false);
  1530. }
  1531. }
  1532. }
  1533. memcpy (vid_curpal, palette, sizeof(vid_curpal));
  1534. if (syscolchg)
  1535. {
  1536. PostMessage (HWND_BROADCAST, WM_SYSCOLORCHANGE, (WPARAM)0, (LPARAM)0);
  1537. syscolchg = false;
  1538. }
  1539. }
  1540. void VID_ShiftPalette (unsigned char *palette)
  1541. {
  1542. VID_SetPalette (palette);
  1543. }
  1544. /*
  1545. =================
  1546. VID_DescribeCurrentMode_f
  1547. =================
  1548. */
  1549. void VID_DescribeCurrentMode_f (void)
  1550. {
  1551. Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
  1552. }
  1553. /*
  1554. =================
  1555. VID_NumModes_f
  1556. =================
  1557. */
  1558. void VID_NumModes_f (void)
  1559. {
  1560. if (nummodes == 1)
  1561. Con_Printf ("%d video mode is available\n", nummodes);
  1562. else
  1563. Con_Printf ("%d video modes are available\n", nummodes);
  1564. }
  1565. /*
  1566. =================
  1567. VID_DescribeMode_f
  1568. =================
  1569. */
  1570. void VID_DescribeMode_f (void)
  1571. {
  1572. int modenum;
  1573. modenum = Q_atoi (Cmd_Argv(1));
  1574. Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
  1575. }
  1576. /*
  1577. =================
  1578. VID_DescribeModes_f
  1579. =================
  1580. */
  1581. void VID_DescribeModes_f (void)
  1582. {
  1583. int i, lnummodes;
  1584. char *pinfo;
  1585. qboolean na;
  1586. vmode_t *pv;
  1587. na = false;
  1588. lnummodes = VID_NumModes ();
  1589. for (i=0 ; i<lnummodes ; i++)
  1590. {
  1591. pv = VID_GetModePtr (i);
  1592. pinfo = VID_GetExtModeDescription (i);
  1593. if (VID_CheckAdequateMem (pv->width, pv->height))
  1594. {
  1595. Con_Printf ("%2d: %s\n", i, pinfo);
  1596. }
  1597. else
  1598. {
  1599. Con_Printf ("**: %s\n", pinfo);
  1600. na = true;
  1601. }
  1602. }
  1603. if (na)
  1604. {
  1605. Con_Printf ("\n[**: not enough system RAM for mode]\n");
  1606. }
  1607. }
  1608. /*
  1609. =================
  1610. VID_TestMode_f
  1611. =================
  1612. */
  1613. void VID_TestMode_f (void)
  1614. {
  1615. int modenum;
  1616. double testduration;
  1617. if (!vid_testingmode)
  1618. {
  1619. modenum = Q_atoi (Cmd_Argv(1));
  1620. if (VID_SetMode (modenum, vid_curpal))
  1621. {
  1622. vid_testingmode = 1;
  1623. testduration = Q_atof (Cmd_Argv(2));
  1624. if (testduration == 0)
  1625. testduration = 5.0;
  1626. vid_testendtime = realtime + testduration;
  1627. }
  1628. }
  1629. }
  1630. /*
  1631. =================
  1632. VID_Windowed_f
  1633. =================
  1634. */
  1635. void VID_Windowed_f (void)
  1636. {
  1637. VID_SetMode ((int)vid_windowed_mode.value, vid_curpal);
  1638. }
  1639. /*
  1640. =================
  1641. VID_Fullscreen_f
  1642. =================
  1643. */
  1644. void VID_Fullscreen_f (void)
  1645. {
  1646. VID_SetMode ((int)vid_fullscreen_mode.value, vid_curpal);
  1647. }
  1648. /*
  1649. =================
  1650. VID_Minimize_f
  1651. =================
  1652. */
  1653. void VID_Minimize_f (void)
  1654. {
  1655. // we only support minimizing windows; if you're fullscreen,
  1656. // switch to windowed first
  1657. if (modestate == MS_WINDOWED)
  1658. ShowWindow (mainwindow, SW_MINIMIZE);
  1659. }
  1660. /*
  1661. =================
  1662. VID_ForceMode_f
  1663. =================
  1664. */
  1665. void VID_ForceMode_f (void)
  1666. {
  1667. int modenum;
  1668. double testduration;
  1669. if (!vid_testingmode)
  1670. {
  1671. modenum = Q_atoi (Cmd_Argv(1));
  1672. force_mode_set = 1;
  1673. VID_SetMode (modenum, vid_curpal);
  1674. force_mode_set = 0;
  1675. }
  1676. }
  1677. void VID_Init (unsigned char *palette)
  1678. {
  1679. int i, bestmatch, bestmatchmetric, t, dr, dg, db;
  1680. int basenummodes;
  1681. byte *ptmp;
  1682. Cvar_RegisterVariable (&vid_mode);
  1683. Cvar_RegisterVariable (&vid_wait);
  1684. Cvar_RegisterVariable (&vid_nopageflip);
  1685. Cvar_RegisterVariable (&_vid_wait_override);
  1686. Cvar_RegisterVariable (&_vid_default_mode);
  1687. Cvar_RegisterVariable (&_vid_default_mode_win);
  1688. Cvar_RegisterVariable (&vid_config_x);
  1689. Cvar_RegisterVariable (&vid_config_y);
  1690. Cvar_RegisterVariable (&vid_stretch_by_2);
  1691. Cvar_RegisterVariable (&_windowed_mouse);
  1692. Cvar_RegisterVariable (&vid_fullscreen_mode);
  1693. Cvar_RegisterVariable (&vid_windowed_mode);
  1694. Cvar_RegisterVariable (&block_switch);
  1695. Cvar_RegisterVariable (&vid_window_x);
  1696. Cvar_RegisterVariable (&vid_window_y);
  1697. Cmd_AddCommand ("vid_testmode", VID_TestMode_f);
  1698. Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
  1699. Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
  1700. Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
  1701. Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
  1702. Cmd_AddCommand ("vid_forcemode", VID_ForceMode_f);
  1703. Cmd_AddCommand ("vid_windowed", VID_Windowed_f);
  1704. Cmd_AddCommand ("vid_fullscreen", VID_Fullscreen_f);
  1705. Cmd_AddCommand ("vid_minimize", VID_Minimize_f);
  1706. if (COM_CheckParm ("-dibonly"))
  1707. dibonly = true;
  1708. VID_InitMGLDIB (global_hInstance);
  1709. basenummodes = nummodes;
  1710. if (!dibonly)
  1711. VID_InitMGLFull (global_hInstance);
  1712. // if there are no non-windowed modes, or only windowed and mode 0x13, then use
  1713. // fullscreen DIBs as well
  1714. if (((nummodes == basenummodes) ||
  1715. ((nummodes == (basenummodes + 1)) && is_mode0x13)) &&
  1716. !COM_CheckParm ("-nofulldib"))
  1717. {
  1718. VID_InitFullDIB (global_hInstance);
  1719. }
  1720. vid.maxwarpwidth = WARP_WIDTH;
  1721. vid.maxwarpheight = WARP_HEIGHT;
  1722. vid.colormap = host_colormap;
  1723. vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
  1724. vid_testingmode = 0;
  1725. // GDI doesn't let us remap palette index 0, so we'll remap color
  1726. // mappings from that black to another one
  1727. bestmatchmetric = 256*256*3;
  1728. for (i=1 ; i<256 ; i++)
  1729. {
  1730. dr = palette[0] - palette[i*3];
  1731. dg = palette[1] - palette[i*3+1];
  1732. db = palette[2] - palette[i*3+2];
  1733. t = (dr * dr) + (dg * dg) + (db * db);
  1734. if (t < bestmatchmetric)
  1735. {
  1736. bestmatchmetric = t;
  1737. bestmatch = i;
  1738. if (t == 0)
  1739. break;
  1740. }
  1741. }
  1742. for (i=0, ptmp = vid.colormap ; i<(1<<(VID_CBITS+8)) ; i++, ptmp++)
  1743. {
  1744. if (*ptmp == 0)
  1745. *ptmp = bestmatch;
  1746. }
  1747. if (COM_CheckParm("-startwindowed"))
  1748. {
  1749. startwindowed = 1;
  1750. vid_default = windowed_default;
  1751. }
  1752. if (hwnd_dialog)
  1753. DestroyWindow (hwnd_dialog);
  1754. // sound initialization has to go here, preceded by a windowed mode set,
  1755. // so there's a window for DirectSound to work with but we're not yet
  1756. // fullscreen so the "hardware already in use" dialog is visible if it
  1757. // gets displayed
  1758. // keep the window minimized until we're ready for the first real mode set
  1759. hide_window = true;
  1760. VID_SetMode (MODE_WINDOWED, palette);
  1761. hide_window = false;
  1762. S_Init ();
  1763. vid_initialized = true;
  1764. force_mode_set = true;
  1765. VID_SetMode (vid_default, palette);
  1766. force_mode_set = false;
  1767. vid_realmode = vid_modenum;
  1768. VID_SetPalette (palette);
  1769. vid_menudrawfn = VID_MenuDraw;
  1770. vid_menukeyfn = VID_MenuKey;
  1771. strcpy (badmode.modedesc, "Bad mode");
  1772. }
  1773. void VID_Shutdown (void)
  1774. {
  1775. HDC hdc;
  1776. int dummy;
  1777. if (vid_initialized)
  1778. {
  1779. if (modestate == MS_FULLDIB)
  1780. ChangeDisplaySettings (NULL, CDS_FULLSCREEN);
  1781. PostMessage (HWND_BROADCAST, WM_PALETTECHANGED, (WPARAM)mainwindow, (LPARAM)0);
  1782. PostMessage (HWND_BROADCAST, WM_SYSCOLORCHANGE, (WPARAM)0, (LPARAM)0);
  1783. AppActivate(false, false);
  1784. DestroyDIBWindow ();
  1785. DestroyFullscreenWindow ();
  1786. DestroyFullDIBWindow ();
  1787. if (hwnd_dialog)
  1788. DestroyWindow (hwnd_dialog);
  1789. if (mainwindow)
  1790. DestroyWindow(mainwindow);
  1791. MGL_exit();
  1792. vid_testingmode = 0;
  1793. vid_initialized = 0;
  1794. }
  1795. }
  1796. /*
  1797. ================
  1798. FlipScreen
  1799. ================
  1800. */
  1801. void FlipScreen(vrect_t *rects)
  1802. {
  1803. HRESULT ddrval;
  1804. // Flip the surfaces
  1805. if (DDActive)
  1806. {
  1807. if (mgldc)
  1808. {
  1809. if (memdc)
  1810. {
  1811. while (rects)
  1812. {
  1813. if (vid_stretched)
  1814. {
  1815. MGL_stretchBltCoord(mgldc, memdc,
  1816. rects->x,
  1817. rects->y,
  1818. rects->x + rects->width,
  1819. rects->y + rects->height,
  1820. rects->x << 1,
  1821. rects->y << 1,
  1822. (rects->x + rects->width) << 1,
  1823. (rects->y + rects->height) << 1);
  1824. }
  1825. else
  1826. {
  1827. MGL_bitBltCoord(mgldc, memdc,
  1828. rects->x, rects->y,
  1829. (rects->x + rects->width),
  1830. (rects->y + rects->height),
  1831. rects->x, rects->y, MGL_REPLACE_MODE);
  1832. }
  1833. rects = rects->pnext;
  1834. }
  1835. }
  1836. if (vid.numpages > 1)
  1837. {
  1838. // We have a flipping surface, so do a hard page flip
  1839. aPage = (aPage+1) % vid.numpages;
  1840. vPage = (vPage+1) % vid.numpages;
  1841. MGL_setActivePage(mgldc,aPage);
  1842. MGL_setVisualPage(mgldc,vPage,waitVRT);
  1843. }
  1844. }
  1845. }
  1846. else
  1847. {
  1848. HDC hdcScreen;
  1849. hdcScreen = GetDC(mainwindow);
  1850. if (windc && dibdc)
  1851. {
  1852. MGL_setWinDC(windc,hdcScreen);
  1853. while (rects)
  1854. {
  1855. if (vid_stretched)
  1856. {
  1857. MGL_stretchBltCoord(windc,dibdc,
  1858. rects->x, rects->y,
  1859. rects->x + rects->width, rects->y + rects->height,
  1860. rects->x << 1, rects->y << 1,
  1861. (rects->x + rects->width) << 1,
  1862. (rects->y + rects->height) << 1);
  1863. }
  1864. else
  1865. {
  1866. MGL_bitBltCoord(windc,dibdc,
  1867. rects->x, rects->y,
  1868. rects->x + rects->width, rects->y + rects->height,
  1869. rects->x, rects->y, MGL_REPLACE_MODE);
  1870. }
  1871. rects = rects->pnext;
  1872. }
  1873. }
  1874. ReleaseDC(mainwindow, hdcScreen);
  1875. }
  1876. }
  1877. void VID_Update (vrect_t *rects)
  1878. {
  1879. vrect_t rect;
  1880. RECT trect;
  1881. if (!vid_palettized && palette_changed)
  1882. {
  1883. palette_changed = false;
  1884. rect.x = 0;
  1885. rect.y = 0;
  1886. rect.width = vid.width;
  1887. rect.height = vid.height;
  1888. rect.pnext = NULL;
  1889. rects = &rect;
  1890. }
  1891. if (firstupdate)
  1892. {
  1893. if (modestate == MS_WINDOWED)
  1894. {
  1895. GetWindowRect (mainwindow, &trect);
  1896. if ((trect.left != (int)vid_window_x.value) ||
  1897. (trect.top != (int)vid_window_y.value))
  1898. {
  1899. if (COM_CheckParm ("-resetwinpos"))
  1900. {
  1901. Cvar_SetValue ("vid_window_x", 0.0);
  1902. Cvar_SetValue ("vid_window_y", 0.0);
  1903. }
  1904. VID_CheckWindowXY ();
  1905. SetWindowPos (mainwindow, NULL, (int)vid_window_x.value,
  1906. (int)vid_window_y.value, 0, 0,
  1907. SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
  1908. }
  1909. }
  1910. if ((_vid_default_mode_win.value != vid_default) &&
  1911. (!startwindowed || (_vid_default_mode_win.value < MODE_FULLSCREEN_DEFAULT)))
  1912. {
  1913. firstupdate = 0;
  1914. if (COM_CheckParm ("-resetwinpos"))
  1915. {
  1916. Cvar_SetValue ("vid_window_x", 0.0);
  1917. Cvar_SetValue ("vid_window_y", 0.0);
  1918. }
  1919. if ((_vid_default_mode_win.value < 0) ||
  1920. (_vid_default_mode_win.value >= nummodes))
  1921. {
  1922. Cvar_SetValue ("_vid_default_mode_win", windowed_default);
  1923. }
  1924. Cvar_SetValue ("vid_mode", _vid_default_mode_win.value);
  1925. }
  1926. }
  1927. // We've drawn the frame; copy it to the screen
  1928. FlipScreen (rects);
  1929. if (vid_testingmode)
  1930. {
  1931. if (realtime >= vid_testendtime)
  1932. {
  1933. VID_SetMode (vid_realmode, vid_curpal);
  1934. vid_testingmode = 0;
  1935. }
  1936. }
  1937. else
  1938. {
  1939. if ((int)vid_mode.value != vid_realmode)
  1940. {
  1941. VID_SetMode ((int)vid_mode.value, vid_curpal);
  1942. Cvar_SetValue ("vid_mode", (float)vid_modenum);
  1943. // so if mode set fails, we don't keep on
  1944. // trying to set that mode
  1945. vid_realmode = vid_modenum;
  1946. }
  1947. }
  1948. // handle the mouse state when windowed if that's changed
  1949. if (modestate == MS_WINDOWED)
  1950. {
  1951. if (!_windowed_mouse.value) {
  1952. if (windowed_mouse) {
  1953. IN_DeactivateMouse ();
  1954. IN_ShowMouse ();
  1955. }
  1956. windowed_mouse = false;
  1957. } else {
  1958. windowed_mouse = true;
  1959. if (key_dest == key_game && !mouseactive && ActiveApp) {
  1960. IN_ActivateMouse ();
  1961. IN_HideMouse ();
  1962. } else if (mouseactive && key_dest != key_game) {
  1963. IN_DeactivateMouse ();
  1964. IN_ShowMouse ();
  1965. }
  1966. }
  1967. }
  1968. }
  1969. /*
  1970. ================
  1971. D_BeginDirectRect
  1972. ================
  1973. */
  1974. void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
  1975. {
  1976. int i, j, reps, repshift;
  1977. vrect_t rect;
  1978. if (!vid_initialized)
  1979. return;
  1980. if (vid.aspect > 1.5)
  1981. {
  1982. reps = 2;
  1983. repshift = 1;
  1984. }
  1985. else
  1986. {
  1987. reps = 1;
  1988. repshift = 0;
  1989. }
  1990. if (vid.numpages == 1)
  1991. {
  1992. VID_LockBuffer ();
  1993. if (!vid.direct)
  1994. Sys_Error ("NULL vid.direct pointer");
  1995. for (i=0 ; i<(height << repshift) ; i += reps)
  1996. {
  1997. for (j=0 ; j<reps ; j++)
  1998. {
  1999. memcpy (&backingbuf[(i + j) * 24],
  2000. vid.direct + x + ((y << repshift) + i + j) * vid.rowbytes,
  2001. width);
  2002. memcpy (vid.direct + x + ((y << repshift) + i + j) * vid.rowbytes,
  2003. &pbitmap[(i >> repshift) * width],
  2004. width);
  2005. }
  2006. }
  2007. VID_UnlockBuffer ();
  2008. rect.x = x;
  2009. rect.y = y;
  2010. rect.width = width;
  2011. rect.height = height << repshift;
  2012. rect.pnext = NULL;
  2013. FlipScreen (&rect);
  2014. }
  2015. else
  2016. {
  2017. // unlock if locked
  2018. if (lockcount > 0)
  2019. MGL_endDirectAccess();
  2020. // set the active page to the displayed page
  2021. MGL_setActivePage (mgldc, vPage);
  2022. // lock the screen
  2023. MGL_beginDirectAccess ();
  2024. // save from and draw to screen
  2025. for (i=0 ; i<(height << repshift) ; i += reps)
  2026. {
  2027. for (j=0 ; j<reps ; j++)
  2028. {
  2029. memcpy (&backingbuf[(i + j) * 24],
  2030. (byte *)mgldc->surface + x +
  2031. ((y << repshift) + i + j) * mgldc->mi.bytesPerLine,
  2032. width);
  2033. memcpy ((byte *)mgldc->surface + x +
  2034. ((y << repshift) + i + j) * mgldc->mi.bytesPerLine,
  2035. &pbitmap[(i >> repshift) * width],
  2036. width);
  2037. }
  2038. }
  2039. // unlock the screen
  2040. MGL_endDirectAccess ();
  2041. // restore the original active page
  2042. MGL_setActivePage (mgldc, aPage);
  2043. // relock the screen if it was locked
  2044. if (lockcount > 0)
  2045. MGL_beginDirectAccess();
  2046. }
  2047. }
  2048. /*
  2049. ================
  2050. D_EndDirectRect
  2051. ================
  2052. */
  2053. void D_EndDirectRect (int x, int y, int width, int height)
  2054. {
  2055. int i, j, reps, repshift;
  2056. vrect_t rect;
  2057. if (!vid_initialized)
  2058. return;
  2059. if (vid.aspect > 1.5)
  2060. {
  2061. reps = 2;
  2062. repshift = 1;
  2063. }
  2064. else
  2065. {
  2066. reps = 1;
  2067. repshift = 0;
  2068. }
  2069. if (vid.numpages == 1)
  2070. {
  2071. VID_LockBuffer ();
  2072. if (!vid.direct)
  2073. Sys_Error ("NULL vid.direct pointer");
  2074. for (i=0 ; i<(height << repshift) ; i += reps)
  2075. {
  2076. for (j=0 ; j<reps ; j++)
  2077. {
  2078. memcpy (vid.direct + x + ((y << repshift) + i + j) * vid.rowbytes,
  2079. &backingbuf[(i + j) * 24],
  2080. width);
  2081. }
  2082. }
  2083. VID_UnlockBuffer ();
  2084. rect.x = x;
  2085. rect.y = y;
  2086. rect.width = width;
  2087. rect.height = height << repshift;
  2088. rect.pnext = NULL;
  2089. FlipScreen (&rect);
  2090. }
  2091. else
  2092. {
  2093. // unlock if locked
  2094. if (lockcount > 0)
  2095. MGL_endDirectAccess();
  2096. // set the active page to the displayed page
  2097. MGL_setActivePage (mgldc, vPage);
  2098. // lock the screen
  2099. MGL_beginDirectAccess ();
  2100. // restore to the screen
  2101. for (i=0 ; i<(height << repshift) ; i += reps)
  2102. {
  2103. for (j=0 ; j<reps ; j++)
  2104. {
  2105. memcpy ((byte *)mgldc->surface + x +
  2106. ((y << repshift) + i + j) * mgldc->mi.bytesPerLine,
  2107. &backingbuf[(i + j) * 24],
  2108. width);
  2109. }
  2110. }
  2111. // unlock the screen
  2112. MGL_endDirectAccess ();
  2113. // restore the original active page
  2114. MGL_setActivePage (mgldc, aPage);
  2115. // relock the screen if it was locked
  2116. if (lockcount > 0)
  2117. MGL_beginDirectAccess();
  2118. }
  2119. }
  2120. //==========================================================================
  2121. byte scantokey[128] =
  2122. {
  2123. // 0 1 2 3 4 5 6 7
  2124. // 8 9 A B C D E F
  2125. 0 , 27, '1', '2', '3', '4', '5', '6',
  2126. '7', '8', '9', '0', '-', '=', K_BACKSPACE, 9, // 0
  2127. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  2128. 'o', 'p', '[', ']', 13 , K_CTRL,'a', 's', // 1
  2129. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  2130. '\'' , '`', K_SHIFT,'\\', 'z', 'x', 'c', 'v', // 2
  2131. 'b', 'n', 'm', ',', '.', '/', K_SHIFT,'*',
  2132. K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
  2133. K_F6, K_F7, K_F8, K_F9, K_F10, K_PAUSE, 0 , K_HOME,
  2134. K_UPARROW,K_PGUP,'-',K_LEFTARROW,'5',K_RIGHTARROW,'+',K_END, //4
  2135. K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
  2136. K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
  2137. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  2138. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
  2139. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  2140. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
  2141. };
  2142. /*
  2143. =======
  2144. MapKey
  2145. Map from windows to quake keynums
  2146. =======
  2147. */
  2148. int MapKey (int key)
  2149. {
  2150. key = (key>>16)&255;
  2151. if (key > 127)
  2152. return 0;
  2153. return scantokey[key];
  2154. }
  2155. void AppActivate(BOOL fActive, BOOL minimize)
  2156. /****************************************************************************
  2157. *
  2158. * Function: AppActivate
  2159. * Parameters: fActive - True if app is activating
  2160. *
  2161. * Description: If the application is activating, then swap the system
  2162. * into SYSPAL_NOSTATIC mode so that our palettes will display
  2163. * correctly.
  2164. *
  2165. ****************************************************************************/
  2166. {
  2167. HDC hdc;
  2168. int i, t;
  2169. static BOOL sound_active;
  2170. ActiveApp = fActive;
  2171. // messy, but it seems to work
  2172. if (vid_fulldib_on_focus_mode)
  2173. {
  2174. Minimized = minimize;
  2175. if (Minimized)
  2176. ActiveApp = false;
  2177. }
  2178. MGL_appActivate(windc, ActiveApp);
  2179. if (vid_initialized)
  2180. {
  2181. // yield the palette if we're losing the focus
  2182. hdc = GetDC(NULL);
  2183. if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
  2184. {
  2185. if (ActiveApp)
  2186. {
  2187. if ((modestate == MS_WINDOWED) || (modestate == MS_FULLDIB))
  2188. {
  2189. if (GetSystemPaletteUse(hdc) == SYSPAL_STATIC)
  2190. {
  2191. // switch to SYSPAL_NOSTATIC and remap the colors
  2192. SetSystemPaletteUse(hdc, SYSPAL_NOSTATIC);
  2193. syscolchg = true;
  2194. pal_is_nostatic = true;
  2195. }
  2196. }
  2197. }
  2198. else if (pal_is_nostatic)
  2199. {
  2200. if (GetSystemPaletteUse(hdc) == SYSPAL_NOSTATIC)
  2201. {
  2202. // switch back to SYSPAL_STATIC and the old mapping
  2203. SetSystemPaletteUse(hdc, SYSPAL_STATIC);
  2204. syscolchg = true;
  2205. }
  2206. pal_is_nostatic = false;
  2207. }
  2208. }
  2209. if (!Minimized)
  2210. VID_SetPalette (vid_curpal);
  2211. scr_fullupdate = 0;
  2212. ReleaseDC(NULL,hdc);
  2213. }
  2214. // enable/disable sound on focus gain/loss
  2215. if (!ActiveApp && sound_active)
  2216. {
  2217. S_BlockSound ();
  2218. S_ClearBuffer ();
  2219. sound_active = false;
  2220. }
  2221. else if (ActiveApp && !sound_active)
  2222. {
  2223. S_UnblockSound ();
  2224. S_ClearBuffer ();
  2225. sound_active = true;
  2226. }
  2227. // minimize/restore fulldib windows/mouse-capture normal windows on demand
  2228. if (!in_mode_set)
  2229. {
  2230. if (ActiveApp)
  2231. {
  2232. if (vid_fulldib_on_focus_mode)
  2233. {
  2234. if (vid_initialized)
  2235. {
  2236. msg_suppress_1 = true; // don't want to see normal mode set message
  2237. VID_SetMode (vid_fulldib_on_focus_mode, vid_curpal);
  2238. msg_suppress_1 = false;
  2239. t = in_mode_set;
  2240. in_mode_set = true;
  2241. AppActivate (true, false);
  2242. in_mode_set = t;
  2243. }
  2244. IN_ActivateMouse ();
  2245. IN_HideMouse ();
  2246. }
  2247. else if ((modestate == MS_WINDOWED) && _windowed_mouse.value && key_dest == key_game)
  2248. {
  2249. IN_ActivateMouse ();
  2250. IN_HideMouse ();
  2251. }
  2252. }
  2253. if (!ActiveApp)
  2254. {
  2255. if (modestate == MS_FULLDIB)
  2256. {
  2257. if (vid_initialized)
  2258. {
  2259. force_minimized = true;
  2260. i = vid_fulldib_on_focus_mode;
  2261. msg_suppress_1 = true; // don't want to see normal mode set message
  2262. VID_SetMode (windowed_default, vid_curpal);
  2263. msg_suppress_1 = false;
  2264. vid_fulldib_on_focus_mode = i;
  2265. force_minimized = false;
  2266. // we never seem to get WM_ACTIVATE inactive from this mode set, so we'll
  2267. // do it manually
  2268. t = in_mode_set;
  2269. in_mode_set = true;
  2270. AppActivate (false, true);
  2271. in_mode_set = t;
  2272. }
  2273. IN_DeactivateMouse ();
  2274. IN_ShowMouse ();
  2275. }
  2276. else if ((modestate == MS_WINDOWED) && _windowed_mouse.value /* && mouseactive */)
  2277. {
  2278. IN_DeactivateMouse ();
  2279. IN_ShowMouse ();
  2280. }
  2281. }
  2282. }
  2283. }
  2284. /*
  2285. ================
  2286. VID_HandlePause
  2287. ================
  2288. */
  2289. void VID_HandlePause (qboolean pause)
  2290. {
  2291. #if 0
  2292. if ((modestate == MS_WINDOWED) && _windowed_mouse.value)
  2293. {
  2294. if (pause)
  2295. {
  2296. IN_DeactivateMouse ();
  2297. IN_ShowMouse ();
  2298. }
  2299. else
  2300. {
  2301. IN_ActivateMouse ();
  2302. IN_HideMouse ();
  2303. }
  2304. }
  2305. #endif
  2306. }
  2307. /*
  2308. ===================================================================
  2309. MAIN WINDOW
  2310. ===================================================================
  2311. */
  2312. LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  2313. /* main window procedure */
  2314. LONG WINAPI MainWndProc (
  2315. HWND hWnd,
  2316. UINT uMsg,
  2317. WPARAM wParam,
  2318. LPARAM lParam)
  2319. {
  2320. LONG lRet = 0;
  2321. int fwKeys, xPos, yPos, fActive, fMinimized, temp;
  2322. HDC hdc;
  2323. PAINTSTRUCT ps;
  2324. extern unsigned int uiWheelMessage;
  2325. static int recursiveflag;
  2326. if ( uMsg == uiWheelMessage ) {
  2327. uMsg = WM_MOUSEWHEEL;
  2328. wParam <<= 16;
  2329. }
  2330. switch (uMsg)
  2331. {
  2332. case WM_CREATE:
  2333. break;
  2334. case WM_SYSCOMMAND:
  2335. // Check for maximize being hit
  2336. switch (wParam & ~0x0F)
  2337. {
  2338. case SC_MAXIMIZE:
  2339. // if minimized, bring up as a window before going fullscreen,
  2340. // so MGL will have the right state to restore
  2341. if (Minimized)
  2342. {
  2343. force_mode_set = true;
  2344. VID_SetMode (vid_modenum, vid_curpal);
  2345. force_mode_set = false;
  2346. }
  2347. VID_SetMode ((int)vid_fullscreen_mode.value, vid_curpal);
  2348. break;
  2349. case SC_SCREENSAVE:
  2350. case SC_MONITORPOWER:
  2351. if (modestate != MS_WINDOWED)
  2352. {
  2353. // don't call DefWindowProc() because we don't want to start
  2354. // the screen saver fullscreen
  2355. break;
  2356. }
  2357. // fall through windowed and allow the screen saver to start
  2358. default:
  2359. if (!in_mode_set)
  2360. {
  2361. S_BlockSound ();
  2362. S_ClearBuffer ();
  2363. }
  2364. lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  2365. if (!in_mode_set)
  2366. {
  2367. S_UnblockSound ();
  2368. }
  2369. }
  2370. break;
  2371. case WM_MOVE:
  2372. window_x = (int) LOWORD(lParam);
  2373. window_y = (int) HIWORD(lParam);
  2374. VID_UpdateWindowStatus ();
  2375. if ((modestate == MS_WINDOWED) && !in_mode_set && !Minimized)
  2376. VID_RememberWindowPos ();
  2377. break;
  2378. case WM_SIZE:
  2379. Minimized = false;
  2380. if (!(wParam & SIZE_RESTORED))
  2381. {
  2382. if (wParam & SIZE_MINIMIZED)
  2383. Minimized = true;
  2384. }
  2385. break;
  2386. case WM_SYSCHAR:
  2387. // keep Alt-Space from happening
  2388. break;
  2389. case WM_ACTIVATE:
  2390. fActive = LOWORD(wParam);
  2391. fMinimized = (BOOL) HIWORD(wParam);
  2392. AppActivate(!(fActive == WA_INACTIVE), fMinimized);
  2393. // fix the leftover Alt from any Alt-Tab or the like that switched us away
  2394. ClearAllStates ();
  2395. if (!in_mode_set)
  2396. {
  2397. if (windc)
  2398. MGL_activatePalette(windc,true);
  2399. VID_SetPalette(vid_curpal);
  2400. }
  2401. break;
  2402. case WM_PAINT:
  2403. hdc = BeginPaint(hWnd, &ps);
  2404. if (!in_mode_set && host_initialized)
  2405. SCR_UpdateWholeScreen ();
  2406. EndPaint(hWnd, &ps);
  2407. break;
  2408. case WM_KEYDOWN:
  2409. case WM_SYSKEYDOWN:
  2410. if (!in_mode_set)
  2411. Key_Event (MapKey(lParam), true);
  2412. break;
  2413. case WM_KEYUP:
  2414. case WM_SYSKEYUP:
  2415. if (!in_mode_set)
  2416. Key_Event (MapKey(lParam), false);
  2417. break;
  2418. // this is complicated because Win32 seems to pack multiple mouse events into
  2419. // one update sometimes, so we always check all states and look for events
  2420. case WM_LBUTTONDOWN:
  2421. case WM_LBUTTONUP:
  2422. case WM_RBUTTONDOWN:
  2423. case WM_RBUTTONUP:
  2424. case WM_MBUTTONDOWN:
  2425. case WM_MBUTTONUP:
  2426. case WM_MOUSEMOVE:
  2427. if (!in_mode_set)
  2428. {
  2429. temp = 0;
  2430. if (wParam & MK_LBUTTON)
  2431. temp |= 1;
  2432. if (wParam & MK_RBUTTON)
  2433. temp |= 2;
  2434. if (wParam & MK_MBUTTON)
  2435. temp |= 4;
  2436. IN_MouseEvent (temp);
  2437. }
  2438. break;
  2439. // JACK: This is the mouse wheel with the Intellimouse
  2440. // Its delta is either positive or neg, and we generate the proper
  2441. // Event.
  2442. case WM_MOUSEWHEEL:
  2443. if ((short) HIWORD(wParam) > 0) {
  2444. Key_Event(K_MWHEELUP, true);
  2445. Key_Event(K_MWHEELUP, false);
  2446. } else {
  2447. Key_Event(K_MWHEELDOWN, true);
  2448. Key_Event(K_MWHEELDOWN, false);
  2449. }
  2450. break;
  2451. // KJB: Added these new palette functions
  2452. case WM_PALETTECHANGED:
  2453. if ((HWND)wParam == hWnd)
  2454. break;
  2455. /* Fall through to WM_QUERYNEWPALETTE */
  2456. case WM_QUERYNEWPALETTE:
  2457. hdc = GetDC(NULL);
  2458. if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
  2459. vid_palettized = true;
  2460. else
  2461. vid_palettized = false;
  2462. ReleaseDC(NULL,hdc);
  2463. scr_fullupdate = 0;
  2464. if (vid_initialized && !in_mode_set && windc && MGL_activatePalette(windc,false) && !Minimized)
  2465. {
  2466. VID_SetPalette (vid_curpal);
  2467. InvalidateRect (mainwindow, NULL, false);
  2468. // specifically required if WM_QUERYNEWPALETTE realizes a new palette
  2469. lRet = TRUE;
  2470. }
  2471. break;
  2472. case WM_DISPLAYCHANGE:
  2473. if (!in_mode_set && (modestate == MS_WINDOWED) && !vid_fulldib_on_focus_mode)
  2474. {
  2475. force_mode_set = true;
  2476. VID_SetMode (vid_modenum, vid_curpal);
  2477. force_mode_set = false;
  2478. }
  2479. break;
  2480. case WM_CLOSE:
  2481. // this causes Close in the right-click task bar menu not to work, but right
  2482. // now bad things happen if Close is handled in that case (garbage and a
  2483. // crash on Win95)
  2484. if (!in_mode_set)
  2485. {
  2486. if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit",
  2487. MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
  2488. {
  2489. Sys_Quit ();
  2490. }
  2491. }
  2492. break;
  2493. case MM_MCINOTIFY:
  2494. lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
  2495. break;
  2496. default:
  2497. /* pass all unhandled messages to DefWindowProc */
  2498. lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  2499. break;
  2500. }
  2501. /* return 0 if handled message, 1 if not */
  2502. return lRet;
  2503. }
  2504. extern void M_Menu_Options_f (void);
  2505. extern void M_Print (int cx, int cy, char *str);
  2506. extern void M_PrintWhite (int cx, int cy, char *str);
  2507. extern void M_DrawCharacter (int cx, int line, int num);
  2508. extern void M_DrawTransPic (int x, int y, qpic_t *pic);
  2509. extern void M_DrawPic (int x, int y, qpic_t *pic);
  2510. static int vid_line, vid_wmodes;
  2511. typedef struct
  2512. {
  2513. int modenum;
  2514. char *desc;
  2515. int iscur;
  2516. int ismode13;
  2517. int width;
  2518. } modedesc_t;
  2519. #define MAX_COLUMN_SIZE 5
  2520. #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 6)
  2521. #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
  2522. static modedesc_t modedescs[MAX_MODEDESCS];
  2523. /*
  2524. ================
  2525. VID_MenuDraw
  2526. ================
  2527. */
  2528. void VID_MenuDraw (void)
  2529. {
  2530. qpic_t *p;
  2531. char *ptr;
  2532. int lnummodes, i, j, k, column, row, dup, dupmode;
  2533. char temp[100];
  2534. vmode_t *pv;
  2535. modedesc_t tmodedesc;
  2536. p = Draw_CachePic ("gfx/vidmodes.lmp");
  2537. M_DrawPic ( (320-p->width)/2, 4, p);
  2538. for (i=0 ; i<3 ; i++)
  2539. {
  2540. ptr = VID_GetModeDescriptionMemCheck (i);
  2541. modedescs[i].modenum = modelist[i].modenum;
  2542. modedescs[i].desc = ptr;
  2543. modedescs[i].ismode13 = 0;
  2544. modedescs[i].iscur = 0;
  2545. if (vid_modenum == i)
  2546. modedescs[i].iscur = 1;
  2547. }
  2548. vid_wmodes = 3;
  2549. lnummodes = VID_NumModes ();
  2550. for (i=3 ; i<lnummodes ; i++)
  2551. {
  2552. ptr = VID_GetModeDescriptionMemCheck (i);
  2553. pv = VID_GetModePtr (i);
  2554. // we only have room for 15 fullscreen modes, so don't allow
  2555. // 360-wide modes, because if there are 5 320-wide modes and
  2556. // 5 360-wide modes, we'll run out of space
  2557. if (ptr && ((pv->width != 360) || COM_CheckParm("-allow360")))
  2558. {
  2559. dup = 0;
  2560. for (j=3 ; j<vid_wmodes ; j++)
  2561. {
  2562. if (!strcmp (modedescs[j].desc, ptr))
  2563. {
  2564. dup = 1;
  2565. dupmode = j;
  2566. break;
  2567. }
  2568. }
  2569. if (dup || (vid_wmodes < MAX_MODEDESCS))
  2570. {
  2571. if (!dup || !modedescs[dupmode].ismode13 || COM_CheckParm("-noforcevga"))
  2572. {
  2573. if (dup)
  2574. {
  2575. k = dupmode;
  2576. }
  2577. else
  2578. {
  2579. k = vid_wmodes;
  2580. }
  2581. modedescs[k].modenum = i;
  2582. modedescs[k].desc = ptr;
  2583. modedescs[k].ismode13 = pv->mode13;
  2584. modedescs[k].iscur = 0;
  2585. modedescs[k].width = pv->width;
  2586. if (i == vid_modenum)
  2587. modedescs[k].iscur = 1;
  2588. if (!dup)
  2589. vid_wmodes++;
  2590. }
  2591. }
  2592. }
  2593. }
  2594. // sort the modes on width (to handle picking up oddball dibonly modes
  2595. // after all the others)
  2596. for (i=3 ; i<(vid_wmodes-1) ; i++)
  2597. {
  2598. for (j=(i+1) ; j<vid_wmodes ; j++)
  2599. {
  2600. if (modedescs[i].width > modedescs[j].width)
  2601. {
  2602. tmodedesc = modedescs[i];
  2603. modedescs[i] = modedescs[j];
  2604. modedescs[j] = tmodedesc;
  2605. }
  2606. }
  2607. }
  2608. M_Print (13*8, 36, "Windowed Modes");
  2609. column = 16;
  2610. row = 36+2*8;
  2611. for (i=0 ; i<3; i++)
  2612. {
  2613. if (modedescs[i].iscur)
  2614. M_PrintWhite (column, row, modedescs[i].desc);
  2615. else
  2616. M_Print (column, row, modedescs[i].desc);
  2617. column += 13*8;
  2618. }
  2619. if (vid_wmodes > 3)
  2620. {
  2621. M_Print (12*8, 36+4*8, "Fullscreen Modes");
  2622. column = 16;
  2623. row = 36+6*8;
  2624. for (i=3 ; i<vid_wmodes ; i++)
  2625. {
  2626. if (modedescs[i].iscur)
  2627. M_PrintWhite (column, row, modedescs[i].desc);
  2628. else
  2629. M_Print (column, row, modedescs[i].desc);
  2630. column += 13*8;
  2631. if (((i - 3) % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
  2632. {
  2633. column = 16;
  2634. row += 8;
  2635. }
  2636. }
  2637. }
  2638. // line cursor
  2639. if (vid_testingmode)
  2640. {
  2641. sprintf (temp, "TESTING %s",
  2642. modedescs[vid_line].desc);
  2643. M_Print (13*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, temp);
  2644. M_Print (9*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6,
  2645. "Please wait 5 seconds...");
  2646. }
  2647. else
  2648. {
  2649. M_Print (9*8, 36 + MODE_AREA_HEIGHT * 8 + 8,
  2650. "Press Enter to set mode");
  2651. M_Print (6*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3,
  2652. "T to test mode for 5 seconds");
  2653. ptr = VID_GetModeDescription2 (vid_modenum);
  2654. if (ptr)
  2655. {
  2656. sprintf (temp, "D to set default: %s", ptr);
  2657. M_Print (2*8, 36 + MODE_AREA_HEIGHT * 8 + 8*5, temp);
  2658. }
  2659. ptr = VID_GetModeDescription2 ((int)_vid_default_mode_win.value);
  2660. if (ptr)
  2661. {
  2662. sprintf (temp, "Current default: %s", ptr);
  2663. M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, temp);
  2664. }
  2665. M_Print (15*8, 36 + MODE_AREA_HEIGHT * 8 + 8*8,
  2666. "Esc to exit");
  2667. row = 36 + 2*8 + (vid_line / VID_ROW_SIZE) * 8;
  2668. column = 8 + (vid_line % VID_ROW_SIZE) * 13*8;
  2669. if (vid_line >= 3)
  2670. row += 3*8;
  2671. M_DrawCharacter (column, row, 12+((int)(realtime*4)&1));
  2672. }
  2673. }
  2674. /*
  2675. ================
  2676. VID_MenuKey
  2677. ================
  2678. */
  2679. void VID_MenuKey (int key)
  2680. {
  2681. if (vid_testingmode)
  2682. return;
  2683. switch (key)
  2684. {
  2685. case K_ESCAPE:
  2686. S_LocalSound ("misc/menu1.wav");
  2687. M_Menu_Options_f ();
  2688. break;
  2689. case K_LEFTARROW:
  2690. S_LocalSound ("misc/menu1.wav");
  2691. vid_line = ((vid_line / VID_ROW_SIZE) * VID_ROW_SIZE) +
  2692. ((vid_line + 2) % VID_ROW_SIZE);
  2693. if (vid_line >= vid_wmodes)
  2694. vid_line = vid_wmodes - 1;
  2695. break;
  2696. case K_RIGHTARROW:
  2697. S_LocalSound ("misc/menu1.wav");
  2698. vid_line = ((vid_line / VID_ROW_SIZE) * VID_ROW_SIZE) +
  2699. ((vid_line + 4) % VID_ROW_SIZE);
  2700. if (vid_line >= vid_wmodes)
  2701. vid_line = (vid_line / VID_ROW_SIZE) * VID_ROW_SIZE;
  2702. break;
  2703. case K_UPARROW:
  2704. S_LocalSound ("misc/menu1.wav");
  2705. vid_line -= VID_ROW_SIZE;
  2706. if (vid_line < 0)
  2707. {
  2708. vid_line += ((vid_wmodes + (VID_ROW_SIZE - 1)) /
  2709. VID_ROW_SIZE) * VID_ROW_SIZE;
  2710. while (vid_line >= vid_wmodes)
  2711. vid_line -= VID_ROW_SIZE;
  2712. }
  2713. break;
  2714. case K_DOWNARROW:
  2715. S_LocalSound ("misc/menu1.wav");
  2716. vid_line += VID_ROW_SIZE;
  2717. if (vid_line >= vid_wmodes)
  2718. {
  2719. vid_line -= ((vid_wmodes + (VID_ROW_SIZE - 1)) /
  2720. VID_ROW_SIZE) * VID_ROW_SIZE;
  2721. while (vid_line < 0)
  2722. vid_line += VID_ROW_SIZE;
  2723. }
  2724. break;
  2725. case K_ENTER:
  2726. S_LocalSound ("misc/menu1.wav");
  2727. VID_SetMode (modedescs[vid_line].modenum, vid_curpal);
  2728. break;
  2729. case 'T':
  2730. case 't':
  2731. S_LocalSound ("misc/menu1.wav");
  2732. // have to set this before setting the mode because WM_PAINT
  2733. // happens during the mode set and does a VID_Update, which
  2734. // checks vid_testingmode
  2735. vid_testingmode = 1;
  2736. vid_testendtime = realtime + 5.0;
  2737. if (!VID_SetMode (modedescs[vid_line].modenum, vid_curpal))
  2738. {
  2739. vid_testingmode = 0;
  2740. }
  2741. break;
  2742. case 'D':
  2743. case 'd':
  2744. S_LocalSound ("misc/menu1.wav");
  2745. firstupdate = 0;
  2746. Cvar_SetValue ("_vid_default_mode_win", vid_modenum);
  2747. break;
  2748. default:
  2749. break;
  2750. }
  2751. }