I_IBM.C 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783
  1. //**************************************************************************
  2. //**
  3. //** I_IBM.C
  4. //**
  5. //**************************************************************************
  6. #include <dos.h>
  7. #include <conio.h>
  8. #include <stdlib.h>
  9. #include <stdarg.h>
  10. #include <graph.h>
  11. #include "h2def.h"
  12. #include "r_local.h"
  13. #include "p_local.h" // for P_AproxDistance
  14. #include "sounds.h"
  15. #include "i_sound.h"
  16. #include "soundst.h"
  17. #include "st_start.h"
  18. #include "dmx.h"
  19. #include "dpmiapi.h"
  20. // Macros
  21. #define DEFAULT_ARCHIVEPATH "o:\\sound\\archive\\"
  22. #define PRIORITY_MAX_ADJUST 10
  23. #define DIST_ADJUST (MAX_SND_DIST/PRIORITY_MAX_ADJUST)
  24. #define DPMI_INT 0x31
  25. #define SEQ_ADDR 0x3C4
  26. #define SEQ_DATA 0x3C5
  27. #define REG_MAPMASK 0x02
  28. #define MASK_PLANE0 0x01
  29. #define MASK_PLANE1 0x02
  30. #define MASK_PLANE2 0x04
  31. #define MASK_PLANE3 0x08
  32. #define P0OFFSET 38400*0
  33. #define P1OFFSET 38400*1
  34. #define P2OFFSET 38400*2
  35. #define P3OFFSET 38400*3
  36. #define VID_INT 0x10
  37. #define VB_SYNC {while(!(inp(0x3da)&8)); while(inp(0x3da)&8);}
  38. #define BITPLANE(p) (outp(SEQ_ADDR,REG_MAPMASK),outp(SEQ_DATA,(p)))
  39. //#define NOKBD
  40. //#define NOTIMER
  41. // Public Data
  42. int DisplayTicker = 0;
  43. // Code
  44. void main(int argc, char **argv)
  45. {
  46. myargc = argc;
  47. myargv = argv;
  48. H2_Main();
  49. }
  50. void I_StartupNet (void);
  51. void I_ShutdownNet (void);
  52. void I_ReadExternDriver(void);
  53. typedef struct
  54. {
  55. unsigned edi, esi, ebp, reserved, ebx, edx, ecx, eax;
  56. unsigned short flags, es, ds, fs, gs, ip, cs, sp, ss;
  57. } dpmiregs_t;
  58. extern dpmiregs_t dpmiregs;
  59. void I_ReadMouse (void);
  60. extern int usemouse, usejoystick;
  61. extern void **lumpcache;
  62. int i_Vector;
  63. externdata_t *i_ExternData;
  64. boolean useexterndriver;
  65. boolean i_CDMusic;
  66. int i_CDTrack;
  67. int i_CDCurrentTrack;
  68. int i_CDMusicLength;
  69. int oldTic;
  70. /*
  71. ===============================================================================
  72. MUSIC & SFX API
  73. ===============================================================================
  74. */
  75. //static channel_t channel[MAX_CHANNELS];
  76. //static int rs; //the current registered song.
  77. //int mus_song = -1;
  78. //int mus_lumpnum;
  79. //void *mus_sndptr;
  80. //byte *soundCurve;
  81. extern sfxinfo_t S_sfx[];
  82. extern musicinfo_t S_music[];
  83. static channel_t Channel[MAX_CHANNELS];
  84. static int RegisteredSong; //the current registered song.
  85. static int NextCleanup;
  86. static boolean MusicPaused;
  87. static int Mus_Song = -1;
  88. static int Mus_LumpNum;
  89. static void *Mus_SndPtr;
  90. static byte *SoundCurve;
  91. static boolean UseSndScript;
  92. static char ArchivePath[128];
  93. extern int snd_MusicDevice;
  94. extern int snd_SfxDevice;
  95. extern int snd_MaxVolume;
  96. extern int snd_MusicVolume;
  97. extern int snd_Channels;
  98. extern int startepisode;
  99. extern int startmap;
  100. // int AmbChan;
  101. //==========================================================================
  102. //
  103. // S_Start
  104. //
  105. //==========================================================================
  106. void S_Start(void)
  107. {
  108. S_StopAllSound();
  109. S_StartSong(gamemap, true);
  110. }
  111. //==========================================================================
  112. //
  113. // S_StartSong
  114. //
  115. //==========================================================================
  116. void S_StartSong(int song, boolean loop)
  117. {
  118. char *songLump;
  119. int track;
  120. if(i_CDMusic)
  121. { // Play a CD track, instead
  122. if(i_CDTrack)
  123. { // Default to the player-chosen track
  124. track = i_CDTrack;
  125. }
  126. else
  127. {
  128. track = P_GetMapCDTrack(gamemap);
  129. }
  130. if(track == i_CDCurrentTrack && i_CDMusicLength > 0)
  131. {
  132. return;
  133. }
  134. if(!I_CDMusPlay(track))
  135. {
  136. if(loop)
  137. {
  138. i_CDMusicLength = 35*I_CDMusTrackLength(track);
  139. oldTic = gametic;
  140. }
  141. else
  142. {
  143. i_CDMusicLength = -1;
  144. }
  145. i_CDCurrentTrack = track;
  146. }
  147. }
  148. else
  149. {
  150. if(song == Mus_Song)
  151. { // don't replay an old song
  152. return;
  153. }
  154. if(RegisteredSong)
  155. {
  156. I_StopSong(RegisteredSong);
  157. I_UnRegisterSong(RegisteredSong);
  158. if(UseSndScript)
  159. {
  160. Z_Free(Mus_SndPtr);
  161. }
  162. else
  163. {
  164. Z_ChangeTag(lumpcache[Mus_LumpNum], PU_CACHE);
  165. }
  166. #ifdef __WATCOMC__
  167. _dpmi_unlockregion(Mus_SndPtr, lumpinfo[Mus_LumpNum].size);
  168. #endif
  169. RegisteredSong = 0;
  170. }
  171. songLump = P_GetMapSongLump(song);
  172. if(!songLump)
  173. {
  174. return;
  175. }
  176. if(UseSndScript)
  177. {
  178. char name[128];
  179. sprintf(name, "%s%s.lmp", ArchivePath, songLump);
  180. M_ReadFile(name, (byte **)&Mus_SndPtr);
  181. }
  182. else
  183. {
  184. Mus_LumpNum = W_GetNumForName(songLump);
  185. Mus_SndPtr = W_CacheLumpNum(Mus_LumpNum, PU_MUSIC);
  186. }
  187. #ifdef __WATCOMC__
  188. _dpmi_lockregion(Mus_SndPtr, lumpinfo[Mus_LumpNum].size);
  189. #endif
  190. RegisteredSong = I_RegisterSong(Mus_SndPtr);
  191. I_PlaySong(RegisteredSong, loop); // 'true' denotes endless looping.
  192. Mus_Song = song;
  193. }
  194. }
  195. //==========================================================================
  196. //
  197. // S_StartSongName
  198. //
  199. //==========================================================================
  200. void S_StartSongName(char *songLump, boolean loop)
  201. {
  202. int cdTrack;
  203. if(!songLump)
  204. {
  205. return;
  206. }
  207. if(i_CDMusic)
  208. {
  209. cdTrack = 0;
  210. if(!strcmp(songLump, "hexen"))
  211. {
  212. cdTrack = P_GetCDTitleTrack();
  213. }
  214. else if(!strcmp(songLump, "hub"))
  215. {
  216. cdTrack = P_GetCDIntermissionTrack();
  217. }
  218. else if(!strcmp(songLump, "hall"))
  219. {
  220. cdTrack = P_GetCDEnd1Track();
  221. }
  222. else if(!strcmp(songLump, "orb"))
  223. {
  224. cdTrack = P_GetCDEnd2Track();
  225. }
  226. else if(!strcmp(songLump, "chess") && !i_CDTrack)
  227. {
  228. cdTrack = P_GetCDEnd3Track();
  229. }
  230. /* Uncomment this, if Kevin writes a specific song for startup
  231. else if(!strcmp(songLump, "start"))
  232. {
  233. cdTrack = P_GetCDStartTrack();
  234. }
  235. */
  236. if(!cdTrack || (cdTrack == i_CDCurrentTrack && i_CDMusicLength > 0))
  237. {
  238. return;
  239. }
  240. if(!I_CDMusPlay(cdTrack))
  241. {
  242. if(loop)
  243. {
  244. i_CDMusicLength = 35*I_CDMusTrackLength(cdTrack);
  245. oldTic = gametic;
  246. }
  247. else
  248. {
  249. i_CDMusicLength = -1;
  250. }
  251. i_CDCurrentTrack = cdTrack;
  252. i_CDTrack = false;
  253. }
  254. }
  255. else
  256. {
  257. if(RegisteredSong)
  258. {
  259. I_StopSong(RegisteredSong);
  260. I_UnRegisterSong(RegisteredSong);
  261. if(UseSndScript)
  262. {
  263. Z_Free(Mus_SndPtr);
  264. }
  265. else
  266. {
  267. Z_ChangeTag(lumpcache[Mus_LumpNum], PU_CACHE);
  268. }
  269. #ifdef __WATCOMC__
  270. _dpmi_unlockregion(Mus_SndPtr, lumpinfo[Mus_LumpNum].size);
  271. #endif
  272. RegisteredSong = 0;
  273. }
  274. if(UseSndScript)
  275. {
  276. char name[128];
  277. sprintf(name, "%s%s.lmp", ArchivePath, songLump);
  278. M_ReadFile(name, (byte **)&Mus_SndPtr);
  279. }
  280. else
  281. {
  282. Mus_LumpNum = W_GetNumForName(songLump);
  283. Mus_SndPtr = W_CacheLumpNum(Mus_LumpNum, PU_MUSIC);
  284. }
  285. #ifdef __WATCOMC__
  286. _dpmi_lockregion(Mus_SndPtr, lumpinfo[Mus_LumpNum].size);
  287. #endif
  288. RegisteredSong = I_RegisterSong(Mus_SndPtr);
  289. I_PlaySong(RegisteredSong, loop); // 'true' denotes endless looping.
  290. Mus_Song = -1;
  291. }
  292. }
  293. //==========================================================================
  294. //
  295. // S_GetSoundID
  296. //
  297. //==========================================================================
  298. int S_GetSoundID(char *name)
  299. {
  300. int i;
  301. for(i = 0; i < NUMSFX; i++)
  302. {
  303. if(!strcmp(S_sfx[i].tagName, name))
  304. {
  305. return i;
  306. }
  307. }
  308. return 0;
  309. }
  310. //==========================================================================
  311. //
  312. // S_StartSound
  313. //
  314. //==========================================================================
  315. void S_StartSound(mobj_t *origin, int sound_id)
  316. {
  317. S_StartSoundAtVolume(origin, sound_id, 127);
  318. }
  319. //==========================================================================
  320. //
  321. // S_StartSoundAtVolume
  322. //
  323. //==========================================================================
  324. void S_StartSoundAtVolume(mobj_t *origin, int sound_id, int volume)
  325. {
  326. int dist, vol;
  327. int i;
  328. int priority;
  329. int sep;
  330. int angle;
  331. int absx;
  332. int absy;
  333. static int sndcount = 0;
  334. int chan;
  335. if(sound_id == 0 || snd_MaxVolume == 0)
  336. return;
  337. if(origin == NULL)
  338. {
  339. origin = players[displayplayer].mo;
  340. }
  341. if(volume == 0)
  342. {
  343. return;
  344. }
  345. // calculate the distance before other stuff so that we can throw out
  346. // sounds that are beyond the hearing range.
  347. absx = abs(origin->x-players[displayplayer].mo->x);
  348. absy = abs(origin->y-players[displayplayer].mo->y);
  349. dist = absx+absy-(absx > absy ? absy>>1 : absx>>1);
  350. dist >>= FRACBITS;
  351. if(dist >= MAX_SND_DIST)
  352. {
  353. return; // sound is beyond the hearing range...
  354. }
  355. if(dist < 0)
  356. {
  357. dist = 0;
  358. }
  359. priority = S_sfx[sound_id].priority;
  360. priority *= (PRIORITY_MAX_ADJUST-(dist/DIST_ADJUST));
  361. if(!S_StopSoundID(sound_id, priority))
  362. {
  363. return; // other sounds have greater priority
  364. }
  365. for(i=0; i<snd_Channels; i++)
  366. {
  367. if(origin->player)
  368. {
  369. i = snd_Channels;
  370. break; // let the player have more than one sound.
  371. }
  372. if(origin == Channel[i].mo)
  373. { // only allow other mobjs one sound
  374. S_StopSound(Channel[i].mo);
  375. break;
  376. }
  377. }
  378. if(i >= snd_Channels)
  379. {
  380. for(i = 0; i < snd_Channels; i++)
  381. {
  382. if(Channel[i].mo == NULL)
  383. {
  384. break;
  385. }
  386. }
  387. if(i >= snd_Channels)
  388. {
  389. // look for a lower priority sound to replace.
  390. sndcount++;
  391. if(sndcount >= snd_Channels)
  392. {
  393. sndcount = 0;
  394. }
  395. for(chan = 0; chan < snd_Channels; chan++)
  396. {
  397. i = (sndcount+chan)%snd_Channels;
  398. if(priority >= Channel[i].priority)
  399. {
  400. chan = -1; //denote that sound should be replaced.
  401. break;
  402. }
  403. }
  404. if(chan != -1)
  405. {
  406. return; //no free channels.
  407. }
  408. else //replace the lower priority sound.
  409. {
  410. if(Channel[i].handle)
  411. {
  412. if(I_SoundIsPlaying(Channel[i].handle))
  413. {
  414. I_StopSound(Channel[i].handle);
  415. }
  416. if(S_sfx[Channel[i].sound_id].usefulness > 0)
  417. {
  418. S_sfx[Channel[i].sound_id].usefulness--;
  419. }
  420. }
  421. }
  422. }
  423. }
  424. if(S_sfx[sound_id].lumpnum == 0)
  425. {
  426. S_sfx[sound_id].lumpnum = I_GetSfxLumpNum(&S_sfx[sound_id]);
  427. }
  428. if(S_sfx[sound_id].snd_ptr == NULL)
  429. {
  430. if(UseSndScript)
  431. {
  432. char name[128];
  433. sprintf(name, "%s%s.lmp", ArchivePath, S_sfx[sound_id].lumpname);
  434. M_ReadFile(name, (byte **)&S_sfx[sound_id].snd_ptr);
  435. }
  436. else
  437. {
  438. S_sfx[sound_id].snd_ptr = W_CacheLumpNum(S_sfx[sound_id].lumpnum,
  439. PU_SOUND);
  440. }
  441. #ifdef __WATCOMC__
  442. _dpmi_lockregion(S_sfx[sound_id].snd_ptr,
  443. lumpinfo[S_sfx[sound_id].lumpnum].size);
  444. #endif
  445. }
  446. vol = (SoundCurve[dist]*(snd_MaxVolume*8)*volume)>>14;
  447. if(origin == players[displayplayer].mo)
  448. {
  449. sep = 128;
  450. // vol = (volume*(snd_MaxVolume+1)*8)>>7;
  451. }
  452. else
  453. {
  454. angle = R_PointToAngle2(players[displayplayer].mo->x,
  455. players[displayplayer].mo->y, Channel[i].mo->x, Channel[i].mo->y);
  456. angle = (angle-viewangle)>>24;
  457. sep = angle*2-128;
  458. if(sep < 64)
  459. sep = -sep;
  460. if(sep > 192)
  461. sep = 512-sep;
  462. // vol = SoundCurve[dist];
  463. }
  464. if(S_sfx[sound_id].changePitch)
  465. {
  466. Channel[i].pitch = (byte)(127+(M_Random()&7)-(M_Random()&7));
  467. }
  468. else
  469. {
  470. Channel[i].pitch = 127;
  471. }
  472. Channel[i].handle = I_StartSound(sound_id, S_sfx[sound_id].snd_ptr, vol,
  473. sep, Channel[i].pitch, 0);
  474. Channel[i].mo = origin;
  475. Channel[i].sound_id = sound_id;
  476. Channel[i].priority = priority;
  477. Channel[i].volume = volume;
  478. if(S_sfx[sound_id].usefulness < 0)
  479. {
  480. S_sfx[sound_id].usefulness = 1;
  481. }
  482. else
  483. {
  484. S_sfx[sound_id].usefulness++;
  485. }
  486. }
  487. //==========================================================================
  488. //
  489. // S_StopSoundID
  490. //
  491. //==========================================================================
  492. boolean S_StopSoundID(int sound_id, int priority)
  493. {
  494. int i;
  495. int lp; //least priority
  496. int found;
  497. if(S_sfx[sound_id].numchannels == -1)
  498. {
  499. return(true);
  500. }
  501. lp = -1; //denote the argument sound_id
  502. found = 0;
  503. for(i=0; i<snd_Channels; i++)
  504. {
  505. if(Channel[i].sound_id == sound_id && Channel[i].mo)
  506. {
  507. found++; //found one. Now, should we replace it??
  508. if(priority >= Channel[i].priority)
  509. { // if we're gonna kill one, then this'll be it
  510. lp = i;
  511. priority = Channel[i].priority;
  512. }
  513. }
  514. }
  515. if(found < S_sfx[sound_id].numchannels)
  516. {
  517. return(true);
  518. }
  519. else if(lp == -1)
  520. {
  521. return(false); // don't replace any sounds
  522. }
  523. if(Channel[lp].handle)
  524. {
  525. if(I_SoundIsPlaying(Channel[lp].handle))
  526. {
  527. I_StopSound(Channel[lp].handle);
  528. }
  529. if(S_sfx[Channel[lp].sound_id].usefulness > 0)
  530. {
  531. S_sfx[Channel[lp].sound_id].usefulness--;
  532. }
  533. Channel[lp].mo = NULL;
  534. }
  535. return(true);
  536. }
  537. //==========================================================================
  538. //
  539. // S_StopSound
  540. //
  541. //==========================================================================
  542. void S_StopSound(mobj_t *origin)
  543. {
  544. int i;
  545. for(i=0;i<snd_Channels;i++)
  546. {
  547. if(Channel[i].mo == origin)
  548. {
  549. I_StopSound(Channel[i].handle);
  550. if(S_sfx[Channel[i].sound_id].usefulness > 0)
  551. {
  552. S_sfx[Channel[i].sound_id].usefulness--;
  553. }
  554. Channel[i].handle = 0;
  555. Channel[i].mo = NULL;
  556. }
  557. }
  558. }
  559. //==========================================================================
  560. //
  561. // S_StopAllSound
  562. //
  563. //==========================================================================
  564. void S_StopAllSound(void)
  565. {
  566. int i;
  567. //stop all sounds
  568. for(i=0; i < snd_Channels; i++)
  569. {
  570. if(Channel[i].handle)
  571. {
  572. S_StopSound(Channel[i].mo);
  573. }
  574. }
  575. memset(Channel, 0, 8*sizeof(channel_t));
  576. }
  577. //==========================================================================
  578. //
  579. // S_SoundLink
  580. //
  581. //==========================================================================
  582. void S_SoundLink(mobj_t *oldactor, mobj_t *newactor)
  583. {
  584. int i;
  585. for(i=0;i<snd_Channels;i++)
  586. {
  587. if(Channel[i].mo == oldactor)
  588. Channel[i].mo = newactor;
  589. }
  590. }
  591. //==========================================================================
  592. //
  593. // S_PauseSound
  594. //
  595. //==========================================================================
  596. void S_PauseSound(void)
  597. {
  598. if(i_CDMusic)
  599. {
  600. I_CDMusStop();
  601. }
  602. else
  603. {
  604. I_PauseSong(RegisteredSong);
  605. }
  606. }
  607. //==========================================================================
  608. //
  609. // S_ResumeSound
  610. //
  611. //==========================================================================
  612. void S_ResumeSound(void)
  613. {
  614. if(i_CDMusic)
  615. {
  616. I_CDMusResume();
  617. }
  618. else
  619. {
  620. I_ResumeSong(RegisteredSong);
  621. }
  622. }
  623. //==========================================================================
  624. //
  625. // S_UpdateSounds
  626. //
  627. //==========================================================================
  628. void S_UpdateSounds(mobj_t *listener)
  629. {
  630. int i, dist, vol;
  631. int angle;
  632. int sep;
  633. int priority;
  634. int absx;
  635. int absy;
  636. if(i_CDMusic)
  637. {
  638. I_UpdateCDMusic();
  639. }
  640. if(snd_MaxVolume == 0)
  641. {
  642. return;
  643. }
  644. // Update any Sequences
  645. SN_UpdateActiveSequences();
  646. if(NextCleanup < gametic)
  647. {
  648. if(UseSndScript)
  649. {
  650. for(i = 0; i < NUMSFX; i++)
  651. {
  652. if(S_sfx[i].usefulness == 0 && S_sfx[i].snd_ptr)
  653. {
  654. S_sfx[i].usefulness = -1;
  655. }
  656. }
  657. }
  658. else
  659. {
  660. for(i = 0; i < NUMSFX; i++)
  661. {
  662. if(S_sfx[i].usefulness == 0 && S_sfx[i].snd_ptr)
  663. {
  664. if(lumpcache[S_sfx[i].lumpnum])
  665. {
  666. if(((memblock_t *)((byte*)
  667. (lumpcache[S_sfx[i].lumpnum])-
  668. sizeof(memblock_t)))->id == 0x1d4a11)
  669. { // taken directly from the Z_ChangeTag macro
  670. Z_ChangeTag2(lumpcache[S_sfx[i].lumpnum],
  671. PU_CACHE);
  672. #ifdef __WATCOMC__
  673. _dpmi_unlockregion(S_sfx[i].snd_ptr,
  674. lumpinfo[S_sfx[i].lumpnum].size);
  675. #endif
  676. }
  677. }
  678. S_sfx[i].usefulness = -1;
  679. S_sfx[i].snd_ptr = NULL;
  680. }
  681. }
  682. }
  683. NextCleanup = gametic+35*30; // every 30 seconds
  684. }
  685. for(i=0;i<snd_Channels;i++)
  686. {
  687. if(!Channel[i].handle || S_sfx[Channel[i].sound_id].usefulness == -1)
  688. {
  689. continue;
  690. }
  691. if(!I_SoundIsPlaying(Channel[i].handle))
  692. {
  693. if(S_sfx[Channel[i].sound_id].usefulness > 0)
  694. {
  695. S_sfx[Channel[i].sound_id].usefulness--;
  696. }
  697. Channel[i].handle = 0;
  698. Channel[i].mo = NULL;
  699. Channel[i].sound_id = 0;
  700. }
  701. if(Channel[i].mo == NULL || Channel[i].sound_id == 0
  702. || Channel[i].mo == listener)
  703. {
  704. continue;
  705. }
  706. else
  707. {
  708. absx = abs(Channel[i].mo->x-listener->x);
  709. absy = abs(Channel[i].mo->y-listener->y);
  710. dist = absx+absy-(absx > absy ? absy>>1 : absx>>1);
  711. dist >>= FRACBITS;
  712. if(dist >= MAX_SND_DIST)
  713. {
  714. S_StopSound(Channel[i].mo);
  715. continue;
  716. }
  717. if(dist < 0)
  718. {
  719. dist = 0;
  720. }
  721. //vol = SoundCurve[dist];
  722. vol = (SoundCurve[dist]*(snd_MaxVolume*8)*Channel[i].volume)>>14;
  723. if(Channel[i].mo == listener)
  724. {
  725. sep = 128;
  726. }
  727. else
  728. {
  729. angle = R_PointToAngle2(listener->x, listener->y,
  730. Channel[i].mo->x, Channel[i].mo->y);
  731. angle = (angle-viewangle)>>24;
  732. sep = angle*2-128;
  733. if(sep < 64)
  734. sep = -sep;
  735. if(sep > 192)
  736. sep = 512-sep;
  737. }
  738. I_UpdateSoundParams(Channel[i].handle, vol, sep,
  739. Channel[i].pitch);
  740. priority = S_sfx[Channel[i].sound_id].priority;
  741. priority *= PRIORITY_MAX_ADJUST-(dist/DIST_ADJUST);
  742. Channel[i].priority = priority;
  743. }
  744. }
  745. }
  746. //==========================================================================
  747. //
  748. // S_Init
  749. //
  750. //==========================================================================
  751. void S_Init(void)
  752. {
  753. SoundCurve = W_CacheLumpName("SNDCURVE", PU_STATIC);
  754. // SoundCurve = Z_Malloc(MAX_SND_DIST, PU_STATIC, NULL);
  755. I_StartupSound();
  756. if(snd_Channels > 8)
  757. {
  758. snd_Channels = 8;
  759. }
  760. I_SetChannels(snd_Channels);
  761. I_SetMusicVolume(snd_MusicVolume);
  762. // Attempt to setup CD music
  763. if(snd_MusicDevice == snd_CDMUSIC)
  764. {
  765. ST_Message(" Attempting to initialize CD Music: ");
  766. if(!cdrom)
  767. {
  768. i_CDMusic = (I_CDMusInit() != -1);
  769. }
  770. else
  771. { // The user is trying to use the cdrom for both game and music
  772. i_CDMusic = false;
  773. }
  774. if(i_CDMusic)
  775. {
  776. ST_Message("initialized.\n");
  777. }
  778. else
  779. {
  780. ST_Message("failed.\n");
  781. }
  782. }
  783. }
  784. //==========================================================================
  785. //
  786. // S_GetChannelInfo
  787. //
  788. //==========================================================================
  789. void S_GetChannelInfo(SoundInfo_t *s)
  790. {
  791. int i;
  792. ChanInfo_t *c;
  793. s->channelCount = snd_Channels;
  794. s->musicVolume = snd_MusicVolume;
  795. s->soundVolume = snd_MaxVolume;
  796. for(i = 0; i < snd_Channels; i++)
  797. {
  798. c = &s->chan[i];
  799. c->id = Channel[i].sound_id;
  800. c->priority = Channel[i].priority;
  801. c->name = S_sfx[c->id].lumpname;
  802. c->mo = Channel[i].mo;
  803. c->distance = P_AproxDistance(c->mo->x-viewx, c->mo->y-viewy)
  804. >>FRACBITS;
  805. }
  806. }
  807. //==========================================================================
  808. //
  809. // S_GetSoundPlayingInfo
  810. //
  811. //==========================================================================
  812. boolean S_GetSoundPlayingInfo(mobj_t *mobj, int sound_id)
  813. {
  814. int i;
  815. for(i = 0; i < snd_Channels; i++)
  816. {
  817. if(Channel[i].sound_id == sound_id && Channel[i].mo == mobj)
  818. {
  819. if(I_SoundIsPlaying(Channel[i].handle))
  820. {
  821. return true;
  822. }
  823. }
  824. }
  825. return false;
  826. }
  827. //==========================================================================
  828. //
  829. // S_SetMusicVolume
  830. //
  831. //==========================================================================
  832. void S_SetMusicVolume(void)
  833. {
  834. if(i_CDMusic)
  835. {
  836. I_CDMusSetVolume(snd_MusicVolume*16); // 0-255
  837. }
  838. else
  839. {
  840. I_SetMusicVolume(snd_MusicVolume);
  841. }
  842. if(snd_MusicVolume == 0)
  843. {
  844. if(!i_CDMusic)
  845. {
  846. I_PauseSong(RegisteredSong);
  847. }
  848. MusicPaused = true;
  849. }
  850. else if(MusicPaused)
  851. {
  852. if(!i_CDMusic)
  853. {
  854. I_ResumeSong(RegisteredSong);
  855. }
  856. MusicPaused = false;
  857. }
  858. }
  859. //==========================================================================
  860. //
  861. // S_ShutDown
  862. //
  863. //==========================================================================
  864. void S_ShutDown(void)
  865. {
  866. extern int tsm_ID;
  867. if(tsm_ID != -1)
  868. {
  869. I_StopSong(RegisteredSong);
  870. I_UnRegisterSong(RegisteredSong);
  871. I_ShutdownSound();
  872. }
  873. if(i_CDMusic)
  874. {
  875. I_CDMusStop();
  876. }
  877. }
  878. //==========================================================================
  879. //
  880. // S_InitScript
  881. //
  882. //==========================================================================
  883. void S_InitScript(void)
  884. {
  885. int p;
  886. int i;
  887. strcpy(ArchivePath, DEFAULT_ARCHIVEPATH);
  888. if(!(p = M_CheckParm("-devsnd")))
  889. {
  890. UseSndScript = false;
  891. SC_OpenLump("sndinfo");
  892. }
  893. else
  894. {
  895. UseSndScript = true;
  896. SC_OpenFile(myargv[p+1]);
  897. }
  898. while(SC_GetString())
  899. {
  900. if(*sc_String == '$')
  901. {
  902. if(!stricmp(sc_String, "$ARCHIVEPATH"))
  903. {
  904. SC_MustGetString();
  905. strcpy(ArchivePath, sc_String);
  906. }
  907. else if(!stricmp(sc_String, "$MAP"))
  908. {
  909. SC_MustGetNumber();
  910. SC_MustGetString();
  911. if(sc_Number)
  912. {
  913. P_PutMapSongLump(sc_Number, sc_String);
  914. }
  915. }
  916. continue;
  917. }
  918. else
  919. {
  920. for(i = 0; i < NUMSFX; i++)
  921. {
  922. if(!strcmp(S_sfx[i].tagName, sc_String))
  923. {
  924. SC_MustGetString();
  925. if(*sc_String != '?')
  926. {
  927. strcpy(S_sfx[i].lumpname, sc_String);
  928. }
  929. else
  930. {
  931. strcpy(S_sfx[i].lumpname, "default");
  932. }
  933. break;
  934. }
  935. }
  936. if(i == NUMSFX)
  937. {
  938. SC_MustGetString();
  939. }
  940. }
  941. }
  942. SC_Close();
  943. for(i = 0; i < NUMSFX; i++)
  944. {
  945. if(!strcmp(S_sfx[i].lumpname, ""))
  946. {
  947. strcpy(S_sfx[i].lumpname, "default");
  948. }
  949. }
  950. }
  951. //==========================================================================
  952. //
  953. // I_UpdateCDMusic
  954. //
  955. // Updates playing time for current track, and restarts the track, if
  956. // needed
  957. //
  958. //==========================================================================
  959. void I_UpdateCDMusic(void)
  960. {
  961. extern boolean MenuActive;
  962. if(MusicPaused || i_CDMusicLength < 0
  963. || (paused && !MenuActive))
  964. { // Non-looping song/song paused
  965. return;
  966. }
  967. i_CDMusicLength -= gametic-oldTic;
  968. oldTic = gametic;
  969. if(i_CDMusicLength <= 0)
  970. {
  971. S_StartSong(gamemap, true);
  972. }
  973. }
  974. /*
  975. ============================================================================
  976. CONSTANTS
  977. ============================================================================
  978. */
  979. #define SC_INDEX 0x3C4
  980. #define SC_RESET 0
  981. #define SC_CLOCK 1
  982. #define SC_MAPMASK 2
  983. #define SC_CHARMAP 3
  984. #define SC_MEMMODE 4
  985. #define CRTC_INDEX 0x3D4
  986. #define CRTC_H_TOTAL 0
  987. #define CRTC_H_DISPEND 1
  988. #define CRTC_H_BLANK 2
  989. #define CRTC_H_ENDBLANK 3
  990. #define CRTC_H_RETRACE 4
  991. #define CRTC_H_ENDRETRACE 5
  992. #define CRTC_V_TOTAL 6
  993. #define CRTC_OVERFLOW 7
  994. #define CRTC_ROWSCAN 8
  995. #define CRTC_MAXSCANLINE 9
  996. #define CRTC_CURSORSTART 10
  997. #define CRTC_CURSOREND 11
  998. #define CRTC_STARTHIGH 12
  999. #define CRTC_STARTLOW 13
  1000. #define CRTC_CURSORHIGH 14
  1001. #define CRTC_CURSORLOW 15
  1002. #define CRTC_V_RETRACE 16
  1003. #define CRTC_V_ENDRETRACE 17
  1004. #define CRTC_V_DISPEND 18
  1005. #define CRTC_OFFSET 19
  1006. #define CRTC_UNDERLINE 20
  1007. #define CRTC_V_BLANK 21
  1008. #define CRTC_V_ENDBLANK 22
  1009. #define CRTC_MODE 23
  1010. #define CRTC_LINECOMPARE 24
  1011. #define GC_INDEX 0x3CE
  1012. #define GC_SETRESET 0
  1013. #define GC_ENABLESETRESET 1
  1014. #define GC_COLORCOMPARE 2
  1015. #define GC_DATAROTATE 3
  1016. #define GC_READMAP 4
  1017. #define GC_MODE 5
  1018. #define GC_MISCELLANEOUS 6
  1019. #define GC_COLORDONTCARE 7
  1020. #define GC_BITMASK 8
  1021. #define ATR_INDEX 0x3c0
  1022. #define ATR_MODE 16
  1023. #define ATR_OVERSCAN 17
  1024. #define ATR_COLORPLANEENABLE 18
  1025. #define ATR_PELPAN 19
  1026. #define ATR_COLORSELECT 20
  1027. #define STATUS_REGISTER_1 0x3da
  1028. #define PEL_WRITE_ADR 0x3c8
  1029. #define PEL_READ_ADR 0x3c7
  1030. #define PEL_DATA 0x3c9
  1031. #define PEL_MASK 0x3c6
  1032. // boolean grmode;
  1033. //==================================================
  1034. //
  1035. // joystick vars
  1036. //
  1037. //==================================================
  1038. boolean joystickpresent;
  1039. extern unsigned joystickx, joysticky;
  1040. boolean I_ReadJoystick (void); // returns false if not connected
  1041. //==================================================
  1042. #define VBLCOUNTER 34000 // hardware tics to a frame
  1043. #define TIMERINT 8
  1044. #define KEYBOARDINT 9
  1045. #define CRTCOFF (_inbyte(STATUS_REGISTER_1)&1)
  1046. #define CLI _disable()
  1047. #define STI _enable()
  1048. #define _outbyte(x,y) (outp(x,y))
  1049. #define _outhword(x,y) (outpw(x,y))
  1050. #define _inbyte(x) (inp(x))
  1051. #define _inhword(x) (inpw(x))
  1052. #define MOUSEB1 1
  1053. #define MOUSEB2 2
  1054. #define MOUSEB3 4
  1055. boolean mousepresent;
  1056. //static int tsm_ID = -1; // tsm init flag
  1057. //===============================
  1058. int ticcount;
  1059. // REGS stuff used for int calls
  1060. union REGS regs;
  1061. struct SREGS segregs;
  1062. boolean novideo; // if true, stay in text mode for debugging
  1063. #define KBDQUESIZE 32
  1064. byte keyboardque[KBDQUESIZE];
  1065. int kbdtail, kbdhead;
  1066. #define KEY_LSHIFT 0xfe
  1067. #define KEY_INS (0x80+0x52)
  1068. #define KEY_DEL (0x80+0x53)
  1069. #define KEY_PGUP (0x80+0x49)
  1070. #define KEY_PGDN (0x80+0x51)
  1071. #define KEY_HOME (0x80+0x47)
  1072. #define KEY_END (0x80+0x4f)
  1073. #define SC_RSHIFT 0x36
  1074. #define SC_LSHIFT 0x2a
  1075. byte scantokey[128] =
  1076. {
  1077. // 0 1 2 3 4 5 6 7
  1078. // 8 9 A B C D E F
  1079. 0 , 27, '1', '2', '3', '4', '5', '6',
  1080. '7', '8', '9', '0', '-', '=', KEY_BACKSPACE, 9, // 0
  1081. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  1082. 'o', 'p', '[', ']', 13 , KEY_RCTRL,'a', 's', // 1
  1083. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  1084. 39 , '`', KEY_LSHIFT,92, 'z', 'x', 'c', 'v', // 2
  1085. 'b', 'n', 'm', ',', '.', '/', KEY_RSHIFT,'*',
  1086. KEY_RALT,' ', 0 , KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, // 3
  1087. KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10,0 , 0 , KEY_HOME,
  1088. KEY_UPARROW,KEY_PGUP,'-',KEY_LEFTARROW,'5',KEY_RIGHTARROW,'+',KEY_END, //4
  1089. KEY_DOWNARROW,KEY_PGDN,KEY_INS,KEY_DEL,0,0, 0, KEY_F11,
  1090. KEY_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
  1091. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  1092. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
  1093. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
  1094. 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
  1095. };
  1096. //==========================================================================
  1097. //--------------------------------------------------------------------------
  1098. //
  1099. // FUNC I_GetTime
  1100. //
  1101. // Returns time in 1/35th second tics.
  1102. //
  1103. //--------------------------------------------------------------------------
  1104. int I_GetTime (void)
  1105. {
  1106. #ifdef NOTIMER
  1107. ticcount++;
  1108. #endif
  1109. return(ticcount);
  1110. }
  1111. //--------------------------------------------------------------------------
  1112. //
  1113. // PROC I_ColorBorder
  1114. //
  1115. //--------------------------------------------------------------------------
  1116. /*
  1117. void I_ColorBorder(void)
  1118. {
  1119. int i;
  1120. I_WaitVBL(1);
  1121. _outbyte(PEL_WRITE_ADR, 0);
  1122. for(i = 0; i < 3; i++)
  1123. {
  1124. _outbyte(PEL_DATA, 63);
  1125. }
  1126. }
  1127. */
  1128. //--------------------------------------------------------------------------
  1129. //
  1130. // PROC I_UnColorBorder
  1131. //
  1132. //--------------------------------------------------------------------------
  1133. /*
  1134. void I_UnColorBorder(void)
  1135. {
  1136. int i;
  1137. I_WaitVBL(1);
  1138. _outbyte(PEL_WRITE_ADR, 0);
  1139. for(i = 0; i < 3; i++)
  1140. {
  1141. _outbyte(PEL_DATA, 0);
  1142. }
  1143. }
  1144. */
  1145. /*
  1146. ============================================================================
  1147. USER INPUT
  1148. ============================================================================
  1149. */
  1150. //--------------------------------------------------------------------------
  1151. //
  1152. // PROC I_WaitVBL
  1153. //
  1154. //--------------------------------------------------------------------------
  1155. void I_WaitVBL(int vbls)
  1156. {
  1157. int stat;
  1158. if(novideo)
  1159. {
  1160. return;
  1161. }
  1162. while(vbls--)
  1163. {
  1164. do
  1165. {
  1166. stat = inp(STATUS_REGISTER_1);
  1167. if(stat&8)
  1168. {
  1169. break;
  1170. }
  1171. } while(1);
  1172. do
  1173. {
  1174. stat = inp(STATUS_REGISTER_1);
  1175. if((stat&8) == 0)
  1176. {
  1177. break;
  1178. }
  1179. } while(1);
  1180. }
  1181. }
  1182. //--------------------------------------------------------------------------
  1183. //
  1184. // PROC I_SetPalette
  1185. //
  1186. // Palette source must use 8 bit RGB elements.
  1187. //
  1188. //--------------------------------------------------------------------------
  1189. void I_SetPalette(byte *palette)
  1190. {
  1191. int i;
  1192. if(novideo)
  1193. {
  1194. return;
  1195. }
  1196. I_WaitVBL(1);
  1197. _outbyte(PEL_WRITE_ADR, 0);
  1198. for(i = 0; i < 768; i++)
  1199. {
  1200. _outbyte(PEL_DATA, (gammatable[usegamma][*palette++])>>2);
  1201. }
  1202. }
  1203. /*
  1204. ============================================================================
  1205. GRAPHICS MODE
  1206. ============================================================================
  1207. */
  1208. byte *pcscreen, *destscreen, *destview;
  1209. /*
  1210. ==============
  1211. =
  1212. = I_Update
  1213. =
  1214. ==============
  1215. */
  1216. int UpdateState;
  1217. extern int screenblocks;
  1218. void I_Update (void)
  1219. {
  1220. int i;
  1221. byte *dest;
  1222. int tics;
  1223. static int lasttic;
  1224. //
  1225. // blit screen to video
  1226. //
  1227. if(DisplayTicker)
  1228. {
  1229. if(screenblocks > 9 || UpdateState&(I_FULLSCRN|I_MESSAGES))
  1230. {
  1231. dest = (byte *)screen;
  1232. }
  1233. else
  1234. {
  1235. dest = (byte *)pcscreen;
  1236. }
  1237. tics = ticcount-lasttic;
  1238. lasttic = ticcount;
  1239. if(tics > 20)
  1240. {
  1241. tics = 20;
  1242. }
  1243. for(i = 0; i < tics; i++)
  1244. {
  1245. *dest = 0xff;
  1246. dest += 2;
  1247. }
  1248. for(i = tics; i < 20; i++)
  1249. {
  1250. *dest = 0x00;
  1251. dest += 2;
  1252. }
  1253. }
  1254. //memset(pcscreen, 255, SCREENHEIGHT*SCREENWIDTH);
  1255. if(UpdateState == I_NOUPDATE)
  1256. {
  1257. return;
  1258. }
  1259. if(UpdateState&I_FULLSCRN)
  1260. {
  1261. memcpy(pcscreen, screen, SCREENWIDTH*SCREENHEIGHT);
  1262. UpdateState = I_NOUPDATE; // clear out all draw types
  1263. }
  1264. if(UpdateState&I_FULLVIEW)
  1265. {
  1266. if(UpdateState&I_MESSAGES && screenblocks > 7)
  1267. {
  1268. for(i = 0; i <
  1269. (viewwindowy+viewheight)*SCREENWIDTH; i += SCREENWIDTH)
  1270. {
  1271. memcpy(pcscreen+i, screen+i, SCREENWIDTH);
  1272. }
  1273. UpdateState &= ~(I_FULLVIEW|I_MESSAGES);
  1274. }
  1275. else
  1276. {
  1277. for(i = viewwindowy*SCREENWIDTH+viewwindowx; i <
  1278. (viewwindowy+viewheight)*SCREENWIDTH; i += SCREENWIDTH)
  1279. {
  1280. memcpy(pcscreen+i, screen+i, viewwidth);
  1281. }
  1282. UpdateState &= ~I_FULLVIEW;
  1283. }
  1284. }
  1285. if(UpdateState&I_STATBAR)
  1286. {
  1287. memcpy(pcscreen+SCREENWIDTH*(SCREENHEIGHT-SBARHEIGHT),
  1288. screen+SCREENWIDTH*(SCREENHEIGHT-SBARHEIGHT),
  1289. SCREENWIDTH*SBARHEIGHT);
  1290. UpdateState &= ~I_STATBAR;
  1291. }
  1292. if(UpdateState&I_MESSAGES)
  1293. {
  1294. memcpy(pcscreen, screen, SCREENWIDTH*28);
  1295. UpdateState &= ~I_MESSAGES;
  1296. }
  1297. // memcpy(pcscreen, screen, SCREENHEIGHT*SCREENWIDTH);
  1298. }
  1299. //--------------------------------------------------------------------------
  1300. //
  1301. // PROC I_InitGraphics
  1302. //
  1303. //--------------------------------------------------------------------------
  1304. void I_InitGraphics(void)
  1305. {
  1306. if(novideo)
  1307. {
  1308. return;
  1309. }
  1310. //grmode = true;
  1311. regs.w.ax = 0x13;
  1312. int386(0x10, (const union REGS *)&regs, &regs);
  1313. pcscreen = destscreen = (byte *)0xa0000;
  1314. I_SetPalette(W_CacheLumpName("PLAYPAL", PU_CACHE));
  1315. }
  1316. //--------------------------------------------------------------------------
  1317. //
  1318. // PROC I_ShutdownGraphics
  1319. //
  1320. //--------------------------------------------------------------------------
  1321. void I_ShutdownGraphics(void)
  1322. {
  1323. byte mode;
  1324. // don't reset mode if it didn't get set
  1325. mode = *(byte *)0x449;
  1326. if(mode == 0x12 || mode == 0x13)
  1327. {
  1328. regs.w.ax = 3;
  1329. int386(0x10, &regs, &regs); // back to text mode
  1330. }
  1331. }
  1332. //--------------------------------------------------------------------------
  1333. //
  1334. // PROC I_ReadScreen
  1335. //
  1336. // Reads the screen currently displayed into a linear buffer.
  1337. //
  1338. //--------------------------------------------------------------------------
  1339. /*
  1340. void I_ReadScreen(byte *scr)
  1341. {
  1342. memcpy(scr, screen, SCREENWIDTH*SCREENHEIGHT);
  1343. }
  1344. */
  1345. //===========================================================================
  1346. /*
  1347. ===================
  1348. =
  1349. = I_StartTic
  1350. =
  1351. // called by D_DoomLoop
  1352. // called before processing each tic in a frame
  1353. // can call D_PostEvent
  1354. // asyncronous interrupt functions should maintain private ques that are
  1355. // read by the syncronous functions to be converted into events
  1356. ===================
  1357. */
  1358. /*
  1359. OLD STARTTIC STUFF
  1360. void I_StartTic (void)
  1361. {
  1362. int k;
  1363. event_t ev;
  1364. I_ReadMouse ();
  1365. //
  1366. // keyboard events
  1367. //
  1368. while (kbdtail < kbdhead)
  1369. {
  1370. k = keyboardque[kbdtail&(KBDQUESIZE-1)];
  1371. // if (k==14)
  1372. // I_Error ("exited");
  1373. kbdtail++;
  1374. // extended keyboard shift key bullshit
  1375. if ( (k&0x7f)==KEY_RSHIFT )
  1376. {
  1377. if ( keyboardque[(kbdtail-2)&(KBDQUESIZE-1)]==0xe0 )
  1378. continue;
  1379. k &= 0x80;
  1380. k |= KEY_RSHIFT;
  1381. }
  1382. if (k==0xe0)
  1383. continue; // special / pause keys
  1384. if (keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0xe1)
  1385. continue; // pause key bullshit
  1386. if (k==0xc5 && keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0x9d)
  1387. {
  1388. ev.type = ev_keydown;
  1389. ev.data1 = KEY_PAUSE;
  1390. D_PostEvent (&ev);
  1391. continue;
  1392. }
  1393. if (k&0x80)
  1394. ev.type = ev_keyup;
  1395. else
  1396. ev.type = ev_keydown;
  1397. k &= 0x7f;
  1398. ev.data1 = k;
  1399. //ev.data1 = scantokey[k];
  1400. D_PostEvent (&ev);
  1401. }
  1402. }
  1403. */
  1404. #define SC_UPARROW 0x48
  1405. #define SC_DOWNARROW 0x50
  1406. #define SC_LEFTARROW 0x4b
  1407. #define SC_RIGHTARROW 0x4d
  1408. void I_StartTic (void)
  1409. {
  1410. int k;
  1411. event_t ev;
  1412. I_ReadMouse ();
  1413. //
  1414. // keyboard events
  1415. //
  1416. while (kbdtail < kbdhead)
  1417. {
  1418. k = keyboardque[kbdtail&(KBDQUESIZE-1)];
  1419. kbdtail++;
  1420. // extended keyboard shift key bullshit
  1421. if ( (k&0x7f)==SC_LSHIFT || (k&0x7f)==SC_RSHIFT )
  1422. {
  1423. if ( keyboardque[(kbdtail-2)&(KBDQUESIZE-1)]==0xe0 )
  1424. continue;
  1425. k &= 0x80;
  1426. k |= SC_RSHIFT;
  1427. }
  1428. if (k==0xe0)
  1429. continue; // special / pause keys
  1430. if (keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0xe1)
  1431. continue; // pause key bullshit
  1432. if (k==0xc5 && keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0x9d)
  1433. {
  1434. ev.type = ev_keydown;
  1435. ev.data1 = KEY_PAUSE;
  1436. H2_PostEvent (&ev);
  1437. continue;
  1438. }
  1439. if (k&0x80)
  1440. ev.type = ev_keyup;
  1441. else
  1442. ev.type = ev_keydown;
  1443. k &= 0x7f;
  1444. switch (k)
  1445. {
  1446. case SC_UPARROW:
  1447. ev.data1 = KEY_UPARROW;
  1448. break;
  1449. case SC_DOWNARROW:
  1450. ev.data1 = KEY_DOWNARROW;
  1451. break;
  1452. case SC_LEFTARROW:
  1453. ev.data1 = KEY_LEFTARROW;
  1454. break;
  1455. case SC_RIGHTARROW:
  1456. ev.data1 = KEY_RIGHTARROW;
  1457. break;
  1458. default:
  1459. ev.data1 = scantokey[k];
  1460. break;
  1461. }
  1462. H2_PostEvent (&ev);
  1463. }
  1464. }
  1465. /*
  1466. void I_ReadKeys (void)
  1467. {
  1468. int k;
  1469. while (1)
  1470. {
  1471. while (kbdtail < kbdhead)
  1472. {
  1473. k = keyboardque[kbdtail&(KBDQUESIZE-1)];
  1474. kbdtail++;
  1475. printf ("0x%x\n",k);
  1476. if (k == 1)
  1477. I_Quit ();
  1478. }
  1479. }
  1480. }
  1481. */
  1482. /*
  1483. ===============
  1484. =
  1485. = I_StartFrame
  1486. =
  1487. ===============
  1488. */
  1489. void I_StartFrame (void)
  1490. {
  1491. I_JoystickEvents ();
  1492. if(useexterndriver)
  1493. {
  1494. DPMIInt(i_Vector);
  1495. }
  1496. }
  1497. /*
  1498. ============================================================================
  1499. TIMER INTERRUPT
  1500. ============================================================================
  1501. */
  1502. /*
  1503. void I_ColorBlack (int r, int g, int b)
  1504. {
  1505. _outbyte (PEL_WRITE_ADR,0);
  1506. _outbyte(PEL_DATA,r);
  1507. _outbyte(PEL_DATA,g);
  1508. _outbyte(PEL_DATA,b);
  1509. }
  1510. */
  1511. /*
  1512. ================
  1513. =
  1514. = I_TimerISR
  1515. =
  1516. ================
  1517. */
  1518. int I_TimerISR (void)
  1519. {
  1520. ticcount++;
  1521. return 0;
  1522. }
  1523. /*
  1524. ============================================================================
  1525. KEYBOARD
  1526. ============================================================================
  1527. */
  1528. void (__interrupt __far *oldkeyboardisr) () = NULL;
  1529. int lastpress;
  1530. /*
  1531. ================
  1532. =
  1533. = I_KeyboardISR
  1534. =
  1535. ================
  1536. */
  1537. void __interrupt I_KeyboardISR (void)
  1538. {
  1539. // Get the scan code
  1540. keyboardque[kbdhead&(KBDQUESIZE-1)] = lastpress = _inbyte(0x60);
  1541. kbdhead++;
  1542. // acknowledge the interrupt
  1543. _outbyte(0x20,0x20);
  1544. }
  1545. /*
  1546. ===============
  1547. =
  1548. = I_StartupKeyboard
  1549. =
  1550. ===============
  1551. */
  1552. void I_StartupKeyboard (void)
  1553. {
  1554. #ifndef NOKBD
  1555. oldkeyboardisr = _dos_getvect(KEYBOARDINT);
  1556. _dos_setvect (0x8000 | KEYBOARDINT, I_KeyboardISR);
  1557. #endif
  1558. //I_ReadKeys ();
  1559. }
  1560. void I_ShutdownKeyboard (void)
  1561. {
  1562. if (oldkeyboardisr)
  1563. _dos_setvect (KEYBOARDINT, oldkeyboardisr);
  1564. *(short *)0x41c = *(short *)0x41a; // clear bios key buffer
  1565. }
  1566. /*
  1567. ============================================================================
  1568. MOUSE
  1569. ============================================================================
  1570. */
  1571. int I_ResetMouse (void)
  1572. {
  1573. regs.w.ax = 0; // reset
  1574. int386 (0x33, &regs, &regs);
  1575. return regs.w.ax;
  1576. }
  1577. /*
  1578. ================
  1579. =
  1580. = StartupMouse
  1581. =
  1582. ================
  1583. */
  1584. void I_StartupCyberMan(void);
  1585. void I_StartupMouse (void)
  1586. {
  1587. //
  1588. // General mouse detection
  1589. //
  1590. mousepresent = 0;
  1591. if ( M_CheckParm ("-nomouse") || !usemouse )
  1592. return;
  1593. if (I_ResetMouse () != 0xffff)
  1594. {
  1595. ST_Message("Mouse: not present\n");
  1596. return;
  1597. }
  1598. ST_Message("Mouse: detected\n");
  1599. mousepresent = 1;
  1600. I_StartupCyberMan();
  1601. }
  1602. /*
  1603. ================
  1604. =
  1605. = ShutdownMouse
  1606. =
  1607. ================
  1608. */
  1609. void I_ShutdownMouse (void)
  1610. {
  1611. if (!mousepresent)
  1612. return;
  1613. I_ResetMouse ();
  1614. }
  1615. /*
  1616. ================
  1617. =
  1618. = I_ReadMouse
  1619. =
  1620. ================
  1621. */
  1622. void I_ReadMouse (void)
  1623. {
  1624. event_t ev;
  1625. //
  1626. // mouse events
  1627. //
  1628. if (!mousepresent)
  1629. return;
  1630. ev.type = ev_mouse;
  1631. memset (&dpmiregs,0,sizeof(dpmiregs));
  1632. dpmiregs.eax = 3; // read buttons / position
  1633. DPMIInt (0x33);
  1634. ev.data1 = dpmiregs.ebx;
  1635. dpmiregs.eax = 11; // read counters
  1636. DPMIInt (0x33);
  1637. ev.data2 = (short)dpmiregs.ecx;
  1638. ev.data3 = -(short)dpmiregs.edx;
  1639. H2_PostEvent (&ev);
  1640. }
  1641. /*
  1642. ============================================================================
  1643. JOYSTICK
  1644. ============================================================================
  1645. */
  1646. int joyxl, joyxh, joyyl, joyyh;
  1647. boolean WaitJoyButton (void)
  1648. {
  1649. int oldbuttons, buttons;
  1650. oldbuttons = 0;
  1651. do
  1652. {
  1653. I_WaitVBL (1);
  1654. buttons = ((inp(0x201) >> 4)&1)^1;
  1655. if (buttons != oldbuttons)
  1656. {
  1657. oldbuttons = buttons;
  1658. continue;
  1659. }
  1660. if ( (lastpress& 0x7f) == 1 )
  1661. {
  1662. joystickpresent = false;
  1663. return false;
  1664. }
  1665. } while ( !buttons);
  1666. do
  1667. {
  1668. I_WaitVBL (1);
  1669. buttons = ((inp(0x201) >> 4)&1)^1;
  1670. if (buttons != oldbuttons)
  1671. {
  1672. oldbuttons = buttons;
  1673. continue;
  1674. }
  1675. if ( (lastpress& 0x7f) == 1 )
  1676. {
  1677. joystickpresent = false;
  1678. return false;
  1679. }
  1680. } while ( buttons);
  1681. return true;
  1682. }
  1683. /*
  1684. ===============
  1685. =
  1686. = I_StartupJoystick
  1687. =
  1688. ===============
  1689. */
  1690. int basejoyx, basejoyy;
  1691. void I_StartupJoystick (void)
  1692. {
  1693. int centerx, centery;
  1694. joystickpresent = 0;
  1695. if ( M_CheckParm ("-nojoy") || !usejoystick )
  1696. return;
  1697. if (!I_ReadJoystick ())
  1698. {
  1699. joystickpresent = false;
  1700. ST_Message("joystick not found\n ");
  1701. return;
  1702. }
  1703. ST_Message("joystick found\n");
  1704. joystickpresent = true;
  1705. ST_RealMessage("CENTER the joystick and press button 1:");
  1706. if (!WaitJoyButton ())
  1707. return;
  1708. I_ReadJoystick ();
  1709. centerx = joystickx;
  1710. centery = joysticky;
  1711. ST_RealMessage("\nPush the joystick to the UPPER LEFT corner and press button 1:");
  1712. if (!WaitJoyButton ())
  1713. return;
  1714. I_ReadJoystick ();
  1715. joyxl = (centerx + joystickx)/2;
  1716. joyyl = (centerx + joysticky)/2;
  1717. ST_RealMessage("\nPush the joystick to the LOWER RIGHT corner and press button 1:");
  1718. if (!WaitJoyButton ())
  1719. return;
  1720. I_ReadJoystick ();
  1721. joyxh = (centerx + joystickx)/2;
  1722. joyyh = (centery + joysticky)/2;
  1723. ST_RealMessage("\n");
  1724. }
  1725. /*
  1726. ===============
  1727. =
  1728. = I_JoystickEvents
  1729. =
  1730. ===============
  1731. */
  1732. void I_JoystickEvents (void)
  1733. {
  1734. event_t ev;
  1735. //
  1736. // joystick events
  1737. //
  1738. if (!joystickpresent)
  1739. return;
  1740. I_ReadJoystick ();
  1741. ev.type = ev_joystick;
  1742. ev.data1 = ((inp(0x201) >> 4)&15)^15;
  1743. if (joystickx < joyxl)
  1744. ev.data2 = -1;
  1745. else if (joystickx > joyxh)
  1746. ev.data2 = 1;
  1747. else
  1748. ev.data2 = 0;
  1749. if (joysticky < joyyl)
  1750. ev.data3 = -1;
  1751. else if (joysticky > joyyh)
  1752. ev.data3 = 1;
  1753. else
  1754. ev.data3 = 0;
  1755. H2_PostEvent (&ev);
  1756. }
  1757. /*
  1758. ============================================================================
  1759. DPMI STUFF
  1760. ============================================================================
  1761. */
  1762. #define REALSTACKSIZE 1024
  1763. dpmiregs_t dpmiregs;
  1764. unsigned realstackseg;
  1765. void I_DivException (void);
  1766. int I_SetDivException (void);
  1767. /*
  1768. void DPMIFarCall (void)
  1769. {
  1770. segread (&segregs);
  1771. regs.w.ax = 0x301;
  1772. regs.w.bx = 0;
  1773. regs.w.cx = 0;
  1774. regs.x.edi = (unsigned)&dpmiregs;
  1775. segregs.es = segregs.ds;
  1776. int386x( DPMI_INT, &regs, &regs, &segregs );
  1777. }
  1778. */
  1779. void DPMIInt (int i)
  1780. {
  1781. dpmiregs.ss = realstackseg;
  1782. dpmiregs.sp = REALSTACKSIZE-4;
  1783. segread (&segregs);
  1784. regs.w.ax = 0x300;
  1785. regs.w.bx = i;
  1786. regs.w.cx = 0;
  1787. regs.x.edi = (unsigned)&dpmiregs;
  1788. segregs.es = segregs.ds;
  1789. int386x( DPMI_INT, &regs, &regs, &segregs );
  1790. }
  1791. /*
  1792. ==============
  1793. =
  1794. = I_StartupDPMI
  1795. =
  1796. ==============
  1797. */
  1798. void I_StartupDPMI (void)
  1799. {
  1800. // extern char __begtext;
  1801. // extern char ___argc;
  1802. // int n,d;
  1803. //
  1804. // allocate a decent stack for real mode ISRs
  1805. //
  1806. realstackseg = (int)I_AllocLow (1024) >> 4;
  1807. //
  1808. // lock the entire program down
  1809. //
  1810. // _dpmi_lockregion (&__begtext, &___argc - &__begtext);
  1811. //
  1812. // catch divide by 0 exception
  1813. //
  1814. #if 0
  1815. segread(&segregs);
  1816. regs.w.ax = 0x0203; // DPMI set processor exception handler vector
  1817. regs.w.bx = 0; // int 0
  1818. regs.w.cx = segregs.cs;
  1819. regs.x.edx = (int)&I_DivException;
  1820. printf ("%x : %x\n",regs.w.cx, regs.x.edx);
  1821. int386( DPMI_INT, &regs, &regs);
  1822. #endif
  1823. #if 0
  1824. n = I_SetDivException ();
  1825. printf ("return: %i\n",n);
  1826. n = 100;
  1827. d = 0;
  1828. printf ("100 / 0 = %i\n",n/d);
  1829. exit (1);
  1830. #endif
  1831. }
  1832. /*
  1833. ============================================================================
  1834. TIMER INTERRUPT
  1835. ============================================================================
  1836. */
  1837. void (__interrupt __far *oldtimerisr) ();
  1838. /*
  1839. void IO_ColorBlack (int r, int g, int b)
  1840. {
  1841. _outbyte (PEL_WRITE_ADR,0);
  1842. _outbyte(PEL_DATA,r);
  1843. _outbyte(PEL_DATA,g);
  1844. _outbyte(PEL_DATA,b);
  1845. }
  1846. */
  1847. /*
  1848. ================
  1849. =
  1850. = IO_TimerISR
  1851. =
  1852. ================
  1853. */
  1854. //void __interrupt IO_TimerISR (void)
  1855. void __interrupt __far IO_TimerISR (void)
  1856. {
  1857. ticcount++;
  1858. _outbyte(0x20,0x20); // Ack the interrupt
  1859. }
  1860. /*
  1861. =====================
  1862. =
  1863. = IO_SetTimer0
  1864. =
  1865. = Sets system timer 0 to the specified speed
  1866. =
  1867. =====================
  1868. */
  1869. void IO_SetTimer0(int speed)
  1870. {
  1871. if (speed > 0 && speed < 150)
  1872. I_Error ("INT_SetTimer0: %i is a bad value",speed);
  1873. _outbyte(0x43,0x36); // Change timer 0
  1874. _outbyte(0x40,speed);
  1875. _outbyte(0x40,speed >> 8);
  1876. }
  1877. /*
  1878. ===============
  1879. =
  1880. = IO_StartupTimer
  1881. =
  1882. ===============
  1883. */
  1884. /*
  1885. void IO_StartupTimer (void)
  1886. {
  1887. oldtimerisr = _dos_getvect(TIMERINT);
  1888. _dos_setvect (0x8000 | TIMERINT, IO_TimerISR);
  1889. IO_SetTimer0 (VBLCOUNTER);
  1890. }
  1891. */
  1892. void IO_ShutdownTimer (void)
  1893. {
  1894. if (oldtimerisr)
  1895. {
  1896. IO_SetTimer0 (0); // back to 18.4 ips
  1897. _dos_setvect (TIMERINT, oldtimerisr);
  1898. }
  1899. }
  1900. //===========================================================================
  1901. /*
  1902. ===============
  1903. =
  1904. = I_Init
  1905. =
  1906. = hook interrupts and set graphics mode
  1907. =
  1908. ===============
  1909. */
  1910. void I_Init (void)
  1911. {
  1912. extern void I_StartupTimer(void);
  1913. novideo = M_CheckParm("novideo");
  1914. ST_Message(" I_StartupDPMI\n");
  1915. I_StartupDPMI();
  1916. ST_Message(" I_StartupMouse ");
  1917. I_StartupMouse();
  1918. // tprintf("I_StartupJoystick ",1);
  1919. // I_StartupJoystick();
  1920. // tprintf("I_StartupKeyboard ",1);
  1921. // I_StartupKeyboard();
  1922. ST_Message(" S_Init... ");
  1923. S_Init();
  1924. //IO_StartupTimer();
  1925. S_Start();
  1926. }
  1927. /*
  1928. ===============
  1929. =
  1930. = I_Shutdown
  1931. =
  1932. = return to default system state
  1933. =
  1934. ===============
  1935. */
  1936. void I_Shutdown (void)
  1937. {
  1938. I_ShutdownGraphics ();
  1939. IO_ShutdownTimer ();
  1940. S_ShutDown ();
  1941. I_ShutdownMouse ();
  1942. I_ShutdownKeyboard ();
  1943. IO_SetTimer0 (0);
  1944. }
  1945. /*
  1946. ================
  1947. =
  1948. = I_Error
  1949. =
  1950. ================
  1951. */
  1952. void I_Error (char *error, ...)
  1953. {
  1954. union REGS regs;
  1955. va_list argptr;
  1956. D_QuitNetGame ();
  1957. I_Shutdown ();
  1958. va_start (argptr,error);
  1959. regs.x.eax = 0x3;
  1960. int386(0x10, &regs, &regs);
  1961. vprintf (error,argptr);
  1962. va_end (argptr);
  1963. printf ("\n");
  1964. exit (1);
  1965. }
  1966. //--------------------------------------------------------------------------
  1967. //
  1968. // I_Quit
  1969. //
  1970. // Shuts down net game, saves defaults, prints the exit text message,
  1971. // goes to text mode, and exits.
  1972. //
  1973. //--------------------------------------------------------------------------
  1974. void I_Quit(void)
  1975. {
  1976. D_QuitNetGame();
  1977. M_SaveDefaults();
  1978. I_Shutdown();
  1979. // scr = (byte *)W_CacheLumpName("ENDTEXT", PU_CACHE);
  1980. /*
  1981. memcpy((void *)0xb8000, scr, 80*25*2);
  1982. regs.w.ax = 0x0200;
  1983. regs.h.bh = 0;
  1984. regs.h.dl = 0;
  1985. regs.h.dh = 23;
  1986. int386(0x10, (const union REGS *)&regs, &regs); // Set text pos
  1987. _settextposition(24, 1);
  1988. */
  1989. printf("\nHexen: Beyond Heretic\n");
  1990. exit(0);
  1991. }
  1992. /*
  1993. ===============
  1994. =
  1995. = I_ZoneBase
  1996. =
  1997. ===============
  1998. */
  1999. byte *I_ZoneBase (int *size)
  2000. {
  2001. int meminfo[32];
  2002. int heap;
  2003. byte *ptr;
  2004. memset (meminfo,0,sizeof(meminfo));
  2005. segread(&segregs);
  2006. segregs.es = segregs.ds;
  2007. regs.w.ax = 0x500; // get memory info
  2008. regs.x.edi = (int)&meminfo;
  2009. int386x( 0x31, &regs, &regs, &segregs );
  2010. heap = meminfo[0];
  2011. ST_Message (" DPMI memory: 0x%x, ",heap);
  2012. ST_Message ("Maxzone: 0x%x\n", maxzone);
  2013. do
  2014. {
  2015. heap -= 0x10000; // leave 64k alone
  2016. if (heap > maxzone)
  2017. heap = maxzone;
  2018. ptr = malloc (heap);
  2019. } while (!ptr);
  2020. ST_Message (" 0x%x allocated for zone, ", heap);
  2021. ST_Message ("ZoneBase: 0x%X\n", (int)ptr);
  2022. if (heap < 0x180000)
  2023. I_Error (" Insufficient DPMI memory!");
  2024. #if 0
  2025. regs.w.ax = 0x501; // allocate linear block
  2026. regs.w.bx = heap>>16;
  2027. regs.w.cx = heap&0xffff;
  2028. int386( 0x31, &regs, &regs);
  2029. if (regs.w.cflag)
  2030. I_Error (" Couldn't allocate DPMI memory!");
  2031. block = (regs.w.si << 16) + regs.w.di;
  2032. #endif
  2033. *size = heap;
  2034. return ptr;
  2035. }
  2036. /*
  2037. =============
  2038. =
  2039. = I_AllocLow
  2040. =
  2041. =============
  2042. */
  2043. byte *I_AllocLow (int length)
  2044. {
  2045. byte *mem;
  2046. // DPMI call 100h allocates DOS memory
  2047. segread(&segregs);
  2048. regs.w.ax = 0x0100; // DPMI allocate DOS memory
  2049. regs.w.bx = (length+15) / 16;
  2050. int386( DPMI_INT, &regs, &regs);
  2051. // segment = regs.w.ax;
  2052. // selector = regs.w.dx;
  2053. if (regs.w.cflag != 0)
  2054. I_Error ("I_AllocLow: DOS alloc of %i failed, %i free",
  2055. length, regs.w.bx*16);
  2056. mem = (void *) ((regs.x.eax & 0xFFFF) << 4);
  2057. memset (mem,0,length);
  2058. return mem;
  2059. }
  2060. /*
  2061. ============================================================================
  2062. NETWORKING
  2063. ============================================================================
  2064. */
  2065. /* // FUCKED LINES
  2066. typedef struct
  2067. {
  2068. char priv[508];
  2069. } doomdata_t;
  2070. */ // FUCKED LINES
  2071. #define DOOMCOM_ID 0x12345678l
  2072. /* // FUCKED LINES
  2073. typedef struct
  2074. {
  2075. long id;
  2076. short intnum; // DOOM executes an int to execute commands
  2077. // communication between DOOM and the driver
  2078. short command; // CMD_SEND or CMD_GET
  2079. short remotenode; // dest for send, set by get (-1 = no packet)
  2080. short datalength; // bytes in doomdata to be sent
  2081. // info common to all nodes
  2082. short numnodes; // console is allways node 0
  2083. short ticdup; // 1 = no duplication, 2-5 = dup for slow nets
  2084. short extratics; // 1 = send a backup tic in every packet
  2085. short deathmatch; // 1 = deathmatch
  2086. short savegame; // -1 = new game, 0-5 = load savegame
  2087. short episode; // 1-3
  2088. short map; // 1-9
  2089. short skill; // 1-5
  2090. // info specific to this node
  2091. short consoleplayer;
  2092. short numplayers;
  2093. short angleoffset; // 1 = left, 0 = center, -1 = right
  2094. short drone; // 1 = drone
  2095. // packet data to be sent
  2096. doomdata_t data;
  2097. } doomcom_t;
  2098. */ // FUCKED LINES
  2099. extern doomcom_t *doomcom;
  2100. /*
  2101. ====================
  2102. =
  2103. = I_InitNetwork
  2104. =
  2105. ====================
  2106. */
  2107. void I_InitNetwork (void)
  2108. {
  2109. int i;
  2110. i = M_CheckParm ("-net");
  2111. if (!i)
  2112. {
  2113. //
  2114. // single player game
  2115. //
  2116. doomcom = malloc (sizeof (*doomcom) );
  2117. memset (doomcom, 0, sizeof(*doomcom) );
  2118. netgame = false;
  2119. doomcom->id = DOOMCOM_ID;
  2120. doomcom->numplayers = doomcom->numnodes = 1;
  2121. doomcom->deathmatch = false;
  2122. doomcom->consoleplayer = 0;
  2123. doomcom->ticdup = 1;
  2124. doomcom->extratics = 0;
  2125. return;
  2126. }
  2127. netgame = true;
  2128. doomcom = (doomcom_t *)atoi(myargv[i+1]);
  2129. //DEBUG
  2130. doomcom->skill = startskill;
  2131. doomcom->episode = startepisode;
  2132. doomcom->map = startmap;
  2133. doomcom->deathmatch = deathmatch;
  2134. }
  2135. void I_NetCmd (void)
  2136. {
  2137. if (!netgame)
  2138. I_Error ("I_NetCmd when not in netgame");
  2139. DPMIInt (doomcom->intnum);
  2140. }
  2141. //=========================================================================
  2142. //
  2143. // I_CheckExternDriver
  2144. //
  2145. // Checks to see if a vector, and an address for an external driver
  2146. // have been passed.
  2147. //=========================================================================
  2148. void I_CheckExternDriver(void)
  2149. {
  2150. int i;
  2151. if(!(i = M_CheckParm("-externdriver")))
  2152. {
  2153. return;
  2154. }
  2155. i_ExternData = (externdata_t *)atoi(myargv[i+1]);
  2156. i_Vector = i_ExternData->vector;
  2157. useexterndriver = true;
  2158. }
  2159. //=========================================================================
  2160. //=========================================================================
  2161. // Hi-Res (mode 12) stuff
  2162. //=========================================================================
  2163. //=========================================================================
  2164. //==========================================================================
  2165. //
  2166. // SetVideoModeHR - Set video mode to 640x480x16
  2167. //
  2168. //==========================================================================
  2169. void SetVideoModeHR(void)
  2170. {
  2171. union REGS regs;
  2172. regs.x.eax = 0x12;
  2173. int386(VID_INT, &regs, &regs);
  2174. }
  2175. //==========================================================================
  2176. //
  2177. // ClearScreenHR - Clear the screen to color 0
  2178. //
  2179. //==========================================================================
  2180. void ClearScreenHR(void)
  2181. {
  2182. BITPLANE(MASK_PLANE0|MASK_PLANE1|MASK_PLANE2|MASK_PLANE3);
  2183. memset((char *)0xa0000,0,38400);
  2184. }
  2185. //==========================================================================
  2186. //
  2187. // SlamHR - copy 4-plane buffer to screen
  2188. //
  2189. //==========================================================================
  2190. void SlamHR(char *buffer)
  2191. {
  2192. BITPLANE(MASK_PLANE0);
  2193. memcpy((char *)0xA0000, buffer+P0OFFSET, 38400);
  2194. BITPLANE(MASK_PLANE1);
  2195. memcpy((char *)0xA0000, buffer+P1OFFSET, 38400);
  2196. BITPLANE(MASK_PLANE2);
  2197. memcpy((char *)0xA0000, buffer+P2OFFSET, 38400);
  2198. BITPLANE(MASK_PLANE3);
  2199. memcpy((char *)0xA0000, buffer+P3OFFSET, 38400);
  2200. }
  2201. //==========================================================================
  2202. //
  2203. // SlamHR - copy 4-plane buffer to screen
  2204. //
  2205. // X and Width should be a multiple of 8
  2206. // src should be 4 planes of block size, back to back
  2207. //==========================================================================
  2208. void SlamBlockHR(int x, int y, int w, int h, char *src)
  2209. {
  2210. int srcwid = w>>3;
  2211. char *dest = ((char *)0xA0000) + (y*(640/8)) + (x>>3);
  2212. char *dst;
  2213. int i;
  2214. VB_SYNC;
  2215. BITPLANE(MASK_PLANE0);
  2216. dst = dest;
  2217. for ( i=0; i<h; i++ )
  2218. {
  2219. memcpy(dst, src, srcwid);
  2220. dst += 640/8;
  2221. src += srcwid;
  2222. }
  2223. BITPLANE(MASK_PLANE1);
  2224. dst = dest;
  2225. for ( i=0; i<h; i++ )
  2226. {
  2227. memcpy(dst, src, srcwid);
  2228. dst += 640/8;
  2229. src += srcwid;
  2230. }
  2231. BITPLANE(MASK_PLANE2);
  2232. dst = dest;
  2233. for ( i=0; i<h; i++ )
  2234. {
  2235. memcpy(dst, src, srcwid);
  2236. dst += 640/8;
  2237. src += srcwid;
  2238. }
  2239. BITPLANE(MASK_PLANE3);
  2240. dst = dest;
  2241. for ( i=0; i<h; i++ )
  2242. {
  2243. memcpy(dst, src, srcwid);
  2244. dst += 640/8;
  2245. src += srcwid;
  2246. }
  2247. }
  2248. //==========================================================================
  2249. //
  2250. // InitPaletteHR
  2251. //
  2252. //==========================================================================
  2253. void InitPaletteHR(void)
  2254. {
  2255. int i;
  2256. union REGS regs;
  2257. // Set palette registers to point into color registers
  2258. for ( i=0; i<16; i++ )
  2259. {
  2260. regs.x.eax = (0x10<<8)|0x00;
  2261. regs.x.ebx = (i<<8)|i;
  2262. int386(VID_INT, &regs, &regs);
  2263. }
  2264. }
  2265. //==========================================================================
  2266. //
  2267. // SetPaletteHR - Set the HR palette
  2268. //
  2269. //==========================================================================
  2270. void SetPaletteHR(byte *palette)
  2271. {
  2272. int i;
  2273. VB_SYNC;
  2274. outp(PEL_WRITE_ADR, 0);
  2275. for(i = 0; i < 16*3; i++)
  2276. {
  2277. outp(PEL_DATA, (*palette++));
  2278. }
  2279. }
  2280. //==========================================================================
  2281. //
  2282. // GetPaletteHR - Get the HR palette
  2283. //
  2284. //==========================================================================
  2285. void GetPaletteHR(byte *palette)
  2286. {
  2287. int i;
  2288. outp(PEL_READ_ADR, 0);
  2289. for (i=0; i<16*3; i++)
  2290. {
  2291. *palette++ = inp(PEL_DATA);
  2292. }
  2293. }
  2294. //==========================================================================
  2295. //
  2296. // FadeToPaletteHR
  2297. //
  2298. //==========================================================================
  2299. void FadeToPaletteHR(byte *palette)
  2300. {
  2301. int i,j;
  2302. int steps=140; // two-seconds
  2303. byte basep[16*3];
  2304. byte work[16*3];
  2305. int delta;
  2306. GetPaletteHR(basep);
  2307. for(i = 0; i < steps; i++)
  2308. {
  2309. for(j = 0; j < 16*3; j++)
  2310. {
  2311. delta = palette[j]-basep[j];
  2312. work[j] = basep[j]+delta*i/steps;
  2313. }
  2314. SetPaletteHR(work);
  2315. }
  2316. SetPaletteHR(palette);
  2317. }
  2318. //==========================================================================
  2319. //
  2320. // FadeToBlackHR - Fades the palette out to black
  2321. //
  2322. //==========================================================================
  2323. /*
  2324. void FadeToBlackHR(void)
  2325. {
  2326. char work[16*3];
  2327. char base[16*3];
  2328. int i,j,steps=70;
  2329. GetPaletteHR(base);
  2330. for (i=0; i<steps; i++)
  2331. {
  2332. for (j=0; j<16*3; j++)
  2333. {
  2334. work[j] = base[j]-(base[j]*i/steps);
  2335. }
  2336. VB_SYNC;
  2337. SetPaletteHR(work);
  2338. }
  2339. memset(work,0,16*3);
  2340. SetPaletteHR(work);
  2341. }
  2342. */
  2343. //==========================================================================
  2344. //
  2345. // BlackPaletteHR - Instantly blacks out the palette
  2346. //
  2347. //==========================================================================
  2348. void BlackPaletteHR(void)
  2349. {
  2350. char blackpal[16*3];
  2351. memset(blackpal,0,16*3);
  2352. SetPaletteHR(blackpal);
  2353. }
  2354. //==========================================================================
  2355. //
  2356. //
  2357. // I_StartupReadKeys
  2358. //
  2359. //
  2360. //==========================================================================
  2361. void I_StartupReadKeys(void)
  2362. {
  2363. int k;
  2364. while (kbdtail < kbdhead)
  2365. {
  2366. k = keyboardque[kbdtail&(KBDQUESIZE-1)];
  2367. kbdtail++;
  2368. if (k == 1)
  2369. I_Quit ();
  2370. }
  2371. }