diff --git a/PerlRPG/Drawing.pm b/PerlRPG/Drawing.pm index 2d19cbd..ba47e77 100644 --- a/PerlRPG/Drawing.pm +++ b/PerlRPG/Drawing.pm @@ -10,6 +10,7 @@ use SDLx::Text; use PerlRPG::Console; use PerlRPG::Game; use PerlRPG::Assets; +use PerlRPG::Math; use vars qw/@ISA @EXPORT @EXPORT_OK %VisibleSprites/; @ISA = qw/Exporter/; @@ -22,7 +23,10 @@ use vars qw/@ISA @EXPORT @EXPORT_OK %VisibleSprites/; sub DrawSpeech { my($sayer, $text)=@_; my $app = GetOption('App'); - my $speech_border = 3; + my $speech_border = GetOption('SpeechBorderSize'); + my $speech_border_color = [split(/\s+/,GetOption('SpeechBorderColor'))]; + my $speech_bg_color = [split(/\s+/,GetOption('SpeechBGColor'))]; + my $speech_name_bg_color = [split(/\s+/,GetOption('SpeechNameBGColor'))]; my $sprite = GetAsset($sayer->{'Sprite'}); my $sprite_x = 0; @@ -42,25 +46,51 @@ sub DrawSpeech { $sprite = undef; # ShowSprite may unload this, we don't want to keep a handle to it ShowSprite('_SayerSprite', $sayer->{'Sprite'}, $sprite_x, $sprite_y, 254, @{ $sayer->{'SpriteOpts'} }); + my $name_obj = SDLx::Text->new(); + $name_obj->color( $sayer->{'TextColor'} ); + $name_obj->text( $sayer->{'DisplayName'} ); + my $text_obj = SDLx::Text->new(); $text_obj->color( $sayer->{'TextColor'} ); $text_obj->text( $text ); my $surface = SDLx::Surface->new( width => $app->width() - $sprite_w, height => $sprite_h, - color => [0, 0, 0, 255], + color => $speech_border_color, ); + $surface->draw_rect( [0, 0, $surface->width(), $name_obj->h()], [@$speech_name_bg_color[0,1,2], 255] ); $surface->draw_rect( [ $speech_border, - $speech_border, + $name_obj->h() + $speech_border, $surface->width()-2*$speech_border, - $surface->height()-2*$speech_border ], - [0, 0, 0, 192] + $surface->height()-$name_obj->h()-(2*$speech_border) ], + [@$speech_bg_color[0,1,2], 255] ); my $text_x = ($surface->width() - $text_obj->w())/2; - my $text_y = ($surface->height() - $text_obj->h())/2; + my $text_y = $name_obj->h() + ($surface->height()-$name_obj->h() - $text_obj->h())/2; + $name_obj->write_xy( $surface, 0, 0 ); $text_obj->write_xy( $surface, $text_x, $text_y ); + # Recreate alpha channels + if($speech_name_bg_color->[3] != 255) { + Surface_ChangeColor( + $surface, + [@$speech_name_bg_color[0,1,2],255], + $speech_name_bg_color, + [0, 0, $surface->width(), $name_obj->h()] + ); + } + + if($speech_bg_color->[3] != 255) { + Surface_ChangeColor( $surface, [@$speech_bg_color[0,1,2],255], $speech_bg_color, + [ $speech_border, + $name_obj->h() + $speech_border, + $surface->width() - 2 * $speech_border, + $surface->height() - $name_obj->h() - 2 * $speech_border + ] + ); + } + $VisibleSprites{'_SayerText'} = { 'Depth' => 255, 'X' => $speech_x, @@ -104,7 +134,7 @@ sub ShowSprite { # Don't want to flip back and forth every time UnloadAsset($file); my $s=$a->surface(); - $a->surface( FlipSurface($s) ); + $a->surface( Surface_Flip($s) ); } elsif($name eq 'ticks') { $val = 1 unless($val); if($val != 1) { @@ -173,8 +203,7 @@ sub RenderSprites { } } -# Fixme -- Still appears broken, but closer -sub FlipSurface { +sub Surface_Flip { my $src=shift(@_); my $dst = SDLx::Surface->new( width => $src->width(), height => $src->height(), @@ -197,10 +226,28 @@ sub FlipSurface { } +# Per pixel, search for $from and change to $to +# Searches within @$rect if provided +sub Surface_ChangeColor { + my($surface, $from, $to, $rect)=@_; + my($start_x, $start_y, $w, $h)=(0,0,$surface->w(),$surface->h()); + if($rect && ref $rect) { + $start_x = Constrain( $rect->[0], 0, $surface->w()); + $start_y = Constrain( $rect->[1], 0, $surface->h()); + $w = Constrain( $rect->[2], 0, $surface->w() - $start_x); + $h = Constrain( $rect->[3], 0, $surface->h() - $start_y); + } + my $frompix = SDL::Video::map_RGBA( $surface->format(), @$from); + my $topix = SDL::Video::map_RGBA( $surface->format(), @$to); - - - + for(my $y=$start_y; $y<$start_y + $h; $y++) { + for(my $x=$start_x; $x<$start_x + $w; $x++) { + if($surface->get_pixel( $y, $x ) == $frompix) { + $surface->set_pixel( $y, $x, $topix ); + } + } + } +} 1; diff --git a/PerlRPG/Game.pm b/PerlRPG/Game.pm index e0c51cc..0a3424d 100644 --- a/PerlRPG/Game.pm +++ b/PerlRPG/Game.pm @@ -22,6 +22,16 @@ my %opt = ( 'App' => undef, ); +my %default_gamevars = ( + 'TargetFPS' => 10, + 'GameRunning' => 1, + 'ResourceDir' => '"."', + 'SpeechBorderSize' => 3, + 'SpeechBorderColor' => '"255 255 255 255"', + 'SpeechBGColor' => '"192 192 192 255"', + 'SpeechNameBGColor' => '"0 0 0 0"', +); + sub GetOption { my $key = shift(@_); if(exists($opt{$key})) { @@ -39,14 +49,11 @@ sub InitApp { return undef if($app); LogData(INFO, "Creating SDL instance"); - DefGameVar('TargetFPS', 10); - DefGameVar('GameRunning', 1); - DefGameVar('ResourceDir', '"."'); + foreach (keys %default_gamevars) { + DefGameVar($_, $default_gamevars{$_}); + } - $app = SDLx::App->new( - width => $width, - height => $height, - ); + $app = SDLx::App->new( width => $width, height => $height); $opt{'App'}=$app; LogData(DEBUG, "SDL Instance created"); return $app; diff --git a/PerlRPG/Script.pm b/PerlRPG/Script.pm index 1b86140..129c02e 100644 --- a/PerlRPG/Script.pm +++ b/PerlRPG/Script.pm @@ -11,7 +11,7 @@ use PerlRPG::Game; use PerlRPG::Assets; use PerlRPG::Drawing; -use vars qw/@ISA @EXPORT @EXPORT_OK/; +use vars qw/@ISA @EXPORT @EXPORT_OK %game_vars/; @ISA = qw/Exporter/; @EXPORT = qw/CompileScripts RunScript RunScriptTick GetGameVar SetGameVar DefGameVar EvalString/; @EXPORT_OK = @EXPORT; @@ -42,7 +42,7 @@ my %script_commands = ( my %sayers = (); my $lastsayer=undef; -my %game_vars = (); +%game_vars = (); sub GetGameVar { my $key = shift(@_); @@ -71,6 +71,7 @@ sub DefGameVar { my($key, $val)=@_; if(!exists $game_vars{$key}) { $game_vars{$key} = StringSubst($val); + LogData(DEBUG, "Defined variable '$key' as '$game_vars{$key}'"); } } diff --git a/Resources/script.gs b/Resources/script.gs index 40f6e0d..50afcda 100644 --- a/Resources/script.gs +++ b/Resources/script.gs @@ -1,13 +1,18 @@ __init__: - SetBackground 128 128 128 255 - SetAssetOption spritesheet.png Animated - AddSayer h "Hermione" aka01.png 200 200 200 255 - AddSayer h2 "Hermione2" aka05.png 200 200 200 255 left flip - - TargetFPS = 20 ResourceDir = "Resources" + TargetFPS = 20 drawroom: + SetBackground 0 0 0 255 + SetAssetOption spritesheet.png Animated + AddSayer h "Hermione" aka01.png 255 0 0 255 + AddSayer h2 "Hermione2" aka05.png 32 32 255 255 left flip + + SpeechBorderSize = 3 + SpeechBorderColor = "128 128 128 255" + SpeechBGColor = "192 192 192 255" + SpeechNameBGColor = "0 0 0 0" + Show bg main_room_day.png 0 0 10 Show window 05_window.png 350 100 11 Show cupboard cupboard.png 120 72 11 @@ -22,7 +27,7 @@ drawroom: Show desk 11_genie_00.png 230 220 13 - Show hermione spritesheet.png 450 310 14 reverse ticks=3 + Show hermione spritesheet.png 450 310 14 reverse ticks=4 convo: Wait