-
Notifications
You must be signed in to change notification settings - Fork 142
/
lighting.c
119 lines (97 loc) · 3.54 KB
/
lighting.c
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
/*
SDLPoP, a port/conversion of the DOS game Prince of Persia.
Copyright (C) 2013-2023 Dávid Nagy
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 3 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, see <https://www.gnu.org/licenses/>.
The authors of this program may be contacted at https://forum.princed.org
*/
#include "common.h"
#ifdef USE_LIGHTING
image_type* screen_overlay = NULL;
Uint32 bgcolor;
const char mask_filename[] = "data/light.png";
const Uint8 ambient_level = 128;
// Called once at startup.
void init_lighting() {
if (!enable_lighting) return;
lighting_mask = IMG_Load(locate_file(mask_filename));
if (lighting_mask == NULL) {
sdlperror("IMG_Load (lighting_mask)");
enable_lighting = 0;
return;
}
screen_overlay = SDL_CreateRGBSurface(0, 320, 192, 32, Rmsk, Gmsk, Bmsk, Amsk);
if (screen_overlay == NULL) {
sdlperror("SDL_CreateRGBSurface (screen_overlay)");
enable_lighting = 0;
return;
}
// "color modulate", i.e. multiply.
int result = SDL_SetSurfaceBlendMode(screen_overlay, SDL_BLENDMODE_MOD);
if (result != 0) {
sdlperror("SDL_SetSurfaceBlendMode (screen_overlay)");
}
result = SDL_SetSurfaceBlendMode(lighting_mask, SDL_BLENDMODE_ADD);
if (result != 0) {
sdlperror("SDL_SetSurfaceBlendMode (lighting_mask)");
}
// ambient lighting
bgcolor = SDL_MapRGBA(screen_overlay->format, ambient_level, ambient_level, ambient_level, SDL_ALPHA_OPAQUE);
}
// Recreate the lighting overlay based on the torches in the current room.
// Called when the current room changes.
void redraw_lighting() {
if (!enable_lighting) return;
if (lighting_mask == NULL) return;
if (curr_room_tiles == NULL) return;
if (is_cutscene) return;
int result = SDL_FillRect(screen_overlay, NULL, bgcolor);
if (result != 0) {
sdlperror("SDL_FillRect (screen_overlay)");
}
// TODO: Also process nearby offscreen torches?
for (int tile_pos = 0; tile_pos < 30; tile_pos++) {
int tile_type = curr_room_tiles[tile_pos] & 0x1F;
if (tile_type == tiles_19_torch || tile_type == tiles_30_torch_with_debris) {
// Center of the flame.
int x = (tile_pos%10)*32+48;
int y = (tile_pos/10)*63+22;
// Align the center of lighting mask to the center of the flame.
SDL_Rect dest_rect;
dest_rect.x = x - lighting_mask->w / 2;
dest_rect.y = y - lighting_mask->h / 2;
dest_rect.w = lighting_mask->w;
dest_rect.h = lighting_mask->h;
int result = SDL_BlitSurface(lighting_mask, NULL, screen_overlay, &dest_rect);
if (result != 0) {
sdlperror("SDL_BlitSurface (lighting_mask)");
}
}
}
if (upside_down) {
flip_screen(screen_overlay);
}
}
// Copy a part of the lighting overlay onto the screen.
// Called when the screen is updated.
void update_lighting(const rect_type* target_rect_ptr) {
if (!enable_lighting) return;
if (lighting_mask == NULL) return;
if (curr_room_tiles == NULL) return;
if (is_cutscene) return;
SDL_Rect sdlrect;
rect_to_sdlrect(target_rect_ptr, &sdlrect);
int result = SDL_BlitSurface(screen_overlay, &sdlrect, onscreen_surface_, &sdlrect);
if (result != 0) {
sdlperror("SDL_BlitSurface (screen_overlay)");
}
}
#endif // USE_LIGHTING