shithub: qk2

Download patch

ref: a3500266c93417a33976c19525dbc5fb010ccecb
parent: 7c0675ccfd6cf7c7d37be11f86ecb11c363b0962
author: qwx <>
date: Sat Jun 2 10:47:17 EDT 2018

vid: clean up

- prevent /dev/mouse being closed before /dev/draw/new by calling IN_Init
  after initdraw
- simplify video code, minor speed bump

--- a/cl_main.c
+++ b/cl_main.c
@@ -871,7 +871,6 @@
 			Com_Printf ("Command packet from remote host.  Ignored.\n");
 			return;
 		}
-		Sys_AppActivate ();
 		s = MSG_ReadString (&net_message);
 		Cbuf_AddText (s);
 		Cbuf_AddText ("\n");
@@ -1641,7 +1640,6 @@
 	CL_PredictMovement ();
 
 	// allow rendering DLL change
-	VID_CheckChanges ();
 	if (!cl.refresh_prepped && cls.state == ca_active)
 		CL_PrepRefresh ();
 
@@ -1697,17 +1695,19 @@
 */
 void CL_Init (void)
 {
-	IN_Init();
 
-	if(dedicated->value)
+	if(dedicated->value){
+		IN_Init();
 		return;		// nothing running on the client
+	}
 
 	// all archived variables will now be loaded
 
 	Con_Init ();
 	initsnd();	
-	VID_Init ();
-	
+	initfb();
+	IN_Init();
+
 	V_Init ();
 	
 	net_message.data = net_message_buffer;
@@ -1751,5 +1751,5 @@
 	CDAudio_Shutdown ();
 	shutsnd();
 	IN_Shutdown ();
-	VID_Shutdown();
+	R_Shutdown();
 }
--- a/dat.h
+++ b/dat.h
@@ -2195,7 +2195,7 @@
 	 * filled to eliminate mip map edge errors, and pics have an implicit
 	 * "pics/" prepended to the name. (a pic name that starts with a slash
 	 * will not use the "pics/" prefix or the ".pcx" postfix) */
-	qboolean	(*Init)(void *, void *);
+	qboolean	(*Init)(void);
 	void	(*Shutdown)(void);
 	void	(*BeginRegistration)(char *);
 	model_t*	(*RegisterModel)(char *);
@@ -2215,7 +2215,6 @@
 	void	(*CinematicSetPalette)(uchar *);
 	void	(*BeginFrame)(float);
 	void	(*EndFrame)(void);
-	void	(*AppActivate)(qboolean);
 };
 extern refexport_t re;
 
--- a/fns.h
+++ b/fns.h
@@ -1,3 +1,6 @@
+void	flipfb(void);
+void	setpal(uchar*);
+void	initfb(void);
 void	shutsnd(void);
 void	restartsnd(void);
 void	initsnd(void);
@@ -59,8 +62,6 @@
 void	Sys_FindClose(void);
 int	Sys_Milliseconds(void);
 void	Sys_Init(void);
-void	Sys_AppActivate(void);
-void	Sys_UnloadGame(void);
 char*	Sys_ConsoleInput(void);
 void	Sys_ConsoleOutput(char *);
 void	Sys_SendKeyEvents(void);
@@ -67,7 +68,6 @@
 void	Sys_Error(char *, ...);
 void	Sys_Quit(void);
 char*	Sys_GetClipboardData(void);
-void	Sys_CopyProtect(void);
 void	Sys_MakeCodeWriteable(ulong, ulong);
 void	Sys_SetFPCW(void);
 vlong	flen(int);
@@ -441,7 +441,7 @@
 void	R_Register(void);
 void	R_UnRegister(void);
 void	Draw_InitLocal(void);
-qboolean	R_Init(void *, void *);
+qboolean	R_Init(void);
 void	R_Shutdown(void);
 void	R_InitCaches(void);
 void	D_FlushCaches(void);
@@ -547,14 +547,6 @@
 void	VID_MenuDraw(void);
 char*	VID_MenuKey(int);
 
-void	SWimp_BeginFrame(float);
-void	SWimp_EndFrame(void);
-int	SWimp_Init(void *, void *);
-void	SWimp_SetPalette(uchar *);
-void	SWimp_Shutdown(void);
-rserr_t	SWimp_SetMode(int *, int *, int, qboolean);
-void	SWimp_AppActivate(qboolean);
-
 void	S_Init(void);
 void	S_Shutdown(void);
 void	S_StartSound(vec3_t, int, int, sfx_t *, float,  float, float);
