added a dedicated reset function to avoid memleaks

This commit is contained in:
Return0ne 2022-04-30 12:06:03 -04:00
parent de1a3fb593
commit d6ecb4aff0
1 changed files with 53 additions and 21 deletions

View File

@ -4,8 +4,6 @@
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#endif #endif
// screen variables // screen variables
static const int screenWidth = 800; static const int screenWidth = 800;
static const int screenHeight = 450; static const int screenHeight = 450;
@ -44,11 +42,12 @@ static int BallFrameCounter;
static int selected = 0; static int selected = 0;
// Game functions // Game functions
static void GameInit(void); static void gameSetup(void);
static void UpdateGame(void); static void updateGame(void);
static void DrawGame(void); static void drawGame(void);
static void UpdateDrawFrame(void); static void gameReset(void);
static void UnloadGame(void); static void gameLoop(void);
static void unloadGame(void);
int main(void) int main(void)
{ {
@ -56,25 +55,26 @@ int main(void)
InitAudioDevice(); InitAudioDevice();
GameInit(); gameSetup();
#if defined(PLATFORM_WEB) #if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1); emscripten_set_main_loop(gameLoop, 0, 1);
#else #else
SetTargetFPS(60); SetTargetFPS(60);
while (!WindowShouldClose()) UpdateDrawFrame(); while (!WindowShouldClose()) gameLoop();
#endif #endif
UnloadGame(); unloadGame();
CloseWindow(); CloseWindow();
return 0; return 0;
} }
void GameInit(void) void gameSetup(void)
{ {
// asset loading & setting of variable values
currentScreen = TITLE; currentScreen = TITLE;
fxbounce = LoadSound("assets/sfx/boing.wav"); fxbounce = LoadSound("assets/sfx/boing.wav");
@ -111,8 +111,9 @@ void GameInit(void)
BallFrameCounter = 0; BallFrameCounter = 0;
} }
void UpdateGame(void) void updateGame(void)
{ {
// code that runs as long as the program is running
if ((IsKeyDown(KEY_LEFT_ALT)) && (IsKeyPressed(KEY_F))) ToggleFullscreen(); if ((IsKeyDown(KEY_LEFT_ALT)) && (IsKeyPressed(KEY_F))) ToggleFullscreen();
switch(currentScreen) { switch(currentScreen) {
@ -150,7 +151,7 @@ void UpdateGame(void)
else if (player.hitbox.y <= 0) player.hitbox.y = 0; else if (player.hitbox.y <= 0) player.hitbox.y = 0;
if (IsKeyPressed(KEY_D)) ball.active = !ball.active; if (IsKeyPressed(KEY_D)) ball.active = !ball.active;
if (IsKeyPressed(KEY_R)) { GameInit(); currentScreen = TITLE; } if (IsKeyPressed(KEY_R)) { gameReset(); currentScreen = TITLE; }
if (ball.active) { if (ball.active) {
BallFrameCounter++; BallFrameCounter++;
// moveiement oof the balls // moveiement oof the balls
@ -186,7 +187,7 @@ void UpdateGame(void)
break; break;
case GAMEOVER: case GAMEOVER:
if (IsKeyPressed(KEY_ENTER)) { if (IsKeyPressed(KEY_ENTER)) {
GameInit(); gameReset();
currentScreen = GAMEPLAY; currentScreen = GAMEPLAY;
} }
break; break;
@ -196,8 +197,9 @@ void UpdateGame(void)
} }
} }
void DrawGame(void) void drawGame(void)
{ {
// code to render the game to the game window
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
@ -257,14 +259,44 @@ void DrawGame(void)
EndDrawing(); EndDrawing();
} }
void gameReset(void)
void UpdateDrawFrame(void)
{ {
UpdateGame(); // code to reset all variables without reloading assets
DrawGame(); currentScreen = TITLE;
player.currentframe = 0;
player.hp = 30;
player.frameRec = (Rectangle) {
0.0f,
0.0f,
(float) player.sprite.width/2,
(float) player.sprite.height
};
player.hitbox = (Rectangle) {
GetScreenWidth()/2.0f - 30,
GetScreenHeight()/2.0f - 30,
70,
70
};
ball.position = (Vector2){ 50, 50 };
ball.radius = 20;
ball.growth = 2;
ball.speed = (Vector2){ 400.0f, 400.0f };
ball.color = MAROON;
ball.active = true;
pauseTimer = 0;
BallFrameCounter = 0;
} }
void UnloadGame(void) void gameLoop(void)
{
updateGame();
drawGame();
}
void unloadGame(void)
{ {
UnloadSound(fxbounce); UnloadSound(fxbounce);
UnloadTexture(player.sprite); UnloadTexture(player.sprite);