Implemented the core functionality of the ObjectStore and started

implementing the functionality for the AssetManager
This commit is contained in:
abdelrahman committed 2022-09-22 01:56:21 +01:00
1 parent 79f28100bd
commit c4f1775392
14 files changed
+330 -383

No files matched your search

+144
View File
@@ -0,0 +1,144 @@
#include "GameEngine.h"
#include "ObjectStore.h"
#include "SDL.h"
#include "SDL_error.h"
#include "SDL_events.h"
#include "SDL_pixels.h"
#include "Window.h"
#include "constants.h"
#include "engine_build_config.h"
#include <stdbool.h>
#ifdef DEBUG_ENGINE
#include <stdio.h>
#endif /* DEBUG_ENGINE */
INTERNAL struct game {
bool running;
float delta;
Window window;
SDL_Event event;
ObjectStore *object_store;
} Game;
bool init_game(const char *game_name, int window_width, int window_height,
bool full_screen) {
bool initialisation_complete = true;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
#ifdef DEBUG_ENGINE
printf("Failed to initialise SDL. Error: %s\n", SDL_GetError());
#endif /* DEBUG_ENGINE */
return false;
}
if (!init_window(&(Game.window), game_name, window_width, window_height,
full_screen)) {
#ifdef DEBUG_ENGINE
printf("Failed to initialise window\n");
#endif /* DEBUG_ENGINE */
initialisation_complete = false;
}
Game.object_store = create_object_store();
if (!(Game.object_store)) {
initialisation_complete = false;
}
if (!initialisation_complete) {
close_game();
return false;
}
Game.running = true;
Game.delta = 0.0f;
#ifdef DEBUG_ENGINE
printf("Game initialised successfully\n");
#endif /* DEBUG_ENGINE */
return true;
}
bool game_running() { return Game.running; }
ObjectID add_object(void *object, EventHandlerPtr event_handler,
UpdateFuncPtr update_func, RenderFuncPtr render_func) {
if (!(Game.running) || !(Game.object_store)) {
return -1;
}
return add_object_to_store(Game.object_store, object, event_handler,
update_func, render_func);
}
void remove_object(ObjectID id) {
if (!(Game.running) || !(Game.object_store)) {
return;
}
remove_object_from_store(Game.object_store, id);
}
void handle_events() {
if (!(Game.running)) {
return;
}
while (SDL_PollEvent(&(Game.event))) {
switch (Game.event.type) {
case SDL_QUIT:
Game.running = false;
break;
}
if (!Game.object_store) {
break;
}
handle_all_objects_events(Game.object_store, &(Game.event));
}
}
void update() {
if (!(Game.running) || !(Game.object_store)) {
return;
}
update_all_objects(Game.object_store, Game.delta);
}
void render() {
if (!(Game.running)) {
return;
}
fill_window_with_color(&(Game.window), (SDL_Color){255, 255, 255, 255});
if (Game.object_store) {
render_all_objects(Game.object_store, Game.window.renderer);
}
swap_render_buffers(&(Game.window));
}
void close_game() {
destroy_object_store(Game.object_store);
Game.object_store = NULL;
destroy_window(&(Game.window));
Game.delta = 0.0f;
Game.running = false;
SDL_Quit();
#ifdef DEBUG_ENGINE
printf("Game closed successfully\n");
#endif /* DEBUG_ENGINE */
}