1 /*
2 SDLPoP, a port/conversion of the DOS game Prince of Persia.
3 Copyright (C) 2013-2021  Dávid Nagy
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (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, see <https://www.gnu.org/licenses/>.
17 
18 The authors of this program may be contacted at https://forum.princed.org
19 */
20 
21 #include "common.h"
22 
23 #ifdef USE_LIGHTING
24 
25 image_type* screen_overlay = NULL;
26 Uint32 bgcolor;
27 
28 const char mask_filename[] = "data/light.png";
29 const Uint8 ambient_level = 128;
30 
31 // Called once at startup.
init_lighting()32 void init_lighting() {
33 	if (!enable_lighting) return;
34 
35 	lighting_mask = IMG_Load(locate_file(mask_filename));
36 	if (lighting_mask == NULL) {
37 		sdlperror("IMG_Load (lighting_mask)");
38 		enable_lighting = 0;
39 		return;
40 	}
41 
42 	screen_overlay = SDL_CreateRGBSurface(0, 320, 192, 32, 0xFF << 0, 0xFF << 8, 0xFF << 16, 0xFF << 24);
43 	if (screen_overlay == NULL) {
44 		sdlperror("SDL_CreateRGBSurface (screen_overlay)");
45 		enable_lighting = 0;
46 		return;
47 	}
48 
49 	// "color modulate", i.e. multiply.
50 	int result = SDL_SetSurfaceBlendMode(screen_overlay, SDL_BLENDMODE_MOD);
51 	if (result != 0) {
52 		sdlperror("SDL_SetSurfaceBlendMode (screen_overlay)");
53 	}
54 
55 	result = SDL_SetSurfaceBlendMode(lighting_mask, SDL_BLENDMODE_ADD);
56 	if (result != 0) {
57 		sdlperror("SDL_SetSurfaceBlendMode (lighting_mask)");
58 	}
59 
60 	// ambient lighting
61 	bgcolor = SDL_MapRGBA(screen_overlay->format, ambient_level, ambient_level, ambient_level, SDL_ALPHA_OPAQUE);
62 }
63 
64 // Recreate the lighting overlay based on the torches in the current room.
65 // Called when the current room changes.
redraw_lighting()66 void redraw_lighting() {
67 	if (!enable_lighting) return;
68 	if (lighting_mask == NULL) return;
69 	if (curr_room_tiles == NULL) return;
70 	if (is_cutscene) return;
71 
72 	int result = SDL_FillRect(screen_overlay, NULL, bgcolor);
73 	if (result != 0) {
74 		sdlperror("SDL_FillRect (screen_overlay)");
75 	}
76 
77 	// TODO: Also process nearby offscreen torches?
78 	for (int tile_pos = 0; tile_pos < 30; tile_pos++) {
79 		int tile_type = curr_room_tiles[tile_pos] & 0x1F;
80 		if (tile_type == tiles_19_torch || tile_type == tiles_30_torch_with_debris) {
81 			// Center of the flame.
82 			int x = (tile_pos%10)*32+48;
83 			int y = (tile_pos/10)*63+22;
84 
85 			// Align the center of lighting mask to the center of the flame.
86 			SDL_Rect dest_rect;
87 			dest_rect.x = x - lighting_mask->w / 2;
88 			dest_rect.y = y - lighting_mask->h / 2;
89 			dest_rect.w = lighting_mask->w;
90 			dest_rect.h = lighting_mask->h;
91 
92 			int result = SDL_BlitSurface(lighting_mask, NULL, screen_overlay, &dest_rect);
93 			if (result != 0) {
94 				sdlperror("SDL_BlitSurface (lighting_mask)");
95 			}
96 		}
97 	}
98 	if (upside_down) {
99 		flip_screen(screen_overlay);
100 	}
101 }
102 
103 // Copy a part of the lighting overlay onto the screen.
104 // Called when the screen is updated.
update_lighting(const rect_type far * target_rect_ptr)105 void update_lighting(const rect_type far *target_rect_ptr) {
106 	if (!enable_lighting) return;
107 	if (lighting_mask == NULL) return;
108 	if (curr_room_tiles == NULL) return;
109 	if (is_cutscene) return;
110 
111 	SDL_Rect sdlrect;
112 	rect_to_sdlrect(target_rect_ptr, &sdlrect);
113 	int result = SDL_BlitSurface(screen_overlay, &sdlrect, onscreen_surface_, &sdlrect);
114 	if (result != 0) {
115 		sdlperror("SDL_BlitSurface (screen_overlay)");
116 	}
117 }
118 
119 #endif // USE_LIGHTING
120