⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xf86.cpp

📁 著名SFC模拟器Snes9x的源代码。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*   XF86 code (xf86.cpp, this file) (c) Copyright 1998 Maciej Babinski,		use restricted to above terms.   Question/comments regarding this file only: maciej@imsa.edu */#ifdef __linux//#define USE_XF86VIDMODE#include <asm/ioctls.h>#include <sys/ioctl.h>#include <sys/time.h>#include <linux/ioctl.h>#include <fcntl.h>#include <sys/types.h>#include <string.h>#include <ctype.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <stdlib.h>#include <signal.h>#include "snes9x.h"#include "memmap.h"#include "debug.h"#include "ppu.h"#include "snapshot.h"#include "gfx.h"#include "display.h"#include "apu.h"#include <asm/io.h>#include <stdlib.h>#include <ctype.h>#include <string.h>#include "snes9x.h"#include "memmap.h"#include "debug.h"#include "ppu.h"#include "snapshot.h"#include "gfx.h"#include "display.h"#include "apu.h"#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/keysym.h>#include <X11/extensions/xf86dga.h>#ifdef USE_XF86VIDMODE/* GH: Added to allow to be compiled using C++ compiler */#if defined (__cplusplus) || defined (c_plusplus)#define private c_private#endif#   include <X11/extensions/xf86vmode.h>static XF86VidModeModeInfo	*orig_mode = NULL;static XF86VidModeModeInfo	*mod320x240 = NULL;bool cmpModeInfo (XF86VidModeModeInfo, XF86VidModeModeInfo);XF86VidModeModeInfo *ModeLine2ModeInfo (XF86VidModeModeLine, int);#endifvoid quit (int sig){	printf ("Received signal %d, quitting\n", sig);	S9xExit ();}struct vidinfo {	int bitdepth;	int screendepth;	int width;	int height;	int banksize;	int memsize;	char *vidMemBegin;	char *scrnBegin;};struct vidinfo	ourvideo;static Display		*ourdisp = NULL;/*   These three vars are "constants" for a particular display, who's value we   calculate in S9xInitDisplay, so that we don't have to do it on the fly in   S9xPutImage.	mEtoE = distance between end of once line of image and beginning of	        next one (units differ between bit-depths).*/static int		mWide, mHigh, mEtoE; static Colormap		cmap;static XColor		colors[256];static int		ourscreen;static bool		is_graphics=FALSE;static Window		inputwin;static Window		rootWindow;static int		is32or24 = 32;extern int		joypads[5];void S9xInitDisplay (int argc, char **argv){	Screen			*scrn;	XSetWindowAttributes	xattr;	signal (SIGINT, quit);	signal (SIGTERM, quit);	if ((ourdisp = XOpenDisplay(NULL)) == NULL) {		printf ("Can't connect to X server!\n");		S9xExit ();	}	ourscreen = DefaultScreen (ourdisp);	scrn = DefaultScreenOfDisplay (ourdisp);	ourvideo.bitdepth = DefaultDepth (ourdisp, ourscreen);	ourvideo.screendepth = ourvideo.bitdepth / 8;	rootWindow = RootWindowOfScreen (scrn);	xattr.override_redirect = True;	inputwin = XCreateWindow (ourdisp, RootWindowOfScreen(scrn),	                          10, 10, 100, 100, 0, 0, InputOutput,	                          DefaultVisualOfScreen(scrn),	                          CWOverrideRedirect, &xattr);	XMapWindow (ourdisp, inputwin);	ourvideo.height = HeightOfScreen (scrn);	switch (ourvideo.bitdepth) {		case  8:			Settings.SixteenBit = FALSE;			Settings.Transparency = FALSE;			cmap = XCreateColormap (ourdisp, rootWindow,			                        DefaultVisualOfScreen (scrn),						AllocAll);			XSetWindowColormap (ourdisp, inputwin, cmap);			for (int i=0; i<256; i++) {				colors[i].pixel = i;				colors[i].flags = DoRed | DoGreen | DoBlue;			}			break;		case 16:			Settings.SixteenBit = TRUE;			if (!Settings.ForceNoTransparency)				Settings.Transparency = TRUE;			break;		case 24:			if (is32or24 == 32) {				ourvideo.bitdepth = 32;				ourvideo.screendepth = 4;			}			Settings.SixteenBit = TRUE;			if (!Settings.ForceNoTransparency)				Settings.Transparency = TRUE;			break;		default:			printf ("Color depth %d not supported!\n");			S9xExit ();			break;	}	printf ("Found %d bit display\n", ourvideo.bitdepth);	XFree(scrn);	XF86DGAGetVideo (ourdisp, ourscreen, &(ourvideo.vidMemBegin),          	       &(ourvideo.width), &(ourvideo.banksize),          	       &(ourvideo.memsize));#ifdef USE_XF86VIDMODE   {	XF86VidModeModeInfo	**all_modes;	int			mode_count;	XF86VidModeModeLine	mod_tmp;	int			dotclock_tmp;	int			x;		XF86VidModeGetModeLine (ourdisp, ourscreen, &dotclock_tmp, &mod_tmp);	orig_mode = ModeLine2ModeInfo (mod_tmp, dotclock_tmp);	if (orig_mode->hdisplay == 320 && orig_mode->vdisplay == 240) {		mod320x240 = orig_mode;	} else {		XF86VidModeGetAllModeLines (ourdisp,ourscreen, &mode_count,		                            &all_modes);		for (x = 0; x < mode_count; x++)			if (all_modes[x]->hdisplay == 320 &&			    all_modes[x]->vdisplay == 240) {				mod320x240 = (XF86VidModeModeInfo *) malloc (				               sizeof(XF86VidModeModeInfo));				*mod320x240 = *(all_modes[x]);				break;			} else				XFree (all_modes[x]->c_private);		if (mod320x240 == NULL) {			printf ("No 320x240 mode available!\n");			S9xExit ();		}	}	XFree(all_modes);   }#endif	GFX.Pitch = IMAGE_WIDTH * (Settings.SixteenBit ? 2 : 1);	GFX.Screen = (uint8 *) malloc (IMAGE_HEIGHT * GFX.Pitch);	if (Settings.Transparency)		GFX.SubScreen = (uint8 *) malloc (IMAGE_HEIGHT * GFX.Pitch);	switch (ourvideo.bitdepth) {		case  8:			mWide = IMAGE_WIDTH / 4;			mHigh = IMAGE_HEIGHT;			mEtoE = (ourvideo.width - IMAGE_WIDTH) /4;			break;		case 16:			mWide = IMAGE_WIDTH / 2;			mHigh = IMAGE_HEIGHT;			mEtoE = (ourvideo.width - IMAGE_WIDTH) / 2;			break;		case 24:			mWide = IMAGE_WIDTH;			mHigh = IMAGE_HEIGHT;			mEtoE = (ourvideo.width - IMAGE_WIDTH) * 3;			break;		case 32:			mWide = IMAGE_WIDTH;			mHigh = IMAGE_HEIGHT;			mEtoE = (ourvideo.width - IMAGE_WIDTH) * 4;		default:			break;	}	ourvideo.scrnBegin = ourvideo.vidMemBegin + (320 - IMAGE_WIDTH) * ourvideo.screendepth / 2 + ourvideo.width * ourvideo.screendepth * 8;}void S9xGraphicsMode (){	if (is_graphics)		return;	XSelectInput (ourdisp, inputwin, KeyPressMask | KeyReleaseMask);	XSetInputFocus (ourdisp, inputwin, RevertToNone, CurrentTime);	XGrabKeyboard (ourdisp, inputwin, True, GrabModeAsync, GrabModeAsync,	               CurrentTime);	XGrabPointer (ourdisp, inputwin, True, 0, 	              GrabModeAsync, GrabModeAsync, inputwin, None, CurrentTime);		XFlush(ourdisp);	XAutoRepeatOff (ourdisp);#ifdef USE_XF86VIDMODE	if (orig_mode != mod320x240) {		XF86VidModeSwitchToMode (ourdisp,ourscreen, mod320x240);	}	XFlush (ourdisp);	XF86VidModeSetViewPort (ourdisp, ourscreen, 0, 0);	XFlush (ourdisp);#endif		XF86DGADirectVideo (ourdisp, ourscreen, XF86DGADirectGraphics);	memset ((void *) ourvideo.vidMemBegin, 0, ourvideo.width * 240 *	        ourvideo.screendepth);	is_graphics = TRUE;	XFlush (ourdisp);}void S9xTextMode () {	if (!is_graphics)		return;	if (ourdisp == NULL)		return;		XF86DGADirectVideo (ourdisp, ourscreen, 0);#ifdef USE_XF86VIDMODE	if (orig_mode != mod320x240) {		XF86VidModeSwitchToMode (ourdisp, ourscreen, orig_mode);	}#endif	XAutoRepeatOn (ourdisp);	XUngrabKeyboard (ourdisp, CurrentTime);	XUngrabPointer (ourdisp, CurrentTime);	XSelectInput (ourdisp, inputwin, 0);	XSync (ourdisp, True);	is_graphics = FALSE;} void S9xDeinitDisplay (){	S9xTextMode ();	if (ourdisp == NULL)		return;	XDestroyWindow (ourdisp, inputwin);	free (GFX.Screen);	free (GFX.SubScreen);#ifdef USE_XF86VIDMODE	free (orig_mode);	free (mod320x240);	if (orig_mode->privsize)		XFree (orig_mode->c_private);	if (mod320x240->privsize)		XFree (orig_mode->c_private);#endif	XFree (ourdisp);}void S9xSetPalette () {	int		Brightness;		if (ourvideo.bitdepth != 8)		return;	Brightness = IPPU.MaxBrightness *138;	for (int i = 0; i < 256; i++) {		colors[i].red = ((PPU.CGDATA[i] >> 0) & 0x1F) * Brightness;		colors[i].green = ((PPU.CGDATA[i] >> 5) & 0x1F) * Brightness;		colors[i].blue = ((PPU.CGDATA[i] >> 10) & 0x1F) * Brightness;	}	XStoreColors (ourdisp, cmap, colors, 256);	XF86DGAInstallColormap (ourdisp, ourscreen, cmap);}#define RED(r)		((uint8) (((r >> 11) & 31) * 256 / 32))#define GREEN(g)	((uint8) (((g >> 5) & 63) * 256 / 64))#define BLUE(b)		((uint8) (((b & 31)) * 256 / 32))void S9xPutImage (int width, int height){	register int x,y;		switch (ourvideo.bitdepth) {		case  8:		case 16:			{			long int	*p, *q;			p = (long int *) GFX.Screen;			q = (long int *) ourvideo.scrnBegin;			for (y=0;y<mHigh;y++) {				for (x=0;x<mWide;x++) {					*q = *p;					q++, p++;					}				q += mEtoE;				}			}			break;/*		case 24:			{			uint16		*p;			uint8	  	*q;			p = (uint16 *) GFX.Screen;			q = (uint8 *) ourvideo.scrnBegin;			for (y=0;y<mHigh;y++) {				for (x=0;x<mWide;x++) {					*q = BLUE(*p);					*(q+1) = GREEN(*p);					*(q+2) = RED(*p);					q+=3, p++;					}				q += mEtoE;				}			}			break;*/		case 24:			{			uint16		*p;			uint8	  	*q;			p = (uint16 *) GFX.Screen;			q = (uint8 *) ourvideo.scrnBegin;			for (y=0;y<mHigh;y++) {				for (x=0;x<mWide;x++) {					*q = BLUE(*p);					*(q+1) = GREEN(*p);					*(q+2) = RED(*p);					q+=3, p++;					}				q += mEtoE;				}			}			break;		case 32:			{			uint16		*p;			uint8	  	*q;			p = (uint16 *) GFX.Screen;			q = (uint8 *) ourvideo.scrnBegin;

⌨️ 快捷键说明

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