indexing description: "[ Animation example. ]" date: "$Date$" revision: "$Revision$" class ANIMATION inherit EM_APPLICATION EM_KEY_CONSTANTS EM_SHARED_STANDARD_FONTS create make feature {NONE} -- Initialization make is -- Create the main application. do -- Initialize `video_subsystem' and `screen'. video_subsystem.set_video_surface_width (width) video_subsystem.set_video_surface_height (height) video_subsystem.set_video_bpp (resolution) initialize_screen -- Set global options set_window_icon ("icon.png") set_window_title ("EiffelMedia Animation Example") set_application_id ("animation") -- Initialize and draw whole scene. x := 200 y := 250 initialize_images draw_scene -- Event loop. create event_loop.make_poll event_loop.key_down_event.subscribe (agent handle_key_down_event (?)) event_loop.quit_event.subscribe (agent handle_quit_event (?)) event_loop.update_event.subscribe (agent handle_update_event) event_loop.dispatch video_subsystem.disable 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 -- Event handling handle_key_down_event (a_keyboard_event: EM_KEYBOARD_EVENT) is -- Handle keyboard events. require a_keyboard_event_not_void: a_keyboard_event /= Void 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. require a_quit_event_not_void: a_quit_event /= Void 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 {NONE} -- Implementation x: INTEGER -- X position y: INTEGER -- Y position event_loop: EM_EVENT_LOOP -- Event loop width: INTEGER is 800 -- Width of the video surface height: INTEGER is 600 -- Height of video surface resolution: INTEGER is 24 -- Resolution of the video surface 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