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

📄 grengine.cpp

📁 SNES game emulator. C and asm files.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
}

//----------------------------------------------------------------------------
void clearscreen (int backg)
{
	if (xres == 256)
		clear64k (vs, (backg << 24) + (backg << 16) + (backg << 8) + backg);
	else
		memset (vs, backg, xres * yres);
}

void swap (int &x, int &y)
{
	x ^= y; y ^= x; x ^= y;
}

void drawbox (int x1, int y1, int x2, int y2, int color)
{
	int lstart;

	if (x1 > x2) swap (x1, x2);
	if (y1 > y2) swap (y1, y2);
	if (x1 >= xres || y1 >= yres || x2 < 0 || y2 < 0) return;
	if (x1 < 0) x1 = 0; if (x2 >= xres) x2 = xres - 1;
	if (y1 < 0) y1 = 0; if (y2 >= yres) y2 = yres - 1;
	//debugf (0, "x1=%d y1=%d x2=%d y2=%d", x1, y1, x2, y2);
	lstart = y1 * xres + x1;
	if (color  == -1) color = curcolor;
	while (y1 <= y2) {
		memset (vs + lstart, color, x2 - x1 + 1);
		lstart += xres;
		y1++;
	}
}

void drawpatternbox (int x1, int y1, int x2, int y2, int color, int skip)
{
	int lstart, p;

	skip++;
	if (x1 > x2) swap (x1, x2);
	if (y1 > y2) swap (y1, y2);
	if (x1 >= xres || y1 >= yres || x2 < 0 || y2 < 0) return;
	if (x1 < 0) x1 = 0; if (x2 >= xres) x2 = xres - 1;
	if (y1 < 0) y1 = 0; if (y2 >= yres) y2 = yres - 1;
	lstart = y1 * xres + x1;
	if (color  == -1) color = curcolor;
	while (y1 <= y2) {
		for (p = lstart + (y1 % skip); p < lstart + x2 - x1 + 1; p += skip)
			vs [p] = color;
		lstart += xres;
		y1++;
	}
}

void hline (int y, int x1, int x2, int color)
{
	if (x1 > x2) swap (x1, x2);
	if (y < 0 || y >= yres || x2 < 0 || x1 >= xres) return;
	if (x1 < 0) x1 = 0;
	if (x2 >= xres) x2 = xres - 1;
	if (color  == -1) color = curcolor;
	memset (vs + y * xres + x1, color, x2 - x1 + 1);
}

void vline (int x, int y1, int y2, int color)
{
	if (y1 > y2) swap (y1, y2);
	if (x < 0 || x >= xres || y2 < 0 || y1 >= yres) return;
	if (y1 < 0) y1 = 0;
	if (y2 >= yres) y2 = yres - 1;
	if (color == -1) color = curcolor;
	while (y1 <= y2) {
		vs [y1 * xres + x] = color;
		y1++;
	}
}

void point (int x, int y, int color) // slow
{
	if (x >= 0 && x < xres && y >= 0 && y < yres)
		vs [y * xres + x] = (color == -1 ? curcolor : color);
}

	// Horizontal/vertical lines
void drawdottedlinebox (int x1, int y1, int x2, int y2, int color)
{
	int y;
	
	if (x1 > x2) swap (x1, x2);
	if (y1 > y2) swap (y1, y2);
	if (x1 >= xres || y1 >= yres || x2 < 0 || y2 < 0) return;
	if (x1 < 0) x1 = 0; if (x2 >= xres) x2 = xres - 1;
	if (y1 < 0) y1 = 0; if (y2 >= yres) y2 = yres - 1;
	if (color  == -1) color = curcolor;
	for (y = y1; y <= y2; y += 2)
		vs [y * xres + x1] = color;
	while (x1 < x2) {
		vs [y1 * xres + x1] = color;
		vs [y2 * xres + x1] = color;
		x1 += 2;
	}
	if (x1 < x2) { y1++; }
	for (y = y1; y <= y2; y += 2)
		vs [y * xres + x2] = color; 
}

void setcolor (int c)
{
	curcolor = c;
}

int textwidth8 (char *t)
{
	int w = 0;

	if (t == NULL) return 0;
	while (*t != '\0') {
		w += width8[*t];
		t++;
	}
	return w;
}

int textwidth10 (char *t)
{
	int w = 0;

	if (t == NULL) return 0;
	while (*t != '\0') {
		w += width10[*t];
		t++;
	}
	return w;
}

