-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatformer_part2.nelua
112 lines (98 loc) · 3.13 KB
/
platformer_part2.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
require"sdl2"
require"utils"
require"os"
require"arg"
require"string"
Debug = true -- for dprint()
local Input = @enum{none = 0,left = 1, right = 2, jump = 3 , restart = 4, quit = 5}
local Game = @record{
renderer: *SDL_Renderer,
inputs : [6]boolean,
}
local WindowSize = Vec2f{1280, 720}
-----------
-- toInput
-----------
local toInput = function (key:integer)
local write = function(str:string)
dwrite(str)
end
write("\n")
if key == SDL_SCANCODE_A or key == SDL_SCANCODE_H or key == SDL_SCANCODE_LEFT then write("LEFT: ") return Input.left
elseif key == SDL_SCANCODE_D or key == SDL_SCANCODE_L or key == SDL_SCANCODE_RIGHT then write("RIGHT: ") return Input.right
elseif key == SDL_SCANCODE_UP or key == SDL_SCANCODE_SPACE or key == SDL_SCANCODE_J or key == SDL_SCANCODE_K then write("JUMP: ") return Input.jump
elseif key == SDL_SCANCODE_R then write("Rstart: ") return Input.restart
elseif key == SDL_SCANCODE_Q then write("Quit: ") return Input.quit
else write("None: ") return Input.none
end
end
------------
--- newGame -- Game type
------------
local newGame = function(renderer: *SDL_Renderer): Game
return Game{
renderer = renderer,
inputs = {false,false,false, false,false,false},
}
end
--------------------
-- Game:handleInput
--------------------
function Game:handleInput()
local event: SDL_Event
while SDL_PollEvent(event) ~= 0 do
local kind = event.type
if kind == SDL_QUIT then
self.inputs[Input.quit] = true
elseif kind == SDL_KEYDOWN then
dprint("KeyDown")
self.inputs[toInput(event.key.keysym.scancode)] = true
elseif kind == SDL_KEYUP then
dprint("KeyUp")
self.inputs[toInput(event.key.keysym.scancode)] = false
end
end
end
----------------
--- Game:render
----------------
function Game:render()
SDL_RenderClear(self.renderer)
SDL_RenderPresent(self.renderer)
end
------------------
--- Main function
------------------
local main = function()
if sdlFailIf(0 == SDL_Init(SDL_INIT_VIDEO + SDL_INIT_TIMER + SDL_INIT_EVENTS),
"SDL2 initialization failed") then os.exit(1) end
if sdlFailIf(SDL_TRUE == SDL_SetHint("SDL_RENDER_SCALE_QUALITY", "2"),
"Linear texture filtering could not be enabled") then os.exit(1) end
local window = SDL_CreateWindow(string.format("%s: [ %s ]","Our own 2D platformer written in NeLua",getExeName(arg[0])),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WindowSize.x, WindowSize.y, SDL_WINDOW_SHOWN)
if sdlFailIf(0 ~= window,"Window could not be created") then os.exit(1) end
local renderer = SDL_CreateRenderer(window,-1,
SDL_RENDERER_ACCELERATED or SDL_RENDERER_PRESENTVSYNC)
if sdlFailIf(0 ~= renderer,"Renderer could not be created") then os.exit(1) end
SDL_SetRenderDrawColor(renderer,110,132,174,255)
local game = newGame(renderer)
--------------
--- Main loop
--------------
-- Game loop, draws each frame
while not game.inputs[Input.quit] do
game:handleInput()
game:render()
end
--------------
--- End procs
--------------
SDL_DestroyRenderer(renderer)
SDL_DestroyWindow(window)
SDL_Quit()
end
---------
--- main
---------
main()