aboutsummaryrefslogtreecommitdiff
path: root/backends/PalmOS/Src/os5_gfx.cpp
blob: c7dc2445581791b61f4b96c0d56d3e0778b79e8b (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2001-2006 The ScummVM project
 * Copyright (C) 2002-2006 Chris Apers - PalmOS 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.
 *
 * $Header$
 *
 */

#include "be_os5.h"
#include <PenInputMgr.h>
#include <palmOneResources.h>

#ifdef PALMOS_ARM
#include <System/WIP.h>
#include <Libraries/AIA/palmOneStatusBarMgrARM.h>
#endif

#include "oscalls.h"

void OSystem_PalmOS5::int_initSize(uint w, uint h, int overlayScale) {
}

WinHandle OSystem_PalmOS5::alloc_screen(Coord w, Coord h) {
	Err e;
	WinHandle winH;
	UInt16 old = WinSetCoordinateSystem(kCoordinatesNative);
	winH = WinCreateOffscreenWindow(w, h, nativeFormat, &e);
	WinSetCoordinateSystem(old);
	
	return winH;
}

void OSystem_PalmOS5::load_gfx_mode() {
	Err e;

	if (_gfxLoaded)
		return;
	_gfxLoaded = true;


	_mouseBackupP = (byte *)MemPtrNew(MAX_MOUSE_W * MAX_MOUSE_H * 2); // *2 if 16bit
	_mouseDataP = (byte *)MemPtrNew(MAX_MOUSE_W * MAX_MOUSE_H);
	_offScreenP	= (byte *)malloc(_screenWidth * _screenHeight);

	UInt32 depth = 16;
	WinScreenMode(winScreenModeSet, NULL, NULL, &depth, NULL);
	clearScreen();

	gVars->indicator.on = RGBToColor(0,255,0);
	gVars->indicator.off = RGBToColor(0,0,0);

	_overlayH =  alloc_screen(_screenWidth, _screenHeight);
	_screenH = WinGetDisplayWindow();

	_overlayP = (OverlayColor *)(BmpGetBits(WinGetBitmap(_overlayH)));
	_screenP = (byte *)(BmpGetBits(WinGetBitmap(_screenH)));

	MemSet(_offScreenP, _screenWidth * _screenHeight, 0);
	MemSet(_nativePal, sizeof(_nativePal), 0);
	MemSet(_currentPalette, sizeof(_currentPalette), 0);

	_isSwitchable = (_screenWidth == 320 && _screenHeight == 200 && OPTIONS_TST(kOptCollapsible));
	if (_screenWidth > 320 || _screenHeight > 200 || !_isSwitchable)
		_mode = GFX_NORMAL;

	hotswap_gfx_mode(_mode);
}

void OSystem_PalmOS5::hotswap_gfx_mode(int mode) {
	Err e;
	UInt32 device;
	Boolean isT3;

	if (_mode != GFX_NORMAL && !_isSwitchable)
		return;

	if (_workScreenH)
		WinDeleteWindow(_workScreenH, false);
	_workScreenH = NULL;

	isT3 = false;
#ifdef PALMOS_ARM
	if (!FtrGet(sysFileCSystem, sysFtrNumOEMDeviceID, &device))
		isT3 = (device == kPalmOneDeviceIDTungstenT3);
#endif

	// prevent bad DIA redraw (Stat part)
	if (mode  == GFX_NORMAL) {		
		// only if this API is available
		if (_stretched && OPTIONS_TST(kOptCollapsible)) {
#ifdef PALMOS_ARM
			if (isT3) {
				//AiaSetInputAreaState(aiaInputAreaShow);
				StatShow_68k();
				PINSetInputAreaState_68k(pinInputAreaOpen);
			} else
#endif
			{
				StatShow();
				PINSetInputAreaState(pinInputAreaOpen);
			}
		}

		_redawOSD = true;
		_stretched = false;
		OPTIONS_RST(kOptDisableOnScrDisp);
		_screenDest.w = _screenWidth;
		_screenDest.h = _screenHeight;

		if (_wasRotated) {
			// restore controls rotation
			SWAP(_keyMouse.bitLeft, _keyMouse.bitRight);
			SWAP(_keyMouse.bitRight, _keyMouse.bitDown);
			SWAP(_keyMouse.bitLeft, _keyMouse.bitUp);
			_wasRotated = false;
		}

		_workScreenH = alloc_screen(_screenWidth, _screenHeight);
		_workScreenP = (int16 *)(BmpGetBits(WinGetBitmap(_workScreenH)));
		MemSet(_workScreenP, _screenWidth * _screenHeight * 2, 0);

		_screenOffset.x = (gVars->screenWidth - _screenWidth) / 2;
		_screenOffset.y = (gVars->screenHeight - _screenHeight)  / 2;
		
		_render = &OSystem_PalmOS5::render_1x;

	} else {
#ifdef PALMOS_ARM
		// T3 DIA library is 68k base, there is no possible native call
		if (isT3) {
			//AiaSetInputAreaState(aiaInputAreaFullScreen);
			PINSetInputAreaState_68k(pinInputAreaClosed);
			StatHide_68k();
		} else
#endif
		{
			PINSetInputAreaState(pinInputAreaClosed);
			StatHide();
		}

		_redawOSD = false;
		_stretched = true;
		OPTIONS_SET(kOptDisableOnScrDisp);

		if (OPTIONS_TST(kOptModeLandscape)) {
			_screenDest.w = 480;
			_screenDest.h = 300;
			_workScreenH = alloc_screen(480, 300);
			_render = &OSystem_PalmOS5::render_landscape;

		} else {
			_screenDest.w = 300;
			_screenDest.h = 480;
			_workScreenH = alloc_screen(300, 480);
			_render = &OSystem_PalmOS5::render_portrait;
			// This mode need a controls rotation
			SWAP(_keyMouse.bitLeft, _keyMouse.bitUp);
			SWAP(_keyMouse.bitRight, _keyMouse.bitDown);
			SWAP(_keyMouse.bitLeft, _keyMouse.bitRight);
			_wasRotated = true;
		}

		_workScreenP = (int16 *)(BmpGetBits(WinGetBitmap(_workScreenH)));
		MemSet(_workScreenP, 480 * 300 * 2, 0);

		_screenOffset.x = 0;
		_screenOffset.y = 10;
	}

	_mode = mode;
	clearScreen();
}

void OSystem_PalmOS5::unload_gfx_mode() {
	if (!_gfxLoaded)
		return;	
	_gfxLoaded = false;
	
	MemPtrFree(_mouseBackupP);
	MemPtrFree(_mouseDataP);
	free(_offScreenP);

	if (_workScreenH)
		WinDeleteWindow(_workScreenH, false);
	if (_overlayH)
		WinDeleteWindow(_overlayH, false);
		
	_workScreenH = NULL;
	_overlayH = NULL;

	UInt32 depth = 8;
	WinScreenMode(winScreenModeSet, NULL, NULL, &depth, NULL);
	clearScreen();

	WinSetCoordinateSystem(_sysOldCoord);
}

void OSystem_PalmOS5::copyRectToScreen(const byte *buf, int pitch, int x, int y, int w, int h) {
	// Clip the coordinates
	if (x < 0) {
		w += x;
		buf -= x;
		x = 0;
	}

	if (y < 0) {
		h += y;
		buf -= y * pitch;
		y = 0;
	}

	if (w > _screenWidth - x)
		w = _screenWidth - x;

	if (h > _screenHeight - y)
		h = _screenHeight - y;

	if (w <= 0 || h <= 0)
		return;

	byte *dst = _offScreenP + y * _screenWidth + x;

	if (w == pitch && w == _screenWidth) {
		MemMove(dst, buf, w * h);
	} else {
		do {
			MemMove(dst, buf, w);
			dst += _screenWidth;
			buf += pitch;
		} while (--h);
	}
}

void OSystem_PalmOS5::int_updateScreen() {
	RectangleType r;
	PointType p;

	draw_mouse();
	((this)->*(_render))(r, p);

	_sysOldCoord = WinSetCoordinateSystem(kCoordinatesNative);
	WinCopyRectangle(_workScreenH, _screenH, &r, p.x, p.y, winPaint);
	WinSetCoordinateSystem(_sysOldCoord);
	undraw_mouse();
}

void OSystem_PalmOS5::clearScreen() {
	RGBColorType rgb = { 0,0,0,0 };
	WinSetDrawWindow(WinGetDisplayWindow());
	WinSetBackColorRGB(&rgb, 0);
	WinEraseWindow();
}

void OSystem_PalmOS5::extras_palette(uint8 index, uint8 r, uint8 g, uint8 b) {
	_nativePal[index] = gfxMakeDisplayRGB( r, g, b);
}

void OSystem_PalmOS5::draw_osd(UInt16 id, Int32 x, Int32 y, Boolean show, UInt8 color) {
	if (_mode != GFX_NORMAL)
		return;
//	MemHandle hTemp = DmGetResource(bitmapRsc, id);
	MemHandle hTemp = DmGetResource('abmp', id + 100);

	if (hTemp) {
		RGBColorType oldRGB;
		static const RGBColorType pal[4] = {
			{0,0,255,0},
			{0,255,255,0},
			{0,255,0,0},
			{0,0,0,0}
		};

		BitmapType *bmTemp;
		bmTemp	= (BitmapType *)MemHandleLock(hTemp);

		Coord w, h;
		BmpGetDimensions(bmTemp, &w, &h, 0);

		PointType dst = { _screenOffset.x + x, _screenOffset.y + y };
		RectangleType c, r = { dst.x, dst.y, w, h };

		UInt16 old = WinSetCoordinateSystem(kCoordinatesNative);
		WinSetDrawWindow(_screenH);
		WinGetClip(&c);
		WinResetClip();

		if (show) {
			WinSetForeColorRGB(&pal[3], &oldRGB);
			WinSetBackColorRGB(&pal[color], &oldRGB);
			WinDrawBitmap(bmTemp, dst.x, dst.y);
		} else {
			WinSetBackColorRGB(&pal[3], &oldRGB);
			WinFillRectangle(&r, 0);
		}

		WinSetClip(&c);
		WinSetCoordinateSystem(old);

		MemPtrUnlock(bmTemp);
		DmReleaseResource(hTemp);
	}
}