Add sayer name above text, add Surface_ChangeColor function

This commit is contained in:
Ryan Shepherd 2018-10-04 15:52:05 -04:00
parent 87a4340aba
commit cfa7723513
4 changed files with 88 additions and 28 deletions

View File

@ -10,6 +10,7 @@ use SDLx::Text;
use PerlRPG::Console; use PerlRPG::Console;
use PerlRPG::Game; use PerlRPG::Game;
use PerlRPG::Assets; use PerlRPG::Assets;
use PerlRPG::Math;
use vars qw/@ISA @EXPORT @EXPORT_OK %VisibleSprites/; use vars qw/@ISA @EXPORT @EXPORT_OK %VisibleSprites/;
@ISA = qw/Exporter/; @ISA = qw/Exporter/;
@ -22,7 +23,10 @@ use vars qw/@ISA @EXPORT @EXPORT_OK %VisibleSprites/;
sub DrawSpeech { sub DrawSpeech {
my($sayer, $text)=@_; my($sayer, $text)=@_;
my $app = GetOption('App'); 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 = GetAsset($sayer->{'Sprite'});
my $sprite_x = 0; 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 $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'} }); 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(); my $text_obj = SDLx::Text->new();
$text_obj->color( $sayer->{'TextColor'} ); $text_obj->color( $sayer->{'TextColor'} );
$text_obj->text( $text ); $text_obj->text( $text );
my $surface = SDLx::Surface->new( width => $app->width() - $sprite_w, my $surface = SDLx::Surface->new( width => $app->width() - $sprite_w,
height => $sprite_h, 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, $surface->draw_rect( [ $speech_border,
$speech_border, $name_obj->h() + $speech_border,
$surface->width()-2*$speech_border, $surface->width()-2*$speech_border,
$surface->height()-2*$speech_border ], $surface->height()-$name_obj->h()-(2*$speech_border) ],
[0, 0, 0, 192] [@$speech_bg_color[0,1,2], 255]
); );
my $text_x = ($surface->width() - $text_obj->w())/2; 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 ); $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'} = { $VisibleSprites{'_SayerText'} = {
'Depth' => 255, 'Depth' => 255,
'X' => $speech_x, 'X' => $speech_x,
@ -104,7 +134,7 @@ sub ShowSprite {
# Don't want to flip back and forth every time # Don't want to flip back and forth every time
UnloadAsset($file); UnloadAsset($file);
my $s=$a->surface(); my $s=$a->surface();
$a->surface( FlipSurface($s) ); $a->surface( Surface_Flip($s) );
} elsif($name eq 'ticks') { } elsif($name eq 'ticks') {
$val = 1 unless($val); $val = 1 unless($val);
if($val != 1) { if($val != 1) {
@ -173,8 +203,7 @@ sub RenderSprites {
} }
} }
# Fixme -- Still appears broken, but closer sub Surface_Flip {
sub FlipSurface {
my $src=shift(@_); my $src=shift(@_);
my $dst = SDLx::Surface->new( width => $src->width(), my $dst = SDLx::Surface->new( width => $src->width(),
height => $src->height(), 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; 1;

View File

@ -22,6 +22,16 @@ my %opt = (
'App' => undef, '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 { sub GetOption {
my $key = shift(@_); my $key = shift(@_);
if(exists($opt{$key})) { if(exists($opt{$key})) {
@ -39,14 +49,11 @@ sub InitApp {
return undef if($app); return undef if($app);
LogData(INFO, "Creating SDL instance"); LogData(INFO, "Creating SDL instance");
DefGameVar('TargetFPS', 10); foreach (keys %default_gamevars) {
DefGameVar('GameRunning', 1); DefGameVar($_, $default_gamevars{$_});
DefGameVar('ResourceDir', '"."'); }
$app = SDLx::App->new( $app = SDLx::App->new( width => $width, height => $height);
width => $width,
height => $height,
);
$opt{'App'}=$app; $opt{'App'}=$app;
LogData(DEBUG, "SDL Instance created"); LogData(DEBUG, "SDL Instance created");
return $app; return $app;

View File

@ -11,7 +11,7 @@ use PerlRPG::Game;
use PerlRPG::Assets; use PerlRPG::Assets;
use PerlRPG::Drawing; use PerlRPG::Drawing;
use vars qw/@ISA @EXPORT @EXPORT_OK/; use vars qw/@ISA @EXPORT @EXPORT_OK %game_vars/;
@ISA = qw/Exporter/; @ISA = qw/Exporter/;
@EXPORT = qw/CompileScripts RunScript RunScriptTick GetGameVar SetGameVar DefGameVar EvalString/; @EXPORT = qw/CompileScripts RunScript RunScriptTick GetGameVar SetGameVar DefGameVar EvalString/;
@EXPORT_OK = @EXPORT; @EXPORT_OK = @EXPORT;
@ -42,7 +42,7 @@ my %script_commands = (
my %sayers = (); my %sayers = ();
my $lastsayer=undef; my $lastsayer=undef;
my %game_vars = (); %game_vars = ();
sub GetGameVar { sub GetGameVar {
my $key = shift(@_); my $key = shift(@_);
@ -71,6 +71,7 @@ sub DefGameVar {
my($key, $val)=@_; my($key, $val)=@_;
if(!exists $game_vars{$key}) { if(!exists $game_vars{$key}) {
$game_vars{$key} = StringSubst($val); $game_vars{$key} = StringSubst($val);
LogData(DEBUG, "Defined variable '$key' as '$game_vars{$key}'");
} }
} }

View File

@ -1,13 +1,18 @@
__init__: __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" ResourceDir = "Resources"
TargetFPS = 20
drawroom: 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 bg main_room_day.png 0 0 10
Show window 05_window.png 350 100 11 Show window 05_window.png 350 100 11
Show cupboard cupboard.png 120 72 11 Show cupboard cupboard.png 120 72 11
@ -22,7 +27,7 @@ drawroom:
Show desk 11_genie_00.png 230 220 13 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: convo:
Wait Wait