@@ -567,10 +559,7 @@
 sfx_t*	S_RegisterSound(char *);
 void	S_EndRegistration(void);
 void	CL_GetEntitySoundOrigin(int, vec3_t);
-qboolean	SNDDMA_Init(void);
 int	SNDDMA_GetDMAPos(void);
-void	SNDDMA_Shutdown(void);
-void	SNDDMA_BeginPainting(void);
 void	SNDDMA_Submit(void);
 wavinfo_t	GetWavinfo(char *, uchar *, int);
 void	S_InitScaletable(void);
--- a/in.c
+++ b/in.c
@@ -321,7 +321,7 @@
 		if((n = read(fd, buf, sizeof buf)) != 1+4*12){
 			if(n < 0 || ++nerr > 10)
 				break;
-			fprint(2, "mproc:read: bad count %d not 49: %r\n", n);
+			fprint(2, "mproc: bad count %d not 49: %r\n", n);
 			continue;
 		}
 		nerr = 0;
--- a/r_main.c
+++ b/r_main.c
@@ -277,7 +277,7 @@
 R_Init
 ===============
 */
-qboolean R_Init( void *hInstance, void *wndProc )
+qboolean R_Init(void)
 {
 	R_InitImages ();
 	Mod_Init ();
@@ -300,7 +300,6 @@
 
 	R_Register ();
 	Draw_GetPalette ();
-	SWimp_Init( hInstance, wndProc );
 
 	// create the window
 	R_BeginFrame( 0 );
@@ -329,8 +328,6 @@
 	R_UnRegister();
 	Mod_FreeAll();
 	R_ShutdownImages();
-
-	SWimp_Shutdown();
 }
 
 /*
@@ -923,7 +920,7 @@
 	}
 
 	R_GammaCorrectAndSetPalette((uchar *)palette[0]);
-//	SWimp_SetPalette( palette[0] );
+//	setpal(palette[0]);
 }
 
 //=======================================================================
@@ -1071,42 +1068,10 @@
 
 	while ( sw_mode->modified || vid_fullscreen->modified )
 	{
-		rserr_t err;
-
-		/*
-		** if this returns rserr_invalid_fullscreen then it set the mode but not as a
-		** fullscreen mode, e.g. 320x200 on a system that doesn't support that res
-		*/
-		if ( ( err = SWimp_SetMode( &vid.width, &vid.height, sw_mode->value, vid_fullscreen->value ) ) == rserr_ok )
-		{
-			R_InitGraphics( vid.width, vid.height );
-
-			sw_state.prev_mode = sw_mode->value;
-			vid_fullscreen->modified = false;
-			sw_mode->modified = false;
-		}
-		else
-		{
-			if ( err == rserr_invalid_mode )
-			{
-				ri.Cvar_SetValue( "sw_mode", sw_state.prev_mode );
-				ri.Con_Printf( PRINT_ALL, "ref_soft::R_BeginFrame() - could not set mode\n" );
-			}
-			else if ( err == rserr_invalid_fullscreen )
-			{
-				R_InitGraphics( vid.width, vid.height );
-
-				ri.Cvar_SetValue( "vid_fullscreen", 0);
-				ri.Con_Printf( PRINT_ALL, "ref_soft::R_BeginFrame() - fullscreen unavailable in this mode\n" );
-				sw_state.prev_mode = sw_mode->value;
-//				vid_fullscreen->modified = false;
-//				sw_mode->modified = false;
-			}
-			else
-			{
-				ri.Sys_Error( ERR_FATAL, "ref_soft::R_BeginFrame() - catastrophic mode change failure\n" );
-			}
-		}
+		R_InitGraphics(vid.width, vid.height);
+		sw_state.prev_mode = sw_mode->value;
+		vid_fullscreen->modified = false;
+		sw_mode->modified = false;
 	}
 }
 
@@ -1121,8 +1086,7 @@
 		sw_state.currentpalette[i*4+1] = sw_state.gammatable[palette[i*4+1]];
 		sw_state.currentpalette[i*4+2] = sw_state.gammatable[palette[i*4+2]];
 	}
