vid_sunxil.c

来自「quake1 dos源代码最新版本」· C语言 代码 · 共 1,318 行 · 第 1/3 页

C
1,318
字号
	}

	x_screen = DefaultScreen( x_disp );
	x_screen_width = WidthOfScreen( ScreenOfDisplay( x_disp, x_screen ) );
	x_screen_height = HeightOfScreen( ScreenOfDisplay( x_disp, x_screen ) );

	x_center_width  = x_screen_width/2;

	x_center_height = x_screen_height/2;

	Con_Printf( "Using screen %d: %dx%d\n", x_screen, x_screen_width, x_screen_height );

	x_root_win = DefaultRootWindow( x_disp);

	//XAutoRepeatOff(x_disp);

// for debugging only
	if (verbose)
		XSynchronize(x_disp, True);

//
// check for command-line window size
//
	if ((pnum=COM_CheckParm("-winsize"))) {
		if (pnum >= com_argc-2)
			Sys_Error("VID: -winsize <width> <height>\n");
		desired_width = Q_atoi(com_argv[pnum+1]);
		desired_height = Q_atoi(com_argv[pnum+2]);
		if (desired_width < 1 || desired_height < 1)
			Sys_Error("VID: Bad window width/height\n");
	}

	template_mask = VisualScreenMask; // make sure we get the right one
	template.screen = x_screen;
//
// specify a visual id
//
	if ((pnum=COM_CheckParm("-visualid"))) {
		if (pnum >= com_argc-1)
			Sys_Error("VID: -visualid <id#>\n");
		template.visualid = Q_atoi(com_argv[pnum+1]);
		template_mask |= VisualIDMask;
	} else	{
		// If not specified, find an 8 bit visual since others don't work
//		template.depth = 8;
//		template_mask |= VisualDepthMask;
		int screen;
		screen = XDefaultScreen(x_disp);
		template.visualid =
			XVisualIDFromVisual(XDefaultVisual(x_disp, screen));
		template_mask = VisualIDMask;
	}
//
// pick a visual- warn if more than one was available
//
	x_visinfo = XGetVisualInfo(x_disp, template_mask, &template, &num_visuals);
	if (num_visuals > 1) {
		printf("Found more than one visual id at depth %d:\n", template.depth);
		for (i=0 ; i<num_visuals ; i++)
			printf("	-visualid %d\n", (int)(x_visinfo[i].visualid));
	}
	else if (num_visuals == 0) {
		if (template_mask == VisualIDMask)
			Sys_Error("VID: Bad visual id %d\n", template.visualid);
		else
			Sys_Error("VID: No visuals at depth %d\n", template.depth);
	}

	if (verbose) {
		printf("Using visualid %d:\n", (int)(x_visinfo->visualid));
		printf("	screen %d\n", x_visinfo->screen);
		printf("	red_mask 0x%x\n", (int)(x_visinfo->red_mask));
		printf("	green_mask 0x%x\n", (int)(x_visinfo->green_mask));
		printf("	blue_mask 0x%x\n", (int)(x_visinfo->blue_mask));
		printf("	colormap_size %d\n", x_visinfo->colormap_size);
		printf("	bits_per_rgb %d\n", x_visinfo->bits_per_rgb);
	}

	x_vis = x_visinfo->visual;
//
// See if we're going to do pixel multiply
//
	if (pixel_multiply->value < 1 || pixel_multiply->value > 4)
		Cvar_Set (pixel_multiply, "2");
	current_pixel_multiply = pixel_multiply->value;

	w = 320*current_pixel_multiply; // minimum width
	h = 200*current_pixel_multiply; // minimum height
	if (desired_width < w)
		desired_width = w;
	if (desired_height < h)
		desired_height = h;

	vid.width = MP(desired_width);
	vid.height = MP(desired_height);

	//
	// patch things up so game doesn't fail if window is too small
	//

	if (vid.width < 320)
		vid.width = 320;
	if (vid.height < 200)
		vid.height = 200;

//
// see if we're going to use threads
//
	if(((sysconf(_SC_NPROCESSORS_ONLN) > 1) || COM_CheckParm("-mt")) &&
		(COM_CheckParm("-no_mt") == 0)) {
		use_mt = 1;
		printf("VID: Using multiple threads!\n");
	}

