vid_win.c 71 KB

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