summaryrefslogtreecommitdiff
path: root/src/libs/graphics/gfx_common.c
blob: 405f614474e50ba0c3df031fa9359363b5b81c9a (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
//Copyright Paul Reiche, Fred Ford. 1992-2002

/*
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "gfxintrn.h"
#include "libs/graphics/gfx_common.h"
#include "libs/graphics/drawcmd.h"
#include "libs/timelib.h"
#include "libs/misc.h"
		// for TFB_DEBUG_HALT


int ScreenWidth;
int ScreenHeight;
int ScreenWidthActual;
int ScreenHeightActual;
int ScreenColorDepth;
int GraphicsDriver;
int TFB_DEBUG_HALT = 0;

volatile int TransitionAmount = 255;
RECT TransitionClipRect;

static int gscale = GSCALE_IDENTITY;
static int gscale_mode = TFB_SCALE_NEAREST;

void
DrawFromExtraScreen (RECT *r)
{
	TFB_DrawScreen_Copy(r, TFB_SCREEN_EXTRA, TFB_SCREEN_MAIN);
}

void
LoadIntoExtraScreen (RECT *r)
{
	TFB_DrawScreen_Copy(r, TFB_SCREEN_MAIN, TFB_SCREEN_EXTRA);
}

int
SetGraphicScale (int scale)
{
	int old_scale = gscale;
	gscale = (scale ? scale : GSCALE_IDENTITY);
	return old_scale;
}

int
GetGraphicScale (void)
{
	return gscale;
}

int
SetGraphicScaleMode (int mode)
{
	int old_mode = gscale_mode;
	assert (mode >= TFB_SCALE_NEAREST && mode <= TFB_SCALE_TRILINEAR);
	gscale_mode = mode;
	return old_mode;
}

int
GetGraphicScaleMode (void)
{
	return gscale_mode;
}

/* Batching and Unbatching functions.  A "Batch" is a collection of
   DrawCommands that will never be flipped to the screen half-rendered.
   BatchGraphics and UnbatchGraphics function vaguely like a non-blocking
   recursive lock to do this respect. */
void
BatchGraphics (void)
{
	TFB_BatchGraphics ();
}

void
UnbatchGraphics (void)
{
	TFB_UnbatchGraphics ();
}

/* Sleeps this thread until all Draw Commands queued by that thread have
   been processed. */

void
FlushGraphics (void)
{
	TFB_DrawScreen_WaitForSignal ();
}

static void
ExpandRect (RECT *rect, int expansion)
{
	if (rect->corner.x - expansion >= 0)
	{
		rect->extent.width += expansion;
		rect->corner.x -= expansion;
	}
	else
	{
		rect->extent.width += rect->corner.x;
		rect->corner.x = 0;
	}

	if (rect->corner.y - expansion >= 0)
	{
		rect->extent.height += expansion;
		rect->corner.y -= expansion;
	}
	else
	{
		rect->extent.height += rect->corner.y;
		rect->corner.y = 0;
	}

	if (rect->corner.x + rect->extent.width + expansion <= ScreenWidth)
		rect->extent.width += expansion;
	else
		rect->extent.width = ScreenWidth - rect->corner.x;

	if (rect->corner.y + rect->extent.height + expansion <= ScreenHeight)
		rect->extent.height += expansion;
	else
		rect->extent.height = ScreenHeight - rect->corner.y;
}

void
SetTransitionSource (const RECT *pRect)
{
	RECT ActualRect;

	if (pRect)
	{	/* expand the rect to accomodate scalers in OpenGL mode */
		ActualRect = *pRect;
		pRect = &ActualRect;
		ExpandRect (&ActualRect, 2);
	}
	TFB_DrawScreen_Copy (pRect, TFB_SCREEN_MAIN, TFB_SCREEN_TRANSITION);
}

// ScreenTransition() is synchronous (does not return until transition done)
void
ScreenTransition (int TransType, const RECT *pRect)
{
	const TimePeriod DURATION = ONE_SECOND * 31 / 60;
	TimeCount startTime;
	(void) TransType;  /* dodge compiler warning */

	if (pRect)
	{
		TransitionClipRect = *pRect;
	}
	else
	{
		TransitionClipRect.corner.x = 0;
		TransitionClipRect.corner.y = 0;
		TransitionClipRect.extent.width = ScreenWidth;
		TransitionClipRect.extent.height = ScreenHeight;
	}

	TFB_UploadTransitionScreen ();
	
	TransitionAmount = 0;
	FlushGraphics ();
	startTime = GetTimeCounter ();
	while (TransitionAmount < 255)
	{
		TimePeriod deltaT;
		int newAmount;

		SleepThread (ONE_SECOND / 100);

		deltaT = GetTimeCounter () - startTime;
		newAmount = deltaT * 255 / DURATION;
		if (newAmount > 255)
			newAmount = 255;

		TransitionAmount = newAmount;
	}
}