Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

SDLx::Text is not aligning

by Anonymous Monk
on Feb 08, 2020 at 14:50 UTC ( #11112628=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello perl monks, i am doing some game programming and am rewriting pong. I am heaving a problem with the horizontal alignment of text when i use the SDLx::Text module. I want the text to be centered, but it will only align to the left. I have made a short example program to reproduce the problem.

use strict; use warnings; use SDL; use SDLx::App; use SDL::Events; use SDLx::Rect; use SDLx::Text; my $app = SDLx::App->new(w =>500,h=>500,t=>"Pong",exit_on_quit=>1,dt=> +0.02); my $player1={paddle=>SDLx::Rect->new(10,$app->h/2,10,40),v_y=>0,score= +>0}; my $player2={paddle=>SDLx::Rect->new($app->w-20,$app->h/2,10,40),v_y=> +0,score=>0}; my $ball = {rect => SDLx::Rect->new($app->w / 2, $app->h / 2, 10, 10), +v_x=>-2.7,v_y=>1.2}; my $score = SDLx::Text->new(h_align=>'center'); $score->write_to($app,$player1->{score}.'x'.$player2->{score}); $app->update; $app->run;

I see only half the text and it is on the left side, not centered. I tried changing it to 'right', but this is also not working, this time i see no text at all. Only if you change it to 'left', or send no value at all it is correct, on the left-hand side. Could someone please tell me what is wrong with this pogram? Tx.

Replies are listed 'Best First'.
Re: SDLx::Text is not aligning
by cxw (Acolyte) on Feb 09, 2020 at 12:23 UTC
    This is my first post here, so take it with a grain of salt :) . If you set the text before rendering with $score->text, what are the values of $score->x, y, w, and h? The h_align method moves text to the left, so possibly off-screen. You might need to set the size smaller than the default 24.
Re: SDLx::Text is not aligning
by Anonymous Monk on Feb 10, 2020 at 11:11 UTC

    To finish my program i changed it into: my $score = SDLx::Text->new(x=>240);

    It looks about wright.

Re: SDLx::Text is not aligning
by Anonymous Monk on Feb 09, 2020 at 11:11 UTC

    I reckon writing text isn't very interesting anyway.

      I know I'm new here, but I still feel comfortable asking - in the future, would you please not make comments disparaging the OP's stated task? Thank you!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11112628]
Approved by haukex
Front-paged by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2020-03-30 19:56 GMT
Find Nodes?
    Voting Booth?
    To "Disagree to disagree" means to:

    Results (176 votes). Check out past polls.