| 1 | package Games::PangZero::Graphics; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use SDL; | ||
| 7 | use SDL::Surface; | ||
| 8 | use SDL::Palette; | ||
| 9 | use SDL::PixelFormat; | ||
| 10 | use SDL::Video; | ||
| 11 | use SDL::Event; | ||
| 12 | use SDL::Events; | ||
| 13 | use SDL::Color; | ||
| 14 | use SDL::Config; | ||
| 15 | use SDL::Cursor; | ||
| 16 | use SDL::GFX::Rotozoom; | ||
| 17 | use SDL::Mixer; | ||
| 18 | use SDL::Mixer::Samples; | ||
| 19 | use SDL::Mixer::Channels; | ||
| 20 | use SDL::Mixer::Music; | ||
| 21 | use SDL::Mixer::MixChunk; | ||
| 22 | use SDL::Mixer::MixMusic; | ||
| 23 | use SDL::Joystick; | ||
| 24 | use SDL::Mouse; | ||
| 25 | use SDL::Image; | ||
| 26 | use SDLx::App; | ||
| 27 | use SDLx::SFont; | ||
| 28 | |||
| 29 | sub LoadSurfaces { | ||
| 30 | my ($i, $transparentColor); | ||
| 31 | |||
| 32 | my %balls = qw ( | ||
| 33 | ball0 Balls-Red128.png ball1 Balls-Red96.png ball2 Balls-Red64.png ball3 Balls-Red32.png ball4 Balls-Red16.png | ||
| 34 | xmas Balls-XMAS128.png | ||
| 35 | ball4 Balls-Red16.png ball3 Balls-Red32.png | ||
| 36 | bouncy2 Balls-Bouncy64.png bouncy3 Balls-Bouncy32.png bouncy4 Balls-Bouncy16.png | ||
| 37 | hexa0 Hexa-64.png hexa1 Hexa-32.png hexa2 Hexa-16.png | ||
| 38 | blue1 Balls-Water96.png blue2 Balls-Water64.png blue3 Balls-Water32.png blue4 Balls-Water16.png | ||
| 39 | frag0 Balls-Fragile128.png frag1 Balls-Fragile96.png frag2 Balls-Fragile64.png frag3 Balls-Fragile32.png frag4 Balls-Fragile16.png | ||
| 40 | green1 Balls-SuperClock96.png green2 Balls-SuperClock64.png gold1 Balls-SuperStar96.png gold2 Balls-SuperStar64.png | ||
| 41 | death2 Balls-Death64.png | ||
| 42 | white2 Balls-Seeker64.png white3 Balls-Seeker32.png | ||
| 43 | quake2 Balls-EarthQ64.png quake3 Balls-EarthQ32.png quake4 Balls-EarthQ16.png | ||
| 44 | upside0 Balls-Upside128.png upside1 Balls-Upside96.png upside2 Balls-Upside64.png upside3 Balls-Upside32.png upside4 Balls-Upside16.png | ||
| 45 | ); | ||
| 46 | |||
| 47 | foreach (sort keys %balls) { | ||
| 48 | $Games::PangZero::BallSurfaces{$_} = SDL::Image::load("$Games::PangZero::DataDir/$balls{$_}"); | ||
| 49 | $Games::PangZero::BallSurfaces{$_} = SDL::Video::display_format($Games::PangZero::BallSurfaces{$_}); | ||
| 50 | $transparentColor = $Games::PangZero::BallSurfaces{$_}->get_pixel(0); | ||
| 51 | SDL::Video::set_color_key($Games::PangZero::BallSurfaces{$_}, SDL_SRCCOLORKEY, $transparentColor ); | ||
| 52 | $Games::PangZero::BallSurfaces{"dark$_"} = SDL::Image::load( "$Games::PangZero::DataDir/$balls{$_}"); | ||
| 53 | $Games::PangZero::BallSurfaces{"dark$_"} = SDL::Video::display_format($Games::PangZero::BallSurfaces{"dark$_"}); | ||
| 54 | SDL::Video::set_color_key($Games::PangZero::BallSurfaces{"dark$_"}, SDL_SRCCOLORKEY, $Games::PangZero::BallSurfaces{"dark$_"}->get_pixel(0) ); | ||
| 55 | SDL::Video::set_alpha($Games::PangZero::BallSurfaces{"dark$_"}, SDL_SRCALPHA, 128); | ||
| 56 | } | ||
| 57 | |||
| 58 | $Games::PangZero::BorderSurface = SDL::Image::load("$Games::PangZero::DataDir/border.png"); | ||
| 59 | $Games::PangZero::RedBorderSurface = SDL::Image::load("$Games::PangZero::DataDir/border.png"); | ||
| 60 | $Games::PangZero::WhiteBorderSurface = SDL::Image::load("$Games::PangZero::DataDir/border.png"); | ||
| 61 | $Games::PangZero::BonusSurface = SDL::Image::load("$Games::PangZero::DataDir/bonus.png"); | ||
| 62 | $Games::PangZero::LevelIndicatorSurface = SDL::Image::load("$Games::PangZero::DataDir/level.png"); | ||
| 63 | $Games::PangZero::LevelIndicatorSurface2 = SDL::Image::load("$Games::PangZero::DataDir/level_empty.png"); | ||
| 64 | |||
| 65 | AlterPalette( $Games::PangZero::RedBorderSurface, sub { 1; }, | ||
| 66 | sub { shift @_; my ($h, $s, $i) = Games::PangZero::Palette::RgbToHsi(@_); | ||
| 67 | return Games::PangZero::Palette::HsiToRgb( $h - 30, $s, $i * 0.75 + 63); } ); | ||
| 68 | AlterPalette( $Games::PangZero::WhiteBorderSurface, sub { 1; }, | ||
| 69 | sub { shift @_; my ($h, $s, $i) = Games::PangZero::Palette::RgbToHsi(@_); | ||
| 70 | return Games::PangZero::Palette::HsiToRgb( 0, 0, $i*0.25 + 191 ); } ); | ||
| 71 | |||
| 72 | MakeGuySurfaces(); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub MakeGuySurface { | ||
| 76 | my ($player) = @_; | ||
| 77 | my ($guySurfaceFile, $guySurface, $whiteGuySurface, $harpoonSurface); | ||
| 78 | |||
| 79 | $guySurfaceFile = $Games::PangZero::DataDir . '/' . $Games::PangZero::GuyImageFiles[ $player->{imagefileindex} % scalar(@Games::PangZero::GuyImageFiles) ]; | ||
| 80 | $guySurface = SDL::Image::load($guySurfaceFile); | ||
| 81 | $whiteGuySurface = SDL::Image::load($guySurfaceFile); | ||
| 82 | $harpoonSurface = SDL::Image::load("$Games::PangZero::DataDir/harpoon.png"); | ||
| 83 | $player->{hue} = $Games::PangZero::GuyColors[$player->{colorindex}]->[0]; | ||
| 84 | $player->{saturation} = $Games::PangZero::GuyColors[$player->{colorindex}]->[1]; | ||
| 85 | |||
| 86 | AlterPalette($whiteGuySurface, sub {1;}, sub { return (255, 255, 255); } ); | ||
| 87 | AlterPalette( $guySurface, sub { $_[3] > $_[2] and $_[3] > $_[1]; }, | ||
| 88 | sub { | ||
| 89 | shift @_; | ||
| 90 | my ($h, $s, $i) = Games::PangZero::Palette::RgbToHsi(@_); | ||
| 91 | return Games::PangZero::Palette::HsiToRgb($player->{hue}, $player->{saturation}, $i); } | ||
| 92 | ); | ||
| 93 | AlterPalette( $harpoonSurface, sub { 1; }, | ||
| 94 | sub { | ||
| 95 | shift @_; | ||
| 96 | my ($h, $s, $i) = Games::PangZero::Palette::RgbToHsi(@_); | ||
| 97 | return Games::PangZero::Palette::HsiToRgb($player->{hue}, $player->{saturation} * $s / 256, $i); } | ||
| 98 | ); | ||
| 99 | $player->{guySurface} = $guySurface; | ||
| 100 | $player->{whiteGuySurface} = $whiteGuySurface; | ||
| 101 | $player->{harpoonSurface} = $harpoonSurface; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub MakeGuySurfaces { | ||
| 105 | foreach my $player (@Games::PangZero::Players) { | ||
| 106 | MakeGuySurface($player); | ||
| 107 | } | ||
| 108 | |||
| 109 | $Games::PangZero::WhiteHarpoonSurface = SDL::Image::load("$Games::PangZero::DataDir/harpoon.png"); | ||
| 110 | AlterPalette($Games::PangZero::WhiteHarpoonSurface, sub {1;}, sub { return (255, 255, 255); } ); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub AlterPalette { | ||
| 114 | my ($surface, $filterSub, $alterSub) = @_; | ||
| 115 | my ($r, $g, $b); | ||
| 116 | my ($palette, $numColors, $n, $color); | ||
| 117 | |||
| 118 | $palette = $surface->format->palette(); | ||
| 119 | $numColors = ($surface->format->BytesPerPixel == 1) ? $palette->ncolors() : -1; | ||
| 120 | for ($n = 0; $n < $numColors; $n++) { | ||
| 121 | $color = $palette->color_index($n); | ||
| 122 | ($r, $g, $b) = ( $color->r, $color->g, $color->b ); | ||
| 123 | |||
| 124 | next unless $filterSub->($n, $r, $g, $b); | ||
| 125 | ($r, $g, $b) = $alterSub->($n, $r, $g, $b); | ||
| 126 | $r = $g = $b = 4 if ($r == 0 and $g == 0 and $b == 0); | ||
| 127 | |||
| 128 | $color->r($r); | ||
| 129 | $color->g($g); | ||
| 130 | $color->b($b); | ||
| 131 | SDL::Video::set_colors($surface, $n, $color); | ||
| 132 | } | ||
| 133 | $surface = SDL::Video::display_format($surface); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub RenderBorder { | ||
| 137 | my ($borderSurface, $targetSurface) = @_; | ||
| 138 | my ($dstrect, $srcrect1, $srcrect2, $xpos, $ypos, $width, $height); | ||
| 139 | |||
| 140 | $width = $Games::PangZero::ScreenWidth + 2 * $Games::PangZero::ScreenMargin; | ||
| 141 | $height = $Games::PangZero::ScreenHeight + 2 * $Games::PangZero::ScreenMargin; | ||
| 142 | |||
| 143 | # Draw the corners | ||
| 144 | $dstrect = SDL::Rect->new(0, 0, 16, 16); | ||
| 145 | $srcrect1 = SDL::Rect->new(0, 0, 16, 16); | ||
| 146 | SDL::Video::blit_surface($borderSurface, $srcrect1, $targetSurface, $dstrect); | ||
| 147 | $dstrect->x($width - 16); $srcrect1->x(144); | ||
| 148 | SDL::Video::blit_surface($borderSurface, $srcrect1, $targetSurface, $dstrect); | ||
| 149 | $dstrect->y($height - 16); $srcrect1->y(144); | ||
| 150 | SDL::Video::blit_surface($borderSurface, $srcrect1, $targetSurface, $dstrect); | ||
| 151 | $dstrect->x(0); $srcrect1->x(0); | ||
| 152 | SDL::Video::blit_surface($borderSurface, $srcrect1, $targetSurface, $dstrect); | ||
| 153 | |||
| 154 | if(SDL::Config->has('SDL_gfx_rotozoom')) { | ||
| 155 | # Top border | ||
| 156 | my $zoom = SDL::Surface->new(SDL_SWSURFACE(), 128, 16, 32); | ||
| 157 | $srcrect1->x(16); $srcrect1->y(0); $srcrect1->w(128); $srcrect1->h(16); | ||
| 158 | SDL::Video::blit_surface($borderSurface, $srcrect1, $zoom, SDL::Rect->new(0, 0, $srcrect1->w, $srcrect1->h) ); | ||
| 159 | $zoom = SDL::GFX::Rotozoom::zoom_surface($zoom, $Games::PangZero::ScreenWidth / 128, 1, SDL::GFX::Rotozoom::SMOOTHING_OFF()); | ||
| 160 | $dstrect->x(16); $dstrect->y(0); | ||
| 161 | SDL::Video::blit_surface($zoom, SDL::Rect->new(0, 0, $zoom->w, $zoom->h), $targetSurface, $dstrect ); | ||
| 162 | |||
| 163 | # Left border | ||
| 164 | $zoom = SDL::Surface->new(SDL_SWSURFACE(), 16, 128, 32); | ||
| 165 | $srcrect1->x(0); $srcrect1->y(16); $srcrect1->h(128); $srcrect1->w(16); | ||
| 166 | SDL::Video::blit_surface($borderSurface, $srcrect1, $zoom, SDL::Rect->new(0, 0, $srcrect1->w, $srcrect1->h) ); | ||
| 167 | $zoom = SDL::GFX::Rotozoom::zoom_surface($zoom, 1, $Games::PangZero::ScreenHeight / 128, SDL::GFX::Rotozoom::SMOOTHING_OFF()); | ||
| 168 | $dstrect->x(0); $dstrect->y(16); | ||
| 169 | SDL::Video::blit_surface($zoom, SDL::Rect->new(0, 0, $zoom->w, $zoom->h), $targetSurface, $dstrect ); | ||
| 170 | } | ||
| 171 | |||
| 172 | # Draw top and bottom border | ||
| 173 | |||
| 174 | $srcrect1->w(128); $srcrect1->x(16); $srcrect1->y(0); | ||
| 175 | $srcrect2 = SDL::Rect->new( 16, 144, 128, 16 ); | ||
| 176 | for ($xpos = 16; $xpos < $width-16; ) { | ||
| 177 | $dstrect->x($xpos); | ||
| 178 | $dstrect->y(0); | ||
| 179 | SDL::Video::blit_surface($borderSurface, $srcrect1, $targetSurface, $dstrect); | ||
| 180 | $dstrect->y($height - 16); | ||
| 181 | SDL::Video::blit_surface($borderSurface, $srcrect2, $targetSurface, $dstrect); | ||
| 182 | $xpos += $srcrect1->w(); | ||
| 183 | $srcrect1->w(16); $srcrect1->x(128); | ||
| 184 | $srcrect2->w(16); $srcrect2->x(128); | ||
| 185 | } | ||
| 186 | |||
| 187 | # Draw left and right border | ||
| 188 | |||
| 189 | $srcrect1->h(128); $srcrect1->y(16); $srcrect1->x(0); | ||
| 190 | $srcrect2->h(128); $srcrect2->y(16); $srcrect2->x(144); | ||
| 191 | for ($ypos = 16; $ypos < $height-16; ) { | ||
| 192 | $dstrect->x(0); | ||
| 193 | $dstrect->y($ypos); | ||
| 194 | SDL::Video::blit_surface($borderSurface, $srcrect1, $targetSurface, $dstrect); | ||
| 195 | $dstrect->x($width - 16); | ||
| 196 | SDL::Video::blit_surface($borderSurface, $srcrect2, $targetSurface, $dstrect); | ||
| 197 | $ypos += $srcrect1->h(); | ||
| 198 | $srcrect1->h(16); $srcrect1->y(128); | ||
| 199 | $srcrect2->h(16); $srcrect2->y(128); | ||
| 200 | } | ||
| 201 | |||
| 202 | if(SDL::Config->has('SDL_gfx_rotozoom')) { | ||
| 203 | # Top border | ||
| 204 | my $zoom = SDL::Surface->new(SDL::Video::SDL_SWSURFACE(), 128, 16, 32); | ||
| 205 | $srcrect1->x(16); $srcrect1->y(0); $srcrect1->w(128); $srcrect1->h(16); | ||
| 206 | SDL::Video::blit_surface($borderSurface, $srcrect1, $zoom, SDL::Rect->new(0, 0, $srcrect1->w, $srcrect1->h) ); | ||
| 207 | $zoom = SDL::GFX::Rotozoom::zoom_surface($zoom, $Games::PangZero::ScreenWidth / 128, 1, SDL::GFX::Rotozoom::SMOOTHING_OFF()); | ||
| 208 | $dstrect->x(16); $dstrect->y(0); | ||
| 209 | SDL::Video::blit_surface($zoom, SDL::Rect->new(0, 0, $zoom->w, $zoom->h), $targetSurface, $dstrect ); | ||
| 210 | |||
| 211 | # Left border | ||
| 212 | $zoom = SDL::Surface->new( SDL_SWSURFACE(), 16, 128, 32); | ||
| 213 | $srcrect1->x(0); $srcrect1->y(16); $srcrect1->h(128); $srcrect1->w(16); | ||
| 214 | SDL::Video::blit_surface($borderSurface, $srcrect1, $zoom, SDL::Rect->new(0, 0, $srcrect1->w, $srcrect1->h) ); | ||
| 215 | $zoom = SDL::GFX::Rotozoom::zoom_surface($zoom, 1, $Games::PangZero::ScreenHeight / 128, SDL::GFX::Rotozoom::SMOOTHING_OFF()); | ||
| 216 | $dstrect->x(0); $dstrect->y(16); | ||
| 217 | SDL::Video::blit_surface($zoom, SDL::Rect->new(0, 0, $zoom->w, $zoom->h), $targetSurface, $dstrect ); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | sub LoadBackground { | ||
| 222 | my $filename = shift; | ||
| 223 | |||
| 224 | SDL::Video::fill_rect($Games::PangZero::Background, SDL::Rect->new(0, 0, $Games::PangZero::PhysicalScreenWidth, $Games::PangZero::PhysicalScreenHeight), SDL::Color->new(0, 0, 0) ); | ||
| 225 | my $backgroundImage = SDL::Image::load("$Games::PangZero::DataDir/$filename"); | ||
| 226 | my $dstrect = SDL::Rect->new($Games::PangZero::ScreenMargin, $Games::PangZero::ScreenMargin, 0, 0); | ||
| 227 | my $srcrect = SDL::Rect->new(0, 0, $Games::PangZero::ScreenWidth, $Games::PangZero::ScreenHeight); | ||
| 228 | if ($Games::PangZero::ScreenWidth != $backgroundImage->w() or $Games::PangZero::ScreenHeight != $backgroundImage->h()) { | ||
| 229 | if (SDL::Config->has('SDL_gfx_rotozoom')) { | ||
| 230 | my $zoomX = $Games::PangZero::ScreenWidth / $backgroundImage->w(); # $zoomX = 1.0 if $zoomX < 1.0; | ||
| 231 | my $zoomY = $Games::PangZero::ScreenHeight / $backgroundImage->h(); # $zoomY = 1.0 if $zoomY < 1.0; | ||
| 232 | $backgroundImage = SDL::GFX::Rotozoom::zoom_surface($backgroundImage, $zoomX, $zoomY, SDL::GFX::Rotozoom::SMOOTHING_OFF()); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | SDL::Video::blit_surface($backgroundImage, $srcrect, $Games::PangZero::Background, $dstrect); | ||
| 236 | |||
| 237 | RenderBorder($Games::PangZero::BorderSurface, $Games::PangZero::Background); | ||
| 238 | } | ||
| 239 | |||
| 240 | sub TextWidth { | ||
| 241 | SDLx::SFont::SDL_TEXTWIDTH(@_); # perl-sdl-2.x | ||
| 242 | } | ||
| 243 | |||
| 244 | sub FindVideoMode { | ||
| 245 | if ($Games::PangZero::FullScreen < 2) { | ||
| 246 | return (800, 600); | ||
| 247 | } | ||
| 248 | |||
| 249 | # Find a suitable widescreen mode | ||
| 250 | # One native resolution: 1680 x 1050 => 1.6 : 1 | ||
| 251 | # Which could translate to: 840 x 525 => 1.6 : 1 | ||
| 252 | # Some adapters have: 848 x 480 => 1.76 : 1 | ||
| 253 | # 720 x 480 => 1.5 : 1 | ||
| 254 | # 800 x 512 => 1.56 : 1 | ||
| 255 | # Conclusion: Any resolution where w in [800,900], h > 480 and r in [1.5, 1.8] is good | ||
| 256 | |||
| 257 | my ($modes, $mode, @goodModes, $w, $h, $ratio); | ||
| 258 | $modes = SDL::ListModes( 0, SDL_HWSURFACE ); #add back fullscreen | ||
| 259 | foreach $mode (@{$modes}) { | ||
| 260 | $w = $mode->w; | ||
| 261 | $h = $mode->h; | ||
| 262 | $ratio = $w / $h; | ||
| 263 | warn sprintf( "%4d x %4d => %0.3f\n", $w, $h, $ratio ); | ||
| 264 | next if $w < 800 or $w > 900; | ||
| 265 | next if $h < 480; | ||
| 266 | next if $ratio < 1.5 or $ratio > 1.8; | ||
| 267 | push @goodModes, ( { -w => $w, -h => $h, -score => abs($ratio - 1.6) * 1000 + abs($w - 800) } ); | ||
| 268 | } | ||
| 269 | @goodModes = sort { $a->{-score} <=> $b->{-score} } @goodModes; | ||
| 270 | return (800, 600) unless @goodModes; | ||
| 271 | foreach $mode (@goodModes) { | ||
| 272 | print sprintf( '%d x %d => %0.3f (score %d)', $mode->{-w}, $mode->{-h}, $mode->{-w} / $mode->{-h}, $mode->{-score} ), "\n"; | ||
| 273 | } | ||
| 274 | return ($goodModes[0]->{-w}, $goodModes[0]->{-h}); | ||
| 275 | } | ||
| 276 | |||
| 277 | 1; |