Skip to content
This repository
branch: nqp_pct
Fetching contributors…

Cannot retrieve contributors at this time

file 351 lines (237 sloc) 6.626 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

=head1 TITLE

bounce_parrot_logo.pir - bounce a Parrot logo with the SDL Parrot bindings

=head1 SYNOPSIS

To run this file, run the following command from the Parrot directory:

    $ parrot examples/sdl/bounce_parrot_logo.pir
    $

You'll see the happy Parrot logo in the middle of a new SDL window. Use the
cursor keys to apply velocity in the appropriate directions. Watch it bounce!
Use the Escape key or close the window when you've had enough.

=cut

.sub _main :main
    load_bytecode "SDL/App.pir"
    load_bytecode "SDL/Color.pir"
    load_bytecode "SDL/Rect.pir"
    load_bytecode "SDL/Image.pir"
    load_bytecode "SDL/Sprite.pir"
    load_bytecode "SDL/EventHandler.pir"
    load_bytecode "SDL/Event.pir"

    .local pmc app
    app = new ['SDL'; 'App']
    app.'init'( 'width' => 640, 'height' => 480, 'bpp' => 0, 'flags' => 0 )

    .local pmc main_screen
    main_screen = app.'surface'()

    .local pmc black
    black = new ['SDL'; 'Color']
    black.'init'( 'r' => 0, 'g' => 0, 'b' => 0 )

    .local pmc image
    image = new ['SDL'; 'Image']
    image.'init'( 'examples/sdl/parrot_small.png' )

    .local pmc sprite
    sprite = new ['SDL'; 'Sprite']
    sprite.'init'( 'surface' => image, 'source_x' => 0, 'source_y' => 0, 'dest_x' => 270, 'dest_y' => 212, 'bgcolor' => black )

    _main_loop( main_screen, sprite )
    end
.end

.sub _main_loop
    .param pmc main_screen
    .param pmc sprite

    .local pmc parent_class
    .local pmc class_type
    get_class parent_class, ['SDL'; 'EventHandler']
    subclass class_type, parent_class, 'MoveLogo::EventHandler'

    .local pmc event_handler
    event_handler = new 'MoveLogo::EventHandler'

    .local pmc event
    event = new ['SDL'; 'Event']
    event.'init'()

    .local pmc handler_args
    handler_args = new 'Hash'
    handler_args[ 'screen' ] = main_screen
    handler_args[ 'sprite' ] = sprite

    .local num last_time
    last_time = time

    .local num counter
    counter = 0

    .local num current_time
    current_time = 0

    .local int frame_count
    frame_count = 1

    .local int updated
    event_handler.'draw_screen'( main_screen, sprite )

loop:
    last_time = current_time
    updated = event_handler.'update_position'( sprite, frame_count )

    if updated == 0 goto increase_count

    event_handler.'draw_screen'( main_screen, sprite )

increase_count:
    inc frame_count
    if frame_count < 6 goto get_event
    frame_count = 0

get_event:
    event.'handle_event'( event_handler, handler_args )

    current_time = time
    counter = current_time - last_time

    if counter < 0.016 goto get_event
    goto loop

.end

.namespace [ 'MoveLogo::EventHandler' ]

.sub update_position :method
    .param pmc sprite
    .param int frame_count

    .local int updated
    updated = 0

    .local int x_velocity
    .local int y_velocity

    x_velocity = sprite.'x_velocity'()
    y_velocity = sprite.'y_velocity'()

    .local int abs_velocity
    .local int delta

check_x_update:
    if x_velocity == 0 goto check_y_update

    abs abs_velocity, x_velocity
    set delta, frame_count
    sub delta, abs_velocity

    if delta > 0 goto check_y_update

    updated = 1

    if x_velocity < 0 goto neg_x_dir

    self.'move_sprite_x'( sprite, 1 )
    goto check_y_update

