indexing description: "[ Image moving example ]" date: "$Date$" revision: "$Revision$" class LOVELY inherit EM_SHARED_BITMAP_FACTORY EM_SHARED_SUBSYSTEMS create make feature {NONE} -- Initialization make is -- Create the main application. do setup_video_subsystem if not Video_subsystem.is_enabled or cow_image = Void or mountain_image = Void then io.error.put_string ("Exiting application due to error.%N") else setup_event_loop event_loop.dispatch Video_subsystem.disable end end setup_video_subsystem is -- Setup the video subsystem. do Video_subsystem.set_video_surface_width (800) Video_subsystem.set_video_surface_height (600) Video_subsystem.enable if not Video_subsystem.is_enabled then io.error.put_string ("Error: could not initialize video subsystem.%N") else Bitmap_factory.create_bitmap_from_image (cow_image_filename) cow_image := bitmap_factory.last_bitmap if cow_image = Void then io.error.put_string ("Error: could not load image '" + cow_image_filename + "'.%N") else bitmap_factory.create_bitmap_from_image (mountain_image_filename) mountain_image := bitmap_factory.last_bitmap if mountain_image = Void then io.error.put_string ("Error: could not load image '" + mountain_image_filename + "'.%N") else cow_x := 300 cow_y := 350 end end end end setup_event_loop is -- Setup event loop. do create event_loop.make_poll event_loop.update_event.subscribe (agent draw_scene) event_loop.quit_event.subscribe (agent quit) event_loop.key_down_event.subscribe (agent process_key_event) ensure event_loop_not_void: event_loop /= Void end feature -- Event handling process_key_event (an_event: EM_KEYBOARD_EVENT) is -- Move cow depending on keyboard event. require an_event_not_void: an_event /= Void do if an_event.key = an_event.sdlk_up then cow_y := cow_y - 10 elseif an_event.key = an_event.sdlk_down then cow_y := cow_y + 10 elseif an_event.key = an_event.sdlk_left then cow_x := cow_x - 10 elseif an_event.key = an_event.sdlk_right then cow_x := cow_x + 10 end end quit (an_event: EM_QUIT_EVENT) is -- Quit application. require an_event_not_void: an_event /= Void do event_loop.stop end draw_scene is -- Draws the image on the root video surface. require video_subsystem_enabled: Video_subsystem.is_enabled mountian_image_not_void: mountain_image /= Void cow_imager_not_void: cow_image /= Void do Video_subsystem.video_surface.blit_surface (mountain_image, 0, 0) Video_subsystem.video_surface.blit_surface (cow_image, cow_x, cow_y) Video_subsystem.video_surface.redraw end feature -- Images cow_image: EM_BITMAP -- Image of a cow mountain_image: EM_BITMAP -- Image of a mountaint cow_image_filename: STRING is "lovely.gif" -- Filename of cow image mountain_image_filename: STRING is "matterhorn.jpg" -- Filename of mountain image feature {NONE} -- Implentation cow_x: INTEGER -- X position of cow cow_y: INTEGER -- Y position of cow event_loop: EM_EVENT_LOOP -- Event loop end