++ed by:

1 PAUSE user

Author image Jeffrey T. Palmer
and 1 contributors


SDLx::Widget::Menu - create menus for your SDL apps easily


Create a simple SDL menu for your game/app:

    my $menu = SDLx::Widget::Menu->new->items(
                   'New Game' => \&play,
                   'Options'  => \&settings,
                   'Quit'     => \&quit,

Or customize it at will:

    my $menu = SDLx::Widget::Menu->new(
                   topleft      => [100, 120],
                   h_align      => 'right',
                   spacing      => 10,
                   mouse        => 1,
                   font         => 'mygame/data/menu_font.ttf',
                   font_size    => 20,
                   font_color   => [255, 0, 0], # RGB (in this case, 'red')
                   select_color => [0, 255, 0],
                   active_color => [0, 0, 255],
                   change_sound => 'game/data/menu_select.ogg',
                   'New Game' => \&play,
                   'Options'  => \&settings,
                   'Quit'     => \&quit,

After that, all you have to do is make sure your menu object's hooks are called at the right time in your game loop:

    # in the event loop
    $menu->event_hook( $event );  # $event is a SDL::Event

    # in the rendering loop
    $menu->render( $screen );     # $screen is a SDL::Surface


Main menus are a very common thing in games. They let the player choose between a new game, loading games, setting up controls, among lots of other stuff. This menu widget is meant to aid developers create menus quickly and easily, so they can concentrate in their game's logic rather than on such a repetitive task. Simple menus, easy. Complex menus, possible!


This is a new module and the API is subject to change without notice. If you care, please join the discussion on the #sdl IRC channel in irc.perl.org. All thoughts on further improving the API are welcome.

You have been warned :)



new( %options )

Creates a new SDLx::Widget::Menu object. No option is mandatory. Available options are:

  • topleft => [ $top, $left ]

    Determines topmost and leftmost positions for the menu. Defaults to [ 0, 0 ].

  • h_align => 'center'

    Sets the preferred menu text alignment. Default is 'center', which will center menu items to their largest entry. Other possible values are 'left' and 'right'.

  • spacing => 20

    Sets the line spacing between menu items. Default value is 20. Setting this to 0 will place one item right below the other.

  • font => $filename

    File name of the font used to render menu text.

  • font_size => $size

    Size of the font used to render menu text.

  • font_color => [ $red, $green, $blue ]

    RGB value to set the font color.

  • select_color => [ $red, $green, $blue ]

    RGB value for the font color of the selected (clicked) item

  • active_color => [ $red, $green, $blue ]

    RGB value for the font color of the active (hovered) item

  • change_sound => $filename

    File name of the sound to play when the selected item changes

  • mouse => 1

    Indicates that menu items can be clicked using a mouse. By default, mouse input is active.

items( 'Item 1' => \&sub1, 'Item 2' => \&sub2, ... )

event_hook( $event )

render( $surface )

Creates menu items, setting up callbacks for each item.


  • Doesn't let you setup other keys to change current selection (yet)

  • Doesn't let you handle menu changes yourself (yet)


Breno G. de Oliveira, <garu at cpan.org>

Kartik thakore <kthakore at cpan.org>


SDL, SDLx::App, SDLx::Controller