-
-	SWimp_SetPalette( sw_state.currentpalette );
+	setpal(sw_state.currentpalette);
 }
 
 void
@@ -1140,8 +1104,7 @@
 		for (j=0 ; j<w ; j++)
 			d[j] = 0;
 	}
-	// flush it to the screen
-	SWimp_EndFrame ();
+	flipfb();
 
 	if ( palette )
 	{
@@ -1340,9 +1303,7 @@
 
 	re.CinematicSetPalette = R_CinematicSetPalette;
 	re.BeginFrame = R_BeginFrame;
-	re.EndFrame = SWimp_EndFrame;
-
-	re.AppActivate = SWimp_AppActivate;
+	re.EndFrame = flipfb;
 
 	Swap_Init ();
 
--- a/snd.c
+++ b/snd.c
@@ -54,11 +54,6 @@
 }
 
 void
-SNDDMA_BeginPainting(void)
-{
-}
-
-void
 shutsnd(void)
 {
 	if(afd < 0)
@@ -82,12 +77,13 @@
 			continue;
 		if(s->cache)
 			Z_Free(s->cache);
-		memset(s, 0, sizeof*s);
+		memset(s, 0, sizeof *s);
 	}
 	num_sfx = 0;
 	soundtime = 0;
 	paintedtime = 0;
 	S_StopAllSounds();
+	wpos = 0;
 	CL_RegisterSounds();
 }
 
--- a/snd_dma.c
+++ b/snd_dma.c
@@ -625,21 +625,11 @@
 */
 void S_ClearBuffer (void)
 {
-	int		clear;
-		
 	if (!sound_started)
 		return;
-
 	s_rawend = 0;
-
-	if (dma.samplebits == 8)
-		clear = 0x80;
-	else
-		clear = 0;
-
-	SNDDMA_BeginPainting ();
-	if (dma.buffer)
-		memset(dma.buffer, clear, dma.samples * dma.samplebits/8);
+	if(dma.buffer)
+		memset(dma.buffer, 0, dma.samples * dma.samplebits/8);
 	SNDDMA_Submit ();
 }
 
@@ -961,9 +951,6 @@
 
 	if (!sound_started)
 		return;
-
-	SNDDMA_BeginPainting ();
-
 	if (!dma.buffer)
 		return;
 
--- a/sv_game.c
+++ b/sv_game.c
@@ -292,7 +292,6 @@
 	if (!ge)
 		return;
 	ge->Shutdown ();
-	Sys_UnloadGame ();
 	ge = NULL;
 }
 
--- a/sv_init.c
+++ b/sv_init.c
@@ -319,18 +319,10 @@
 	{
 		if (maxclients->value <= 1 || maxclients->value > 4)
 			Cvar_FullSet ("maxclients", "4", CVAR_SERVERINFO | CVAR_LATCH);
-#ifdef COPYPROTECT
-		if (!sv.attractloop && !dedicated->value)
-			Sys_CopyProtect ();
-#endif
 	}
 	else	// non-deathmatch, non-coop is one player
 	{
 		Cvar_FullSet ("maxclients", "1", CVAR_SERVERINFO | CVAR_LATCH);
-#ifdef COPYPROTECT
-		if (!sv.attractloop)
-			Sys_CopyProtect ();
-#endif
 	}
 
 	svs.spawncount = rand();
--- a/sys.c
+++ b/sys.c
@@ -340,16 +340,6 @@
 }
 
 void
-Sys_UnloadGame(void)
-{
-}
-
-void
-Sys_AppActivate(void)
-{
-}
-
-void
 Sys_SendKeyEvents(void)
 {
 	KBD_Update();
@@ -363,11 +353,6 @@
 }
 
 void
