-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.nelua
125 lines (97 loc) · 3.88 KB
/
camera.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
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
--[[
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
]]
--[[
In this demo, is presented a basic camera control
Actually, there is no "camera" on Nene, instead, there is a
`Vec2` offset done in all rendering calls, this offset is the
`render_offset` field.
You can modify this field in order to add a basic camera control.
]]
local Nene = require 'nene.core'
local Color = require 'nene.color'
local Rect = require 'nene.math.rect'
--[[
In this example it's showcased how to split your game logic into multiple functions.
It's pretty simple, since Nene it's a singleton*, you can get the instance of nene
just by calling `Nene.instance()`, note however that Nene must be initialized before
using this function.
*Singleton pattern: https://en.wikipedia.org/wiki/Singleton_pattern
]]
local function draw_parallax_grid()
-- let's draw a grid with parallax effect!
-- get the running nene instance
local nene = Nene.instance()
-- save our original render_offset value
local original_offset = nene.render_offset
-- multiply render_offset by half, this is what makes the parallax effect here.
-- you can read more about parallax on Pedro's tutorial:
-- https://www.patreon.com/posts/parallax-and-7863658
nene.render_offset = nene.render_offset * 0.5
-- draw the grid
for i = -64, 64 do
Nene.render_draw_line({16 * i, -2048}, {16 * i, 2048}, Color.black, true)
Nene.render_draw_line({-2048, 16 * i}, {2048, 16 * i}, Color.black, true)
end
-- at the end of the function, we set the render_offset to the original value
nene.render_offset = original_offset
end
local function draw_parallax_shadow()
-- same logic as draw_parallax_grid
local nene = Nene.instance()
local original_offset = nene.render_offset
nene.render_offset = nene.render_offset * 0.5
-- we can also use defer, so this statement is done at the end of the function
-- see more here: https://nelua.io/overview/#defer
defer nene.render_offset = original_offset end
-- draw shadow
Nene.render_draw_rect({ {-8, 8}, {16, 16} }, false, {0, 0, 0, 100}, true)
end
-- all the life-cycle of this example is contained in this function
local function camera()
--[[
Initialize and test nene initialization and defer it's termination (https://nelua.io/overview/#defer).
When nene is initialized, you get an ok status which should be asserted;
the ok status is `true` on initialization success.
]]
local ok = Nene.init('nene basic camera', 256, 256)
assert(ok, 'error: nene initialization failed')
defer Nene.terminate() end
--[[
As already descibried, If you need access to the instance, you can get it using `Nene.instance`,
the instance it's never reallocated, thus you don't need to worry about getting an invalidated
pointer.
]]
local nene = Nene.instance()
-- let's define a rect
local rect_on_center: Rect = { pos = { -16, 16 }, size = { 32, 32 } }
repeat
-- update nene state, always do this on the beg
Nene.update()
-- modify `render_offset` field in order to get a basic camera control
if Nene.is_scancode_held(SDL_SCANCODE_RIGHT) then
nene.render_offset.x = nene.render_offset.x + 1
elseif Nene.is_scancode_held(SDL_SCANCODE_LEFT) then
nene.render_offset.x = nene.render_offset.x - 1
end
if Nene.is_scancode_held(SDL_SCANCODE_UP) then
nene.render_offset.y = nene.render_offset.y - 1
elseif Nene.is_scancode_held(SDL_SCANCODE_DOWN) then
nene.render_offset.y = nene.render_offset.y + 1
end
-- rendering logic --
Nene.render_clear(Color.bg)
do
draw_parallax_grid()
draw_parallax_shadow()
-- draw the rect, note that rect_on_center is never modified
Nene.render_draw_rect(rect_on_center, false, Color.white, true)
end
Nene.render_present()
until Nene.should_quit()
end
-- run example
camera()