//----------------------------------------------------------------------------
void printf8  (int x, int y, char *format, ...)
{
	static char s [80];
	va_list arglist;

	va_start (arglist, format);
	vsprintf (s, format, arglist);
	va_end (arglist);
	outtext8 (x, y, s);
}

void printf10 (int x, int y, char *format, ...)
{
	static char s [80];
	va_list arglist;

	va_start (arglist, format);
	vsprintf (s, format, arglist);
	va_end (arglist);
	outtext10 (x, y, s);
}

void outtext8 (int x, int y, char *s, boolean focusbox)
{
	byte c;
	int l, p, tw = textwidth8 (s), sx = x;

	if (y < 0 || y >= yres || s == NULL)
		return;
	while (*s != '\0') {
		c = *s & 127;
		//debugf (0, "x=%d y=%d c=%d", x, y, c);
		if (x >= 0 && x < xres) {
			p = y * xres + x;
			for (l = 0; l < 8; l++) {
				if ((font8[c][l]) & 1)      vs [p + 7] = curcolor;
				if ((font8[c][l] >> 1) & 1) vs [p + 6] = curcolor;
				if ((font8[c][l] >> 2) & 1) vs [p + 5] = curcolor;
				if ((font8[c][l] >> 3) & 1) vs [p + 4] = curcolor;
				if ((font8[c][l] >> 4) & 1) vs [p + 3] = curcolor;
				if ((font8[c][l] >> 5) & 1) vs [p + 2] = curcolor;
				if ((font8[c][l] >> 6) & 1) vs [p + 1] = curcolor;
				if ((font8[c][l] >> 7))     vs [p] = curcolor;
				p += xres;
			}
		}
		x += width8 [c];
		s++;
	}
	if (focusbox) {
		drawdottedlinebox (sx - 1, y - 1, sx + tw, y + 7, CCOLOR (set.focus, 5));
	}
}

void outtext10 (int x, int y, char *s, boolean focusbox)
{
	byte c;
	int l, p, tw = textwidth10 (s), sx = x;

	if (y < 0 || y >= yres || s == NULL)
		return;
	while (*s != '\0') {
		c = *s & 127;
		if (x >= 0 && x < xres) {
			p = y * xres + x;
			for (l = 0; l < 10; l++) {
				if ((font10[c][l]) & 1)      vs [p + 7] = curcolor;
				if ((font10[c][l] >> 1) & 1) vs [p + 6] = curcolor;
				if ((font10[c][l] >> 2) & 1) vs [p + 5] = curcolor;
				if ((font10[c][l] >> 3) & 1) vs [p + 4] = curcolor;
				if ((font10[c][l] >> 4) & 1) vs [p + 3] = curcolor;
				if ((font10[c][l] >> 5) & 1) vs [p + 2] = curcolor;
				if ((font10[c][l] >> 6) & 1) vs [p + 1] = curcolor;
				if ((font10[c][l] >> 7))     vs [p] = curcolor;
				p += xres;
			}
		}
		x += width10 [c];
		s++;
	}
	if (focusbox) {
		drawdottedlinebox (sx - 1, y - 1, sx + tw, y + 9, CCOLOR (set.focus, 5));
	}
}

dword pal [256], custompalette [256], gamepalette [256];
byte translationtable [16][16][16];

// dword RGB values: 0x00DDEEFF
//                    Red^ ^ ^Blue
//                         +-Green

//----------------------------------------------------------------------------
void setpaletteentry (byte entry, byte r, byte g, byte b)
{
	int rgb = (r << 16) + (g << 8) + b;

	if (rgb == pal[entry]) return;
	outp (0x3C8, entry);
	outp (0x3C9, r >> 2);
	outp (0x3C9, g >> 2);
	outp (0x3C9, b >> 2);
	pal [entry] = rgb;
}

void setpaletteentry (byte entry, dword rgb)
{
	if (rgb == pal[entry]) return;
	outp (0x3C8, entry);
	outp (0x3C9, (rgb >> 18));
	outp (0x3C9, (rgb >> 10) & 63);
	outp (0x3C9, (rgb >> 2) & 63);
	pal [entry] = rgb;
}

