aboutsummaryrefslogtreecommitdiff
path: root/samples/post-processing/main.lua
blob: 491e34e350a27863514d1e859ce287aac03f284e (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
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
io.stdout:setvbuf("no")
music = nil
local tex = nil
local timer = nil
local tb = {x = 1, y = 2}
local spr = nil 
local bitmap = nil
local sprs = {}
local animator = nil
local spr = nil
local ps = nil
local mesh = nil
local function createAnimation(path, count, r, c, w, h, loop, speed)
	local tex = jin.graphics.newTexture(path)
	local ssheet = jin.graphics.newSpriteSheet(tex)
	local sprs = ssheet:newSprites(count, r, c, w, h, 96, 168)
	return jin.graphics.newAnimation(sprs, loop, speed)
end
local particle_sprites = {}
local Pi = 3.1415
local sinShader = nil
local screen = nil
local pp = nil
local pixel = nil 
local rgbsplit = nil
local sobel = nil
local bloom = nil
local noise = nil
local radial = nil
local glow = nil

local panel_shader = nil

function jin.core.onLoad()
    jin.log.info("=============== Start Game ===============")
    
	screen = jin.graphics.newCanvas(jin.graphics.getSize())
	-- bitmap = jin.graphics.newBitmap(200, 200, function(w, h, x, y)
		-- return {255*math.sin(x/w),255 - 255,255*math.cos(y/w),255}
	-- end)
	local tex = jin.graphics.newTexture("splash.png")
	mesh = jin.graphics.newMesh() 
	mesh:setGraphic(tex)
	mesh:pushVertex(-20, -20, 0, 0, {255, 0, 0, 255})
	mesh:pushVertex(20, -20, 0.8, 0, {255, 0, 255, 255})
	mesh:pushVertex(20, 20, 0.8, 0.8, {255, 255, 0, 255})
	mesh:pushVertex(-20, 120, 0, 0.8, {255, 255, 255, 255})
	ps = jin.graphics.newParticleSystem()
	for i = 0, 10 do 
		local t = jin.graphics.newTexture("dust/s_dust_A_" .. i .. ".png")
		local spr = jin.graphics.newSprite(t, jin.graphics.SpriteOrigin.MIDDLECENTER)
		ps:addParticleSprite(spr)
	end
	ps:setEmitRate(0.02)
    ps:setEmitForce(80, 120)
    ps:setEmitPosition({0, 0})
    ps:setEmitDirection(-Pi / 10 - Pi / 2, Pi / 10 - Pi / 2)
    ps:setParticleLinearAccelaration({0, 10})
    ps:setParticleSpritesMode(jin.graphics.SpriteMode.ANIMATED)
	ps:setParticleColor({255, 30, 0, 255})
    ps:addParticleTransparencyPoint(1, 0)
    ps:addParticleTransparencyPoint(0, 0.5)
    ps:enableParticleBlendAdditive(true)
    ps:setParticleLife(0.5, 2)
    ps:addParticleScalePoint(3, 0)
    ps:addParticleScalePoint(0, 1)
	ps:setPosition(250, 200)
	local animation = createAnimation("anim2.png", 27, 3, 10, 200, 200, true, 50)
	animator = jin.graphics.newAnimator(animation)
	spr = animation:getFrame(1)
	local tex3 = jin.graphics.newTexture("anim2.png")
	tex3 = nil
    -- music = jin.audio.newSource("forest.ogg")
    -- music:setVolume(0.5)
    -- music:setLoop(true)
    -- music:play()
    timer = jin.time.newTimer()
    timer:every(3, function(p)
        --timer:cancel(h)
		if animator:getSpeed() == 50 then 
			animator:setSpeed(100)
		elseif animator:getSpeed() == 100 then 
			animator:setSpeed(50)
		end
	end, animator)
	jin.graphics.pushMatrix()
	jin.graphics.translate(0, 0)
	--jin.graphics.rotate(0.2)
    
    local fsm = jin.ai.newStateMachine({
      events = {
        { name = 'startup', from = 'none',  to = 'green' },
        { name = 'panic',   from = 'green', to = 'red'   },
        { name = 'calm',    from = 'red',   to = 'green' },
    }})
    jin.log.info(fsm.current) -- "none"
    fsm:startup()
    jin.log.info(fsm.current) -- "green"
    jin.log.info(jin.utils.json.encode({ 1, 2, 3, { x = 10 } }))
    jin.log.info(jin.utils.json.decode('[1,2,3,{"x":10}]'))
    sinShader = jin.graphics.newShader[[
        #VERTEX_SHADER
		Vertex vert(Vertex v)
		{
			return v;
		}
		#END_VERTEX_SHADER
		#FRAGMENT_SHADER
		Color frag(Color col, Texture tex, Vertex v)
		{
            col.r = sin(jin_Time.x);
            col.g = cos(jin_Time.x); 
            col.b = sin(jin_Time.x);
            return col;
		}
		#END_FRAGMENT_SHADER
    ]]
	panel_shader = jin.graphics.newShader[[
        #VERTEX_SHADER
        Vertex vert(Vertex v)
        {
            return v;
        }
        #END_VERTEX_SHADER
        #FRAGMENT_SHADER
        Color frag(Color col, Texture tex, Vertex v)
        {
            col.a *= sin(jin_Time.x);
            return col;
        }
        #END_FRAGMENT_SHADER
    ]]
	pp = jin.graphics.newShaderf("./shaders/curling.jsl")
	
	pixel = jin.graphics.newShaderf("./shaders/pixel.jsl")
    
    rgbsplit = jin.graphics.newShaderf("./shaders/rgb-split.jsl")
    
    sobel = jin.graphics.newShaderf("./shaders/sobel.jsl")
    
    noise = jin.graphics.newShaderf("./shaders/noise.jsl")

    radial = jin.graphics.newShaderf("./shaders/radial.jsl")
    
    jin.log.info("test")
    
    timer:every(1, function() 
        jin.log.info(jin.time.getFPS() .. " fps")
        local stats = jin.graphics.getStats()
        jin.log.info("textures " .. stats.textures)
    end)
    
    --local xmlParser = jin.utils.xml.newParser()
    local xml = jin.utils.xml.parseXmlText[[
        <test one="two">
        <three four="five" four="six"/>
        <three>eight</three>
        <nine ten="eleven">twelve</nine>
        </test>
    ]]
    jin.log.info(xml.test["@one"])
    
    jin.graphics.showWindow()
end

function jin.core.onEvent(e)
    if e.type == "Quit" then 
        jin.core.stop()
    end
	if e.type == "MouseButtonDown" then 
		if e.button == "Left" then 
			ps:setPosition(e.x, e.y)
		end 
	end 
end

local t = 0
function jin.core.onUpdate(dt)
    tb.x = t
    t = t + jin.time.getDelta()
	animator:update(jin.time.getDelta())
	timer:update(jin.time.getDelta())
	ps:update(dt)
	local mx, my = jin.mouse.getPosition()
	--ps:setPosition(mx, my)
    --ps:setEmitDirection(t - 0.3, t + 0.3)
end

function jin.core.onDraw()
	jin.graphics.setClearColor(0, 0, 0, 0xff)
	jin.graphics.clear()
    jin.graphics.setColor(255, 255, 255, 255)

	jin.graphics.print("before canvas", 10, 300)
    jin.graphics.draw(spr, 100, 300, 1, 1, 0)

	jin.graphics.bindCanvas(screen)
	jin.graphics.setClearColor(0, 0, 0, 0)
	jin.graphics.clear()
    jin.graphics.useShader(jin.graphics.Shaders.Shape)
    jin.graphics.setColor(255, 0, 255, 100)
    jin.graphics.rect(jin.graphics.RenderMode.FILL, 30, 50, 100, 200)
    jin.graphics.setColor(255, 255, 255, 255)
    jin.graphics.unuseShader()
    --jin.graphics.draw(sprs[2], 150, 150, 1, 1, 0)
	local x, y = jin.mouse.getPosition()
	animator:render(350, 150, 1, 1, 0)
    jin.graphics.useShader(jin.graphics.Shaders.Font)
	jin.graphics.print("* Particle system test\n* Animation test.", 10, 10)
    jin.graphics.unuseShader()
    jin.graphics.draw(spr, 100, 200, 1, 1, 0)
	jin.graphics.useShader(jin.graphics.Shaders.Mesh)
	jin.graphics.draw(mesh, 200, 100)
	jin.graphics.unuseShader();
    ps:render()
    jin.graphics.useShader(sinShader)
    jin.graphics.rect(jin.graphics.RenderMode.FILL, 300, 300, 100, 50)
    jin.graphics.unuseShader()
    
    jin.graphics.setColor(100, 100, 100, 255)
    jin.graphics.useShader(panel_shader)
    jin.graphics.rect(jin.graphics.RenderMode.FILL, 440, 0, 140, 150)
    jin.graphics.unuseShader()
    jin.graphics.setColor(255, 255, 255, 255)
    jin.graphics.useShader(jin.graphics.Shaders.Font)
    jin.graphics.unuseShader()
    jin.graphics.unbindCanvas(screen)
     
	jin.graphics.useShader(rgbsplit)
    local mode = jin.graphics.getBlendMode()
	jin.graphics.setBlendMode(jin.graphics.BlendMode.PREMULTIPLIEDALPHA)
    jin.graphics.draw(screen, 0, 0)
    jin.graphics.setBlendMode(mode)
	jin.graphics.unuseShader()
	jin.graphics.print("after canvas", 10, 340)
end

function jin.core.onQuit() 
    jin.log.info("=============== Quit Game ===============")
end