note
	description: "[
		v_video.c
		Gamma correction LUT stuff.
		Functions to draw patches (by post) directly to screen.
		Functions to blit a block to the screen
	]"
	license: "[
				Copyright (C) 1993-1996 by id Software, Inc.
				Copyright (C) 2005-2014 Simon Howard
				Copyright (C) 2021 Ilgiz Mustafin
		
				This program is free software; you can redistribute it and/or modify
				it under the terms of the GNU General Public License as published by
				the Free Software Foundation; either version 2 of the License, or
				(at your option) any later version.
		
				This program is distributed in the hope that it will be useful,
				but WITHOUT ANY WARRANTY; without even the implied warranty of
				MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
				GNU General Public License for more details.
		
				You should have received a copy of the GNU General Public License along
				with this program; if not, write to the Free Software Foundation, Inc.,
				51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
	]"

class 
	V_VIDEO

inherit
	DOOMDEF_H

create 
	make

feature 

	i_main: I_MAIN

	make (a_i_main: like i_main)
		do
			i_main := a_i_main
			create dest_screen.make (0)
		end

	v_init
		do
		end

	Gammatable: ARRAY [ARRAY [NATURAL_8]]
		once
			create Result.make_filled (create {ARRAY [NATURAL_8]}.make_empty, 0, 4)
			Result [0] := ARRAY [NATURAL_8] <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255>>
			Result [1] := ARRAY [NATURAL_8] <<2, 4, 5, 7, 8, 10, 11, 12, 14, 15, 16, 18, 19, 20, 21, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 36, 37, 38, 39, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 214, 215, 216, 217, 218, 219, 220, 221, 222, 222, 223, 224, 225, 226, 227, 228, 229, 230, 230, 231, 232, 233, 234, 235, 236, 237, 237, 238, 239, 240, 241, 242, 243, 244, 245, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255>>
			Result [2] := ARRAY [NATURAL_8] <<4, 7, 9, 11, 13, 15, 17, 19, 21, 22, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 40, 42, 43, 45, 46, 47, 48, 50, 51, 52, 54, 55, 56, 57, 59, 60, 61, 62, 63, 65, 66, 67, 68, 69, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 153, 154, 155, 156, 157, 158, 159, 160, 160, 161, 162, 163, 164, 165, 166, 166, 167, 168, 169, 170, 171, 172, 172, 173, 174, 175, 176, 177, 178, 178, 179, 180, 181, 182, 183, 183, 184, 185, 186, 187, 188, 188, 189, 190, 191, 192, 193, 193, 194, 195, 196, 197, 197, 198, 199, 200, 201, 201, 202, 203, 204, 205, 206, 206, 207, 208, 209, 210, 210, 211, 212, 213, 213, 214, 215, 216, 217, 217, 218, 219, 220, 221, 221, 222, 223, 224, 224, 225, 226, 227, 228, 228, 229, 230, 231, 231, 232, 233, 234, 235, 235, 236, 237, 238, 238, 239, 240, 241, 241, 242, 243, 244, 244, 245, 246, 247, 247, 248, 249, 250, 251, 251, 252, 253, 254, 254, 255>>
			Result [3] := ARRAY [NATURAL_8] <<8, 12, 16, 19, 22, 24, 27, 29, 31, 34, 36, 38, 40, 41, 43, 45, 47, 49, 50, 52, 53, 55, 57, 58, 60, 61, 63, 64, 65, 67, 68, 70, 71, 72, 74, 75, 76, 77, 79, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 135, 136, 137, 138, 139, 140, 141, 142, 143, 143, 144, 145, 146, 147, 148, 149, 150, 150, 151, 152, 153, 154, 155, 155, 156, 157, 158, 159, 160, 160, 161, 162, 163, 164, 165, 165, 166, 167, 168, 169, 169, 170, 171, 172, 173, 173, 174, 175, 176, 176, 177, 178, 179, 180, 180, 181, 182, 183, 183, 184, 185, 186, 186, 187, 188, 189, 189, 190, 191, 192, 192, 193, 194, 195, 195, 196, 197, 197, 198, 199, 200, 200, 201, 202, 202, 203, 204, 205, 205, 206, 207, 207, 208, 209, 210, 210, 211, 212, 212, 213, 214, 214, 215, 216, 216, 217, 218, 219, 219, 220, 221, 221, 222, 223, 223, 224, 225, 225, 226, 227, 227, 228, 229, 229, 230, 231, 231, 232, 233, 233, 234, 235, 235, 236, 237, 237, 238, 238, 239, 240, 240, 241, 242, 242, 243, 244, 244, 245, 246, 246, 247, 247, 248, 249, 249, 250, 251, 251, 252, 253, 253, 254, 254, 255>>
			Result [4] := ARRAY [NATURAL_8] <<16, 23, 28, 32, 36, 39, 42, 45, 48, 50, 53, 55, 57, 60, 62, 64, 66, 68, 69, 71, 73, 75, 76, 78, 80, 81, 83, 84, 86, 87, 89, 90, 92, 93, 94, 96, 97, 98, 100, 101, 102, 103, 105, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 143, 144, 145, 146, 147, 148, 149, 150, 150, 151, 152, 153, 154, 155, 155, 156, 157, 158, 159, 159, 160, 161, 162, 163, 163, 164, 165, 166, 166, 167, 168, 169, 169, 170, 171, 172, 172, 173, 174, 175, 175, 176, 177, 177, 178, 179, 180, 180, 181, 182, 182, 183, 184, 184, 185, 186, 187, 187, 188, 189, 189, 190, 191, 191, 192, 193, 193, 194, 195, 195, 196, 196, 197, 198, 198, 199, 200, 200, 201, 202, 202, 203, 203, 204, 205, 205, 206, 207, 207, 208, 208, 209, 210, 210, 211, 211, 212, 213, 213, 214, 214, 215, 216, 216, 217, 217, 218, 219, 219, 220, 220, 221, 221, 222, 223, 223, 224, 224, 225, 225, 226, 227, 227, 228, 228, 229, 229, 230, 230, 231, 232, 232, 233, 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 239, 239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245, 245, 246, 246, 247, 247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252, 253, 254, 254, 255, 255>>
		end
	
