Debian Codegraph @

src/backgrnd.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "backgrnd.h"
20
21 #include "config.h"
22 #include "mtrand.h"
23 #include "opentyr.h"
24 #include "varz.h"
25 #include "video.h"
26
27 #include <assert.h>
28
29 /*Special Background 2 and Background 3*/
30
31 /*Back Pos 3*/
32 JE_word backPos, backPos2, backPos3;
33 JE_word backMove, backMove2, backMove3;
34
35 /*Main Maps*/
36 JE_word mapX, mapY, mapX2, mapX3, mapY2, mapY3;
37 JE_byte **mapYPos, **mapY2Pos, **mapY3Pos;
38 JE_word mapXPos, oldMapXOfs, mapXOfs, mapX2Ofs, mapX2Pos, mapX3Pos, oldMapX3Ofs, mapX3Ofs, tempMapXOfs;
39 intptr_t mapXbpPos, mapX2bpPos, mapX3bpPos;
40 JE_byte map1YDelay, map1YDelayMax, map2YDelay, map2YDelayMax;
41
42 JE_boolean anySmoothies;
43 JE_byte smoothie_data[9]; /* [1..9] */
44
45 void JE_darkenBackground(JE_word neat) /* wild detail level */
46 {
47 Uint8 *s = VGAScreen->pixels; /* screen pointer, 8-bit specific */
48 int x, y;
49
50 s += 24;
51
52 for (y = 184; y; y--)
53 {
54 for (x = 264; x; x--)
55 {
56 *s = ((((*s & 0x0f) << 4) - (*s & 0x0f) + ((((x - neat - y) >> 2) + *(s-2) + (y == 184 ? 0 : *(s-(VGAScreen->pitch-1)))) & 0x0f)) >> 4) | (*s & 0xf0);
57 s++;
58 }
59 s += VGAScreen->pitch - 264;
60 }
61 }
62
63 void blit_background_row(SDL_Surface *surface, int x, int y, Uint8 **map)
64 {
65 assert(surface->format->BitsPerPixel == 8);
66
67 Uint8 *pixels = (Uint8 *)surface->pixels + (y * surface->pitch) + x,
68 *pixels_ll = (Uint8 *)surface->pixels, // lower limit
69 *pixels_ul = (Uint8 *)surface->pixels + (surface->h * surface->pitch); // upper limit
70
71 for (int y = 0; y < 28; y++)
72 {
73 // not drawing on screen yet; skip y
74 if ((pixels + (12 * 24)) < pixels_ll)
75 {
76 pixels += surface->pitch;
77 continue;
78 }
79
80 for (int tile = 0; tile < 12; tile++)
81 {
82 Uint8 *data = *(map + tile);
83
84 // no tile; skip tile
85 if (data == NULL)
86 {
87 pixels += 24;
88 continue;
89 }
90
91 data += y * 24;
92
93 for (int x = 24; x; x--)
94 {
95 if (pixels >= pixels_ul)
96 return;
97 if (pixels >= pixels_ll && *data != 0)
98 *pixels = *data;
99
100 pixels++;
101 data++;
102 }
103 }
104
105 pixels += surface->pitch - 12 * 24;
106 }
107 }
108
109 void blit_background_row_blend(SDL_Surface *surface, int x, int y, Uint8 **map)
110 {
111 assert(surface->format->BitsPerPixel == 8);
112
113 Uint8 *pixels = (Uint8 *)surface->pixels + (y * surface->pitch) + x,
114 *pixels_ll = (Uint8 *)surface->pixels, // lower limit
115 *pixels_ul = (Uint8 *)surface->pixels + (surface->h * surface->pitch); // upper limit
116
117 for (int y = 0; y < 28; y++)
118 {
119 // not drawing on screen yet; skip y
120 if ((pixels + (12 * 24)) < pixels_ll)
121 {
122 pixels += surface->pitch;
123 continue;
124 }
125
126 for (int tile = 0; tile < 12; tile++)
127 {
128 Uint8 *data = *(map + tile);
129
130 // no tile; skip tile
131 if (data == NULL)
132 {
133 pixels += 24;
134 continue;
135 }
136
137 data += y * 24;
138
139 for (int x = 24; x; x--)
140 {
141 if (pixels >= pixels_ul)
142 return;
143 if (pixels >= pixels_ll && *data != 0)
144 *pixels = (*data & 0xf0) | (((*pixels & 0x0f) + (*data & 0x0f)) / 2);
145
146 pixels++;
147 data++;
148 }
149 }
150
151 pixels += surface->pitch - 12 * 24;
152 }
153 }
154
155 void draw_background_1(SDL_Surface *surface)
156 {
157 SDL_FillRect(surface, NULL, 0);
158
159 Uint8 **map = (Uint8 **)mapYPos + mapXbpPos - 12;
160
161 for (int i = -1; i < 7; i++)
162 {
163 blit_background_row(surface, mapXPos, (i * 28) + backPos, map);
164
165 map += 14;
166 }
167 }
168
169 void draw_background_2(SDL_Surface *surface)
170 {
171 if (map2YDelayMax > 1 && backMove2 < 2)
172 backMove2 = (map2YDelay == 1) ? 1 : 0;
173
174 if (background2 != 0)
175 {
176 // water effect combines background 1 and 2 by synchronizing the x coordinate
177 int x = smoothies[1] ? mapXPos : mapX2Pos;
178
179 Uint8 **map = (Uint8 **)mapY2Pos + (smoothies[1] ? mapXbpPos : mapX2bpPos) - 12;
180
181 for (int i = -1; i < 7; i++)
182 {
183 blit_background_row(surface, x, (i * 28) + backPos2, map);
184
185 map += 14;
186 }
187 }
188
189 /*Set Movement of background*/
190 if (--map2YDelay == 0)
191 {
192 map2YDelay = map2YDelayMax;
193
194 backPos2 += backMove2;
195
196 if (backPos2 > 27)
197 {
198 backPos2 -= 28;
199 mapY2--;
200 mapY2Pos -= 14; /*Map Width*/
201 }
202 }
203 }
204
205 void draw_background_2_blend(SDL_Surface *surface)
206 {
207 if (map2YDelayMax > 1 && backMove2 < 2)
208 backMove2 = (map2YDelay == 1) ? 1 : 0;
209
210 Uint8 **map = (Uint8 **)mapY2Pos + mapX2bpPos - 12;
211
212 for (int i = -1; i < 7; i++)
213 {
214 blit_background_row_blend(surface, mapX2Pos, (i * 28) + backPos2, map);
215
216 map += 14;
217 }
218
219 /*Set Movement of background*/
220 if (--map2YDelay == 0)
221 {
222 map2YDelay = map2YDelayMax;
223
224 backPos2 += backMove2;
225
226 if (backPos2 > 27)
227 {
228 backPos2 -= 28;
229 mapY2--;
230 mapY2Pos -= 14; /*Map Width*/
231 }
232 }
233 }
234
235 void draw_background_3(SDL_Surface *surface)
236 {
237 /* Movement of background */
238 backPos3 += backMove3;
239
240 if (backPos3 > 27)
241 {
242 backPos3 -= 28;
243 mapY3--;
244 mapY3Pos -= 15; /*Map Width*/
245 }
246
247 Uint8 **map = (Uint8 **)mapY3Pos + mapX3bpPos - 12;
248
249 for (int i = -1; i < 7; i++)
250 {
251 blit_background_row(surface, mapX3Pos, (i * 28) + backPos3, map);
252
253 map += 15;
254 }
255 }
256
257 void JE_filterScreen(JE_shortint col, JE_shortint int_)
258 {
259 Uint8 *s = NULL; /* screen pointer, 8-bit specific */
260 int x, y;
261 unsigned int temp;
262
263 if (filterFade)
264 {
265 levelBrightness += levelBrightnessChg;
266 if ((filterFadeStart && levelBrightness < -14) || levelBrightness > 14)
267 {
268 levelBrightnessChg = -levelBrightnessChg;
269 filterFadeStart = false;
270 levelFilter = levelFilterNew;
271 }
272 if (!filterFadeStart && levelBrightness == 0)
273 {
274 filterFade = false;
275 levelBrightness = -99;
276 }
277 }
278
279 if (col != -99 && filtrationAvail)
280 {
281 s = VGAScreen->pixels;
282 s += 24;
283
284 col <<= 4;
285
286 for (y = 184; y; y--)
287 {
288 for (x = 264; x; x--)
289 {
290 *s = col | (*s & 0x0f);
291 s++;
292 }
293 s += VGAScreen->pitch - 264;
294 }
295 }
296
297 if (int_ != -99 && explosionTransparent)
298 {
299 s = VGAScreen->pixels;
300 s += 24;
301
302 for (y = 184; y; y--)
303 {
304 for (x = 264; x; x--)
305 {
306 temp = (*s & 0x0f) + int_;
307 *s = (*s & 0xf0) | (temp >= 0x1f ? 0 : (temp >= 0x0f ? 0x0f : temp));
308 s++;
309 }
310 s += VGAScreen->pitch - 264;
311 }
312 }
313 }
314
315 void JE_checkSmoothies(void)
316 {
317 anySmoothies = (processorType > 2 && (smoothies[1-1] || smoothies[2-1])) || (processorType > 1 && (smoothies[3-1] || smoothies[4-1] || smoothies[5-1]));
318 }
319
320 void lava_filter(SDL_Surface *dst, SDL_Surface *src)
321 {
322 assert(src->format->BitsPerPixel == 8 && dst->format->BitsPerPixel == 8);
323
324 /* we don't need to check for over-reading the pixel surfaces since we only
325 * read from the top 185+1 scanlines, and there should be 320 */
326
327 const int dst_pitch = dst->pitch;
328 Uint8 *dst_pixel = (Uint8 *)dst->pixels + (185 * dst_pitch);
329 const Uint8 * const dst_pixel_ll = (Uint8 *)dst->pixels; // lower limit
330
331 const int src_pitch = src->pitch;
332 const Uint8 *src_pixel = (Uint8 *)src->pixels + (185 * src->pitch);
333 const Uint8 * const src_pixel_ll = (Uint8 *)src->pixels; // lower limit
334
335 int w = 320 * 185 - 1;
336
337 for (int y = 185 - 1; y >= 0; --y)
338 {
339 dst_pixel -= (dst_pitch - 320); // in case pitch is not 320
340 src_pixel -= (src_pitch - 320); // in case pitch is not 320
341
342 for (int x = 320 - 1; x >= 0; x -= 8)
343 {
344 int waver = abs(((w >> 9) & 0x0f) - 8) - 1;
345 w -= 8;
346
347 for (int xi = 8 - 1; xi >= 0; --xi)
348 {
349 --dst_pixel;
350 --src_pixel;
351
352 // value is average value of source pixel (2x), destination pixel above, and destination pixel below (all with waver)
353 // hue is red
354 Uint8 value = 0;
355
356 if (src_pixel + waver >= src_pixel_ll)
357 value += (*(src_pixel + waver) & 0x0f) * 2;
358 value += *(dst_pixel + waver + dst_pitch) & 0x0f;
359 if (dst_pixel + waver - dst_pitch >= dst_pixel_ll)
360 value += *(dst_pixel + waver - dst_pitch) & 0x0f;
361
362 *dst_pixel = (value / 4) | 0x70;
363 }
364 }
365 }
366 }
367
368 void water_filter(SDL_Surface *dst, SDL_Surface *src)
369 {
370 assert(src->format->BitsPerPixel == 8 && dst->format->BitsPerPixel == 8);
371
372 Uint8 hue = smoothie_data[1] << 4;
373
374 /* we don't need to check for over-reading the pixel surfaces since we only
375 * read from the top 185+1 scanlines, and there should be 320 */
376
377 const int dst_pitch = dst->pitch;
378 Uint8 *dst_pixel = (Uint8 *)dst->pixels + (185 * dst_pitch);
379
380 const Uint8 *src_pixel = (Uint8 *)src->pixels + (185 * src->pitch);
381
382 int w = 320 * 185 - 1;
383
384 for (int y = 185 - 1; y >= 0; --y)
385 {
386 dst_pixel -= (dst_pitch - 320); // in case pitch is not 320
387 src_pixel -= (src->pitch - 320); // in case pitch is not 320
388
389 for (int x = 320 - 1; x >= 0; x -= 8)
390 {
391 int waver = abs(((w >> 10) & 0x07) - 4) - 1;
392 w -= 8;
393
394 for (int xi = 8 - 1; xi >= 0; --xi)
395 {
396 --dst_pixel;
397 --src_pixel;
398
399 // pixel is copied from source if not blue
400 // otherwise, value is average of value of source pixel and destination pixel below (with waver)
401 if ((*src_pixel & 0x30) == 0)
402 {
403 *dst_pixel = *src_pixel;
404 }
405 else
406 {
407 Uint8 value = *src_pixel & 0x0f;
408 value += *(dst_pixel + waver + dst_pitch) & 0x0f;
409 *dst_pixel = (value / 2) | hue;
410 }
411 }
412 }
413 }
414 }
415
416 void iced_blur_filter(SDL_Surface *dst, SDL_Surface *src)
417 {
418 assert(src->format->BitsPerPixel == 8 && dst->format->BitsPerPixel == 8);
419
420 Uint8 *dst_pixel = dst->pixels;
421 const Uint8 *src_pixel = src->pixels;
422
423 for (int y = 0; y < 184; ++y)
424 {
425 for (int x = 0; x < 320; ++x)
426 {
427 // value is average value of source pixel and destination pixel
428 // hue is icy blue
429
430 const Uint8 value = (*src_pixel & 0x0f) + (*dst_pixel & 0x0f);
431 *dst_pixel = (value / 2) | 0x80;
432
433 ++dst_pixel;
434 ++src_pixel;
435 }
436
437 dst_pixel += (dst->pitch - 320); // in case pitch is not 320
438 src_pixel += (src->pitch - 320); // in case pitch is not 320
439 }
440 }
441
442 void blur_filter(SDL_Surface *dst, SDL_Surface *src)
443 {
444 assert(src->format->BitsPerPixel == 8 && dst->format->BitsPerPixel == 8);
445
446 Uint8 *dst_pixel = dst->pixels;
447 const Uint8 *src_pixel = src->pixels;
448
449 for (int y = 0; y < 184; ++y)
450 {
451 for (int x = 0; x < 320; ++x)
452 {
453 // value is average value of source pixel and destination pixel
454 // hue is source pixel hue
455
456 const Uint8 value = (*src_pixel & 0x0f) + (*dst_pixel & 0x0f);
457 *dst_pixel = (value / 2) | (*src_pixel & 0xf0);
458
459 ++dst_pixel;
460 ++src_pixel;
461 }
462
463 dst_pixel += (dst->pitch - 320); // in case pitch is not 320
464 src_pixel += (src->pitch - 320); // in case pitch is not 320
465 }
466 }
467
468 /* Background Starfield */
469 typedef struct
470 {
471 Uint8 color;
472 JE_word position; // relies on overflow wrap-around
473 int speed;
474 } StarfieldStar;
475
476 #define MAX_STARS 100
477 #define STARFIELD_HUE 0x90
478 static StarfieldStar starfield_stars[MAX_STARS];
479 int starfield_speed;
480
481 void initialize_starfield(void)
482 {
483 for (int i = MAX_STARS-1; i >= 0; --i)
484 {
485 starfield_stars[i].position = mt_rand() % 320 + mt_rand() % 200 * VGAScreen->pitch;
486 starfield_stars[i].speed = mt_rand() % 3 + 2;
487 starfield_stars[i].color = mt_rand() % 16 + STARFIELD_HUE;
488 }
489 }
490
491 void update_and_draw_starfield(SDL_Surface* surface, int move_speed)
492 {
493 Uint8* p = (Uint8*)surface->pixels;
494
495 for (int i = MAX_STARS-1; i >= 0; --i)
496 {
497 StarfieldStar* star = &starfield_stars[i];
498
499 star->position += (star->speed + move_speed) * surface->pitch;
500
501 if (star->position < 177 * surface->pitch)
502 {
503 if (p[star->position] == 0)
504 {
505 p[star->position] = star->color;
506 }
507
508 // If star is bright enough, draw surrounding pixels
509 if (star->color - 4 >= STARFIELD_HUE)
510 {
511 if (p[star->position + 1] == 0)
512 p[star->position + 1] = star->color - 4;
513
514 if (star->position > 0 && p[star->position - 1] == 0)
515 p[star->position - 1] = star->color - 4;
516
517 if (p[star->position + surface->pitch] == 0)
518 p[star->position + surface->pitch] = star->color - 4;
519
520 if (star->position >= surface->pitch && p[star->position - surface->pitch] == 0)
521 p[star->position - surface->pitch] = star->color - 4;
522 }
523 }
524 }
525 }