From 3a8be291483d136887522fbcef36e271c450bd77 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Mon, 23 May 2022 13:24:16 -0400 Subject: [PATCH] break out GameOrbit and GameState to game.h --- Makefile | 2 +- src/game.cpp | 35 ++++++++++++++++++++++++++++++++ src/game.h | 34 +++++++++++++++++++++++++++++++ src/main.cpp | 56 +--------------------------------------------------- 4 files changed, 71 insertions(+), 56 deletions(-) create mode 100644 src/game.cpp create mode 100644 src/game.h diff --git a/Makefile b/Makefile index 0b83a01..5068b25 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ IMGUI_SOURCES := $(filter-out $(IMGUI_DIR)/imgui_demo.cpp, $(wildcard $(IMGUI_DI IMGUI_OBJECTS := $(patsubst $(IMGUI_DIR)/%.cpp, $(OBJDIR)/%.o, $(IMGUI_SOURCES)) IMGUI_IMP_OBJECTS += $(patsubst $(IMGUI_IMPL_DIR)/%.cpp, $(OBJDIR)/%.o, $(IMGUI_IMPL_SOURCES)) -SOURCES := $(SRCDIR)/main.cpp $(SRCDIR)/gooey.cpp +SOURCES := $(SRCDIR)/main.cpp $(SRCDIR)/gooey.cpp $(SRCDIR)/game.cpp OBJECTS := $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(SOURCES)) BIN := $(BINDIR)/orbital_shipping diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..a336995 --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,35 @@ + +#include "game.h" + + +GameOrbit* +getFreeOrbit(GameState* gs) +{ + GameOrbit* orbit = nullptr; + + // NOTE: first check if we have a freed orbit to use + for (u32 i = 0; i < gs->num_orbits; i++) { + if (!gs->orbits[i].in_use) + orbit = &gs->orbits[i]; + } + + // NOTE: if we can't re-use a freed orbit, initialize a new one + if (!orbit) { + assert(gs->num_orbits < gs->max_orbits); + orbit = &gs->orbits[gs->num_orbits++]; + } + + orbit->in_use = true; + return orbit; +} + +void +disableGameOrbit(GameOrbit* orbit) +{ + orbit->in_use = false; + orbit->selected = false; + orbit->system = {0}; + orbit->grav_body = nullptr; + orbit->ellipse_entity = nullptr; + orbit->satellite_entity = nullptr; +} diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..7f972ed --- /dev/null +++ b/src/game.h @@ -0,0 +1,34 @@ +#pragma once + +#include "tangerine.h" +#include "orbits.h" + + +struct GameOrbit +{ + TwoBodySystem system; + + Entity* grav_body; + Entity* ellipse_entity; + Entity* satellite_entity; + bool in_use; + bool selected; +}; + +struct GameState +{ + bool running; + MemoryArena* arena; + + u64 game_time_ms; + u64 sim_time_ms; + float sim_speed; + + GameOrbit* orbits; + u32 num_orbits; + u32 max_orbits; +}; + +GameOrbit* getFreeOrbit(GameState* gs); + +void disableGameOrbit(GameOrbit* orbit); diff --git a/src/main.cpp b/src/main.cpp index 905c2ab..bf1fe6c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,6 +22,7 @@ #include "input.h" #include "tangerine.h" +#include "game.h" #include "gooey.h" #include "orbits.h" @@ -32,61 +33,6 @@ const vec4 g_light_color = vec4(0.5, 0.5, 0.5, 1); const uvec4 g_light_intensities = uvec4(1, 0, 0, 0); -struct GameOrbit -{ - TwoBodySystem system; - - Entity* grav_body; - Entity* ellipse_entity; - Entity* satellite_entity; - bool in_use; -}; - -struct GameState -{ - bool running; - MemoryArena* arena; - - u64 game_time_ms; - u64 sim_time_ms; - float sim_speed; - - GameOrbit* orbits; - u32 num_orbits; - u32 max_orbits; -}; - -GameOrbit* -getFreeOrbit(GameState* gs) -{ - GameOrbit* orbit = nullptr; - - // NOTE: first check if we have a freed orbit to use - for (u32 i = 0; i < gs->num_orbits; i++) { - if (!gs->orbits[i].in_use) - orbit = &gs->orbits[i]; - } - - // NOTE: if we can't re-use a freed orbit, initialize a new one - if (!orbit) { - assert(gs->num_orbits < gs->max_orbits); - orbit = &gs->orbits[gs->num_orbits++]; - } - - orbit->in_use = true; - return orbit; -} - -void -disableGameOrbit(GameOrbit* orbit) -{ - orbit->in_use = false; - orbit->system = {0}; - orbit->grav_body = nullptr; - orbit->ellipse_entity = nullptr; - orbit->satellite_entity = nullptr; -} - void initCamera(RenderState* rs, vec3 cam_pos, vec3 cam_focus, vec3 cam_up) {