void setsnespalette (byte entry, word value, byte brightness)
{
	if (brightness == 0xF)
		setpaletteentry (entry, (value & 0x1F) << 3, ((value >> 5) & 0x1F) << 3, ((value >> 10) & 0x1F) << 3);
	else	
		setpaletteentry (entry, ((value & 0x1F) * (brightness + 1)) >> 1,
			(((value >> 5) & 0x1F) * (brightness + 1)) >> 1, (((value >> 10) & 0x1F) * (brightness+1)) >> 1);
}

void initshades (int start, dword rgb, int numhigh)
{
	int x, hr = (rgb >> 16) & 0xFF, hg = (rgb >> 8) & 0xFF, hb = rgb & 0xFF, numlow = (16-numhigh);
	int dr = 255-hr, dg = 255-hg, db = 255-hb;

	for (x = 1; x <= numlow; x++) {
		custompalette [start+x-1] = ((hr * x / numlow) << 16) + ((hg * x / numlow) << 8) + (hb * x / numlow);
	}
	//    x       ?       x       ?
	//  ----- = -----   ----- = -----
	//  numlow   hr       8      255
	for (x = numlow; x < 16; x++) {
		custompalette [start+x] = (((x-numlow+1) * dr / (numhigh+1) + hr) << 16) + \
									(((x-numlow+1) * dg / (numhigh+1) + hg) << 8) + \
									((x-numlow+1) * db / (numhigh+1) + hb);
	}
	//  x-numlow+1     ?  
	//  ---------- = ----- custompalette[start+x] = ? + hr
	//  numhigh+1     dr  
}

void initcustompalette (void)
{
	int x, y, r, g, b;
	
	for (x = 0, y = 0; x < 16; x++, y += 17)
		custompalette [x] = (y << 16) + (y << 8) + y;
	initshades ( 16, 0x0000FFFF, 3); //13/18
	initshades ( 32, 0x0000FF80, 4); //12/18
	initshades ( 48, 0x0000FF00, 4); //12/18
	initshades ( 64, 0x0080FF00, 5); //11/18
	initshades ( 80, 0x00FFFF00, 3); //13/18
	initshades ( 96, 0x00FF8000, 6); //10/18
	initshades (112, 0x00FF0000, 9); //6/18
	initshades (128, 0x00FF0080, 8); //8/18
	initshades (144, 0x00FF00FF, 6); //10/18
	initshades (160, 0x008000FF, 9); //7/18
	initshades (176, 0x000000FF, 9); //4/18
	initshades (192, 0x000080FF, 8); //8/18
	initshades (208, 0x008040FF, 7); //9/18
	initshades (224, 0x0040FF80, 4); //12/18
	initshades (240, 0x00FF8040, 5); //11/18
						 
	for (y = 0; y < 256; y++) {
		pal [y] = custompalette [y];
	}
	for (r = 0; r < 16; r++)
		for (g = 0; g < 16; g++)
			for (b = 0; b < 16; b++) {
				translationtable [r][g][b] = findclosestmatch ((r << 20) + (g << 12) + (b << 4));
			}
}

void applypalettearray (void)
{
	int y;
	
	outp (0x3C8, 0);
	for (y = 0; y < 256; y++) {
		outp (0x3C9, (pal [y] >> 18));
		outp (0x3C9, (pal [y] >> 10) & 63);
		outp (0x3C9, (pal [y] >> 2) & 63);  
	}
}

void applycustompalette (void)
{
	memcpy (pal, custompalette, sizeof (dword) * 256);
	applypalettearray ();
}

void savegamepalette (void)
{
	memcpy (gamepalette, pal, sizeof (dword) * 256);
}

void applygamepalette (void)
{
	memcpy (pal, gamepalette, sizeof (dword) * 256);
	applypalettearray ();
}

int rgbtocpalette (dword rgb)
{
	return translationtable [(rgb >> 20)] [(rgb >> 12) & 15] [(rgb >> 4) & 15];
}

inline int absdif (int num1, int num2)
{
	if (num1 > num2) return num1 - num2;
	else return num2 - num1;
}

int findclosestmatch (dword rgb)
{
	int x, cur, low = 768, lowpos, r = (rgb >> 16) & 0xFF, g = (rgb >> 8) & 0xFF, b = (rgb & 0xFF);

	for (x = 0; x < 256; x++) {
		cur = absdif ((pal[x] >> 16) & 0xFF, r) + absdif ((pal[x] >> 8) & 0xFF, g) + absdif (pal[x] & 0xFF, b);
		if (cur < low) {
			low = cur;
			lowpos = x;
		}
	}
	return lowpos;
}

⌨️ 快捷键说明

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