Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

SDLx::Text is not aligning

by Anonymous Monk
on Feb 08, 2020 at 14:50 UTC ( [id://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 (Scribe) 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.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-20 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found