-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.nim
233 lines (219 loc) · 6.85 KB
/
main.nim
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
# ****************************************************************************************
#
# raylib game template
#
# <Game title>
# <Game description>
#
# This game has been created using raylib (www.raylib.com)
# raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
#
# Copyright (c) 2021 Ramon Santamaria (@raysan5)
#
# ****************************************************************************************
import
raylib, src/screens, src/screen_logo, src/screen_title, src/screen_options,
src/screen_gameplay, src/screen_ending, src/platformutils
# ----------------------------------------------------------------------------------------
# Local Variables Definition (local to this module)
# ----------------------------------------------------------------------------------------
const
screenWidth = 800
screenHeight = 450
# Required variables to manage screen transitions (fade-in, fade-out)
var
transAlpha: float32 = 0
onTransition: bool = false
transFadeOut: bool = false
transFromScreen: GameScreen = Unknown
transToScreen: GameScreen = Unknown
# ----------------------------------------------------------------------------------------
# Module specific Functions Definition
# ----------------------------------------------------------------------------------------
proc changeToScreen(screen: GameScreen) =
# Change to next screen, no transition
# Unload current screen
case currentScreen
of Logo:
unloadLogoScreen()
of Title:
unloadTitleScreen()
of Gameplay:
unloadGameplayScreen()
of Ending:
unloadEndingScreen()
else:
discard
# Init next screen
case screen
of Logo:
initLogoScreen()
of Title:
initTitleScreen()
of Gameplay:
initGameplayScreen()
of Ending:
initEndingScreen()
else:
discard
currentScreen = screen
proc transitionToScreen(screen: GameScreen) =
# Request transition to next screen
onTransition = true
transFadeOut = false
transFromScreen = currentScreen
transToScreen = screen
transAlpha = 0
proc updateTransition =
# Update transition effect (fade-in, fade-out)
if not transFadeOut: # Transition fade out logic
transAlpha += 0.05
# NOTE: Due to float internal representation, condition jumps on 1.0f instead of 1.05f
# For that reason we compare against 1.01f, to avoid last frame loading stop
if transAlpha > 1.01'f32:
transAlpha = 1
# Unload current screen
case transFromScreen
of Logo:
unloadLogoScreen()
of Title:
unloadTitleScreen()
of Gameplay:
unloadGameplayScreen()
of Ending:
unloadEndingScreen()
else:
discard
# Init next screen
case transToScreen
of Logo:
initLogoScreen()
of Title:
initTitleScreen()
of Gameplay:
initGameplayScreen()
of Ending:
initEndingScreen()
else:
discard
currentScreen = transToScreen
# Activate fade out effect to next loaded screen
transFadeOut = true
else:
transAlpha -= 0.02
if transAlpha < -0.01'f32:
transAlpha = 0
transFadeOut = false
onTransition = false
transFromScreen = Unknown
transToScreen = Unknown
proc drawTransition =
# Draw transition effect (full-screen rectangle)
drawRectangle(0, 0, getScreenWidth(), getScreenHeight(), fade(Black, transAlpha))
proc updateDrawFrame {.cdecl.} =
# Update and draw game frame
# Update
# --------------------------------------------------------------------------------------
updateMusicStream(music)
# NOTE: Music keeps playing between screens
if not onTransition:
case currentScreen
of Logo:
updateLogoScreen()
if finishLogoScreen() == 1:
transitionToScreen(Title)
of Title:
updateTitleScreen()
if finishTitleScreen() == 1:
transitionToScreen(Options)
elif finishTitleScreen() == 2:
transitionToScreen(Gameplay)
of Options:
updateOptionsScreen()
if finishOptionsScreen() == 1:
transitionToScreen(Title)
of Gameplay:
updateGameplayScreen()
if finishGameplayScreen() == 1:
transitionToScreen(Ending)
of Ending:
updateEndingScreen()
if finishEndingScreen() == 1:
transitionToScreen(Title)
else:
discard
else:
updateTransition()
# Update transition (fade-in, fade-out)
# --------------------------------------------------------------------------------------
# Draw
# --------------------------------------------------------------------------------------
beginDrawing()
clearBackground(RayWhite)
case currentScreen
of Logo:
drawLogoScreen()
of Title:
drawTitleScreen()
of Options:
drawOptionsScreen()
of Gameplay:
drawGameplayScreen()
of Ending:
drawEndingScreen()
else:
discard
# Draw full screen rectangle in front of everything
if onTransition:
drawTransition()
endDrawing()
# --------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------
# Main entry point
# ----------------------------------------------------------------------------------------
proc main =
# Initialization
# --------------------------------------------------------------------------------------
initWindow(screenWidth, screenHeight, "raylib game template")
initAudioDevice() # Initialize audio device
try:
# Load global data (assets that must be available in all screens, i.e. font)
font = loadFont(getResourcePath("resources/mecha.png"))
music = loadMusicStream(getResourcePath("resources/ambient.ogg"))
fxCoin = loadSound(getResourcePath("resources/coin.wav"))
setMusicVolume(music, 1)
playMusicStream(music)
# Setup and init first screen
currentScreen = Logo
initLogoScreen()
when defined(emscripten):
emscriptenSetMainLoop(updateDrawFrame, 60, 1)
else:
setTargetFPS(60) # Set our game to run at 60 frames-per-second
# ----------------------------------------------------------------------------------
# Main game loop
while not windowShouldClose(): # Detect window close button or ESC key
updateDrawFrame()
# De-Initialization
# ------------------------------------------------------------------------------------
# Unload current screen data before closing
case currentScreen
of Logo:
unloadLogoScreen()
of Title:
unloadTitleScreen()
of Gameplay:
unloadGameplayScreen()
of Ending:
unloadEndingScreen()
else:
discard
# Unload global data loaded
reset(font)
reset(music)
reset(fxCoin)
finally:
closeAudioDevice() # Close audio context
closeWindow() # Close window and OpenGL context
# --------------------------------------------------------------------------------------
main()