note
	description: "[
		A GAME_WINDOW that directly use GAME_SURFACE to render.
		Note that GAME_WINDOW_SURFACED don't use hardware acceleration and is very slow.
		It should be use for slow application only. To use hardware acceleration,
		use the GAME_WINDOW_RENDERED type.
	]"
	author: "Louis Marchand"
	date: "Thu, 02 Apr 2015 02:40:10 +0000"
	revision: "2.0"

class 
	GAME_WINDOW_SURFACED

inherit
	GAME_WINDOW

	GAME_DRAWING_TOOLS
		undefine
			default_create
		end

create 
	make

feature -- Access

	surface: GAME_SURFACE
			-- The surface that represent the drawable area of Current. After drawing on this
			-- surface, you have to use update, update_rectangle or update_rectangles to
			-- actually show the modification in Current
		local
			l_surface_pointer: POINTER
			l_source: GAME_IMAGE
			l_surface: GAME_SURFACE
		do
			if attached internal_surface as la_surface and then (width = internal_surface_width and height = internal_surface_height) then
				Result := la_surface
			else
				l_surface_pointer := {GAME_SDL_EXTERNAL}.sdl_getwindowsurface (item)
				create l_source.share_from_pointer (l_surface_pointer)
				if l_source.is_openable then
					l_source.open
					if l_source.is_open then
						create l_surface.share_from_image (l_source)
						internal_surface := l_surface
						internal_surface_height := height
						internal_surface_width := width
						Result := l_surface
					else
						Io.Error.put_string ("An error occured while creating the surfaced window.%N")
						has_error := True
						create Result.make_for_window (Current, 0, 0)
					end
				else
					Io.Error.put_string ("An error occured while creating the surfaced window.%N")
					has_error := True
					create Result.make_for_window (Current, 0, 0)
				end
				check
						not has_error
				end
			end
		end

	update
			-- Print the visual buffer modification to the screen
		local
			l_error: INTEGER_32
		do
			clear_error
			l_error := {GAME_SDL_EXTERNAL}.sdl_updatewindowsurface (item)
			manage_error_code (l_error, "An error occured while updating the window surface.")
		end

	update_rectangle (a_x, a_y, a_width, a_height: INTEGER_32)
			-- Only show the modification in the rectangle starting at (a_x,a_y)
			-- of dimension a_widthxa_height.
		do
			update_rectangles (create {ARRAYED_LIST [TUPLE [x: INTEGER_32; y: INTEGER_32; width: INTEGER_32; height: INTEGER_32]]}.make_from_array (<<[a_x, a_y, a_width, a_height]>>))
		end

	update_rectangles (a_rectangles: CHAIN [TUPLE [x: INTEGER_32; y: INTEGER_32; width: INTEGER_32; height: INTEGER_32]])
			-- Only show the modification in the rectangles starting at each (x,y)
			-- of dimension a_widthxa_height in the a_rectangles sequence.
		require
			renderer_exists: exists
		local
			l_array_rectangles, l_rectangle: POINTER
			l_rectangle_size, l_error: INTEGER_32
			l_normalized_rectangle: TUPLE [x: INTEGER_32; y: INTEGER_32; width: INTEGER_32; height: INTEGER_32]
		do
			l_rectangle_size := {GAME_SDL_EXTERNAL}.c_sizeof_sdl_rect
			l_array_rectangles := l_array_rectangles.memory_alloc (l_rectangle_size * a_rectangles.count)
			l_rectangle := l_array_rectangles
			across
				a_rectangles as la_rectangles
			loop
				l_normalized_rectangle := normalize_rectangle (la_rectangles.item.x, la_rectangles.item.y, la_rectangles.item.width, la_rectangles.item.height)
				{GAME_SDL_EXTERNAL}.set_rect_struct_x (l_rectangle, l_normalized_rectangle.x)
				{GAME_SDL_EXTERNAL}.set_rect_struct_y (l_rectangle, l_normalized_rectangle.y)
				{GAME_SDL_EXTERNAL}.set_rect_struct_w (l_rectangle, l_normalized_rectangle.width)
				{GAME_SDL_EXTERNAL}.set_rect_struct_h (l_rectangle, l_normalized_rectangle.height)
				l_rectangle := l_rectangle.plus (l_rectangle_size)
			end
			clear_error
			l_error := {GAME_SDL_EXTERNAL}.sdl_updatewindowsurfacerects (item, l_array_rectangles, a_rectangles.count)
			manage_error_code (l_error, "An error occured while updating the screen.");
			l_array_rectangles.memory_free
		end
	
feature {NONE} -- Implementation

	internal_surface: detachable GAME_SURFACE
			-- The internal value of lazy evaluated surface attribute

	internal_surface_width: INTEGER_32
			-- The dimension of Current when internal_surface was created

	internal_surface_height: INTEGER_32
			-- The dimension of Current when internal_surface was created
	
end -- class GAME_WINDOW_SURFACED

Generated by ISE EiffelStudio