Ransom has asked for the wisdom of the Perl Monks concerning the following question:
I'm testing out SDL and SDLx, as well as learning Moose. According to the SDLx docs, when setting up move and show handler callbacks, SDLx will automatically pass some variables in. Namely, ($step, $app, $time) = @_;. This was all well and good when I was practicing outside of Moose. All values were passed correctly.
Once Moose was incorporated, I passed an object method as the callback instead of a sub in the main file. Now when the callback is called, I only receive the blessed hashref of the object, none of the $step, $app, or $time values. This effectively breaks my movement and drawing methods.
The main code is like this:#! /opt/perl/bin/perl package game; use strict; use warnings; use SDL; use SDL::Event; use SDL::Events; use SDLx::App; use Switch; use Mob; my $app = SDLx::App->new( w => 400, h => 400, t => "Pew Pew", hw_surface => 1, double_buf => 1, dt => .1, delay => 10, ); SDL::Events::enable_key_repeat(10, 10); my $ship = Mob->new(); $app->add_event_handler(\&moveship); $app->add_move_handler ( \&{$ship->move} ); $app->add_show_handler ( \&{$ship->draw} ); sub moveship { my $event = shift; my $controller = shift; if ($event->type == SDL_KEYDOWN) { switch($event->key_sym()) { case SDLK_UP { $ship->{velocity}->{y} -= 1} case SDLK_DOWN { $ship->{velocity }->{y} += 1} case SDLK_LEFT{ $ship->{velocity }->{x} -= 1} case SDLK_RIGHT{ $ship->{velocity }->{x} += 1} } } } $app->run;
#! /opt/perl/bin/perl package Mob; use Moose; use Data::Dumper; has 'position' => (is => 'rw', builder => 'build_pos'); has 'velocity' => (is => 'rw', builder => 'build_vel'); sub build_pos { return {x => 0, y => 0}; } sub build_vel { return {x => 0, y => 0}; } sub move { print Dumper("Move", @_); } sub draw { print Dumper("Draw", @_); } 1;
As you can see, I've stripped the methods of anything useful in order to just print the argument lists. The counter example of this is to move the callback method to the main file, in which it will pass all the values the documentation specifies.
How can I get the values from SDLx app stuff into my Moose method calls?
Occasionally putting words into a post is beyond my ability so please don't hesitate to ask me for more information or a better description.
Ransom
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: SDLx handlers and Moose
by chromatic (Archbishop) on May 24, 2012 at 20:36 UTC | |
by Ransom (Beadle) on May 25, 2012 at 12:18 UTC | |
by chromatic (Archbishop) on May 25, 2012 at 17:08 UTC | |
by Mr. Muskrat (Canon) on May 25, 2012 at 17:53 UTC | |
by Anonymous Monk on May 25, 2012 at 13:18 UTC | |
by Ransom (Beadle) on May 25, 2012 at 14:16 UTC | |
by chromatic (Archbishop) on May 25, 2012 at 17:12 UTC | |
|