| 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 "palette.h"cxx . . $ `<file>/src/palette.h`/ |
| 20 | |
| 21 | #include "file.h"cxx . . $ `<file>/src/file.h`/ |
| 22 | #include "nortsong.h"cxx . . $ `<file>/src/nortsong.h`/ |
| 23 | #include "opentyr.h"cxx . . $ `<file>/src/opentyr.h`/ |
| 24 | #include "video.h"cxx . . $ `<file>/src/video.h`/ |
| 25 | |
| 26 | #include <assert.h> |
| 27 | |
| 28 | static Uint32cxx . . $ Uint32# rgb_to_yuvcxx . . $ rgb_to_yuv(530956271a9518a5).(int r, int g, int b); |
| 29 | |
| 30 | #define PALETTE_COUNTcxx . . $ `src/palette.c:30:9`! 23 |
| 31 | |
| 32 | Palettecxx . . $ Palette# palettescxx . . $ palettes.[PALETTE_COUNTcxx . . $ `src/palette.c:30:9`!]; |
| 33 | int palette_countcxx . . $ palette_count.; |
| 34 | |
| 35 | static Palettecxx . . $ Palette# palettecxx . . $ palette.; |
| 36 | Uint32cxx . . $ Uint32# rgb_palettecxx . . $ rgb_palette.[256], yuv_palettecxx . . $ yuv_palette.[256]; |
| 37 | |
| 38 | Palettecxx . . $ Palette# colorscxx . . $ colors.; |
| 39 | |
| 40 | void JE_loadPalscxx . . $ JE_loadPals(49f6e7a06ebc5aa8).(void) |
| 41 | { |
| 42 | FILEcxx . . $ FILE# *f = dir_fopen_diecxx . . $ dir_fopen_die(2493aa730c89ee72).(data_dircxx . . $ data_dir(33a1f659e94feb42).(), "palette.dat", "rb"); |
| 43 | |
| 44 | palette_countcxx . . $ palette_count. = ftell_eofcxx . . $ ftell_eof(1dcafcd8ef4c6e6c).(f) / (256 * 3); |
| 45 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(palette_count == PALETTE_COUNT); |
| 46 | |
| 47 | for (int p = 0; p < palette_countcxx . . $ palette_count.; ++p) |
| 48 | { |
| 49 | for (int i = 0; i < 256; ++i) |
| 50 | { |
| 51 | // The VGA hardware palette used only 6 bits per component, so the values need to be rescaled to |
| 52 | // 8 bits. The naive way to do this is to simply do (c << 2), padding it with 0's, however this |
| 53 | // makes the maximum value 252 instead of the proper 255. A trick to fix this is to use the upper 2 |
| 54 | // bits of the original value instead. This ensures that the value goes to 255 as the original goes |
| 55 | // to 63. |
| 56 | |
| 57 | Uint8cxx . . $ Uint8# rgb[3]; |
| 58 | fread_u8_diecxx . . $ fread_u8_die(77ebc59a7bf9f340).(rgb, 3, f); |
| 59 | palettescxx . . $ palettes.[p][i].rcxx . . $ SDL_Color#r. = (rgb[0] << 2) | (rgb[0] >> 4); |
| 60 | palettescxx . . $ palettes.[p][i].gcxx . . $ SDL_Color#g. = (rgb[1] << 2) | (rgb[1] >> 4); |
| 61 | palettescxx . . $ palettes.[p][i].bcxx . . $ SDL_Color#b. = (rgb[2] << 2) | (rgb[2] >> 4); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(f); |
| 66 | } |
| 67 | |
| 68 | void set_palettecxx . . $ set_palette(e7c4d2139a064c7f).(Palettecxx . . $ Palette# colors, unsigned int first_color, unsigned int last_color) |
| 69 | { |
| 70 | for (uintcxx . . $ uint# i = first_color; i <= last_color; ++i) |
| 71 | { |
| 72 | palettecxx . . $ palette.[i] = colors[i]; |
| 73 | rgb_palettecxx . . $ rgb_palette.[i] = SDL_MapRGBcxx . . $ SDL_MapRGB(495601ea59c5b7cb).(main_window_tex_formatcxx . . $ main_window_tex_format., palettecxx . . $ palette.[i].rcxx . . $ SDL_Color#r., palettecxx . . $ palette.[i].gcxx . . $ SDL_Color#g., palettecxx . . $ palette.[i].bcxx . . $ SDL_Color#b.); |
| 74 | yuv_palettecxx . . $ yuv_palette.[i] = rgb_to_yuvcxx . . $ rgb_to_yuv(530956271a9518a5).(palettecxx . . $ palette.[i].rcxx . . $ SDL_Color#r., palettecxx . . $ palette.[i].gcxx . . $ SDL_Color#g., palettecxx . . $ palette.[i].bcxx . . $ SDL_Color#b.); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void set_colorscxx . . $ set_colors(1ccd9883187beb24).(SDL_Colorcxx . . $ SDL_Color# color, unsigned int first_color, unsigned int last_color) |
| 79 | { |
| 80 | for (uintcxx . . $ uint# i = first_color; i <= last_color; ++i) |
| 81 | { |
| 82 | palettecxx . . $ palette.[i] = color; |
| 83 | rgb_palettecxx . . $ rgb_palette.[i] = SDL_MapRGBcxx . . $ SDL_MapRGB(495601ea59c5b7cb).(main_window_tex_formatcxx . . $ main_window_tex_format., palettecxx . . $ palette.[i].rcxx . . $ SDL_Color#r., palettecxx . . $ palette.[i].gcxx . . $ SDL_Color#g., palettecxx . . $ palette.[i].bcxx . . $ SDL_Color#b.); |
| 84 | yuv_palettecxx . . $ yuv_palette.[i] = rgb_to_yuvcxx . . $ rgb_to_yuv(530956271a9518a5).(palettecxx . . $ palette.[i].rcxx . . $ SDL_Color#r., palettecxx . . $ palette.[i].gcxx . . $ SDL_Color#g., palettecxx . . $ palette.[i].bcxx . . $ SDL_Color#b.); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void init_step_fade_palettecxx . . $ init_step_fade_palette(78767d3cf152c690).(int diff[256][3], Palettecxx . . $ Palette# colors, unsigned int first_color, unsigned int last_color) |
| 89 | { |
| 90 | for (unsigned int i = first_color; i <= last_color; i++) |
| 91 | { |
| 92 | diff[i][0] = (int)colors[i].rcxx . . $ SDL_Color#r. - palettecxx . . $ palette.[i].rcxx . . $ SDL_Color#r.; |
| 93 | diff[i][1] = (int)colors[i].gcxx . . $ SDL_Color#g. - palettecxx . . $ palette.[i].gcxx . . $ SDL_Color#g.; |
| 94 | diff[i][2] = (int)colors[i].bcxx . . $ SDL_Color#b. - palettecxx . . $ palette.[i].bcxx . . $ SDL_Color#b.; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void init_step_fade_solidcxx . . $ init_step_fade_solid(2c3657619c5a01cf).(int diff[256][3], SDL_Colorcxx . . $ SDL_Color# color, unsigned int first_color, unsigned int last_color) |
| 99 | { |
| 100 | for (unsigned int i = first_color; i <= last_color; i++) |
| 101 | { |
| 102 | diff[i][0] = (int)color.rcxx . . $ SDL_Color#r. - palettecxx . . $ palette.[i].rcxx . . $ SDL_Color#r.; |
| 103 | diff[i][1] = (int)color.gcxx . . $ SDL_Color#g. - palettecxx . . $ palette.[i].gcxx . . $ SDL_Color#g.; |
| 104 | diff[i][2] = (int)color.bcxx . . $ SDL_Color#b. - palettecxx . . $ palette.[i].bcxx . . $ SDL_Color#b.; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void step_fade_palettecxx . . $ step_fade_palette(ccf0b4adc1b21d7).(int diff[256][3], int steps, unsigned int first_color, unsigned int last_color) |
| 109 | { |
| 110 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(steps > 0); |
| 111 | |
| 112 | for (unsigned int i = first_color; i <= last_color; i++) |
| 113 | { |
| 114 | const int delta[3] = { diff[i][0] / steps, diff[i][1] / steps, diff[i][2] / steps }; |
| 115 | |
| 116 | diff[i][0] -= delta[0]; |
| 117 | diff[i][1] -= delta[1]; |
| 118 | diff[i][2] -= delta[2]; |
| 119 | |
| 120 | palettecxx . . $ palette.[i].rcxx . . $ SDL_Color#r. += delta[0]; |
| 121 | palettecxx . . $ palette.[i].gcxx . . $ SDL_Color#g. += delta[1]; |
| 122 | palettecxx . . $ palette.[i].bcxx . . $ SDL_Color#b. += delta[2]; |
| 123 | |
| 124 | rgb_palettecxx . . $ rgb_palette.[i] = SDL_MapRGBcxx . . $ SDL_MapRGB(495601ea59c5b7cb).(main_window_tex_formatcxx . . $ main_window_tex_format., palettecxx . . $ palette.[i].rcxx . . $ SDL_Color#r., palettecxx . . $ palette.[i].gcxx . . $ SDL_Color#g., palettecxx . . $ palette.[i].bcxx . . $ SDL_Color#b.); |
| 125 | yuv_palettecxx . . $ yuv_palette.[i] = rgb_to_yuvcxx . . $ rgb_to_yuv(530956271a9518a5).(palettecxx . . $ palette.[i].rcxx . . $ SDL_Color#r., palettecxx . . $ palette.[i].gcxx . . $ SDL_Color#g., palettecxx . . $ palette.[i].bcxx . . $ SDL_Color#b.); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void fade_palettecxx . . $ fade_palette(59ab901998e00635).(Palettecxx . . $ Palette# colors, int steps, unsigned int first_color, unsigned int last_color) |
| 130 | { |
| 131 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(steps > 0); |
| 132 | |
| 133 | static int diff[256][3]; |
| 134 | init_step_fade_palettecxx . . $ init_step_fade_palette(78767d3cf152c690).(diff, colors, first_color, last_color); |
| 135 | |
| 136 | for (; steps > 0; steps--) |
| 137 | { |
| 138 | setDelaycxx . . $ setDelay(d4f767463ce0a6b3).(1); |
| 139 | |
| 140 | step_fade_palettecxx . . $ step_fade_palette(ccf0b4adc1b21d7).(diff, steps, first_color, last_color); |
| 141 | |
| 142 | JE_showVGAcxx . . $ JE_showVGA(49f6e7a06ebc5aa8).(); |
| 143 | |
| 144 | service_wait_delaycxx . . $ service_wait_delay(49f6e7a06ebc5aa8).(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void fade_solidcxx . . $ fade_solid(e5acc1e0a74570f4).(SDL_Colorcxx . . $ SDL_Color# color, int steps, unsigned int first_color, unsigned int last_color) |
| 149 | { |
| 150 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(steps > 0); |
| 151 | |
| 152 | static int diff[256][3]; |
| 153 | init_step_fade_solidcxx . . $ init_step_fade_solid(2c3657619c5a01cf).(diff, color, first_color, last_color); |
| 154 | |
| 155 | for (; steps > 0; steps--) |
| 156 | { |
| 157 | setDelaycxx . . $ setDelay(d4f767463ce0a6b3).(1); |
| 158 | |
| 159 | step_fade_palettecxx . . $ step_fade_palette(ccf0b4adc1b21d7).(diff, steps, first_color, last_color); |
| 160 | |
| 161 | JE_showVGAcxx . . $ JE_showVGA(49f6e7a06ebc5aa8).(); |
| 162 | |
| 163 | service_wait_delaycxx . . $ service_wait_delay(49f6e7a06ebc5aa8).(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void fade_blackcxx . . $ fade_black(d4f767463ce0a6b3).(int steps) |
| 168 | { |
| 169 | SDL_Colorcxx . . $ SDL_Color# black = { 0, 0, 0 }; |
| 170 | fade_solidcxx . . $ fade_solid(e5acc1e0a74570f4).(black, steps, 0, 255); |
| 171 | } |
| 172 | |
| 173 | void fade_whitecxx . . $ fade_white(d4f767463ce0a6b3).(int steps) |
| 174 | { |
| 175 | SDL_Colorcxx . . $ SDL_Color# white = { 255, 255, 255 }; |
| 176 | fade_solidcxx . . $ fade_solid(e5acc1e0a74570f4).(white, steps, 0, 255); |
| 177 | } |
| 178 | |
| 179 | static Uint32cxx . . $ Uint32# rgb_to_yuvcxx . . $ rgb_to_yuv(530956271a9518a5).(int r, int g, int b) |
| 180 | { |
| 181 | int y = (r + g + b) >> 2, |
| 182 | u = 128 + ((r - b) >> 2), |
| 183 | v = 128 + ((-r + 2 * g - b) >> 3); |
| 184 | return (y << 16) + (u << 8) + v; |
| 185 | } |