note
	description: "An image source used to create a GAME_SURFACE or a GAME_TEXTURE"
	author: "Louis Marchand"
	date: "Thu, 02 Apr 2015 02:40:10 +0000"
	revision: "2.0"

class 
	GAME_IMAGE

inherit
	GAME_RESSOURCE
		rename
			has_error as has_ressource_error
		end

	DISPOSABLE
		undefine
			default_create
		end

	GAME_LIBRARY_SHARED
		undefine
			default_create
		end

	GAME_SDL_ANY
		undefine
			default_create
		end

create {GAME_SDL_ANY}
	share_from_pointer,
	own_from_pointer,
	make_from_pointer


create 
	make_from_other

feature {NONE} -- Initialisation

	share_from_pointer (a_internal_pointer: POINTER)
			-- Initialization for Current from an already open a_internal_pointer image.
			-- The image in memory is not copied.
			-- Note that a_internal_pointer will not be free by Current
		require
			image_source_video_is_enable: Game_library.is_video_enable
		do
			item := a_internal_pointer
			must_free := False
		end

	own_from_pointer (a_internal_pointer: POINTER)
			-- Initialization for Current from an already open a_internal_pointer image.
			-- The image in memory is not copied.
			-- Note that a_internal_pointer will be free by Current
		require
			image_source_video_is_enable: Game_library.is_video_enable
		do
			item := a_internal_pointer
			must_free := True
		end

	make_from_pointer (a_internal_pointer: POINTER)
			-- Initialization for Current by copying the internal memory value pointed by a_internal_pointer.
			-- Note that a_internal_pointer will not be free by Current.
		require
			image_source_video_is_enable: Game_library.is_video_enable
			image_source_make_from_pointer_not_null: not a_internal_pointer.is_default_pointer
		local
			l_format_pointer, l_image_pointer: POINTER
		do
			l_format_pointer := {GAME_SDL_EXTERNAL}.get_sdl_surface_struct_format (a_internal_pointer)
			clear_error
			l_image_pointer := {GAME_SDL_EXTERNAL}.sdl_convertsurface (a_internal_pointer, l_format_pointer, 0)
			manage_error_pointer (l_image_pointer, "An error occured while copying the image source.")
			own_from_pointer (l_image_pointer)
		end

	make_from_other (a_other: GAME_IMAGE)
			-- Initialization for Current by copying the memory value of a_other.
		require
			image_source_video_is_enable: Game_library.is_video_enable
		do
			make_from_pointer (a_other.item)
		end
	
feature -- Access

	is_openable: BOOLEAN
			-- Can Current be open
		do
			Result := exists
		end

	open
			-- Open Current
		do
			is_open := True
		ensure then
			exists: not has_error implies exists
			is_open: not has_error implies is_open
		end

	exists: BOOLEAN
			-- Is item valid memory pointer
		do
			Result := not item.is_default_pointer
		end
	
feature {GAME_SDL_ANY} 

	item: POINTER
			-- Internal pointer of Current
	
feature {NONE} -- Implementation

	must_free: BOOLEAN
			-- Is it the responsability of Current to free item

	dispose
			-- Action to be executed just before garbage collection
			-- reclaims an object.
			-- Effect it in descendants to perform specific dispose
			-- actions. Those actions should only take care of freeing
			-- external resources; they should not perform remote calls
			-- on other objects since these may also be dead and reclaimed.
		do
			if must_free and exists then
				{GAME_SDL_EXTERNAL}.sdl_freesurface (item)
				is_open := False
				item := create {POINTER}
			end
		end
	
invariant
	image_source_valid: is_open implies exists

end -- class GAME_IMAGE

Generated by ISE EiffelStudio