aboutsummaryrefslogtreecommitdiff
path: root/backends/gp32/gp32std_grap.cpp
blob: 04ac81a6a27ff0be3fcd6c5d360f43fd3e21a988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001-2006 The ScummVM project
 * Copyright (C) 2005 Won Star - GP32 Backend
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include "stdafx.h"
#include "common/scummsys.h"
#include "common/system.h"
#include "backends/intern.h"

#include "backends/gp32/gp32std.h"
#include "backends/gp32/gp32std_grap.h"

#include "backends/gp32/globals.h"

GPDRAWSURFACE lcdSurface[2];

uint8 flipIndex = 1;

uint16 *frameBuffer1;
uint16 *frameBuffer2;

uint8 gammaLUT[256];
uint8 gammaLUTInv[256];

extern const unsigned char fontresEng1[];
extern const unsigned char fontresKor1[];

void gp_putBitmap8x16(uint16 *frameBuffer, int x, int y, byte *lpBitmap, uint16 wColor) {
	byte *pBitmap  = lpBitmap;
	for (int nRow = 0; nRow < 12; nRow ++) {
		byte data = *pBitmap++;
		for (int nCol = 0; nCol < 7; nCol ++) {
			if (data & 0x80)
				if (x + nCol >= 0 && y + nRow >= 0 && x + nCol < 320 && y + nRow < 240)
					gpd_drawPixel16(frameBuffer, x + nCol, y + nRow, wColor);

			data <<= 1;
		}
	}
}

void gp_putEngFont(uint16 *frameBuffer, int x, int y, char c, uint16 wColor) {
	byte *pBitmap = (byte *) &fontresEng1[c * 16];
	gp_putBitmap8x16(frameBuffer, x, y, pBitmap, wColor);
}

void gp_textOut(uint16 *frameBuffer, int x, int y, char* lpszText, uint16 wColor) {
	// TODO: Handle korean font
	int nPos = x;
	char* pszText = lpszText;

	while (*pszText != '\0') {
		if (*pszText == '\n') {
			nPos = x;
			y += 8;
		} else {
			gp_putEngFont(frameBuffer, nPos, y, *pszText, wColor);
			nPos += 7;
		}

		pszText++;
	}
}

void gp_fillRect(uint16 *frameBuffer, int16 x, int16 y, int16 w, int16 h, uint16 color) {
	uint16 *buffer = &frameBuffer[(240 - (y + h)) + (240 * x)];
	for (int i = 0; i < w; i++) {
		for (int j = 0; j < h; j++) {
			*buffer++ = color;
		}
		buffer += 240 - h;
	}
}

void gp_initGammaTable(float value)
{
	for (int i = 0; i < 256; i++) {
		if (value == 1.0f) {
			gammaLUT[i] = i;
			gammaLUTInv[i] = i;
		} else {
			gammaLUT[i] = (uint8)(pow((double)i / 256, 1 / (double)value) * 256);
			gammaLUTInv[i] = (uint8)(pow((double)i / 256, (double)value) * 256);
		}
	}
}

uint16 gp_RGBTo16(uint16 r, uint16 g, uint16 b) {
	// GP32 16bit color 5551
	if (g_vars.gammaRamp != 10000) {
		r = gammaLUT[r];
		g = gammaLUT[g];
		b = gammaLUT[b];
	}
	return (((r >> 3) & 0x1F) << 11) | (((g >> 3) & 0x1F) << 6) | ((b >> 3) & 0x1F) << 1;
}

void gp_16ToRGB(uint16 color, uint8 *r, uint8 *g, uint8 *b) {
	*r = ((((color) >> 11) & 0x1F) << 3);
	*g = ((((color) >> 6) & 0x1F) << 3);	//(((color>>5)&0x3F) << 2);
	*b = ((((color) >> 1) & 0x1F) << 3);	//((color&0x1F) << 3);

	if (g_vars.gammaRamp != 10000) {
		*r = gammaLUTInv[*r];
		*g = gammaLUTInv[*g];
		*b = gammaLUTInv[*b];
	}
}

void gp_flipScreen() {
	uint16 *frameBuffer1_old = frameBuffer1;
	uint16 *frameBuffer2_old = frameBuffer2;

	GpSurfaceFlip(&lcdSurface[flipIndex]);
	flipIndex = 1 - flipIndex;
	frameBuffer1 = frameBuffer2_old;
	frameBuffer2 = frameBuffer1_old;
}

void gp_initFrameBuffer() {
	GpLcdSurfaceGet(&lcdSurface[0], 0);
	GpLcdSurfaceGet(&lcdSurface[1], 1);
	GpSurfaceSet(&lcdSurface[0]);
	frameBuffer1 = (uint16 *)lcdSurface[0].ptbuffer;
	frameBuffer2 = (uint16 *)lcdSurface[1].ptbuffer;

	memset(frameBuffer1, 0xFF, LCD_WIDTH * LCD_HEIGHT * sizeof(uint16));
	memset(frameBuffer2, 0xFF, LCD_WIDTH * LCD_HEIGHT * sizeof(uint16));
}