12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783 |
- //**************************************************************************
- //**
- //** I_IBM.C
- //**
- //**************************************************************************
- #include <dos.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <graph.h>
- #include "h2def.h"
- #include "r_local.h"
- #include "p_local.h" // for P_AproxDistance
- #include "sounds.h"
- #include "i_sound.h"
- #include "soundst.h"
- #include "st_start.h"
- #include "dmx.h"
- #include "dpmiapi.h"
- // Macros
- #define DEFAULT_ARCHIVEPATH "o:\\sound\\archive\\"
- #define PRIORITY_MAX_ADJUST 10
- #define DIST_ADJUST (MAX_SND_DIST/PRIORITY_MAX_ADJUST)
- #define DPMI_INT 0x31
- #define SEQ_ADDR 0x3C4
- #define SEQ_DATA 0x3C5
- #define REG_MAPMASK 0x02
- #define MASK_PLANE0 0x01
- #define MASK_PLANE1 0x02
- #define MASK_PLANE2 0x04
- #define MASK_PLANE3 0x08
- #define P0OFFSET 38400*0
- #define P1OFFSET 38400*1
- #define P2OFFSET 38400*2
- #define P3OFFSET 38400*3
- #define VID_INT 0x10
- #define VB_SYNC {while(!(inp(0x3da)&8)); while(inp(0x3da)&8);}
- #define BITPLANE(p) (outp(SEQ_ADDR,REG_MAPMASK),outp(SEQ_DATA,(p)))
- //#define NOKBD
- //#define NOTIMER
- // Public Data
- int DisplayTicker = 0;
- // Code
- void main(int argc, char **argv)
- {
- myargc = argc;
- myargv = argv;
- H2_Main();
- }
- void I_StartupNet (void);
- void I_ShutdownNet (void);
- void I_ReadExternDriver(void);
- typedef struct
- {
- unsigned edi, esi, ebp, reserved, ebx, edx, ecx, eax;
- unsigned short flags, es, ds, fs, gs, ip, cs, sp, ss;
- } dpmiregs_t;
- extern dpmiregs_t dpmiregs;
- void I_ReadMouse (void);
- extern int usemouse, usejoystick;
- extern void **lumpcache;
- int i_Vector;
- externdata_t *i_ExternData;
- boolean useexterndriver;
- boolean i_CDMusic;
- int i_CDTrack;
- int i_CDCurrentTrack;
- int i_CDMusicLength;
- int oldTic;
- /*
- ===============================================================================
- MUSIC & SFX API
- ===============================================================================
- */
- //static channel_t channel[MAX_CHANNELS];
- //static int rs; //the current registered song.
- //int mus_song = -1;
- //int mus_lumpnum;
- //void *mus_sndptr;
- //byte *soundCurve;
- extern sfxinfo_t S_sfx[];
- extern musicinfo_t S_music[];
- static channel_t Channel[MAX_CHANNELS];
- static int RegisteredSong; //the current registered song.
- static int NextCleanup;
- static boolean MusicPaused;
- static int Mus_Song = -1;
- static int Mus_LumpNum;
- static void *Mus_SndPtr;
- static byte *SoundCurve;
- static boolean UseSndScript;
- static char ArchivePath[128];
- extern int snd_MusicDevice;
- extern int snd_SfxDevice;
- extern int snd_MaxVolume;
- extern int snd_MusicVolume;
- extern int snd_Channels;
- extern int startepisode;
- extern int startmap;
- // int AmbChan;
- //==========================================================================
- //
- // S_Start
- //
- //==========================================================================
- void S_Start(void)
- {
- S_StopAllSound();
- S_StartSong(gamemap, true);
- }
- //==========================================================================
- //
- // S_StartSong
- //
- //==========================================================================
- void S_StartSong(int song, boolean loop)
- {
- char *songLump;
- int track;
- if(i_CDMusic)
- { // Play a CD track, instead
- if(i_CDTrack)
- { // Default to the player-chosen track
- track = i_CDTrack;
- }
- else
- {
- track = P_GetMapCDTrack(gamemap);
- }
- if(track == i_CDCurrentTrack && i_CDMusicLength > 0)
- {
- return;
- }
- if(!I_CDMusPlay(track))
- {
- if(loop)
- {
- i_CDMusicLength = 35*I_CDMusTrackLength(track);
- oldTic = gametic;
- }
- else
- {
- i_CDMusicLength = -1;
- }
- i_CDCurrentTrack = track;
- }
- }
- else
- {
- if(song == Mus_Song)
- { // don't replay an old song
- return;
- }
- if(RegisteredSong)
- {
- I_StopSong(RegisteredSong);
- I_UnRegisterSong(RegisteredSong);
- if(UseSndScript)
- {
- Z_Free(Mus_SndPtr);
- }
- else
- {
- Z_ChangeTag(lumpcache[Mus_LumpNum], PU_CACHE);
- }
- #ifdef __WATCOMC__
- _dpmi_unlockregion(Mus_SndPtr, lumpinfo[Mus_LumpNum].size);
- #endif
- RegisteredSong = 0;
- }
- songLump = P_GetMapSongLump(song);
- if(!songLump)
- {
- return;
- }
- if(UseSndScript)
- {
- char name[128];
- sprintf(name, "%s%s.lmp", ArchivePath, songLump);
- M_ReadFile(name, (byte **)&Mus_SndPtr);
- }
- else
- {
- Mus_LumpNum = W_GetNumForName(songLump);
- Mus_SndPtr = W_CacheLumpNum(Mus_LumpNum, PU_MUSIC);
- }
- #ifdef __WATCOMC__
- _dpmi_lockregion(Mus_SndPtr, lumpinfo[Mus_LumpNum].size);
- #endif
- RegisteredSong = I_RegisterSong(Mus_SndPtr);
- I_PlaySong(RegisteredSong, loop); // 'true' denotes endless looping.
- Mus_Song = song;
- }
- }
- //==========================================================================
- //
- // S_StartSongName
- //
- //==========================================================================
- void S_StartSongName(char *songLump, boolean loop)
- {
- int cdTrack;
- if(!songLump)
- {
- return;
- }
- if(i_CDMusic)
- {
- cdTrack = 0;
- if(!strcmp(songLump, "hexen"))
- {
- cdTrack = P_GetCDTitleTrack();
- }
- else if(!strcmp(songLump, "hub"))
- {
- cdTrack = P_GetCDIntermissionTrack();
- }
- else if(!strcmp(songLump, "hall"))
- {
- cdTrack = P_GetCDEnd1Track();
- }
- else if(!strcmp(songLump, "orb"))
- {
- cdTrack = P_GetCDEnd2Track();
- }
- else if(!strcmp(songLump, "chess") && !i_CDTrack)
- {
- cdTrack = P_GetCDEnd3Track();
- }
- /* Uncomment this, if Kevin writes a specific song for startup
- else if(!strcmp(songLump, "start"))
- {
- cdTrack = P_GetCDStartTrack();
- }
- */
- if(!cdTrack || (cdTrack == i_CDCurrentTrack && i_CDMusicLength > 0))
- {
- return;
- }
- if(!I_CDMusPlay(cdTrack))
- {
- if(loop)
- {
- i_CDMusicLength = 35*I_CDMusTrackLength(cdTrack);
- oldTic = gametic;
- }
- else
- {
- i_CDMusicLength = -1;
- }
- i_CDCurrentTrack = cdTrack;
- i_CDTrack = false;
- }
- }
- else
- {
- if(RegisteredSong)
- {
- I_StopSong(RegisteredSong);
- I_UnRegisterSong(RegisteredSong);
- if(UseSndScript)
- {
- Z_Free(Mus_SndPtr);
- }
- else
- {
- Z_ChangeTag(lumpcache[Mus_LumpNum], PU_CACHE);
- }
- #ifdef __WATCOMC__
- _dpmi_unlockregion(Mus_SndPtr, lumpinfo[Mus_LumpNum].size);
- #endif
- RegisteredSong = 0;
- }
- if(UseSndScript)
- {
- char name[128];
- sprintf(name, "%s%s.lmp", ArchivePath, songLump);
- M_ReadFile(name, (byte **)&Mus_SndPtr);
- }
- else
- {
- Mus_LumpNum = W_GetNumForName(songLump);
- Mus_SndPtr = W_CacheLumpNum(Mus_LumpNum, PU_MUSIC);
- }
- #ifdef __WATCOMC__
- _dpmi_lockregion(Mus_SndPtr, lumpinfo[Mus_LumpNum].size);
- #endif
- RegisteredSong = I_RegisterSong(Mus_SndPtr);
- I_PlaySong(RegisteredSong, loop); // 'true' denotes endless looping.
- Mus_Song = -1;
- }
- }
- //==========================================================================
- //
- // S_GetSoundID
- //
- //==========================================================================
- int S_GetSoundID(char *name)
- {
- int i;
- for(i = 0; i < NUMSFX; i++)
- {
- if(!strcmp(S_sfx[i].tagName, name))
- {
- return i;
- }
- }
- return 0;
- }
- //==========================================================================
- //
- // S_StartSound
- //
- //==========================================================================
- void S_StartSound(mobj_t *origin, int sound_id)
- {
- S_StartSoundAtVolume(origin, sound_id, 127);
- }
- //==========================================================================
- //
- // S_StartSoundAtVolume
- //
- //==========================================================================
- void S_StartSoundAtVolume(mobj_t *origin, int sound_id, int volume)
- {
- int dist, vol;
- int i;
- int priority;
- int sep;
- int angle;
- int absx;
- int absy;
- static int sndcount = 0;
- int chan;
- if(sound_id == 0 || snd_MaxVolume == 0)
- return;
- if(origin == NULL)
- {
- origin = players[displayplayer].mo;
- }
- if(volume == 0)
- {
- return;
- }
- // calculate the distance before other stuff so that we can throw out
- // sounds that are beyond the hearing range.
- absx = abs(origin->x-players[displayplayer].mo->x);
- absy = abs(origin->y-players[displayplayer].mo->y);
- dist = absx+absy-(absx > absy ? absy>>1 : absx>>1);
- dist >>= FRACBITS;
- if(dist >= MAX_SND_DIST)
- {
- return; // sound is beyond the hearing range...
- }
- if(dist < 0)
- {
- dist = 0;
- }
- priority = S_sfx[sound_id].priority;
- priority *= (PRIORITY_MAX_ADJUST-(dist/DIST_ADJUST));
- if(!S_StopSoundID(sound_id, priority))
- {
- return; // other sounds have greater priority
- }
- for(i=0; i<snd_Channels; i++)
- {
- if(origin->player)
- {
- i = snd_Channels;
- break; // let the player have more than one sound.
- }
- if(origin == Channel[i].mo)
- { // only allow other mobjs one sound
- S_StopSound(Channel[i].mo);
- break;
- }
- }
- if(i >= snd_Channels)
- {
- for(i = 0; i < snd_Channels; i++)
- {
- if(Channel[i].mo == NULL)
- {
- break;
- }
- }
- if(i >= snd_Channels)
- {
- // look for a lower priority sound to replace.
- sndcount++;
- if(sndcount >= snd_Channels)
- {
- sndcount = 0;
- }
- for(chan = 0; chan < snd_Channels; chan++)
- {
- i = (sndcount+chan)%snd_Channels;
- if(priority >= Channel[i].priority)
- {
- chan = -1; //denote that sound should be replaced.
- break;
- }
- }
- if(chan != -1)
- {
- return; //no free channels.
- }
- else //replace the lower priority sound.
- {
- if(Channel[i].handle)
- {
- if(I_SoundIsPlaying(Channel[i].handle))
- {
- I_StopSound(Channel[i].handle);
- }
- if(S_sfx[Channel[i].sound_id].usefulness > 0)
- {
- S_sfx[Channel[i].sound_id].usefulness--;
- }
- }
- }
- }
- }
- if(S_sfx[sound_id].lumpnum == 0)
- {
- S_sfx[sound_id].lumpnum = I_GetSfxLumpNum(&S_sfx[sound_id]);
- }
- if(S_sfx[sound_id].snd_ptr == NULL)
- {
- if(UseSndScript)
- {
- char name[128];
- sprintf(name, "%s%s.lmp", ArchivePath, S_sfx[sound_id].lumpname);
- M_ReadFile(name, (byte **)&S_sfx[sound_id].snd_ptr);
- }
- else
- {
- S_sfx[sound_id].snd_ptr = W_CacheLumpNum(S_sfx[sound_id].lumpnum,
- PU_SOUND);
- }
- #ifdef __WATCOMC__
- _dpmi_lockregion(S_sfx[sound_id].snd_ptr,
- lumpinfo[S_sfx[sound_id].lumpnum].size);
- #endif
- }
- vol = (SoundCurve[dist]*(snd_MaxVolume*8)*volume)>>14;
- if(origin == players[displayplayer].mo)
- {
- sep = 128;
- // vol = (volume*(snd_MaxVolume+1)*8)>>7;
- }
- else
- {
- angle = R_PointToAngle2(players[displayplayer].mo->x,
- players[displayplayer].mo->y, Channel[i].mo->x, Channel[i].mo->y);
- angle = (angle-viewangle)>>24;
- sep = angle*2-128;
- if(sep < 64)
- sep = -sep;
- if(sep > 192)
- sep = 512-sep;
- // vol = SoundCurve[dist];
- }
- if(S_sfx[sound_id].changePitch)
- {
- Channel[i].pitch = (byte)(127+(M_Random()&7)-(M_Random()&7));
- }
- else
- {
- Channel[i].pitch = 127;
- }
- Channel[i].handle = I_StartSound(sound_id, S_sfx[sound_id].snd_ptr, vol,
- sep, Channel[i].pitch, 0);
- Channel[i].mo = origin;
- Channel[i].sound_id = sound_id;
- Channel[i].priority = priority;
- Channel[i].volume = volume;
- if(S_sfx[sound_id].usefulness < 0)
- {
- S_sfx[sound_id].usefulness = 1;
- }
- else
- {
- S_sfx[sound_id].usefulness++;
- }
- }
- //==========================================================================
- //
- // S_StopSoundID
- //
- //==========================================================================
- boolean S_StopSoundID(int sound_id, int priority)
- {
- int i;
- int lp; //least priority
- int found;
- if(S_sfx[sound_id].numchannels == -1)
- {
- return(true);
- }
- lp = -1; //denote the argument sound_id
- found = 0;
- for(i=0; i<snd_Channels; i++)
- {
- if(Channel[i].sound_id == sound_id && Channel[i].mo)
- {
- found++; //found one. Now, should we replace it??
- if(priority >= Channel[i].priority)
- { // if we're gonna kill one, then this'll be it
- lp = i;
- priority = Channel[i].priority;
- }
- }
- }
- if(found < S_sfx[sound_id].numchannels)
- {
- return(true);
- }
- else if(lp == -1)
- {
- return(false); // don't replace any sounds
- }
- if(Channel[lp].handle)
- {
- if(I_SoundIsPlaying(Channel[lp].handle))
- {
- I_StopSound(Channel[lp].handle);
- }
- if(S_sfx[Channel[lp].sound_id].usefulness > 0)
- {
- S_sfx[Channel[lp].sound_id].usefulness--;
- }
- Channel[lp].mo = NULL;
- }
- return(true);
- }
- //==========================================================================
- //
- // S_StopSound
- //
- //==========================================================================
- void S_StopSound(mobj_t *origin)
- {
- int i;
- for(i=0;i<snd_Channels;i++)
- {
- if(Channel[i].mo == origin)
- {
- I_StopSound(Channel[i].handle);
- if(S_sfx[Channel[i].sound_id].usefulness > 0)
- {
- S_sfx[Channel[i].sound_id].usefulness--;
- }
- Channel[i].handle = 0;
- Channel[i].mo = NULL;
- }
- }
- }
- //==========================================================================
- //
- // S_StopAllSound
- //
- //==========================================================================
- void S_StopAllSound(void)
- {
- int i;
- //stop all sounds
- for(i=0; i < snd_Channels; i++)
- {
- if(Channel[i].handle)
- {
- S_StopSound(Channel[i].mo);
- }
- }
- memset(Channel, 0, 8*sizeof(channel_t));
- }
- //==========================================================================
- //
- // S_SoundLink
- //
- //==========================================================================
- void S_SoundLink(mobj_t *oldactor, mobj_t *newactor)
- {
- int i;
- for(i=0;i<snd_Channels;i++)
- {
- if(Channel[i].mo == oldactor)
- Channel[i].mo = newactor;
- }
- }
- //==========================================================================
- //
- // S_PauseSound
- //
- //==========================================================================
- void S_PauseSound(void)
- {
- if(i_CDMusic)
- {
- I_CDMusStop();
- }
- else
- {
- I_PauseSong(RegisteredSong);
- }
- }
- //==========================================================================
- //
- // S_ResumeSound
- //
- //==========================================================================
- void S_ResumeSound(void)
- {
- if(i_CDMusic)
- {
- I_CDMusResume();
- }
- else
- {
- I_ResumeSong(RegisteredSong);
- }
- }
- //==========================================================================
- //
- // S_UpdateSounds
- //
- //==========================================================================
- void S_UpdateSounds(mobj_t *listener)
- {
- int i, dist, vol;
- int angle;
- int sep;
- int priority;
- int absx;
- int absy;
- if(i_CDMusic)
- {
- I_UpdateCDMusic();
- }
- if(snd_MaxVolume == 0)
- {
- return;
- }
- // Update any Sequences
- SN_UpdateActiveSequences();
- if(NextCleanup < gametic)
- {
- if(UseSndScript)
- {
- for(i = 0; i < NUMSFX; i++)
- {
- if(S_sfx[i].usefulness == 0 && S_sfx[i].snd_ptr)
- {
- S_sfx[i].usefulness = -1;
- }
- }
- }
- else
- {
- for(i = 0; i < NUMSFX; i++)
- {
- if(S_sfx[i].usefulness == 0 && S_sfx[i].snd_ptr)
- {
- if(lumpcache[S_sfx[i].lumpnum])
- {
- if(((memblock_t *)((byte*)
- (lumpcache[S_sfx[i].lumpnum])-
- sizeof(memblock_t)))->id == 0x1d4a11)
- { // taken directly from the Z_ChangeTag macro
- Z_ChangeTag2(lumpcache[S_sfx[i].lumpnum],
- PU_CACHE);
- #ifdef __WATCOMC__
- _dpmi_unlockregion(S_sfx[i].snd_ptr,
- lumpinfo[S_sfx[i].lumpnum].size);
- #endif
- }
- }
- S_sfx[i].usefulness = -1;
- S_sfx[i].snd_ptr = NULL;
- }
- }
- }
- NextCleanup = gametic+35*30; // every 30 seconds
- }
- for(i=0;i<snd_Channels;i++)
- {
- if(!Channel[i].handle || S_sfx[Channel[i].sound_id].usefulness == -1)
- {
- continue;
- }
- if(!I_SoundIsPlaying(Channel[i].handle))
- {
- if(S_sfx[Channel[i].sound_id].usefulness > 0)
- {
- S_sfx[Channel[i].sound_id].usefulness--;
- }
- Channel[i].handle = 0;
- Channel[i].mo = NULL;
- Channel[i].sound_id = 0;
- }
- if(Channel[i].mo == NULL || Channel[i].sound_id == 0
- || Channel[i].mo == listener)
- {
- continue;
- }
- else
- {
- absx = abs(Channel[i].mo->x-listener->x);
- absy = abs(Channel[i].mo->y-listener->y);
- dist = absx+absy-(absx > absy ? absy>>1 : absx>>1);
- dist >>= FRACBITS;
- if(dist >= MAX_SND_DIST)
- {
- S_StopSound(Channel[i].mo);
- continue;
- }
- if(dist < 0)
- {
- dist = 0;
- }
- //vol = SoundCurve[dist];
- vol = (SoundCurve[dist]*(snd_MaxVolume*8)*Channel[i].volume)>>14;
- if(Channel[i].mo == listener)
- {
- sep = 128;
- }
- else
- {
- angle = R_PointToAngle2(listener->x, listener->y,
- Channel[i].mo->x, Channel[i].mo->y);
- angle = (angle-viewangle)>>24;
- sep = angle*2-128;
- if(sep < 64)
- sep = -sep;
- if(sep > 192)
- sep = 512-sep;
- }
- I_UpdateSoundParams(Channel[i].handle, vol, sep,
- Channel[i].pitch);
- priority = S_sfx[Channel[i].sound_id].priority;
- priority *= PRIORITY_MAX_ADJUST-(dist/DIST_ADJUST);
- Channel[i].priority = priority;
- }
- }
- }
- //==========================================================================
- //
- // S_Init
- //
- //==========================================================================
- void S_Init(void)
- {
- SoundCurve = W_CacheLumpName("SNDCURVE", PU_STATIC);
- // SoundCurve = Z_Malloc(MAX_SND_DIST, PU_STATIC, NULL);
- I_StartupSound();
- if(snd_Channels > 8)
- {
- snd_Channels = 8;
- }
- I_SetChannels(snd_Channels);
- I_SetMusicVolume(snd_MusicVolume);
- // Attempt to setup CD music
- if(snd_MusicDevice == snd_CDMUSIC)
- {
- ST_Message(" Attempting to initialize CD Music: ");
- if(!cdrom)
- {
- i_CDMusic = (I_CDMusInit() != -1);
- }
- else
- { // The user is trying to use the cdrom for both game and music
- i_CDMusic = false;
- }
- if(i_CDMusic)
- {
- ST_Message("initialized.\n");
- }
- else
- {
- ST_Message("failed.\n");
- }
- }
- }
- //==========================================================================
- //
- // S_GetChannelInfo
- //
- //==========================================================================
- void S_GetChannelInfo(SoundInfo_t *s)
- {
- int i;
- ChanInfo_t *c;
- s->channelCount = snd_Channels;
- s->musicVolume = snd_MusicVolume;
- s->soundVolume = snd_MaxVolume;
- for(i = 0; i < snd_Channels; i++)
- {
- c = &s->chan[i];
- c->id = Channel[i].sound_id;
- c->priority = Channel[i].priority;
- c->name = S_sfx[c->id].lumpname;
- c->mo = Channel[i].mo;
- c->distance = P_AproxDistance(c->mo->x-viewx, c->mo->y-viewy)
- >>FRACBITS;
- }
- }
- //==========================================================================
- //
- // S_GetSoundPlayingInfo
- //
- //==========================================================================
- boolean S_GetSoundPlayingInfo(mobj_t *mobj, int sound_id)
- {
- int i;
- for(i = 0; i < snd_Channels; i++)
- {
- if(Channel[i].sound_id == sound_id && Channel[i].mo == mobj)
- {
- if(I_SoundIsPlaying(Channel[i].handle))
- {
- return true;
- }
- }
- }
- return false;
- }
- //==========================================================================
- //
- // S_SetMusicVolume
- //
- //==========================================================================
- void S_SetMusicVolume(void)
- {
- if(i_CDMusic)
- {
- I_CDMusSetVolume(snd_MusicVolume*16); // 0-255
- }
- else
- {
- I_SetMusicVolume(snd_MusicVolume);
- }
- if(snd_MusicVolume == 0)
- {
- if(!i_CDMusic)
- {
- I_PauseSong(RegisteredSong);
- }
- MusicPaused = true;
- }
- else if(MusicPaused)
- {
- if(!i_CDMusic)
- {
- I_ResumeSong(RegisteredSong);
- }
- MusicPaused = false;
- }
- }
- //==========================================================================
- //
- // S_ShutDown
- //
- //==========================================================================
- void S_ShutDown(void)
- {
- extern int tsm_ID;
- if(tsm_ID != -1)
- {
- I_StopSong(RegisteredSong);
- I_UnRegisterSong(RegisteredSong);
- I_ShutdownSound();
- }
- if(i_CDMusic)
- {
- I_CDMusStop();
- }
- }
- //==========================================================================
- //
- // S_InitScript
- //
- //==========================================================================
- void S_InitScript(void)
- {
- int p;
- int i;
- strcpy(ArchivePath, DEFAULT_ARCHIVEPATH);
- if(!(p = M_CheckParm("-devsnd")))
- {
- UseSndScript = false;
- SC_OpenLump("sndinfo");
- }
- else
- {
- UseSndScript = true;
- SC_OpenFile(myargv[p+1]);
- }
- while(SC_GetString())
- {
- if(*sc_String == '$')
- {
- if(!stricmp(sc_String, "$ARCHIVEPATH"))
- {
- SC_MustGetString();
- strcpy(ArchivePath, sc_String);
- }
- else if(!stricmp(sc_String, "$MAP"))
- {
- SC_MustGetNumber();
- SC_MustGetString();
- if(sc_Number)
- {
- P_PutMapSongLump(sc_Number, sc_String);
- }
- }
- continue;
- }
- else
- {
- for(i = 0; i < NUMSFX; i++)
- {
- if(!strcmp(S_sfx[i].tagName, sc_String))
- {
- SC_MustGetString();
- if(*sc_String != '?')
- {
- strcpy(S_sfx[i].lumpname, sc_String);
- }
- else
- {
- strcpy(S_sfx[i].lumpname, "default");
- }
- break;
- }
- }
- if(i == NUMSFX)
- {
- SC_MustGetString();
- }
- }
- }
- SC_Close();
- for(i = 0; i < NUMSFX; i++)
- {
- if(!strcmp(S_sfx[i].lumpname, ""))
- {
- strcpy(S_sfx[i].lumpname, "default");
- }
- }
- }
- //==========================================================================
- //
- // I_UpdateCDMusic
- //
- // Updates playing time for current track, and restarts the track, if
- // needed
- //
- //==========================================================================
- void I_UpdateCDMusic(void)
- {
- extern boolean MenuActive;
- if(MusicPaused || i_CDMusicLength < 0
- || (paused && !MenuActive))
- { // Non-looping song/song paused
- return;
- }
- i_CDMusicLength -= gametic-oldTic;
- oldTic = gametic;
- if(i_CDMusicLength <= 0)
- {
- S_StartSong(gamemap, true);
- }
- }
- /*
- ============================================================================
- CONSTANTS
- ============================================================================
- */
- #define SC_INDEX 0x3C4
- #define SC_RESET 0
- #define SC_CLOCK 1
- #define SC_MAPMASK 2
- #define SC_CHARMAP 3
- #define SC_MEMMODE 4
- #define CRTC_INDEX 0x3D4
- #define CRTC_H_TOTAL 0
- #define CRTC_H_DISPEND 1
- #define CRTC_H_BLANK 2
- #define CRTC_H_ENDBLANK 3
- #define CRTC_H_RETRACE 4
- #define CRTC_H_ENDRETRACE 5
- #define CRTC_V_TOTAL 6
- #define CRTC_OVERFLOW 7
- #define CRTC_ROWSCAN 8
- #define CRTC_MAXSCANLINE 9
- #define CRTC_CURSORSTART 10
- #define CRTC_CURSOREND 11
- #define CRTC_STARTHIGH 12
- #define CRTC_STARTLOW 13
- #define CRTC_CURSORHIGH 14
- #define CRTC_CURSORLOW 15
- #define CRTC_V_RETRACE 16
- #define CRTC_V_ENDRETRACE 17
- #define CRTC_V_DISPEND 18
- #define CRTC_OFFSET 19
- #define CRTC_UNDERLINE 20
- #define CRTC_V_BLANK 21
- #define CRTC_V_ENDBLANK 22
- #define CRTC_MODE 23
- #define CRTC_LINECOMPARE 24
- #define GC_INDEX 0x3CE
- #define GC_SETRESET 0
- #define GC_ENABLESETRESET 1
- #define GC_COLORCOMPARE 2
- #define GC_DATAROTATE 3
- #define GC_READMAP 4
- #define GC_MODE 5
- #define GC_MISCELLANEOUS 6
- #define GC_COLORDONTCARE 7
- #define GC_BITMASK 8
- #define ATR_INDEX 0x3c0
- #define ATR_MODE 16
- #define ATR_OVERSCAN 17
- #define ATR_COLORPLANEENABLE 18
- #define ATR_PELPAN 19
- #define ATR_COLORSELECT 20
- #define STATUS_REGISTER_1 0x3da
- #define PEL_WRITE_ADR 0x3c8
- #define PEL_READ_ADR 0x3c7
- #define PEL_DATA 0x3c9
- #define PEL_MASK 0x3c6
- // boolean grmode;
- //==================================================
- //
- // joystick vars
- //
- //==================================================
- boolean joystickpresent;
- extern unsigned joystickx, joysticky;
- boolean I_ReadJoystick (void); // returns false if not connected
- //==================================================
- #define VBLCOUNTER 34000 // hardware tics to a frame
- #define TIMERINT 8
- #define KEYBOARDINT 9
- #define CRTCOFF (_inbyte(STATUS_REGISTER_1)&1)
- #define CLI _disable()
- #define STI _enable()
- #define _outbyte(x,y) (outp(x,y))
- #define _outhword(x,y) (outpw(x,y))
- #define _inbyte(x) (inp(x))
- #define _inhword(x) (inpw(x))
- #define MOUSEB1 1
- #define MOUSEB2 2
- #define MOUSEB3 4
- boolean mousepresent;
- //static int tsm_ID = -1; // tsm init flag
- //===============================
- int ticcount;
- // REGS stuff used for int calls
- union REGS regs;
- struct SREGS segregs;
- boolean novideo; // if true, stay in text mode for debugging
- #define KBDQUESIZE 32
- byte keyboardque[KBDQUESIZE];
- int kbdtail, kbdhead;
- #define KEY_LSHIFT 0xfe
- #define KEY_INS (0x80+0x52)
- #define KEY_DEL (0x80+0x53)
- #define KEY_PGUP (0x80+0x49)
- #define KEY_PGDN (0x80+0x51)
- #define KEY_HOME (0x80+0x47)
- #define KEY_END (0x80+0x4f)
- #define SC_RSHIFT 0x36
- #define SC_LSHIFT 0x2a
- byte scantokey[128] =
- {
- // 0 1 2 3 4 5 6 7
- // 8 9 A B C D E F
- 0 , 27, '1', '2', '3', '4', '5', '6',
- '7', '8', '9', '0', '-', '=', KEY_BACKSPACE, 9, // 0
- 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
- 'o', 'p', '[', ']', 13 , KEY_RCTRL,'a', 's', // 1
- 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
- 39 , '`', KEY_LSHIFT,92, 'z', 'x', 'c', 'v', // 2
- 'b', 'n', 'm', ',', '.', '/', KEY_RSHIFT,'*',
- KEY_RALT,' ', 0 , KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, // 3
- KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10,0 , 0 , KEY_HOME,
- KEY_UPARROW,KEY_PGUP,'-',KEY_LEFTARROW,'5',KEY_RIGHTARROW,'+',KEY_END, //4
- KEY_DOWNARROW,KEY_PGDN,KEY_INS,KEY_DEL,0,0, 0, KEY_F11,
- KEY_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
- 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
- };
- //==========================================================================
- //--------------------------------------------------------------------------
- //
- // FUNC I_GetTime
- //
- // Returns time in 1/35th second tics.
- //
- //--------------------------------------------------------------------------
- int I_GetTime (void)
- {
- #ifdef NOTIMER
- ticcount++;
- #endif
- return(ticcount);
- }
- //--------------------------------------------------------------------------
- //
- // PROC I_ColorBorder
- //
- //--------------------------------------------------------------------------
- /*
- void I_ColorBorder(void)
- {
- int i;
- I_WaitVBL(1);
- _outbyte(PEL_WRITE_ADR, 0);
- for(i = 0; i < 3; i++)
- {
- _outbyte(PEL_DATA, 63);
- }
- }
- */
- //--------------------------------------------------------------------------
- //
- // PROC I_UnColorBorder
- //
- //--------------------------------------------------------------------------
- /*
- void I_UnColorBorder(void)
- {
- int i;
- I_WaitVBL(1);
- _outbyte(PEL_WRITE_ADR, 0);
- for(i = 0; i < 3; i++)
- {
- _outbyte(PEL_DATA, 0);
- }
- }
- */
- /*
- ============================================================================
- USER INPUT
- ============================================================================
- */
- //--------------------------------------------------------------------------
- //
- // PROC I_WaitVBL
- //
- //--------------------------------------------------------------------------
- void I_WaitVBL(int vbls)
- {
- int stat;
- if(novideo)
- {
- return;
- }
- while(vbls--)
- {
- do
- {
- stat = inp(STATUS_REGISTER_1);
- if(stat&8)
- {
- break;
- }
- } while(1);
- do
- {
- stat = inp(STATUS_REGISTER_1);
- if((stat&8) == 0)
- {
- break;
- }
- } while(1);
- }
- }
- //--------------------------------------------------------------------------
- //
- // PROC I_SetPalette
- //
- // Palette source must use 8 bit RGB elements.
- //
- //--------------------------------------------------------------------------
- void I_SetPalette(byte *palette)
- {
- int i;
- if(novideo)
- {
- return;
- }
- I_WaitVBL(1);
- _outbyte(PEL_WRITE_ADR, 0);
- for(i = 0; i < 768; i++)
- {
- _outbyte(PEL_DATA, (gammatable[usegamma][*palette++])>>2);
- }
- }
- /*
- ============================================================================
- GRAPHICS MODE
- ============================================================================
- */
- byte *pcscreen, *destscreen, *destview;
- /*
- ==============
- =
- = I_Update
- =
- ==============
- */
- int UpdateState;
- extern int screenblocks;
- void I_Update (void)
- {
- int i;
- byte *dest;
- int tics;
- static int lasttic;
- //
- // blit screen to video
- //
- if(DisplayTicker)
- {
- if(screenblocks > 9 || UpdateState&(I_FULLSCRN|I_MESSAGES))
- {
- dest = (byte *)screen;
- }
- else
- {
- dest = (byte *)pcscreen;
- }
- tics = ticcount-lasttic;
- lasttic = ticcount;
- if(tics > 20)
- {
- tics = 20;
- }
- for(i = 0; i < tics; i++)
- {
- *dest = 0xff;
- dest += 2;
- }
- for(i = tics; i < 20; i++)
- {
- *dest = 0x00;
- dest += 2;
- }
- }
- //memset(pcscreen, 255, SCREENHEIGHT*SCREENWIDTH);
- if(UpdateState == I_NOUPDATE)
- {
- return;
- }
- if(UpdateState&I_FULLSCRN)
- {
- memcpy(pcscreen, screen, SCREENWIDTH*SCREENHEIGHT);
- UpdateState = I_NOUPDATE; // clear out all draw types
- }
- if(UpdateState&I_FULLVIEW)
- {
- if(UpdateState&I_MESSAGES && screenblocks > 7)
- {
- for(i = 0; i <
- (viewwindowy+viewheight)*SCREENWIDTH; i += SCREENWIDTH)
- {
- memcpy(pcscreen+i, screen+i, SCREENWIDTH);
- }
- UpdateState &= ~(I_FULLVIEW|I_MESSAGES);
- }
- else
- {
- for(i = viewwindowy*SCREENWIDTH+viewwindowx; i <
- (viewwindowy+viewheight)*SCREENWIDTH; i += SCREENWIDTH)
- {
- memcpy(pcscreen+i, screen+i, viewwidth);
- }
- UpdateState &= ~I_FULLVIEW;
- }
- }
- if(UpdateState&I_STATBAR)
- {
- memcpy(pcscreen+SCREENWIDTH*(SCREENHEIGHT-SBARHEIGHT),
- screen+SCREENWIDTH*(SCREENHEIGHT-SBARHEIGHT),
- SCREENWIDTH*SBARHEIGHT);
- UpdateState &= ~I_STATBAR;
- }
- if(UpdateState&I_MESSAGES)
- {
- memcpy(pcscreen, screen, SCREENWIDTH*28);
- UpdateState &= ~I_MESSAGES;
- }
- // memcpy(pcscreen, screen, SCREENHEIGHT*SCREENWIDTH);
- }
- //--------------------------------------------------------------------------
- //
- // PROC I_InitGraphics
- //
- //--------------------------------------------------------------------------
- void I_InitGraphics(void)
- {
- if(novideo)
- {
- return;
- }
- //grmode = true;
- regs.w.ax = 0x13;
- int386(0x10, (const union REGS *)®s, ®s);
- pcscreen = destscreen = (byte *)0xa0000;
- I_SetPalette(W_CacheLumpName("PLAYPAL", PU_CACHE));
- }
- //--------------------------------------------------------------------------
- //
- // PROC I_ShutdownGraphics
- //
- //--------------------------------------------------------------------------
- void I_ShutdownGraphics(void)
- {
- byte mode;
- // don't reset mode if it didn't get set
- mode = *(byte *)0x449;
- if(mode == 0x12 || mode == 0x13)
- {
- regs.w.ax = 3;
- int386(0x10, ®s, ®s); // back to text mode
- }
- }
- //--------------------------------------------------------------------------
- //
- // PROC I_ReadScreen
- //
- // Reads the screen currently displayed into a linear buffer.
- //
- //--------------------------------------------------------------------------
- /*
- void I_ReadScreen(byte *scr)
- {
- memcpy(scr, screen, SCREENWIDTH*SCREENHEIGHT);
- }
- */
- //===========================================================================
- /*
- ===================
- =
- = I_StartTic
- =
- // called by D_DoomLoop
- // called before processing each tic in a frame
- // can call D_PostEvent
- // asyncronous interrupt functions should maintain private ques that are
- // read by the syncronous functions to be converted into events
- ===================
- */
- /*
- OLD STARTTIC STUFF
- void I_StartTic (void)
- {
- int k;
- event_t ev;
- I_ReadMouse ();
- //
- // keyboard events
- //
- while (kbdtail < kbdhead)
- {
- k = keyboardque[kbdtail&(KBDQUESIZE-1)];
- // if (k==14)
- // I_Error ("exited");
- kbdtail++;
- // extended keyboard shift key bullshit
- if ( (k&0x7f)==KEY_RSHIFT )
- {
- if ( keyboardque[(kbdtail-2)&(KBDQUESIZE-1)]==0xe0 )
- continue;
- k &= 0x80;
- k |= KEY_RSHIFT;
- }
- if (k==0xe0)
- continue; // special / pause keys
- if (keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0xe1)
- continue; // pause key bullshit
- if (k==0xc5 && keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0x9d)
- {
- ev.type = ev_keydown;
- ev.data1 = KEY_PAUSE;
- D_PostEvent (&ev);
- continue;
- }
- if (k&0x80)
- ev.type = ev_keyup;
- else
- ev.type = ev_keydown;
- k &= 0x7f;
- ev.data1 = k;
- //ev.data1 = scantokey[k];
- D_PostEvent (&ev);
- }
- }
- */
- #define SC_UPARROW 0x48
- #define SC_DOWNARROW 0x50
- #define SC_LEFTARROW 0x4b
- #define SC_RIGHTARROW 0x4d
- void I_StartTic (void)
- {
- int k;
- event_t ev;
- I_ReadMouse ();
- //
- // keyboard events
- //
- while (kbdtail < kbdhead)
- {
- k = keyboardque[kbdtail&(KBDQUESIZE-1)];
- kbdtail++;
- // extended keyboard shift key bullshit
- if ( (k&0x7f)==SC_LSHIFT || (k&0x7f)==SC_RSHIFT )
- {
- if ( keyboardque[(kbdtail-2)&(KBDQUESIZE-1)]==0xe0 )
- continue;
- k &= 0x80;
- k |= SC_RSHIFT;
- }
- if (k==0xe0)
- continue; // special / pause keys
- if (keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0xe1)
- continue; // pause key bullshit
- if (k==0xc5 && keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0x9d)
- {
- ev.type = ev_keydown;
- ev.data1 = KEY_PAUSE;
- H2_PostEvent (&ev);
- continue;
- }
- if (k&0x80)
- ev.type = ev_keyup;
- else
- ev.type = ev_keydown;
- k &= 0x7f;
- switch (k)
- {
- case SC_UPARROW:
- ev.data1 = KEY_UPARROW;
- break;
- case SC_DOWNARROW:
- ev.data1 = KEY_DOWNARROW;
- break;
- case SC_LEFTARROW:
- ev.data1 = KEY_LEFTARROW;
- break;
- case SC_RIGHTARROW:
- ev.data1 = KEY_RIGHTARROW;
- break;
- default:
- ev.data1 = scantokey[k];
- break;
- }
- H2_PostEvent (&ev);
- }
- }
- /*
- void I_ReadKeys (void)
- {
- int k;
- while (1)
- {
- while (kbdtail < kbdhead)
- {
- k = keyboardque[kbdtail&(KBDQUESIZE-1)];
- kbdtail++;
- printf ("0x%x\n",k);
- if (k == 1)
- I_Quit ();
- }
- }
- }
- */
- /*
- ===============
- =
- = I_StartFrame
- =
- ===============
- */
- void I_StartFrame (void)
- {
- I_JoystickEvents ();
- if(useexterndriver)
- {
- DPMIInt(i_Vector);
- }
- }
- /*
- ============================================================================
- TIMER INTERRUPT
- ============================================================================
- */
- /*
- void I_ColorBlack (int r, int g, int b)
- {
- _outbyte (PEL_WRITE_ADR,0);
- _outbyte(PEL_DATA,r);
- _outbyte(PEL_DATA,g);
- _outbyte(PEL_DATA,b);
- }
- */
- /*
- ================
- =
- = I_TimerISR
- =
- ================
- */
- int I_TimerISR (void)
- {
- ticcount++;
- return 0;
- }
- /*
- ============================================================================
- KEYBOARD
- ============================================================================
- */
- void (__interrupt __far *oldkeyboardisr) () = NULL;
- int lastpress;
- /*
- ================
- =
- = I_KeyboardISR
- =
- ================
- */
- void __interrupt I_KeyboardISR (void)
- {
- // Get the scan code
- keyboardque[kbdhead&(KBDQUESIZE-1)] = lastpress = _inbyte(0x60);
- kbdhead++;
- // acknowledge the interrupt
- _outbyte(0x20,0x20);
- }
- /*
- ===============
- =
- = I_StartupKeyboard
- =
- ===============
- */
- void I_StartupKeyboard (void)
- {
- #ifndef NOKBD
- oldkeyboardisr = _dos_getvect(KEYBOARDINT);
- _dos_setvect (0x8000 | KEYBOARDINT, I_KeyboardISR);
- #endif
- //I_ReadKeys ();
- }
- void I_ShutdownKeyboard (void)
- {
- if (oldkeyboardisr)
- _dos_setvect (KEYBOARDINT, oldkeyboardisr);
- *(short *)0x41c = *(short *)0x41a; // clear bios key buffer
- }
- /*
- ============================================================================
- MOUSE
- ============================================================================
- */
- int I_ResetMouse (void)
- {
- regs.w.ax = 0; // reset
- int386 (0x33, ®s, ®s);
- return regs.w.ax;
- }
- /*
- ================
- =
- = StartupMouse
- =
- ================
- */
- void I_StartupCyberMan(void);
- void I_StartupMouse (void)
- {
- //
- // General mouse detection
- //
- mousepresent = 0;
- if ( M_CheckParm ("-nomouse") || !usemouse )
- return;
- if (I_ResetMouse () != 0xffff)
- {
- ST_Message("Mouse: not present\n");
- return;
- }
- ST_Message("Mouse: detected\n");
- mousepresent = 1;
- I_StartupCyberMan();
- }
- /*
- ================
- =
- = ShutdownMouse
- =
- ================
- */
- void I_ShutdownMouse (void)
- {
- if (!mousepresent)
- return;
- I_ResetMouse ();
- }
- /*
- ================
- =
- = I_ReadMouse
- =
- ================
- */
- void I_ReadMouse (void)
- {
- event_t ev;
- //
- // mouse events
- //
- if (!mousepresent)
- return;
- ev.type = ev_mouse;
- memset (&dpmiregs,0,sizeof(dpmiregs));
- dpmiregs.eax = 3; // read buttons / position
- DPMIInt (0x33);
- ev.data1 = dpmiregs.ebx;
- dpmiregs.eax = 11; // read counters
- DPMIInt (0x33);
- ev.data2 = (short)dpmiregs.ecx;
- ev.data3 = -(short)dpmiregs.edx;
- H2_PostEvent (&ev);
- }
- /*
- ============================================================================
- JOYSTICK
- ============================================================================
- */
- int joyxl, joyxh, joyyl, joyyh;
- boolean WaitJoyButton (void)
- {
- int oldbuttons, buttons;
- oldbuttons = 0;
- do
- {
- I_WaitVBL (1);
- buttons = ((inp(0x201) >> 4)&1)^1;
- if (buttons != oldbuttons)
- {
- oldbuttons = buttons;
- continue;
- }
- if ( (lastpress& 0x7f) == 1 )
- {
- joystickpresent = false;
- return false;
- }
- } while ( !buttons);
- do
- {
- I_WaitVBL (1);
- buttons = ((inp(0x201) >> 4)&1)^1;
- if (buttons != oldbuttons)
- {
- oldbuttons = buttons;
- continue;
- }
- if ( (lastpress& 0x7f) == 1 )
- {
- joystickpresent = false;
- return false;
- }
- } while ( buttons);
- return true;
- }
- /*
- ===============
- =
- = I_StartupJoystick
- =
- ===============
- */
- int basejoyx, basejoyy;
- void I_StartupJoystick (void)
- {
- int centerx, centery;
- joystickpresent = 0;
- if ( M_CheckParm ("-nojoy") || !usejoystick )
- return;
- if (!I_ReadJoystick ())
- {
- joystickpresent = false;
- ST_Message("joystick not found\n ");
- return;
- }
- ST_Message("joystick found\n");
- joystickpresent = true;
- ST_RealMessage("CENTER the joystick and press button 1:");
- if (!WaitJoyButton ())
- return;
- I_ReadJoystick ();
- centerx = joystickx;
- centery = joysticky;
- ST_RealMessage("\nPush the joystick to the UPPER LEFT corner and press button 1:");
- if (!WaitJoyButton ())
- return;
- I_ReadJoystick ();
- joyxl = (centerx + joystickx)/2;
- joyyl = (centerx + joysticky)/2;
- ST_RealMessage("\nPush the joystick to the LOWER RIGHT corner and press button 1:");
- if (!WaitJoyButton ())
- return;
- I_ReadJoystick ();
- joyxh = (centerx + joystickx)/2;
- joyyh = (centery + joysticky)/2;
- ST_RealMessage("\n");
- }
- /*
- ===============
- =
- = I_JoystickEvents
- =
- ===============
- */
- void I_JoystickEvents (void)
- {
- event_t ev;
- //
- // joystick events
- //
- if (!joystickpresent)
- return;
- I_ReadJoystick ();
- ev.type = ev_joystick;
- ev.data1 = ((inp(0x201) >> 4)&15)^15;
- if (joystickx < joyxl)
- ev.data2 = -1;
- else if (joystickx > joyxh)
- ev.data2 = 1;
- else
- ev.data2 = 0;
- if (joysticky < joyyl)
- ev.data3 = -1;
- else if (joysticky > joyyh)
- ev.data3 = 1;
- else
- ev.data3 = 0;
- H2_PostEvent (&ev);
- }
- /*
- ============================================================================
- DPMI STUFF
- ============================================================================
- */
- #define REALSTACKSIZE 1024
- dpmiregs_t dpmiregs;
- unsigned realstackseg;
- void I_DivException (void);
- int I_SetDivException (void);
- /*
- void DPMIFarCall (void)
- {
- segread (&segregs);
- regs.w.ax = 0x301;
- regs.w.bx = 0;
- regs.w.cx = 0;
- regs.x.edi = (unsigned)&dpmiregs;
- segregs.es = segregs.ds;
- int386x( DPMI_INT, ®s, ®s, &segregs );
- }
- */
- void DPMIInt (int i)
- {
- dpmiregs.ss = realstackseg;
- dpmiregs.sp = REALSTACKSIZE-4;
- segread (&segregs);
- regs.w.ax = 0x300;
- regs.w.bx = i;
- regs.w.cx = 0;
- regs.x.edi = (unsigned)&dpmiregs;
- segregs.es = segregs.ds;
- int386x( DPMI_INT, ®s, ®s, &segregs );
- }
- /*
- ==============
- =
- = I_StartupDPMI
- =
- ==============
- */
- void I_StartupDPMI (void)
- {
- // extern char __begtext;
- // extern char ___argc;
- // int n,d;
- //
- // allocate a decent stack for real mode ISRs
- //
- realstackseg = (int)I_AllocLow (1024) >> 4;
- //
- // lock the entire program down
- //
- // _dpmi_lockregion (&__begtext, &___argc - &__begtext);
- //
- // catch divide by 0 exception
- //
- #if 0
- segread(&segregs);
- regs.w.ax = 0x0203; // DPMI set processor exception handler vector
- regs.w.bx = 0; // int 0
- regs.w.cx = segregs.cs;
- regs.x.edx = (int)&I_DivException;
- printf ("%x : %x\n",regs.w.cx, regs.x.edx);
- int386( DPMI_INT, ®s, ®s);
- #endif
- #if 0
- n = I_SetDivException ();
- printf ("return: %i\n",n);
- n = 100;
- d = 0;
- printf ("100 / 0 = %i\n",n/d);
- exit (1);
- #endif
- }
- /*
- ============================================================================
- TIMER INTERRUPT
- ============================================================================
- */
- void (__interrupt __far *oldtimerisr) ();
- /*
- void IO_ColorBlack (int r, int g, int b)
- {
- _outbyte (PEL_WRITE_ADR,0);
- _outbyte(PEL_DATA,r);
- _outbyte(PEL_DATA,g);
- _outbyte(PEL_DATA,b);
- }
- */
- /*
- ================
- =
- = IO_TimerISR
- =
- ================
- */
- //void __interrupt IO_TimerISR (void)
- void __interrupt __far IO_TimerISR (void)
- {
- ticcount++;
- _outbyte(0x20,0x20); // Ack the interrupt
- }
- /*
- =====================
- =
- = IO_SetTimer0
- =
- = Sets system timer 0 to the specified speed
- =
- =====================
- */
- void IO_SetTimer0(int speed)
- {
- if (speed > 0 && speed < 150)
- I_Error ("INT_SetTimer0: %i is a bad value",speed);
- _outbyte(0x43,0x36); // Change timer 0
- _outbyte(0x40,speed);
- _outbyte(0x40,speed >> 8);
- }
- /*
- ===============
- =
- = IO_StartupTimer
- =
- ===============
- */
- /*
- void IO_StartupTimer (void)
- {
- oldtimerisr = _dos_getvect(TIMERINT);
- _dos_setvect (0x8000 | TIMERINT, IO_TimerISR);
- IO_SetTimer0 (VBLCOUNTER);
- }
- */
- void IO_ShutdownTimer (void)
- {
- if (oldtimerisr)
- {
- IO_SetTimer0 (0); // back to 18.4 ips
- _dos_setvect (TIMERINT, oldtimerisr);
- }
- }
- //===========================================================================
- /*
- ===============
- =
- = I_Init
- =
- = hook interrupts and set graphics mode
- =
- ===============
- */
- void I_Init (void)
- {
- extern void I_StartupTimer(void);
- novideo = M_CheckParm("novideo");
- ST_Message(" I_StartupDPMI\n");
- I_StartupDPMI();
- ST_Message(" I_StartupMouse ");
- I_StartupMouse();
- // tprintf("I_StartupJoystick ",1);
- // I_StartupJoystick();
- // tprintf("I_StartupKeyboard ",1);
- // I_StartupKeyboard();
- ST_Message(" S_Init... ");
- S_Init();
- //IO_StartupTimer();
- S_Start();
- }
- /*
- ===============
- =
- = I_Shutdown
- =
- = return to default system state
- =
- ===============
- */
- void I_Shutdown (void)
- {
- I_ShutdownGraphics ();
- IO_ShutdownTimer ();
- S_ShutDown ();
- I_ShutdownMouse ();
- I_ShutdownKeyboard ();
- IO_SetTimer0 (0);
- }
- /*
- ================
- =
- = I_Error
- =
- ================
- */
- void I_Error (char *error, ...)
- {
- union REGS regs;
- va_list argptr;
- D_QuitNetGame ();
- I_Shutdown ();
- va_start (argptr,error);
- regs.x.eax = 0x3;
- int386(0x10, ®s, ®s);
- vprintf (error,argptr);
- va_end (argptr);
- printf ("\n");
- exit (1);
- }
- //--------------------------------------------------------------------------
- //
- // I_Quit
- //
- // Shuts down net game, saves defaults, prints the exit text message,
- // goes to text mode, and exits.
- //
- //--------------------------------------------------------------------------
- void I_Quit(void)
- {
- D_QuitNetGame();
- M_SaveDefaults();
- I_Shutdown();
- // scr = (byte *)W_CacheLumpName("ENDTEXT", PU_CACHE);
- /*
- memcpy((void *)0xb8000, scr, 80*25*2);
- regs.w.ax = 0x0200;
- regs.h.bh = 0;
- regs.h.dl = 0;
- regs.h.dh = 23;
- int386(0x10, (const union REGS *)®s, ®s); // Set text pos
- _settextposition(24, 1);
- */
- printf("\nHexen: Beyond Heretic\n");
- exit(0);
- }
- /*
- ===============
- =
- = I_ZoneBase
- =
- ===============
- */
- byte *I_ZoneBase (int *size)
- {
- int meminfo[32];
- int heap;
- byte *ptr;
- memset (meminfo,0,sizeof(meminfo));
- segread(&segregs);
- segregs.es = segregs.ds;
- regs.w.ax = 0x500; // get memory info
- regs.x.edi = (int)&meminfo;
- int386x( 0x31, ®s, ®s, &segregs );
- heap = meminfo[0];
- ST_Message (" DPMI memory: 0x%x, ",heap);
- ST_Message ("Maxzone: 0x%x\n", maxzone);
- do
- {
- heap -= 0x10000; // leave 64k alone
- if (heap > maxzone)
- heap = maxzone;
- ptr = malloc (heap);
- } while (!ptr);
- ST_Message (" 0x%x allocated for zone, ", heap);
- ST_Message ("ZoneBase: 0x%X\n", (int)ptr);
- if (heap < 0x180000)
- I_Error (" Insufficient DPMI memory!");
- #if 0
- regs.w.ax = 0x501; // allocate linear block
- regs.w.bx = heap>>16;
- regs.w.cx = heap&0xffff;
- int386( 0x31, ®s, ®s);
- if (regs.w.cflag)
- I_Error (" Couldn't allocate DPMI memory!");
- block = (regs.w.si << 16) + regs.w.di;
- #endif
- *size = heap;
- return ptr;
- }
- /*
- =============
- =
- = I_AllocLow
- =
- =============
- */
- byte *I_AllocLow (int length)
- {
- byte *mem;
- // DPMI call 100h allocates DOS memory
- segread(&segregs);
- regs.w.ax = 0x0100; // DPMI allocate DOS memory
- regs.w.bx = (length+15) / 16;
- int386( DPMI_INT, ®s, ®s);
- // segment = regs.w.ax;
- // selector = regs.w.dx;
- if (regs.w.cflag != 0)
- I_Error ("I_AllocLow: DOS alloc of %i failed, %i free",
- length, regs.w.bx*16);
- mem = (void *) ((regs.x.eax & 0xFFFF) << 4);
- memset (mem,0,length);
- return mem;
- }
- /*
- ============================================================================
- NETWORKING
- ============================================================================
- */
- /* // FUCKED LINES
- typedef struct
- {
- char priv[508];
- } doomdata_t;
- */ // FUCKED LINES
- #define DOOMCOM_ID 0x12345678l
- /* // FUCKED LINES
- typedef struct
- {
- long id;
- short intnum; // DOOM executes an int to execute commands
- // communication between DOOM and the driver
- short command; // CMD_SEND or CMD_GET
- short remotenode; // dest for send, set by get (-1 = no packet)
- short datalength; // bytes in doomdata to be sent
- // info common to all nodes
- short numnodes; // console is allways node 0
- short ticdup; // 1 = no duplication, 2-5 = dup for slow nets
- short extratics; // 1 = send a backup tic in every packet
- short deathmatch; // 1 = deathmatch
- short savegame; // -1 = new game, 0-5 = load savegame
- short episode; // 1-3
- short map; // 1-9
- short skill; // 1-5
- // info specific to this node
- short consoleplayer;
- short numplayers;
- short angleoffset; // 1 = left, 0 = center, -1 = right
- short drone; // 1 = drone
- // packet data to be sent
- doomdata_t data;
- } doomcom_t;
- */ // FUCKED LINES
- extern doomcom_t *doomcom;
- /*
- ====================
- =
- = I_InitNetwork
- =
- ====================
- */
- void I_InitNetwork (void)
- {
- int i;
- i = M_CheckParm ("-net");
- if (!i)
- {
- //
- // single player game
- //
- doomcom = malloc (sizeof (*doomcom) );
- memset (doomcom, 0, sizeof(*doomcom) );
- netgame = false;
- doomcom->id = DOOMCOM_ID;
- doomcom->numplayers = doomcom->numnodes = 1;
- doomcom->deathmatch = false;
- doomcom->consoleplayer = 0;
- doomcom->ticdup = 1;
- doomcom->extratics = 0;
- return;
- }
- netgame = true;
- doomcom = (doomcom_t *)atoi(myargv[i+1]);
- //DEBUG
- doomcom->skill = startskill;
- doomcom->episode = startepisode;
- doomcom->map = startmap;
- doomcom->deathmatch = deathmatch;
- }
- void I_NetCmd (void)
- {
- if (!netgame)
- I_Error ("I_NetCmd when not in netgame");
- DPMIInt (doomcom->intnum);
- }
- //=========================================================================
- //
- // I_CheckExternDriver
- //
- // Checks to see if a vector, and an address for an external driver
- // have been passed.
- //=========================================================================
- void I_CheckExternDriver(void)
- {
- int i;
- if(!(i = M_CheckParm("-externdriver")))
- {
- return;
- }
- i_ExternData = (externdata_t *)atoi(myargv[i+1]);
- i_Vector = i_ExternData->vector;
- useexterndriver = true;
- }
- //=========================================================================
- //=========================================================================
- // Hi-Res (mode 12) stuff
- //=========================================================================
- //=========================================================================
- //==========================================================================
- //
- // SetVideoModeHR - Set video mode to 640x480x16
- //
- //==========================================================================
- void SetVideoModeHR(void)
- {
- union REGS regs;
- regs.x.eax = 0x12;
- int386(VID_INT, ®s, ®s);
- }
- //==========================================================================
- //
- // ClearScreenHR - Clear the screen to color 0
- //
- //==========================================================================
- void ClearScreenHR(void)
- {
- BITPLANE(MASK_PLANE0|MASK_PLANE1|MASK_PLANE2|MASK_PLANE3);
- memset((char *)0xa0000,0,38400);
- }
- //==========================================================================
- //
- // SlamHR - copy 4-plane buffer to screen
- //
- //==========================================================================
- void SlamHR(char *buffer)
- {
- BITPLANE(MASK_PLANE0);
- memcpy((char *)0xA0000, buffer+P0OFFSET, 38400);
- BITPLANE(MASK_PLANE1);
- memcpy((char *)0xA0000, buffer+P1OFFSET, 38400);
- BITPLANE(MASK_PLANE2);
- memcpy((char *)0xA0000, buffer+P2OFFSET, 38400);
- BITPLANE(MASK_PLANE3);
- memcpy((char *)0xA0000, buffer+P3OFFSET, 38400);
- }
- //==========================================================================
- //
- // SlamHR - copy 4-plane buffer to screen
- //
- // X and Width should be a multiple of 8
- // src should be 4 planes of block size, back to back
- //==========================================================================
- void SlamBlockHR(int x, int y, int w, int h, char *src)
- {
- int srcwid = w>>3;
- char *dest = ((char *)0xA0000) + (y*(640/8)) + (x>>3);
- char *dst;
- int i;
- VB_SYNC;
- BITPLANE(MASK_PLANE0);
- dst = dest;
- for ( i=0; i<h; i++ )
- {
- memcpy(dst, src, srcwid);
- dst += 640/8;
- src += srcwid;
- }
- BITPLANE(MASK_PLANE1);
- dst = dest;
- for ( i=0; i<h; i++ )
- {
- memcpy(dst, src, srcwid);
- dst += 640/8;
- src += srcwid;
- }
- BITPLANE(MASK_PLANE2);
- dst = dest;
- for ( i=0; i<h; i++ )
- {
- memcpy(dst, src, srcwid);
- dst += 640/8;
- src += srcwid;
- }
- BITPLANE(MASK_PLANE3);
- dst = dest;
- for ( i=0; i<h; i++ )
- {
- memcpy(dst, src, srcwid);
- dst += 640/8;
- src += srcwid;
- }
- }
- //==========================================================================
- //
- // InitPaletteHR
- //
- //==========================================================================
- void InitPaletteHR(void)
- {
- int i;
- union REGS regs;
- // Set palette registers to point into color registers
- for ( i=0; i<16; i++ )
- {
- regs.x.eax = (0x10<<8)|0x00;
- regs.x.ebx = (i<<8)|i;
- int386(VID_INT, ®s, ®s);
- }
- }
- //==========================================================================
- //
- // SetPaletteHR - Set the HR palette
- //
- //==========================================================================
- void SetPaletteHR(byte *palette)
- {
- int i;
- VB_SYNC;
- outp(PEL_WRITE_ADR, 0);
- for(i = 0; i < 16*3; i++)
- {
- outp(PEL_DATA, (*palette++));
- }
- }
- //==========================================================================
- //
- // GetPaletteHR - Get the HR palette
- //
- //==========================================================================
- void GetPaletteHR(byte *palette)
- {
- int i;
- outp(PEL_READ_ADR, 0);
- for (i=0; i<16*3; i++)
- {
- *palette++ = inp(PEL_DATA);
- }
- }
- //==========================================================================
- //
- // FadeToPaletteHR
- //
- //==========================================================================
- void FadeToPaletteHR(byte *palette)
- {
- int i,j;
- int steps=140; // two-seconds
- byte basep[16*3];
- byte work[16*3];
- int delta;
- GetPaletteHR(basep);
- for(i = 0; i < steps; i++)
- {
- for(j = 0; j < 16*3; j++)
- {
- delta = palette[j]-basep[j];
- work[j] = basep[j]+delta*i/steps;
- }
- SetPaletteHR(work);
- }
- SetPaletteHR(palette);
- }
- //==========================================================================
- //
- // FadeToBlackHR - Fades the palette out to black
- //
- //==========================================================================
- /*
- void FadeToBlackHR(void)
- {
- char work[16*3];
- char base[16*3];
- int i,j,steps=70;
- GetPaletteHR(base);
- for (i=0; i<steps; i++)
- {
- for (j=0; j<16*3; j++)
- {
- work[j] = base[j]-(base[j]*i/steps);
- }
- VB_SYNC;
- SetPaletteHR(work);
- }
- memset(work,0,16*3);
- SetPaletteHR(work);
- }
- */
- //==========================================================================
- //
- // BlackPaletteHR - Instantly blacks out the palette
- //
- //==========================================================================
- void BlackPaletteHR(void)
- {
- char blackpal[16*3];
- memset(blackpal,0,16*3);
- SetPaletteHR(blackpal);
- }
- //==========================================================================
- //
- //
- // I_StartupReadKeys
- //
- //
- //==========================================================================
- void I_StartupReadKeys(void)
- {
- int k;
- while (kbdtail < kbdhead)
- {
- k = keyboardque[kbdtail&(KBDQUESIZE-1)];
- kbdtail++;
- if (k == 1)
- I_Quit ();
- }
- }
|