summaryrefslogtreecommitdiff
path: root/examples/blend_modes/main.lua
blob: 75e90d20b2ecf780e711c1ed94bae24fd8cd7d82 (plain)
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
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
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"

Vector2 = require( "vector2" )
Rect = require( "rectangle" )

local monitor = 0
local winSize = Vector2:new( 1920, 1080 )

local catTex = nil
local tileTex = nil
local lightTex = nil
local lightTexSize = Vector2:new()

local buttonSize = Vector2:new( 20, 20 )
local fontSize = 20
local textTint = RL.BLACK

local guiRect = Rect:new( 0, 0, 550, 210 )
local lightPos = Vector2:new()
local lightPos2 = Vector2:new()

local framebuffer = nil

local blendMode = {
	mode = 1,
	options = {
		{ name = "BLEND_ALPHA", value = RL.BLEND_ALPHA },
		{ name = "BLEND_ADDITIVE", value = RL.BLEND_ADDITIVE },
		{ name = "BLEND_MULTIPLIED", value = RL.BLEND_MULTIPLIED },
		{ name = "BLEND_ADD_COLORS", value = RL.BLEND_ADD_COLORS },
		{ name = "BLEND_SUBTRACT_COLORS", value = RL.BLEND_SUBTRACT_COLORS },
		{ name = "BLEND_ALPHA_PREMULTIPLY", value = RL.BLEND_ALPHA_PREMULTIPLY },
		{ name = "BLEND_CUSTOM", value = RL.BLEND_CUSTOM },
		{ name = "BLEND_CUSTOM_SEPARATE", value = RL.BLEND_CUSTOM_SEPARATE },
	}
}
local blendFactor = {
	srcRGB = 1,
	dstRGB = 1,
	srcAlpha = 1,
	dstAlpha = 1,
	options = {
		{ name = "RL_ZERO", value = RL.RL_ZERO },
		{ name = "RL_ONE", value = RL.RL_ONE },
		{ name = "RL_SRC_COLOR", value = RL.RL_SRC_COLOR },
		{ name = "RL_ONE_MINUS_SRC_COLOR", value = RL.RL_ONE_MINUS_SRC_COLOR },
		{ name = "RL_SRC_ALPHA", value = RL.RL_SRC_ALPHA },
		{ name = "RL_ONE_MINUS_SRC_ALPHA", value = RL.RL_ONE_MINUS_SRC_ALPHA },
		{ name = "RL_DST_ALPHA", value = RL.RL_DST_ALPHA },
		{ name = "RL_ONE_MINUS_DST_ALPHA", value = RL.RL_ONE_MINUS_DST_ALPHA },
		{ name = "RL_DST_COLOR", value = RL.RL_DST_COLOR },
		{ name = "RL_ONE_MINUS_DST_COLOR", value = RL.RL_ONE_MINUS_DST_COLOR },
		{ name = "RL_SRC_ALPHA_SATURATE", value = RL.RL_SRC_ALPHA_SATURATE },
		{ name = "RL_CONSTANT_COLOR", value = RL.RL_CONSTANT_COLOR },
		{ name = "RL_ONE_MINUS_CONSTANT_COLOR", value = RL.RL_ONE_MINUS_CONSTANT_COLOR },
		{ name = "RL_CONSTANT_ALPHA", value = RL.RL_CONSTANT_ALPHA },
		{ name = "RL_ONE_MINUS_CONSTANT_ALPHA", value = RL.RL_ONE_MINUS_CONSTANT_ALPHA },
	}
}
local blendFunction = {
	eqRGB = 1,
	eqAlpha = 1,
	options = {
		{ name = "RL_FUNC_ADD", value = RL.RL_FUNC_ADD },
		{ name = "RL_MIN", value = RL.RL_MIN },
		{ name = "RL_MAX", value = RL.RL_MAX },
		{ name = "RL_FUNC_SUBTRACT", value = RL.RL_FUNC_SUBTRACT },
		{ name = "RL_FUNC_REVERSE_SUBTRACT", value = RL.RL_FUNC_REVERSE_SUBTRACT },
		{ name = "RL_BLEND_EQUATION", value = RL.RL_BLEND_EQUATION },
		{ name = "RL_BLEND_EQUATION_RGB", value = RL.RL_BLEND_EQUATION_RGB },
		{ name = "RL_BLEND_EQUATION_ALPHA", value = RL.RL_BLEND_EQUATION_ALPHA },
		{ name = "RL_BLEND_DST_RGB", value = RL.RL_BLEND_DST_RGB },
		{ name = "RL_BLEND_SRC_RGB", value = RL.RL_BLEND_SRC_RGB },
		{ name = "RL_BLEND_DST_ALPHA", value = RL.RL_BLEND_DST_ALPHA },
		{ name = "RL_BLEND_SRC_ALPHA", value = RL.RL_BLEND_SRC_ALPHA },
		{ name = "RL_BLEND_COLOR", value = RL.RL_BLEND_COLOR },
	}
}

