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
This commit is contained in:
abdelrahman committed 2022-09-23 04:39:17 +01:00
1 parent c4f1775392
commit 3f71cf0f3f
11 files changed
+156 -33

No files matched your search

+49 -5
View File
@@ -1,13 +1,19 @@
#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>
@@ -19,10 +25,11 @@ INTERNAL struct game {
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,
bool full_screen) {
SDL_Color window_background_color, bool full_screen) {
bool initialisation_complete = true;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
@@ -33,8 +40,12 @@ bool init_game(const char *game_name, int window_width, int window_height,
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,
full_screen)) {
window_background_color, full_screen)) {
#ifdef DEBUG_ENGINE
printf("Failed to initialise window\n");
#endif /* DEBUG_ENGINE */
@@ -48,6 +59,12 @@ bool init_game(const char *game_name, int window_width, int window_height,
initialisation_complete = false;
}
Game.asset_manager = create_asset_manager();
if (!(Game.asset_manager)) {
initialisation_complete = false;
}
if (!initialisation_complete) {
close_game();
@@ -55,7 +72,8 @@ bool init_game(const char *game_name, int window_width, int window_height,
}
Game.running = true;
Game.delta = 0.0f;
// Game.delta = 0.0f;
Game.delta = 1.0f / 60.0f;
#ifdef DEBUG_ENGINE
printf("Game initialised successfully\n");
@@ -84,6 +102,27 @@ void remove_object(ObjectID id) {
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;
@@ -117,16 +156,19 @@ void render() {
return;
}
fill_window_with_color(&(Game.window), (SDL_Color){255, 255, 255, 255});
clear_window(&(Game.window));
if (Game.object_store) {
render_all_objects(Game.object_store, Game.window.renderer);
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;
@@ -136,6 +178,8 @@ void close_game() {
Game.running = false;
IMG_Quit();
SDL_Quit();
#ifdef DEBUG_ENGINE