Files
punchie-engine/src/GameEngine.c
T
abdelrahman 3f71cf0f3f Connected the AssetManager to the GameEngine and added function in the
GameEngine API to allow for loading and removing assets as well as rendering
textures using their AssetID
2022-09-23 04:39:17 +01:00

189 lines
3.9 KiB
C

#include "GameEngine.h"
#include "AssetManager.h"
#include "ObjectStore.h"
#include "Renderer.h"
#include "SDL.h"
#include "SDL_error.h"
#include "SDL_events.h"
#include "SDL_image.h"
#include "SDL_pixels.h"
#include "SDL_render.h"
#include "Window.h"
#include "constants.h"
#include "engine_build_config.h"
#include "reusable_ids.h"
#include <stdbool.h>
#include <stdlib.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;
AssetManager *asset_manager;
} Game;
bool init_game(const char *game_name, int window_width, int window_height,
SDL_Color window_background_color, 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 (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) {
initialisation_complete = false;
}
if (!init_window(&(Game.window), game_name, window_width, window_height,
window_background_color, 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;
}
Game.asset_manager = create_asset_manager();
if (!(Game.asset_manager)) {
initialisation_complete = false;
}
if (!initialisation_complete) {
close_game();
return false;
}
Game.running = true;
// Game.delta = 0.0f;
Game.delta = 1.0f / 60.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);
}
AssetID load_asset(const char *path, AssetType type) {
return type == ASSET_TYPE_IMAGE
? load_asset_to_manager(Game.asset_manager, path, type,
Game.window.renderer)
: load_asset_to_manager(Game.asset_manager, path, type, NULL);
}
void render_texture_asset(AssetID id, float x, float y, float w, float h) {
if (id < 0) {
return;
}
SDL_Texture *texture = get_texture(Game.asset_manager, id);
render_texture(Game.window.renderer, texture, x, y, w, h);
}
void close_asset(AssetID id) {
remove_asset_from_manager(Game.asset_manager, 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;
}
clear_window(&(Game.window));
if (Game.object_store) {
render_all_objects(Game.object_store);
}
swap_render_buffers(&(Game.window));
}
void close_game() {
destroy_asset_manager(Game.asset_manager);
Game.asset_manager = NULL;
destroy_object_store(Game.object_store);
Game.object_store = NULL;
destroy_window(&(Game.window));
Game.delta = 0.0f;
Game.running = false;
IMG_Quit();
SDL_Quit();
#ifdef DEBUG_ENGINE
printf("Game closed successfully\n");
#endif /* DEBUG_ENGINE */
}