-Sys_CopyProtect(void)
-{
-}
-
-void
 Sys_Quit(void)
 {
 	chanfree(fuckchan);
@@ -415,6 +400,7 @@
 	setfcr(getfcr() & ~(FPOVFL|FPUNFL|FPINVAL|FPZDIV));	/* assumed ignored in code */
 	notify(croak);
 
+	srand(getpid());
 	Qcommon_Init(argc, argv);
 
 	oldtime = Sys_Milliseconds();
--- a/vid.c
+++ b/vid.c
@@ -13,88 +13,39 @@
 typedef ulong PIXEL;
 
 static int rwon;
-static uchar *framebuf;
-static Image *fbim;
-static PIXEL st2d_8to24table[256];
-static int shiftmask_fl;
-static int r_shift, g_shift, b_shift;
-static uint r_mask, g_mask, b_mask;
+static uchar *fb;
+static Image *fbi;
+static s32int fbpal[256];
 
 refexport_t GetRefAPI(refimport_t);
 
 
 static void
-shiftmask_init(void)
+drawfb(void)
 {
-	uint x;
+	uchar *s;
+	int n, we, w8, wr, *d, dy;
 
-	r_mask = 0xff0000;
-	g_mask = 0xff00;
-	b_mask = 0xff;
-	for(r_shift = -8, x = 1; x < r_mask; x <<= 1)
-		r_shift++;
-	for(g_shift = -8, x = 1; x < g_mask; x <<= 1)
-		g_shift++;
-	for(b_shift = -8, x = 1; x < b_mask; x <<= 1)
-		b_shift++;
-	shiftmask_fl = 1;
-}
-
-static PIXEL
-rgb24(int r, int g, int b)
-{
-	PIXEL p = 0;
-
-	if(shiftmask_fl == 0)
-		shiftmask_init();
-
-	if(r_shift > 0)
-		p = r<<r_shift & r_mask;
-	else if(r_shift < 0)
-        	p = r>>-r_shift & r_mask;
-	else
-		p |= r & r_mask;
-	if(g_shift > 0)
-		p |= g<<g_shift & g_mask;
-	else if(g_shift < 0)
-		p |= g>>-g_shift & g_mask;
-	else
-		p |= g & g_mask;
-	if(b_shift > 0)
-		p |= b<<b_shift & b_mask;
-	else if(b_shift < 0)
-		p |= b>>-b_shift & b_mask;
-	else
-		p |= b & b_mask;
-	return p;
-}
-
-static void
-st3_fixup(void)
-{
-	int x, y;
-	uchar *src;
-	PIXEL *dest;
-
-	for(y = 0; y < vid.height; y++){
-		src = &framebuf[y*vid.rowbytes];
-		dest = (PIXEL*)src;
-
-		/* vid.width % 8 not always 0
-		for(x = vid.width-1; x >= 0; x -= 8) {
-			dest[x  ] = st2d_8to24table[src[x  ]];
-			dest[x-1] = st2d_8to24table[src[x-1]];
-			dest[x-2] = st2d_8to24table[src[x-2]];
-			dest[x-3] = st2d_8to24table[src[x-3]];
-			dest[x-4] = st2d_8to24table[src[x-4]];
-			dest[x-5] = st2d_8to24table[src[x-5]];
-			dest[x-6] = st2d_8to24table[src[x-6]];
-			dest[x-7] = st2d_8to24table[src[x-7]];
+	we = vid.width - 1;
+	w8 = vid.width + 7 >> 3;
+	wr = vid.width % 8;
+	dy = vid.height * vid.rowbytes;
+	while((dy -= vid.rowbytes) >= 0){
+		s = fb + dy;
+		d = ((int *)s) + we;
+		s += we;
+		n = w8;
+		switch(wr){
+		case 0:	do{	*d-- = fbpal[*s--];
+		case 7:		*d-- = fbpal[*s--];
+		case 6:		*d-- = fbpal[*s--];
+		case 5:		*d-- = fbpal[*s--];
+		case 4:		*d-- = fbpal[*s--];
+		case 3:		*d-- = fbpal[*s--];
+		case 2:		*d-- = fbpal[*s--];
+		case 1:		*d-- = fbpal[*s--];
+			}while(--n > 0);
 		}
-		*/
-
-		for(x = vid.width-1; x >= 0; x--)
-			dest[x] = st2d_8to24table[src[x]];
 	}
 }
 
@@ -103,92 +54,45 @@
 {
 	vid.width = Dx(screen->r);
 	vid.height = Dy(screen->r);
-	if(framebuf != nil){
-		free(framebuf);
-		framebuf = nil;
-	}
-	if(fbim != nil){
-		freeimage(fbim);
-		fbim = nil;
-	}
-	if((framebuf = malloc(sizeof *framebuf * vid.width * vid.height * screen->depth/8)) == nil)
-		sysfatal("resetfb:malloc: %r");
-	if((fbim = allocimage(display, Rect(0, 0, vid.width, vid.height), XRGB32, 0, 0)) == nil)
-		sysfatal("resetfb: %r");
-	vid.buffer = framebuf;
-	vid.rowbytes = vid.width * screen->depth/8;
+	free(fb);
+	fb = emalloc(vid.width * vid.height * sizeof *fbpal);
+	freeimage(fbi);
+	if((fbi = allocimage(display, Rect(0, 0, vid.width, vid.height), XRGB32, 0, 0)) == nil)
+		sysfatal("allocimage: %r");
+	vid.buffer = fb;
+	vid.rowbytes = vid.width * sizeof *fbpal;
 	center = addpt(screen->r.min, Pt(vid.width/2, vid.height/2));
 	sw_mode->modified = true;	/* make ref_soft refresh its shit */
 }
 
-int
-SWimp_Init(void *, void *)
-{
-	srand(getpid());
-
-	if(initdraw(nil, nil, "quake2") < 0)
-		sysfatal("VID_Init:initdraw: %r\n");
-	resetfb();
-	rwon = 1;
-	vidref_val = VIDREF_SOFT;
-	return 1;
-}
-
-/* copy backbuffer to front buffer */
 void
-SWimp_EndFrame(void)
+flipfb(void)
 {
-	if(resized){		/* skip frame if window resizes */
+	if(resized){
 		resized = 0;
 		if(getwindow(display, Refnone) < 0)
-			sysfatal("SWimp_EndFrame:getwindow: %r\n");
+			sysfatal("getwindow: %r\n");
 		resetfb();
 		return;
 	}
-	st3_fixup();
-	loadimage(fbim, fbim->r, framebuf, vid.height * vid.rowbytes);
-	draw(screen, screen->r, fbim, nil, ZP);
+	drawfb();
+	loadimage(fbi, fbi->r, fb, vid.height * vid.rowbytes);
+	draw(screen, screen->r, fbi, nil, ZP);
 	flushimage(display, 1);
 }
 
-rserr_t
-SWimp_SetMode(int */*pwidth*/, int */*pheight*/, int /*mode*/, qboolean /*fullscreen*/)
-{
-	return rserr_ok;
-}
-
-/* nil palette == use existing; palette is expected to be in padded 4-byte xRGB format */
 void
-SWimp_SetPalette(uchar *palette)
+setpal(uchar *p)
 {
-	int i;
+	int *fp;
 
-	if(!rwon)
-		return;
-	if(!palette)
-        	palette = (uchar *)sw_state.currentpalette;
-	for(i = 0; i < 256; i++)
-		st2d_8to24table[i] = rgb24(palette[i*4], palette[i*4+1], palette[i*4+2]);
+	if(p == nil)
+        	p = (uchar *)sw_state.currentpalette;
+	for(fp=fbpal; fp<fbpal+nelem(fbpal); p+=4)
+		*fp++ = p[0] << 16 | p[1] << 8 | p[2];
 }
 
 void
-SWimp_Shutdown(void)
-{
-	if(!rwon)
-		return;
-	if(framebuf != nil)
-		free(framebuf);
-	if(fbim != nil)
-		freeimage(fbim);
-	rwon = 0;
-}
-
-void
-SWimp_AppActivate(qboolean /*active*/)
-{
-}
-
-void
 VID_Printf(int print_level, char *fmt, ...)
 {
 	va_list argptr;
@@ -216,21 +120,13 @@
 }
 
 void
-VID_CheckChanges(void)
+initfb(void)
 {
-}
-
-void
-VID_Shutdown(void)
-{
-	R_Shutdown();
-}
-
-void
-VID_Init(void)
-{
 	refimport_t ri;
 
+	if(initdraw(nil, nil, "quake2") < 0)
+		sysfatal("VID_Init:initdraw: %r\n");
+	vidref_val = VIDREF_SOFT;
 	ri.Cmd_AddCommand = Cmd_AddCommand;
 	ri.Cmd_RemoveCommand = Cmd_RemoveCommand;
 	ri.Cmd_Argc = Cmd_Argc;
@@ -247,5 +143,6 @@
 	ri.Vid_MenuInit = VID_MenuInit;
 
 	re = GetRefAPI(ri);
-	re.Init(nil, nil);
+	re.Init();
+	resetfb();
 }