function RL.init()
	RL.SetWindowTitle( "Blend modes" )
	RL.SetWindowState( RL.FLAG_VSYNC_HINT )
	RL.SetWindowSize( winSize )

	local monitorPos = Vector2:newT( RL.GetMonitorPosition( monitor ) )
	local monitorSize = Vector2:newT( RL.GetMonitorSize( monitor ) )
	RL.SetWindowPosition( { monitorPos.x + monitorSize.x / 2 - winSize.x / 2, monitorPos.y + monitorSize.y / 2 - winSize.y / 2 } )

	local path = RL.GetBasePath().."../resources/images/"

	catTex = RL.LoadTexture( path.."cat.png" )
	RL.SetTextureWrap( catTex, RL.TEXTURE_WRAP_REPEAT )

	tileTex = RL.LoadTexture( path.."tile.png" )
	RL.SetTextureWrap( tileTex, RL.TEXTURE_WRAP_REPEAT )

	lightTex = RL.LoadTexture( path.."light.png" )
	lightTexSize:setT( RL.GetTextureSize( lightTex ) )

	framebuffer = RL.LoadRenderTexture( winSize )

	lightPos:setV( winSize:scale( 0.5 ) - lightTexSize:scale( 0.5 ) )
	lightPos2:setV( winSize:scale( 0.5 ) - lightTexSize:scale( 0.5 ) )
end

function RL.update( delta )
	local mousePos = Vector2:newT( RL.GetMousePosition() )

	if not RL.CheckCollisionPointRec( mousePos, guiRect ) then
		if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_LEFT ) then
			lightPos:setV( mousePos - lightTexSize:scale( 0.5 ) )
		end
		if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_RIGHT ) then
			lightPos2:setV( mousePos - lightTexSize:scale( 0.5 ) )
		end
	end
end

local function drawControl( pos, t, name )
	if RL.GuiButton( { pos.x, pos.y, buttonSize.x, buttonSize.y }, RL.GuiIconText( RL.ICON_ARROW_LEFT, "" ) ) == 1 then
		t[ name ] = 1 < t[ name ] and t[ name ] - 1 or #t.options
	end
	pos.x = pos.x + buttonSize.x + 2

	if RL.GuiButton( { pos.x, pos.y, buttonSize.x, buttonSize.y }, RL.GuiIconText( RL.ICON_ARROW_RIGHT, "" ) ) == 1 then
		t[ name ] = t[ name ] < #t.options and t[ name ] + 1 or 1
	end
	pos.x = pos.x + buttonSize.x + 2

	RL.DrawText( name..": "..t.options[ t[ name ] ].name, pos, fontSize, textTint )

	pos.x = 2
	pos.y = pos.y + 2 + buttonSize.y
end

local function drawControls()
	local pos = Vector2:new( 2 )

	-- Blend mode.
	drawControl( pos, blendMode, "mode" )
	-- Blend RPG.
	pos.y = pos.y + 8
	drawControl( pos, blendFactor, "srcRGB" )
	drawControl( pos, blendFactor, "dstRGB" )
	drawControl( pos, blendFunction, "eqRGB" )
	-- Blend Alpha.
	pos.y = pos.y + 8
	drawControl( pos, blendFactor, "srcAlpha" )
	drawControl( pos, blendFactor, "dstAlpha" )
	drawControl( pos, blendFunction, "eqAlpha" )
	
	pos.y = pos.y + 8
	RL.DrawText( "Set texture positions with mouse right and left.", pos, 20, RL.BLACK )
end

function RL.draw()
	RL.ClearBackground( RL.BLACK )

	if framebuffer then
		RL.BeginTextureMode( framebuffer )
			RL.ClearBackground( RL.BLACK )
			RL.DrawTexturePro( catTex,
				{ 0, 0, winSize.x, winSize.y },
				{ 0, 0, winSize.x, winSize.y },
				{ 0, 0 },
				0.0,
				RL.WHITE
			)
			RL.rlSetBlendFactors(
				blendFactor.options[ blendFactor.srcRGB ].value,
				blendFactor.options[ blendFactor.dstRGB ].value,
				blendFunction.options[ blendFunction.eqRGB ].value
			)
			RL.rlSetBlendFactorsSeparate(
				blendFactor.options[ blendFactor.srcRGB ].value,
				blendFactor.options[ blendFactor.dstRGB ].value,
				blendFactor.options[ blendFactor.srcAlpha ].value,
				blendFactor.options[ blendFactor.dstAlpha ].value,
				blendFunction.options[ blendFunction.eqRGB ].value,
				blendFunction.options[ blendFunction.eqAlpha ].value
			)
			RL.BeginBlendMode( blendMode.options[ blendMode.mode ].value )
				RL.DrawTexture( lightTex, lightPos, RL.RED )
				RL.DrawTexture( lightTex, lightPos2, RL.BLUE )
			RL.EndBlendMode()
		RL.EndTextureMode()
	end

	RL.DrawTexturePro( tileTex,
		{ 0, 0, winSize.x, winSize.y },
		{ 0, 0, winSize.x, winSize.y },
		{ 0, 0 },
		0.0,
		RL.WHITE
	)
	RL.DrawTexturePro(
		RL.GetRenderTextureTexture( framebuffer ),
		{ 0, 0, winSize.x, -winSize.y },
		{ 0, 0, winSize.x, winSize.y },
		{ 0, 0 },
		0.0,
		RL.WHITE
	)
	RL.DrawRectangle( guiRect, { 255, 255, 255, 200 } )
	drawControls()
end