author | rubidium |
Wed, 09 Jan 2008 18:11:12 +0000 | |
branch | noai |
changeset 9723 | eee46cb39750 |
parent 9703 | d2a6acdbd665 |
child 9724 | b39bc69bb2f2 |
permissions | -rw-r--r-- |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9703
diff
changeset
|
1 |
/* $Id$ */ |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9703
diff
changeset
|
2 |
|
9629 | 3 |
#include "../stdafx.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9703
diff
changeset
|
4 |
#include "../core/alloc_func.hpp" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9703
diff
changeset
|
5 |
#include "../gfx_func.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9703
diff
changeset
|
6 |
#include "../zoom_func.h" |
9629 | 7 |
#include "../debug.h" |
8 |
#include "../table/sprites.h" |
|
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9629
diff
changeset
|
9 |
#include "../video/video_driver.hpp" |
9629 | 10 |
#include "32bpp_anim.hpp" |
11 |
||
12 |
static FBlitter_32bppAnim iFBlitter_32bppAnim; |
|
13 |
||
14 |
void Blitter_32bppAnim::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) |
|
15 |
{ |
|
9703
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
16 |
if (bp->dst < _screen.dst_ptr || bp->dst > (uint32 *)_screen.dst_ptr + _screen.width * _screen.height) { |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
17 |
/* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent Draw() */ |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
18 |
Blitter_32bppOptimized::Draw(bp, mode, zoom); |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
19 |
return; |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
20 |
} |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
21 |
|
9629 | 22 |
const SpriteLoader::CommonPixel *src, *src_line; |
23 |
uint32 *dst, *dst_line; |
|
24 |
uint8 *anim, *anim_line; |
|
25 |
||
26 |
if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height) { |
|
27 |
/* The size of the screen changed; we can assume we can wipe all data from our buffer */ |
|
28 |
free(this->anim_buf); |
|
29 |
this->anim_buf = CallocT<uint8>(_screen.width * _screen.height); |
|
30 |
this->anim_buf_width = _screen.width; |
|
31 |
this->anim_buf_height = _screen.height; |
|
32 |
} |
|
33 |
||
34 |
/* Find where to start reading in the source sprite */ |
|
35 |
src_line = (const SpriteLoader::CommonPixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom); |
|
36 |
dst_line = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left; |
|
37 |
anim_line = this->anim_buf + ((uint32 *)bp->dst - (uint32 *)_screen.dst_ptr) + bp->top * this->anim_buf_width + bp->left; |
|
38 |
||
39 |
for (int y = 0; y < bp->height; y++) { |
|
40 |
dst = dst_line; |
|
41 |
dst_line += bp->pitch; |
|
42 |
||
43 |
src = src_line; |
|
44 |
src_line += bp->sprite_width * ScaleByZoom(1, zoom); |
|
45 |
||
46 |
anim = anim_line; |
|
47 |
anim_line += this->anim_buf_width; |
|
48 |
||
49 |
for (int x = 0; x < bp->width; x++) { |
|
9703
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
50 |
if (src->a == 0) { |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
51 |
/* src->r is 'misused' here to indicate how much more pixels are following with an alpha of 0 */ |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
52 |
int skip = UnScaleByZoom(src->r, zoom); |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
53 |
|
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
54 |
dst += skip; |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
55 |
anim += skip; |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
56 |
x += skip - 1; |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
57 |
src += ScaleByZoom(1, zoom) * skip; |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
58 |
continue; |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
59 |
} |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
60 |
|
9629 | 61 |
switch (mode) { |
62 |
case BM_COLOUR_REMAP: |
|
63 |
/* In case the m-channel is zero, do not remap this pixel in any way */ |
|
64 |
if (src->m == 0) { |
|
9703
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
65 |
*dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst); |
9629 | 66 |
*anim = 0; |
67 |
} else { |
|
68 |
if (bp->remap[src->m] != 0) { |
|
69 |
*dst = ComposeColourPA(this->LookupColourInPalette(bp->remap[src->m]), src->a, *dst); |
|
70 |
*anim = bp->remap[src->m]; |
|
71 |
} |
|
72 |
} |
|
73 |
break; |
|
74 |
||
75 |
case BM_TRANSPARENT: |
|
76 |
/* TODO -- We make an assumption here that the remap in fact is transparency, not some color. |
|
77 |
* This is never a problem with the code we produce, but newgrfs can make it fail... or at least: |
|
78 |
* we produce a result the newgrf maker didn't expect ;) */ |
|
79 |
||
80 |
/* Make the current color a bit more black, so it looks like this image is transparent */ |
|
9703
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
81 |
*dst = MakeTransparent(*dst, 192); |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
82 |
*anim = bp->remap[*anim]; |
9629 | 83 |
break; |
84 |
||
85 |
default: |
|
9703
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
86 |
/* Above 217 is palette animation */ |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
87 |
if (src->m >= 217) *dst = ComposeColourPA(this->LookupColourInPalette(src->m), src->a, *dst); |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
88 |
else *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst); |
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
89 |
*anim = src->m; |
9629 | 90 |
break; |
91 |
} |
|
92 |
dst++; |
|
93 |
anim++; |
|
94 |
src += ScaleByZoom(1, zoom); |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
void Blitter_32bppAnim::DrawColorMappingRect(void *dst, int width, int height, int pal) |
|
100 |
{ |
|
101 |
uint32 *udst = (uint32 *)dst; |
|
102 |
uint8 *anim; |
|
103 |
||
104 |
anim = this->anim_buf + ((uint32 *)dst - (uint32 *)_screen.dst_ptr); |
|
105 |
||
106 |
if (pal == PALETTE_TO_TRANSPARENT) { |
|
107 |
do { |
|
108 |
for (int i = 0; i != width; i++) { |
|
9703
d2a6acdbd665
(svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents:
9631
diff
changeset
|
109 |
*udst = MakeTransparent(*udst, 154); |
9629 | 110 |
*anim = 0; |
111 |
udst++; |
|
112 |
anim++; |
|
113 |
} |
|
114 |
udst = udst - width + _screen.pitch; |
|
115 |
anim = anim - width + this->anim_buf_width; |
|
116 |
} while (--height); |
|
117 |
return; |
|
118 |
} |
|
119 |
if (pal == PALETTE_TO_STRUCT_GREY) { |
|
120 |
do { |
|
121 |
for (int i = 0; i != width; i++) { |
|
122 |
*udst = MakeGrey(*udst); |
|
123 |
*anim = 0; |
|
124 |
udst++; |
|
125 |
anim++; |
|
126 |
} |
|
127 |
udst = udst - width + _screen.pitch; |
|
128 |
anim = anim - width + this->anim_buf_width; |
|
129 |
} while (--height); |
|
130 |
return; |
|
131 |
} |
|
132 |
||
133 |
DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this color table ('%d')", pal); |
|
134 |
} |
|
135 |
||
136 |
void Blitter_32bppAnim::SetPixel(void *video, int x, int y, uint8 color) |
|
137 |
{ |
|
138 |
*((uint32 *)video + x + y * _screen.pitch) = LookupColourInPalette(color); |
|
139 |
/* Set the color in the anim-buffer too */ |
|
140 |
this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = color; |
|
141 |
} |
|
142 |
||
143 |
void Blitter_32bppAnim::SetPixelIfEmpty(void *video, int x, int y, uint8 color) |
|
144 |
{ |
|
145 |
uint32 *dst = (uint32 *)video + x + y * _screen.pitch; |
|
146 |
if (*dst == 0) { |
|
147 |
*dst = LookupColourInPalette(color); |
|
148 |
/* Set the color in the anim-buffer too */ |
|
149 |
this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = color; |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
void Blitter_32bppAnim::DrawRect(void *video, int width, int height, uint8 color) |
|
154 |
{ |
|
155 |
uint32 color32 = LookupColourInPalette(color); |
|
156 |
uint8 *anim_line; |
|
157 |
||
158 |
anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf; |
|
159 |
||
160 |
do { |
|
161 |
uint32 *dst = (uint32 *)video; |
|
162 |
uint8 *anim = anim_line; |
|
163 |
||
164 |
for (int i = width; i > 0; i--) { |
|
165 |
*dst = color32; |
|
166 |
/* Set the color in the anim-buffer too */ |
|
167 |
*anim = color; |
|
168 |
dst++; |
|
169 |
anim++; |
|
170 |
} |
|
171 |
video = (uint32 *)video + _screen.pitch; |
|
172 |
anim_line += this->anim_buf_width; |
|
173 |
} while (--height); |
|
174 |
} |
|
175 |
||
176 |
void Blitter_32bppAnim::CopyFromBuffer(void *video, const void *src, int width, int height) |
|
177 |
{ |
|
178 |
assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch); |
|
179 |
uint32 *dst = (uint32 *)video; |
|
180 |
uint32 *usrc = (uint32 *)src; |
|
181 |
uint8 *anim_line; |
|
182 |
||
183 |
anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf; |
|
184 |
||
185 |
for (; height > 0; height--) { |
|
186 |
memcpy(dst, usrc, width * sizeof(uint32)); |
|
187 |
usrc += width; |
|
188 |
dst += _screen.pitch; |
|
189 |
/* Copy back the anim-buffer */ |
|
190 |
memcpy(anim_line, usrc, width * sizeof(uint8)); |
|
191 |
usrc = (uint32 *)((uint8 *)usrc + width); |
|
192 |
anim_line += this->anim_buf_width; |
|
193 |
} |
|
194 |
||
195 |
/* We update the palette (or the pixels that do animation) immediatly, to avoid graphical glitches */ |
|
196 |
this->PaletteAnimate(217, _use_dos_palette ? 38 : 28); |
|
197 |
} |
|
198 |
||
199 |
void Blitter_32bppAnim::CopyToBuffer(const void *video, void *dst, int width, int height) |
|
200 |
{ |
|
201 |
assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch); |
|
202 |
uint32 *udst = (uint32 *)dst; |
|
203 |
uint32 *src = (uint32 *)video; |
|
204 |
uint8 *anim_line; |
|
205 |
||
206 |
if (this->anim_buf == NULL) return; |
|
207 |
||
208 |
anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf; |
|
209 |
||
210 |
for (; height > 0; height--) { |
|
211 |
memcpy(udst, src, width * sizeof(uint32)); |
|
212 |
src += _screen.pitch; |
|
213 |
udst += width; |
|
214 |
/* Copy the anim-buffer */ |
|
215 |
memcpy(udst, anim_line, width * sizeof(uint8)); |
|
216 |
udst = (uint32 *)((uint8 *)udst + width); |
|
217 |
anim_line += this->anim_buf_width; |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
void Blitter_32bppAnim::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) |
|
222 |
{ |
|
223 |
uint8 *dst, *src; |
|
224 |
||
225 |
/* We need to scroll the anim-buffer too */ |
|
226 |
if (scroll_y > 0) { |
|
227 |
dst = this->anim_buf + left + (top + height - 1) * this->anim_buf_width; |
|
228 |
src = dst - scroll_y * this->anim_buf_width; |
|
229 |
||
230 |
/* Adjust left & width */ |
|
231 |
if (scroll_x >= 0) dst += scroll_x; |
|
232 |
else src -= scroll_x; |
|
233 |
||
234 |
uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x); |
|
235 |
uint th = height - scroll_y; |
|
236 |
for (; th > 0; th--) { |
|
237 |
memcpy(dst, src, tw * sizeof(uint8)); |
|
238 |
src -= this->anim_buf_width; |
|
239 |
dst -= this->anim_buf_width; |
|
240 |
} |
|
241 |
} else { |
|
242 |
/* Calculate pointers */ |
|
243 |
dst = this->anim_buf + left + top * this->anim_buf_width; |
|
244 |
src = dst - scroll_y * this->anim_buf_width; |
|
245 |
||
246 |
/* Adjust left & width */ |
|
247 |
if (scroll_x >= 0) dst += scroll_x; |
|
248 |
else src -= scroll_x; |
|
249 |
||
250 |
/* the y-displacement may be 0 therefore we have to use memmove, |
|
251 |
* because source and destination may overlap */ |
|
252 |
uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x); |
|
253 |
uint th = height + scroll_y; |
|
254 |
for (; th > 0; th--) { |
|
255 |
memmove(dst, src, tw * sizeof(uint8)); |
|
256 |
src += this->anim_buf_width; |
|
257 |
dst += this->anim_buf_width; |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
Blitter_32bppBase::ScrollBuffer(video, left, top, width, height, scroll_x, scroll_y); |
|
262 |
} |
|
263 |
||
264 |
int Blitter_32bppAnim::BufferSize(int width, int height) |
|
265 |
{ |
|
266 |
return width * height * (sizeof(uint32) + sizeof(uint8)); |
|
267 |
} |
|
268 |
||
269 |
void Blitter_32bppAnim::PaletteAnimate(uint start, uint count) |
|
270 |
{ |
|
271 |
uint8 *anim = this->anim_buf; |
|
272 |
||
273 |
/* Never repaint the transparency pixel */ |
|
274 |
if (start == 0) start++; |
|
275 |
||
276 |
/* Let's walk the anim buffer and try to find the pixels */ |
|
277 |
for (int y = 0; y < this->anim_buf_height; y++) { |
|
278 |
for (int x = 0; x < this->anim_buf_width; x++) { |
|
279 |
if (*anim >= start && *anim <= start + count) { |
|
280 |
/* Update this pixel */ |
|
281 |
this->SetPixel(_screen.dst_ptr, x, y, *anim); |
|
282 |
} |
|
283 |
anim++; |
|
284 |
} |
|
285 |
} |
|
286 |
||
287 |
/* Make sure the backend redraws the whole screen */ |
|
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9629
diff
changeset
|
288 |
_video_driver->MakeDirty(0, 0, _screen.width, _screen.height); |
9629 | 289 |
} |
290 |
||
291 |
Blitter::PaletteAnimation Blitter_32bppAnim::UsePaletteAnimation() |
|
292 |
{ |
|
293 |
return Blitter::PALETTE_ANIMATION_BLITTER; |
|
294 |
} |