-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite_animation.nelua
97 lines (75 loc) · 2.98 KB
/
sprite_animation.nelua
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
--[[
Copyright (c) 2021-present André Luiz Alvares
Nene is licensed under the Zlib license.
Please refer to the LICENSE file for details
SPDX-License-Identifier: Zlib
]]
-- This example presents how to use the texture atlas module and using some more advanced texture drawing
local math = require 'math'
local Nene = require 'nene.core'
local Color = require 'nene.color'
local Texture = require 'nene.texture'
local TextureAtlas = require 'nene.texture_atlas'
local Animation = require 'nene.animation'
local function game()
-- initialize nene and test if got succesfully initialized
local ok = Nene.init('nene sprite animation', (64*4), (64*4))
assert(ok, 'error: nene initialization failed')
local nene = Nene.instance()
-- rotation parameter, it'll be increased over time
local rotation = 0.0
local anim: Animation = { interval = 500 }
local anim2: Animation = { interval = 500 }
print('eq test:', anim == anim2)
--[[
Create a texture atlas with img_shapes.png image on it,
then test initialization and defers it's destruction to the end of scope
using a to-be-closed variable.
note that img_shapes images contains four shapes, on a grid with 32x32 size per cell.
More details can be read on the texture_atlas example.
]]
local ok, spritesheet = TextureAtlas.load('../../resources/img_shapes.png', 2, { cell_size = {32, 32} })
assert(ok, "error: 'img_shapes.png' could not be loaded")
defer
spritesheet:destroy()
end
--[[
Let's create a timer, this will store the time just after nene initialization.
this variable will only be modified when the sprite changes.
]]
local prev_time = nene.current_time
--[[
The sprite index which we want to render on the current frame, this
refers to the sub-sprite of the spritesheet, the first one is on top-left,
the last is on bottom-right, the flow is left-to-right, up-to-down.
]]
local sprite_index = 0
-- with everything setup, now we run our game loop until the game will quit
repeat
-- updates the nene internal state
Nene.update()
-- increase rotation over time
rotation = nene.current_time * 0.01
--[[
If the difference between the last time we changed the sprite and now
is greater than one second, then we update prev_time and change
the sprite index used on spritesheet rendering.
]]
if nene.current_time - prev_time > 1000 then
prev_time = nene.current_time
sprite_index = (sprite_index + 1) % 4
end
-- let's draw our game, first we need to clear the backbuffer.
Nene.render_clear(Color.bg)
--[[
Then, draw the sprite from the spritesheet at {10, 10} position, at screen space,
using the spritesheet texture atlas, using the sprite index.
]]
spritesheet:draw_nth_sub_texture_ex(sprite_index, { 10, 10 }, false, rotation, spritesheet:get_sub_texture_center())
-- finally, swap buffers and present our final result
Nene.render_present()
until Nene.should_quit()
Nene.terminate()
end
-- run the game
game()