// setup attributes for main window
	{
		int attribmask = CWEventMask  | CWColormap | CWBorderPixel;
		XSetWindowAttributes attribs;
		Colormap tmpcmap;

		tmpcmap = XCreateColormap(x_disp, XRootWindow(x_disp,
			x_visinfo->screen), x_vis, AllocNone);

		attribs.event_mask = x_std_event_mask;
		attribs.border_pixel = 0;
		attribs.colormap = tmpcmap;

// create the main window
		x_win = XCreateWindow(	x_disp,
			XRootWindow(x_disp, x_visinfo->screen),
			0, 0,	// x, y
			desired_width, desired_height,
			0, // borderwidth
			x_visinfo->depth,
			InputOutput,
			x_vis,
			attribmask,
			&attribs );

		if (x_visinfo->class != TrueColor)
			XFreeColormap(x_disp, tmpcmap);

	}

	if (x_visinfo->depth == 8) {

	// create and upload the palette
		if (x_visinfo->class == PseudoColor) {
			x_cmap = XCreateColormap(x_disp, x_win, x_vis, AllocAll);
			VID_SetPalette(palette);
			XSetWindowColormap(x_disp, x_win, x_cmap);
		}

	}

	VID_SetWindowTitle( x_win, "Quake" );

// inviso cursor
	XDefineCursor(x_disp, x_win, CreateNullCursor(x_disp, x_win));

// create the GC
	{
		XGCValues xgcvalues;
		int valuemask = GCGraphicsExposures;
		xgcvalues.graphics_exposures = False;
		x_gc = XCreateGC(x_disp, x_win, valuemask, &xgcvalues );
	}

// map the window
	XMapWindow(x_disp, x_win);
	XSync(x_disp, True) ;        /* wait for map */
//
// wait for first exposure event
//
	{
		XEvent event;
		do{
			XNextEvent(x_disp, &event);
			if (event.type == Expose && !event.xexpose.count)
				oktodraw = true;
		} while (!oktodraw);
	}
//
// initialize XIL
//

	state = xil_open();

	if(state == NULL) {
		//
		//  XIL's default error handler will print an error msg on stderr
		//
		Sys_Error("xil_open failed\n");
	}

	X11_active = true;

	VID_ResetFramebuffer();

	D_InitCaches (surfcache, sizeof(surfcache));

	vid_menudrawfn = VID_MenuDraw;
	vid_menukeyfn = VID_MenuKey;
}

VID_ResetFramebuffer()
{
	XilMemoryStorage storage;

	if (use_mt) {
		VID_ResetFramebuffer_MT();
		return;
	}

//printf("VID_ResetFramebuffer: vid.width %d, vid.height %d\n", vid.width, vid.height);

	xil_destroy(display_image);

	xil_destroy(quake_image);

	display_image = xil_create_from_window(state, x_disp, x_win);
	quake_image = xil_create(state, vid.width, vid.height, 1, XIL_BYTE);

	xil_export(quake_image);

	if (xil_get_memory_storage(quake_image, &storage) == FALSE)
		Sys_Error("xil_get_memory_storage");

	xil_import(quake_image, TRUE);
	xil_export(quake_image);

	if (xil_get_memory_storage(quake_image, &storage) == FALSE)
		Sys_Error("xil_get_memory_storage");

	vid.rowbytes = storage.byte.scanline_stride;
	vid.buffer =   storage.byte.data;
	vid.conbuffer = vid.buffer;
	vid.conrowbytes = vid.rowbytes;
	vid.conwidth = vid.width;
	vid.conheight = vid.height;

	vid.maxwarpwidth = WARP_WIDTH;
	vid.maxwarpheight = WARP_HEIGHT;
	vid.recalc_refdef = 1;				// force a surface cache flush

	free(d_pzbuffer);

	d_pzbuffer = malloc(PM(vid.width)*PM(vid.height)*sizeof(*d_pzbuffer));
		//Hunk_HighAllocName(PM(vid.width)*PM(vid.height)*sizeof(*d_pzbuffer),"zbuff");
}

VID_ResetFramebuffer_MT()
{
	XilMemoryStorage storage;
	XilImage drain_renderpipeline();
	XilImage old_display_image;

	void * update_thread();

	printf("VID_ResetFramebuffer: vid.width %d, vid.height %d\n", vid.width, vid.height);

	old_display_image = display_image;

	display_image = xil_create_from_window(state, x_disp, x_win);

	if (quake_image == NULL)
		if (thr_create(NULL, NULL, update_thread, NULL, THR_NEW_LWP, NULL) != 0)
			Sys_Error("VID: thr_create");

	quake_image = drain_renderpipeline(quake_image);

	xil_destroy(old_display_image);

	free(d_pzbuffer);

	d_pzbuffer = malloc(PM(vid.width)*PM(vid.height)*sizeof(*d_pzbuffer));
}

void VID_ShiftPalette(unsigned char *p)
{
	VID_SetPalette(p);
}

void VID_SetPalette(unsigned char *palette)
{

	int i;
	XColor colors[256];

	if (x_visinfo->class == PseudoColor && x_visinfo->depth == 8) {
			if (palette != current_palette)
				memcpy(current_palette, palette, 768);
			for (i=0 ; i<256 ; i++)
				{
					colors[i].pixel = i;
					colors[i].flags = DoRed|DoGreen|DoBlue;
					colors[i].red = vid_gamma[palette[i*3]] * 257;
					colors[i].green = vid_gamma[palette[i*3+1]] * 257;
					colors[i].blue = vid_gamma[palette[i*3+2]] * 257;
				}
			XStoreColors(x_disp, x_cmap, colors, 256);
		}

}