feature 

	usegamma: INTEGER_32 assign set_usegamma

	set_usegamma (a_usegamma: like usegamma)
		do
			usegamma := a_usegamma
		end
	
feature 

	dirtybox: ARRAY [FIXED_T]
		attribute
			create Result.make_filled (create {FIXED_T}.from_integer (0), 0, 3)
		end
	
feature 

	v_drawpatchdirect (x, y: INTEGER_32; patch: PATCH_T)
			-- Draws directly to the screen on the pc.
		do
			v_drawpatch (x, y, patch)
		end

	v_drawpatch (a_x, a_y: INTEGER_32; patch: PATCH_T)
			-- Masks a column based masked pic to the screen.
		local
			x, y: INTEGER_32
			count: INTEGER_32
			col: INTEGER_32
			column: COLUMN_T
			desttop: INTEGER_32
			dest: INTEGER_32
			source: ARRAY [NATURAL_8]
			w: INTEGER_32
			cols: ARRAYED_LIST [COLUMN_T]
			i: INTEGER_32
		do
			{NOT_IMPLEMENTED}.not_implemented ("V_DrawPatch", False)
			y := a_y - patch.topoffset.to_integer_32
			x := a_x - patch.leftoffset.to_integer_32
			v_markrect (x, y, patch.width.to_integer_32, patch.height.to_integer_32)
			desttop := y * Screenwidth + x
			w := patch.width.to_integer_32
			cols := patch.columns
			from
				col := 1
			until
				col > w
			loop
				column := cols [col]
				across
					column.posts as post
				loop
					source := post.item.body
					dest := desttop + post.item.topdelta.to_integer_32 * Screenwidth
					from
						count := post.item.length.to_integer_32
						i := source.lower
					until
						count <= 0
					loop
						count := count - 1
						dest_screen.put (source [i], dest)
						i := i + 1
						dest := dest + Screenwidth
					end
				end
				x := x + 1
				col := col + 1
				desttop := desttop + 1
			end
		end

	v_drawblock (x, y, width, a_height: INTEGER_32; a_src: PIXEL_T_BUFFER)
			-- Draw a linear block of pixels into the view buffer.
		require
			rangecheck: x >= 0 and x + width <= {DOOMDEF_H}.screenwidth and y >= 0 and y + a_height <= {DOOMDEF_H}.screenheight
		local
			dest: POINTER
			height: INTEGER_32
			src: POINTER
		do
			v_markrect (x, y, width, height)
			dest := dest_screen.p.item + (y * {DOOMDEF_H}.screenwidth + x)
			src := a_src.p.item
			from
				height := a_height - 1
			until
				height = 0
			loop
				dest.memory_copy (src, width * {PIXEL_T_BUFFER}.pixel_t_size)
				src := src + width * {PIXEL_T_BUFFER}.pixel_t_size
				dest := dest + ({DOOMDEF_H}.screenwidth * {PIXEL_T_BUFFER}.pixel_t_size)
				height := height - 1
			end
		end

	v_markrect (x, y, width, height: INTEGER_32)
		do
			{M_BBOX}.m_addtobox (dirtybox, create {FIXED_T}.from_integer (x), create {FIXED_T}.from_integer (y))
			{M_BBOX}.m_addtobox (dirtybox, create {FIXED_T}.from_integer (x + width - 1), create {FIXED_T}.from_integer (y + height - 1))
		end
	
feature 

	dest_screen: PIXEL_T_BUFFER

	v_usebuffer (buffer: PIXEL_T_BUFFER)
		do
			dest_screen := buffer
		end

	v_restorebuffer
		do
			dest_screen := i_main.I_video.i_videobuffer
		end
	
feature 

	v_copyrect (srcx, srcy: INTEGER_32; source: PIXEL_T_BUFFER; width, a_height, destx, desty: INTEGER_32)
		require
			rangecheck: srcx >= 0 and srcx + width <= Screenwidth and srcy >= 0 and srcy + a_height <= Screenheight
			rangecheck: destx >= 0 and destx + width <= Screenwidth and desty >= 0 and desty + a_height <= Screenheight
		local
			src: PIXEL_T_BUFFER
			dest: PIXEL_T_BUFFER
			height: INTEGER_32
		do
			height := a_height
			v_markrect (destx, desty, width, height)
			src := source + Screenwidth * srcy + srcx
			dest := dest_screen + Screenwidth * desty + destx
			from
			until
				height <= 0
			loop
				dest.copy_from_count (src, width)
				height := height - 1
				if height > 0 then
					src := src + Screenwidth
					dest := dest + Screenwidth
				end
			end
		end
	
end -- class V_VIDEO

Generated by ISE EiffelStudio