neg_x_dir:
    self.'move_sprite_x'( sprite, -1 )

check_y_update:
    if y_velocity == 0 goto return

    abs abs_velocity, y_velocity
    set delta, frame_count
    sub delta, abs_velocity

    if delta > 0 goto return

    updated = 1

    if y_velocity < 0 goto neg_y_dir

    self.'move_sprite_y'( sprite, 1 )
    goto return

neg_y_dir:
    self.'move_sprite_y'( sprite, -1 )

return:
    .return( updated )
.end

.sub draw_screen :method
    .param pmc screen
    .param pmc sprite

    .local pmc prev_rect
    .local pmc rect
    .local pmc rect_array
    rect_array = new 'ResizablePMCArray'
    set rect_array, 2

    (prev_rect, rect) = sprite.'draw_undraw'( screen )
    set rect_array[ 0 ], prev_rect
    set rect_array[ 1 ], rect

    screen.'update_rects'( rect_array )
# screen.'update_rect'( rect )

.end

.sub move_sprite_x :method
    .param pmc sprite
    .param int direction

    .local int x
    x = sprite.'x'()

    add x, direction

    if x >= 0 goto check_x_max
    x = 0
    goto x_bounce

check_x_max:
    if x < 540 goto x_update
    x = 540

x_bounce:
    .local int x_velocity
    x_velocity = sprite.'x_velocity'()
    x_velocity = -x_velocity
    sprite.'x_velocity'( x_velocity )

x_update:
    sprite.'x'( x )

.end

.sub move_sprite_y :method
    .param pmc sprite
    .param int direction

    .local int y
    y = sprite.'y'()

    add y, direction

    if y >= 0 goto check_y_max
    y = 0
    goto y_bounce

check_y_max:
    if y < 424 goto y_update
    y = 424

y_bounce:
    .local int y_velocity
    y_velocity = sprite.'y_velocity'()
    y_velocity = -y_velocity
    sprite.'y_velocity'( y_velocity )

y_update:
    sprite.'y'( y )

.end

.sub key_down_down :method
    .param pmc event_args

    .local pmc screen
    .local pmc sprite

    screen = event_args[ 'screen' ]
    sprite = event_args[ 'sprite' ]

    .local int y_vel
    y_vel = sprite.'y_velocity'()

    if y_vel == 5 goto return
    inc y_vel

    sprite.'y_velocity'( y_vel )

return:

.end

.sub key_down_up :method
    .param pmc event_args

    .local pmc screen
    .local pmc sprite

    screen = event_args[ 'screen' ]
    sprite = event_args[ 'sprite' ]

    .local int y_vel
    y_vel = sprite.'y_velocity'()

    if y_vel == -5 goto return
    dec y_vel

    sprite.'y_velocity'( y_vel )

return:

.end

.sub key_down_left :method
    .param pmc event_args

    .local pmc screen
    .local pmc sprite

    screen = event_args[ 'screen' ]
    sprite = event_args[ 'sprite' ]

    .local int x_vel
    x_vel = sprite.'x_velocity'()

    if x_vel == -5 goto return
    dec x_vel

    sprite.'x_velocity'( x_vel )

return:

.end

.sub key_down_right :method
    .param pmc event_args

    .local pmc screen
    .local pmc sprite

    screen = event_args[ 'screen' ]
    sprite = event_args[ 'sprite' ]

    .local int x_vel
    x_vel = sprite.'x_velocity'()

    if x_vel == 5 goto return
    inc x_vel

    sprite.'x_velocity'( x_vel )

return:

.end

.sub key_down_escape :method
    .param pmc event_args

    # XXX: a silly way to quit
    end
.end

=head1 AUTHOR

chromatic, E<lt>chromatic at wgz dot orgE<gt>.

=head1 COPYRIGHT

Copyright (C) 2004-2008, Parrot Foundation.

=cut

# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
Something went wrong with that request. Please try again.