|
16 | 16 | my $app = init();
|
17 | 17 |
|
18 | 18 | my $ball = {
|
19 |
| - x => 0, |
20 |
| - y => 0, |
21 |
| - w => 20, |
22 |
| - h => 20, |
23 |
| - vel => 200, |
24 |
| - x_vel => 0, |
25 |
| - y_vel => 0, |
| 19 | + x => 0, |
| 20 | + y => 0, |
| 21 | + w => 20, |
| 22 | + h => 20, |
| 23 | + vel => 200, |
| 24 | + x_vel => 0, |
| 25 | + y_vel => 0, |
26 | 26 |
|
27 | 27 | };
|
28 | 28 |
|
29 | 29 | my $r_ball = {
|
30 |
| - x => 0, |
31 |
| - y => 0, |
32 |
| - w => 20, |
33 |
| - h => 20, |
34 |
| - radians => 0, |
35 |
| - rot_vel => 50, |
36 |
| - radius => 100, |
37 |
| - c_x => $app->w / 2, |
38 |
| - c_y => $app->h / 2, |
| 30 | + x => 0, |
| 31 | + y => 0, |
| 32 | + w => 20, |
| 33 | + h => 20, |
| 34 | + radians => 0, |
| 35 | + rot_vel => 50, |
| 36 | + radius => 100, |
| 37 | + c_x => $app->w / 2, |
| 38 | + c_y => $app->h / 2, |
39 | 39 | };
|
40 | 40 |
|
41 | 41 | sub init {
|
42 | 42 |
|
43 |
| - # Initing video |
44 |
| - # Die here if we cannot make video init |
45 |
| - croak 'Cannot init ' . SDL::get_error() |
46 |
| - if ( SDL::init(SDL_INIT_VIDEO) == -1 ); |
| 43 | + # Initing video |
| 44 | + # Die here if we cannot make video init |
| 45 | + croak 'Cannot init ' . SDL::get_error() |
| 46 | + if ( SDL::init(SDL_INIT_VIDEO) == -1 ); |
47 | 47 |
|
48 |
| - # Create our display window |
49 |
| - # This is our actual SDL application window |
50 |
| - my $a = SDL::Video::set_video_mode( |
51 |
| - 800, 600, 32, |
52 |
| - SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWACCEL |
53 |
| - ); |
| 48 | + # Create our display window |
| 49 | + # This is our actual SDL application window |
| 50 | + my $a = SDL::Video::set_video_mode( 800, 600, 32, |
| 51 | + SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWACCEL ); |
54 | 52 |
|
55 |
| - croak 'Cannot init video mode 800x600x32: ' . SDL::get_error() |
56 |
| - unless $a; |
| 53 | + croak 'Cannot init video mode 800x600x32: ' . SDL::get_error() |
| 54 | + unless $a; |
57 | 55 |
|
58 |
| - return $a; |
| 56 | + return $a; |
59 | 57 | }
|
60 | 58 |
|
61 | 59 | my $game = SDLx::Controller->new();
|
62 | 60 |
|
63 | 61 | sub on_move {
|
64 |
| - my $dt = shift; |
65 |
| - $dt = $dt / 1000; |
66 |
| - $ball->{x} += $ball->{x_vel} * $dt; |
| 62 | + my $dt = shift; |
| 63 | + $dt = $dt / 1000; |
| 64 | + $ball->{x} += $ball->{x_vel} * $dt; |
67 | 65 |
|
68 |
| - $ball->{y} += $ball->{y_vel} * $dt; |
| 66 | + $ball->{y} += $ball->{y_vel} * $dt; |
69 | 67 |
|
70 |
| - # Period = $r_ball->{vel} |
71 |
| - # cc_speed = 2 * pi * r / T |
72 |
| - $r_ball->{radians} += $r_ball->{rot_vel} * $dt; |
73 |
| - $r_ball->{x} = $r_ball->{c_x} + sin( $r_ball->{radians} * pi / 180 ) * $r_ball->{radius}; |
74 |
| - $r_ball->{y} = $r_ball->{c_y} + cos( $r_ball->{radians} * pi / 180 ) * $r_ball->{radius}; |
| 68 | + # Period = $r_ball->{vel} |
| 69 | + # cc_speed = 2 * pi * r / T |
| 70 | + $r_ball->{radians} += $r_ball->{rot_vel} * $dt; |
| 71 | + $r_ball->{x} = |
| 72 | + $r_ball->{c_x} + sin( $r_ball->{radians} * pi / 180 ) * $r_ball->{radius}; |
| 73 | + $r_ball->{y} = |
| 74 | + $r_ball->{c_y} + cos( $r_ball->{radians} * pi / 180 ) * $r_ball->{radius}; |
75 | 75 |
|
76 |
| - return 1; |
| 76 | + return 1; |
77 | 77 | }
|
78 | 78 |
|
79 | 79 | sub on_event {
|
80 |
| - my $event = shift; |
81 |
| - |
82 |
| - if ( $event->type == SDL_KEYDOWN ) { |
83 |
| - my $key = $event->key_sym; |
84 |
| - if ( $key == SDLK_PRINT ) { |
85 |
| - my $screen_shot_index = 1; |
86 |
| - map { $screen_shot_index = $1 + 1 if $_ =~ /Shot(\d+)\.bmp/ && $1 >= $screen_shot_index } |
87 |
| - <Shot*\.bmp>; |
88 |
| - SDL::Video::save_BMP( |
89 |
| - $app, |
90 |
| - sprintf( "Shot%04d.bmp", $screen_shot_index ) |
91 |
| - ); |
92 |
| - } |
93 |
| - $ball->{y_vel} -= $ball->{vel} if $key == SDLK_UP; |
94 |
| - $ball->{y_vel} += $ball->{vel} if $key == SDLK_DOWN; |
95 |
| - $ball->{x_vel} -= $ball->{vel} if $key == SDLK_LEFT; |
96 |
| - $ball->{x_vel} += $ball->{vel} if $key == SDLK_RIGHT; |
97 |
| - $r_ball->{rot_vel} += 50 if $key == SDLK_SPACE; |
98 |
| - |
99 |
| - } elsif ( $event->type == SDL_KEYUP ) { |
100 |
| - my $key = $event->key_sym; |
101 |
| - $ball->{y_vel} += $ball->{vel} if $key == SDLK_UP; |
102 |
| - $ball->{y_vel} -= $ball->{vel} if $key == SDLK_DOWN; |
103 |
| - $ball->{x_vel} += $ball->{vel} if $key == SDLK_LEFT; |
104 |
| - $ball->{x_vel} -= $ball->{vel} if $key == SDLK_RIGHT; |
105 |
| - |
106 |
| - } elsif ( $event->type == SDL_QUIT ) { |
107 |
| - return 0; |
108 |
| - } |
109 |
| - |
110 |
| - return 1; |
| 80 | + my $event = shift; |
| 81 | + |
| 82 | + if ( $event->type == SDL_KEYDOWN ) { |
| 83 | + my $key = $event->key_sym; |
| 84 | + if ( $key == SDLK_PRINT ) { |
| 85 | + my $screen_shot_index = 1; |
| 86 | + map { |
| 87 | + $screen_shot_index = $1 + 1 |
| 88 | + if $_ =~ /Shot(\d+)\.bmp/ |
| 89 | + && $1 >= $screen_shot_index |
| 90 | + } <Shot*\.bmp>; |
| 91 | + SDL::Video::save_BMP( $app, |
| 92 | + sprintf( "Shot%04d.bmp", $screen_shot_index ) ); |
| 93 | + } |
| 94 | + $ball->{y_vel} -= $ball->{vel} if $key == SDLK_UP; |
| 95 | + $ball->{y_vel} += $ball->{vel} if $key == SDLK_DOWN; |
| 96 | + $ball->{x_vel} -= $ball->{vel} if $key == SDLK_LEFT; |
| 97 | + $ball->{x_vel} += $ball->{vel} if $key == SDLK_RIGHT; |
| 98 | + $r_ball->{rot_vel} += 50 if $key == SDLK_SPACE; |
| 99 | + |
| 100 | + } |
| 101 | + elsif ( $event->type == SDL_KEYUP ) { |
| 102 | + my $key = $event->key_sym; |
| 103 | + $ball->{y_vel} += $ball->{vel} if $key == SDLK_UP; |
| 104 | + $ball->{y_vel} -= $ball->{vel} if $key == SDLK_DOWN; |
| 105 | + $ball->{x_vel} += $ball->{vel} if $key == SDLK_LEFT; |
| 106 | + $ball->{x_vel} -= $ball->{vel} if $key == SDLK_RIGHT; |
| 107 | + |
| 108 | + } |
| 109 | + elsif ( $event->type == SDL_QUIT ) { |
| 110 | + return 0; |
| 111 | + } |
| 112 | + |
| 113 | + return 1; |
111 | 114 | }
|
112 | 115 |
|
113 | 116 | sub on_show {
|
114 |
| - SDL::Video::fill_rect( |
115 |
| - $app, |
116 |
| - SDL::Rect->new( 0, 0, $app->w, $app->h ), |
117 |
| - SDL::Video::map_RGB( $app->format, 0, 0, 0 ) |
118 |
| - ); |
119 |
| - |
120 |
| - SDL::Video::fill_rect( |
121 |
| - $app, |
122 |
| - SDL::Rect->new( $ball->{x}, $ball->{y}, $ball->{w}, $ball->{h} ), |
123 |
| - SDL::Video::map_RGB( $app->format, 0, 0, 255 ) |
124 |
| - ); |
125 |
| - |
126 |
| - SDL::Video::fill_rect( |
127 |
| - $app, |
128 |
| - SDL::Rect->new( |
129 |
| - $r_ball->{x}, $r_ball->{y}, $r_ball->{w}, $r_ball->{h} |
130 |
| - ), |
131 |
| - SDL::Video::map_RGB( $app->format, 255, 0, 0 ) |
132 |
| - ); |
133 |
| - |
134 |
| - SDL::Video::flip($app); |
135 |
| - |
136 |
| - return 0; |
| 117 | + SDL::Video::fill_rect( |
| 118 | + $app, |
| 119 | + SDL::Rect->new( 0, 0, $app->w, $app->h ), |
| 120 | + SDL::Video::map_RGB( $app->format, 0, 0, 0 ) |
| 121 | + ); |
| 122 | + |
| 123 | + SDL::Video::fill_rect( |
| 124 | + $app, |
| 125 | + SDL::Rect->new( $ball->{x}, $ball->{y}, $ball->{w}, $ball->{h} ), |
| 126 | + SDL::Video::map_RGB( $app->format, 0, 0, 255 ) |
| 127 | + ); |
| 128 | + |
| 129 | + SDL::Video::fill_rect( |
| 130 | + $app, |
| 131 | + SDL::Rect->new( |
| 132 | + $r_ball->{x}, $r_ball->{y}, $r_ball->{w}, $r_ball->{h} |
| 133 | + ), |
| 134 | + SDL::Video::map_RGB( $app->format, 255, 0, 0 ) |
| 135 | + ); |
| 136 | + |
| 137 | + SDL::Video::flip($app); |
| 138 | + |
| 139 | + return 0; |
137 | 140 | }
|
138 | 141 |
|
139 | 142 | my $move_id = $game->add_move_handler( \&on_move );
|
|
0 commit comments