shithub: qk1

Download patch

ref: 1c82ddb6bf18c515c840f4430913cba4ab7915e9
parent: 2433bad34328371645a8c869c71d2c2cc9b46d72
author: Konstantinn Bonnet <qu7uux@gmail.com>
date: Tue Feb 17 06:37:27 EST 2015

reduce redundant syscalls a bit

- in_9: don't center mouse unless mouse moved
- vid: draw (everything) once per VID_Update call, group some instructions

--- a/in_9.c
+++ b/in_9.c
@@ -153,22 +153,10 @@
 	if((fd = open("/dev/kbd", OREAD)) < 0)
 		sysfatal("open /dev/kbd: %r");
 
-	/* FIXMESS */
-
 	while((n = read(fd, buf, sizeof(buf))) > 0){
 		buf[n-1] = 0;
-
 		switch(*buf){
 		case 'c':
-			/*
-			chartorune(&r, buf+1);
-			if(r){
-				keyq[keyq_head].key = runetokey(r);
-				keyq[keyq_head].down = true;
-				keyq_head = keyq_head+1 & 63;
-			}
-			*/
-			/* fall through */
 		default:
 			continue;
 		case 'k':
@@ -207,12 +195,11 @@
 {
 	int b, n, nerr, fd;
 	char buf[1+5*12];
-	Point m;
+	float x, y;
 
 	if((fd = open("/dev/mouse", ORDWR)) < 0)
 		sysfatal("open /dev/mouse: %r");
 
-	memset(&m, 0, sizeof m);
 	nerr = 0;
 	for(;;){
 		if((n = read(fd, buf, sizeof buf)) != 1+4*12){
@@ -230,15 +217,13 @@
 			if(!mouseactive)
 				break;
 
-			m.x = atoi(buf+1+0*12);
-			m.y = atoi(buf+1+1*12);
-			//m.buttons = atoi(buf+1+2*12);
+			x = atoi(buf+1+0*12) - center.x;
+			y = atoi(buf+1+1*12) - center.y;
 			b = atoi(buf+1+2*12);
 
-			/* FIXME: cursor can still get out */
-			mouse_x += (float)(m.x - center.x);
-			mouse_y += (float)(m.y - center.y);
-			if(m_windowed.value)
+			mouse_x += x;
+			mouse_y += y;
+			if(m_windowed.value && x + y != 0)
 				fprint(fd, "m%d %d", center.x, center.y);
 
 			mouse_buttonstate = b&1 | (b&2)<<1 | (b&4)>>1;
@@ -296,8 +281,6 @@
 	Cvar_RegisterVariable(&m_filter);
 	Cvar_RegisterVariable(&m_freelook);
 	notify(sucks);
-	if(m_windowed.value)
-		IN_Grabm(1);
 	if((pid = rfork(RFPROC|RFMEM|RFFDG)) == 0){
 		kproc();
 		exits(nil);
@@ -305,6 +288,8 @@
 	kpid = pid;
 	if(COM_CheckParm("-nomouse"))
 		return;
+	if(m_windowed.value)
+		IN_Grabm(1);
 	if((pid = rfork(RFPROC|RFMEM|RFFDG)) == 0){
 		mproc();
 		exits(nil);
--- a/mkfile
+++ b/mkfile
@@ -124,6 +124,5 @@
 
 </sys/src/cmd/mkone
 
-# FIXME (packing error?)
+# FIXME
 CFLAGS=-FVw
-#LDFLAGS=-v
--- a/snd_9.c
+++ b/snd_9.c
@@ -6,9 +6,9 @@
 int audio_fd;
 int snd_inited;
 int wpos;
-
 int tryrates[] = { 11025, 22051, 44100, 8000 };
 
+
 qboolean SNDDMA_Init(void)
 {
 	int i;
@@ -57,7 +57,7 @@
 
 int SNDDMA_GetDMAPos(void)
 {
-	if (!snd_inited)
+	if(!snd_inited)
 		return 0;
 	shm->samplepos = wpos / (shm->samplebits/8);
 	return shm->samplepos;
--- a/vid_9.c
+++ b/vid_9.c
@@ -14,6 +14,7 @@
 unsigned short d_8to16table[256];
 int d_con_indirect = 0;
 uchar *framebuf;	/* draw buffer */
+Image *fbim;	/* framebuf image */
 byte current_palette[768];
 int X11_highhunkmark;
 int X11_buffersize;
@@ -172,10 +173,12 @@
 	}
 }
 
+/* vid.height and vid.width must be set correctly before this call */
 void ResetFrameBuffer (void)
 {
-	if(framebuf)
+	if(framebuf != nil)
 		free(framebuf);
+
 	if(d_pzbuffer){
 		D_FlushCaches();
 		Hunk_FreeToHighMark(X11_highhunkmark);
@@ -182,23 +185,29 @@
 		d_pzbuffer = nil;
 	}
 	X11_highhunkmark = Hunk_HighMark();
-
 	// alloc an extra line in case we want to wrap, and allocate the z-buffer
 	X11_buffersize = vid.width * vid.height * sizeof(*d_pzbuffer);
 	vid_surfcachesize = D_SurfaceCacheForRes(vid.width, vid.height);
 	X11_buffersize += vid_surfcachesize;
-
-	d_pzbuffer = Hunk_HighAllocName(X11_buffersize, "video");
-	if(d_pzbuffer == nil)
+	if((d_pzbuffer = Hunk_HighAllocName(X11_buffersize, "video")) == nil)
 		Sys_Error("Not enough memory for video mode\n");
-
 	vid_surfcache = (byte *)d_pzbuffer + vid.width * vid.height * sizeof(*d_pzbuffer);
-
 	D_InitCaches(vid_surfcache, vid_surfcachesize);
 
 	framebuf = malloc(sizeof *framebuf * Dx(screen->r) * Dy(screen->r) * screen->depth/8);
 	vid.buffer = framebuf;
+	vid.rowbytes = Dx(screen->r) * screen->depth/8;
+	vid.aspect = (float)vid.height / (float)vid.width * (320.0/240.0);	/* FIXME */
 	vid.conbuffer = vid.buffer;
+	vid.conrowbytes = vid.rowbytes;
+	vid.conwidth = vid.width;
+	vid.conheight = vid.height;
+	center = addpt(screen->r.min, Pt(Dx(screen->r)/2, Dy(screen->r)/2));
+	grabout = insetrect(screen->r, Dx(screen->r)/8);
+	if(fbim != nil)
+		freeimage(fbim);
+	/* FIXME: seems to crash later with DBlack rather than DNofill, why? */
+	fbim = allocimage(display, Rect(0, 0, vid.width, vid.height), screen->chan, 1, DNofill);
 }
 
 // Called at startup to set up translation tables, takes 256 8 bit RGB values
@@ -205,13 +214,6 @@
 // the palette data will go away after the call, so it must be copied off if
 // the video driver will need it again
 
-void resetscr(void)
-{
-	ResetFrameBuffer();
-	center = addpt(screen->r.min, Pt(Dx(screen->r)/2, Dy(screen->r)/2));
-	grabout = insetrect(screen->r, Dx(screen->r)/8);
-}
-
 void VID_Init (unsigned char *palette)
 {
 	int pnum;
@@ -229,7 +231,7 @@
 	//vid.grades = VID_GRADES;
 	vid.fullbright = 256 - LittleLong(*((int *)vid.colormap + 2048));
 
-	srand(getpid());	/* glibc srandom() affects rand() too, right? */
+	srand(getpid());
 
 	if(pnum = COM_CheckParm("-winsize")){
 		if(pnum >= com_argc-2)
@@ -261,23 +263,10 @@
 		vid.width = Dx(screen->r);
 	if(vid.height == -1)
 		vid.height = Dy(screen->r);
-
-	/* TODO: resize properly if -winsize,.. params provided, draw centered */
-
 	if(screen->chan == CMAP8)
 		VID_SetPalette(palette);
-	resetscr();
-	vid.rowbytes = Dx(screen->r) * screen->depth/8;
-	vid.buffer = framebuf;
+	ResetFrameBuffer();
 	vid.direct = 0;
-	vid.conbuffer = framebuf;
-	vid.conrowbytes = vid.rowbytes;
-	vid.conwidth = vid.width;
-	vid.conheight = vid.height;
-	/* FIXME */
-	vid.aspect = (float)vid.height / (float)vid.width * (320.0/240.0);
-
-	draw(screen, screen->r, display->black, nil, ZP);
 }
 
 void VID_ShiftPalette (unsigned char *p)
@@ -314,14 +303,12 @@
 void VID_Shutdown (void)
 {
 	Con_Printf("VID_Shutdown\n");
+	freeimage(fbim);
 }
 
 /* flush given rectangles from view buffer to the screen */
 void VID_Update (vrect_t *rects)
 {
-	Rectangle svr, dr;
-	Image *im;
-
 	if(config_notify){		/* skip this frame if window resize */
 		config_notify = 0;
 		if(getwindow(display, Refnone) < 0)
@@ -328,13 +315,7 @@
 			Sys_Error("VID_Update:getwindow");
 		vid.width = Dx(screen->r);
 		vid.height = Dy(screen->r);
-		resetscr();
-		vid.rowbytes = vid.width * screen->depth/8;
-		vid.buffer = framebuf;
-		vid.conbuffer = framebuf;
-		vid.conwidth = vid.width;
-		vid.conheight = vid.height;
-		vid.conrowbytes = vid.rowbytes;
+		ResetFrameBuffer();
 		vid.recalc_refdef = 1;			// force a surface cache flush
 		Con_CheckResize();
 		Con_Clear_f();
@@ -344,10 +325,6 @@
 	if(screen->chan != CMAP8)	/* force full update if not 8bit */
 		scr_fullupdate = 0;
 
-	/* FIXME: seems to crash later with DBlack rather than DNofill, why? */
-	im = allocimage(display, Rect(0, 0, vid.width, vid.height), screen->chan, 1, DNofill);
-
-	svr = screen->clipr;
 	while(rects){
 		if(screen->chan == RGB16)
 			st2_fixup(framebuf, rects->x, rects->y,
@@ -356,18 +333,10 @@
 		else if(screen->chan == RGB24 || screen->chan == RGBA32 || screen->chan == XRGB32)
 			st3_fixup(framebuf, rects->x, rects->y,
 				rects->width, rects->height);
-
-		/* FIXME */
-		//loadimage(im, im->r, &framebuf[rects->x*vid.rowbytes];
-		loadimage(im, im->r, framebuf, vid.height * vid.rowbytes);
-		dr = Rpt(addpt(screen->r.min, Pt(rects->x, rects->y)),
-			addpt(screen->r.min, Pt(rects->x+rects->width, rects->y+rects->height)));
-		//replclipr(screen, 0, dr);	/* FIXME: necessary? */
-		draw(screen, dr, im, nil, ZP);
 		rects = rects->pnext;
 	}
-	replclipr(screen, 0, svr);
-	freeimage(im);
+	loadimage(fbim, fbim->r, framebuf, vid.height * vid.rowbytes);
+	draw(screen, screen->r, fbim, nil, ZP);
 	flushimage(display, 1);
 }