indexing description: "[ Main scene of animation example ]" date: "$Date$" revision: "$Revision$" class ANIMATION_SCENE inherit EM_SCENE redefine handle_key_down_event, handle_quit_event, handle_update_event end EM_KEY_CONSTANTS EM_SHARED_STANDARD_FONTS create make_scene feature -- Initialization initialize_scene is -- Initialize scene. do -- Initialize and draw whole scene. x := 200 y := 250 initialize_images end feature -- Event handling handle_key_down_event (a_keyboard_event: EM_KEYBOARD_EVENT) is -- Handle keyboard events. do -- press spacebar to pause and start again if a_keyboard_event.key = a_keyboard_event.sdlk_space then if sprite.time_paused = -1 and sprite.playing then -- the sprite is not paused -> pause it sprite.pause traffic_light.pause else -- the sprite is currently paused -> start again sprite.start traffic_light.start end end -- press enter to stop/start the animations if a_keyboard_event.key = a_keyboard_event.sdlk_return then if sprite.playing then sprite.stop traffic_light.stop else sprite.start traffic_light.start end end if a_keyboard_event.key = a_keyboard_event.sdlk_escape then event_loop.stop end end handle_quit_event (a_quit_event: EM_QUIT_EVENT) is -- Handle quit events. do event_loop.stop end handle_update_event is -- Handle the outside event. local keyboard: EM_KEYBOARD do create keyboard.make_snapshot if keyboard.is_pressed (sdlk_up) then y := y - Delta end if keyboard.is_pressed (sdlk_left) then x := x - Delta end if keyboard.is_pressed (sdlk_right) then x := x + Delta end if keyboard.is_pressed (sdlk_down) then y := y + Delta end draw_scene end feature -- Drawing redraw is -- Make feature `redraw' effective do end feature {NONE} -- Implementation draw_scene is -- Draws the image on the screen. do screen.clear sprite.draw (screen) traffic_light.draw (screen) font1.draw_string ("Press spacebar to pause and resume the animation", screen, 20, 20) font1.draw_string ("Press return to stop and play the animation", screen, 20, 40) font2.draw_string ("EM_SPRITE generated by light.anim", screen, 100, 75) font2.draw_string ("EM_SPRITE_COMPOSITION", screen, 100, 175) screen.redraw end draw_image_part (a_surface: EM_SURFACE; an_x: INTEGER; an_y: INTEGER; a_width: INTEGER; a_height: INTEGER; dest_x: INTEGER; dest_y: INTEGER) is -- Draws `an_image' part from `an_x' and `an_y' to `a_width' and `a_height' on `screen' at `dest_x', `dest_y'. require a_surface_not_void: a_surface /= Void do screen.blit_surface_part (a_surface, an_x, an_y, a_width, a_height, dest_x, dest_y) end initialize_images is -- Initialize all images. do -- create top animation create sprite.make_from_animation (create {EM_ANIMATION}.make_from_file ("light.anim"), 100, 100) sprite.start sprite.set_do_loop (True) -- create bottom animation (traffic light) create traffic_light.make_light traffic_light.start traffic_light.set_do_loop (True) font1 := standard_ttf_fonts.bitstream_vera_sans (16) font2 := standard_ttf_fonts.bitstream_vera_sans (14) ensure sprite_not_void: sprite /= Void end feature {NONE} -- Implementation x: INTEGER -- X position y: INTEGER -- Y position Delta: INTEGER is 5 -- Delta indicates how far the surface should -- be moved upon an arrow key press border: INTEGER is -- The width of the clipping border do Result := 2 * Delta end both_borders: INTEGER is -- The width of both clipping borders do Result := 2 * border end sprite: EM_SPRITE -- Animation traffic_light: TRAFFIC_LIGHT -- a composition of sprites font1, font2: EM_TTF_FONT -- fonts for example texts end