// Called at shutdown

void	VID_Shutdown (void)
{
	X11_active = false;
	Con_Printf("VID_Shutdown\n");
	//XAutoRepeatOn(x_disp);
	xil_destroy(display_image);
	xil_destroy(quake_image);
	display_image = NULL;
	quake_image = NULL;
	XCloseDisplay(x_disp);
}

int XLateKey(XKeyEvent *ev)
{

	int key;
	char buf[64];
	KeySym keysym;

	XLookupString(ev, buf, sizeof buf, &keysym, 0);

	switch(keysym) {
		case XK_Page_Up:	 key = K_PGUP; break;
		case XK_Page_Down:	 key = K_PGDN; break;
		case XK_Home:	 key = K_HOME; break;
		case XK_End:	 key = K_END; break;
		case XK_Left:	 key = K_LEFTARROW; break;
		case XK_Right:	key = K_RIGHTARROW;		break;
		case XK_Down:	 key = K_DOWNARROW; break;
		case XK_Up:		 key = K_UPARROW;	 break;
		case XK_Escape: key = K_ESCAPE;		break;
		case XK_Return: key = K_ENTER;		 break;
		case XK_Tab:		key = K_TAB;			 break;
		case XK_Help:
		case XK_F1:		 key = K_F1;				break;
		case XK_F2:		 key = K_F2;				break;
		case XK_F3:		 key = K_F3;				break;
		case XK_F4:		 key = K_F4;				break;
		case XK_F5:		 key = K_F5;				break;
		case XK_F6:		 key = K_F6;				break;
		case XK_F7:		 key = K_F7;				break;
		case XK_F8:		 key = K_F8;				break;
		case XK_F9:		 key = K_F9;				break;
		case XK_F10:		key = K_F10;			 break;
		case XK_F11:		key = K_F11;			 break;
		case XK_F12:		key = K_F12;			 break;
		case XK_BackSpace:
		case XK_Delete: key = K_BACKSPACE; break;
		case XK_Pause:	key = K_PAUSE;		 break;
		case XK_Shift_L:
		case XK_Shift_R:		key = K_SHIFT;		break;
		case XK_Control_L:
		case XK_Control_R:	key = K_CTRL;		 break;
		case XK_Alt_L:
		case XK_Meta_L:
		case XK_Alt_R:
		case XK_Meta_R: key = K_ALT;			break;
			// various other keys on the keyboard
				   case XK_F27: key = K_HOME; break;
		case XK_F29: key = K_PGUP; break;
		case XK_F33: key = K_END; break;
		case XK_F35: key = K_PGDN; break;
		case XK_Insert:
		case XK_KP_Insert: key = K_INS; break;
		case XK_F24: key = '-'; break;
		case XK_KP_Add: key = '+'; break;
		case XK_KP_Subtract: key = '-'; break;
		case XK_F25: key = '/'; break;
		case XK_F26: key = '*'; break;

		default:
			key = (unsigned char)*buf;
			break;
		}

	return key;

}

struct {
	int key;
	int down;
} keyq[64];

int keyq_head=0;
int keyq_tail=0;

int config_notify=0;
int config_notify_width;
int config_notify_height;

void GetEvent(void)
{
	XEvent x_event;
	int b;

	XNextEvent(x_disp, &x_event);
	switch(x_event.type) {
		case KeyPress:
			Key_Event(XLateKey(&x_event.xkey), true);
			break;
		case KeyRelease:
			Key_Event(XLateKey(&x_event.xkey), false);
			break;

		case MotionNotify:

			if (_windowed_mouse->value) {
				mouse_x = (float) ((int)x_event.xmotion.x - (int)(vid.width/2));
				mouse_y = (float) ((int)x_event.xmotion.y - (int)(vid.height/2));
	//printf("m: x=%d,y=%d, mx=%3.2f,my=%3.2f\n",
	//	x_event.xmotion.x, x_event.xmotion.y, mouse_x, mouse_y);

				/* move the mouse to the window center again */
				XSelectInput( x_disp, x_win, x_std_event_mask & ~PointerMotionMask );
				XWarpPointer(x_disp,None,x_win,0,0,0,0,
					(vid.width/2),(vid.height/2));
				XSelectInput( x_disp, x_win, x_std_event_mask );
			} else {
				mouse_x = (float) (x_event.xmotion.x-p_mouse_x);
				mouse_y = (float) (x_event.xmotion.y-p_mouse_y);
				p_mouse_x=x_event.xmotion.x;
				p_mouse_y=x_event.xmotion.y;
			}
			break;

		case ButtonPress:
			b=-1;
			if (x_event.xbutton.button == 1)
				b = 0;
			else if (x_event.xbutton.button == 2)
				b = 2;
			else if (x_event.xbutton.button == 3)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?