Enabled SDL_image's support for PNG and TIFF files and improved the
AssetManager with a hashing function, a linked list structure to store the hashes as well as a reference counter to support multiple users sharing the same asset.
This commit is contained in:
1 parent
3f71cf0f3f
commit
dfee36ffe9
12 files changed
+292
-60
No files matched your search
+63
-44
@@ -8,25 +8,29 @@
|
||||
#include "SDL_image.h"
|
||||
#include "SDL_pixels.h"
|
||||
#include "SDL_render.h"
|
||||
#include "SDL_timer.h"
|
||||
#include "Window.h"
|
||||
#include "constants.h"
|
||||
#include "engine_build_config.h"
|
||||
#include "reusable_ids.h"
|
||||
#include <SDL2/SDL_mixer.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef DEBUG_ENGINE
|
||||
#include <stdio.h>
|
||||
#endif /* DEBUG_ENGINE */
|
||||
|
||||
INTERNAL struct game {
|
||||
INTERNAL struct game_engine {
|
||||
bool running;
|
||||
float delta;
|
||||
uint64_t last_time;
|
||||
Window window;
|
||||
SDL_Event event;
|
||||
ObjectStore *object_store;
|
||||
AssetManager *asset_manager;
|
||||
} Game;
|
||||
} GameEngine;
|
||||
|
||||
bool init_game(const char *game_name, int window_width, int window_height,
|
||||
SDL_Color window_background_color, bool full_screen) {
|
||||
@@ -40,11 +44,15 @@ bool init_game(const char *game_name, int window_width, int window_height,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) {
|
||||
uint32_t img_flags = IMG_INIT_PNG | IMG_INIT_TIF;
|
||||
if ((IMG_Init(img_flags) & img_flags) != img_flags) {
|
||||
#ifdef DEBUG_ENGINE
|
||||
printf("Failed to initialise SDL. Error: %s\n", IMG_GetError());
|
||||
#endif /* DEBUG_ENGINE */
|
||||
initialisation_complete = false;
|
||||
}
|
||||
|
||||
if (!init_window(&(Game.window), game_name, window_width, window_height,
|
||||
if (!init_window(&(GameEngine.window), game_name, window_width, window_height,
|
||||
window_background_color, full_screen)) {
|
||||
#ifdef DEBUG_ENGINE
|
||||
printf("Failed to initialise window\n");
|
||||
@@ -53,15 +61,15 @@ bool init_game(const char *game_name, int window_width, int window_height,
|
||||
initialisation_complete = false;
|
||||
}
|
||||
|
||||
Game.object_store = create_object_store();
|
||||
GameEngine.object_store = create_object_store();
|
||||
|
||||
if (!(Game.object_store)) {
|
||||
if (!(GameEngine.object_store)) {
|
||||
initialisation_complete = false;
|
||||
}
|
||||
|
||||
Game.asset_manager = create_asset_manager();
|
||||
GameEngine.asset_manager = create_asset_manager();
|
||||
|
||||
if (!(Game.asset_manager)) {
|
||||
if (!(GameEngine.asset_manager)) {
|
||||
initialisation_complete = false;
|
||||
}
|
||||
|
||||
@@ -71,9 +79,9 @@ bool init_game(const char *game_name, int window_width, int window_height,
|
||||
return false;
|
||||
}
|
||||
|
||||
Game.running = true;
|
||||
// Game.delta = 0.0f;
|
||||
Game.delta = 1.0f / 60.0f;
|
||||
GameEngine.running = true;
|
||||
GameEngine.delta = 0.0f;
|
||||
GameEngine.last_time = SDL_GetTicks64();
|
||||
|
||||
#ifdef DEBUG_ENGINE
|
||||
printf("Game initialised successfully\n");
|
||||
@@ -82,31 +90,32 @@ bool init_game(const char *game_name, int window_width, int window_height,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool game_running() { return Game.running; }
|
||||
bool game_running() { return GameEngine.running; }
|
||||
|
||||
ObjectID add_object(void *object, EventHandlerPtr event_handler,
|
||||
UpdateFuncPtr update_func, RenderFuncPtr render_func) {
|
||||
if (!(Game.running) || !(Game.object_store)) {
|
||||
if (!(GameEngine.running) || !(GameEngine.object_store)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return add_object_to_store(Game.object_store, object, event_handler,
|
||||
return add_object_to_store(GameEngine.object_store, object, event_handler,
|
||||
update_func, render_func);
|
||||
}
|
||||
|
||||
void remove_object(ObjectID id) {
|
||||
if (!(Game.running) || !(Game.object_store)) {
|
||||
void remove_object(ObjectID *id) {
|
||||
if (!(GameEngine.running) || !(GameEngine.object_store)) {
|
||||
return;
|
||||
}
|
||||
|
||||
remove_object_from_store(Game.object_store, id);
|
||||
remove_object_from_store(GameEngine.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);
|
||||
? load_asset_to_manager(GameEngine.asset_manager, path, type,
|
||||
GameEngine.window.renderer)
|
||||
: load_asset_to_manager(GameEngine.asset_manager, path, type,
|
||||
NULL);
|
||||
}
|
||||
|
||||
void render_texture_asset(AssetID id, float x, float y, float w, float h) {
|
||||
@@ -114,69 +123,79 @@ void render_texture_asset(AssetID id, float x, float y, float w, float h) {
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_Texture *texture = get_texture(Game.asset_manager, id);
|
||||
SDL_Texture *texture = get_texture(GameEngine.asset_manager, id);
|
||||
|
||||
render_texture(Game.window.renderer, texture, x, y, w, h);
|
||||
render_texture(GameEngine.window.renderer, texture, x, y, w, h);
|
||||
}
|
||||
|
||||
void close_asset(AssetID id) {
|
||||
remove_asset_from_manager(Game.asset_manager, id);
|
||||
void close_asset(AssetID *id) {
|
||||
remove_asset_from_manager(GameEngine.asset_manager, id);
|
||||
}
|
||||
|
||||
void handle_events() {
|
||||
if (!(Game.running)) {
|
||||
if (!(GameEngine.running)) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (SDL_PollEvent(&(Game.event))) {
|
||||
switch (Game.event.type) {
|
||||
while (SDL_PollEvent(&(GameEngine.event))) {
|
||||
switch (GameEngine.event.type) {
|
||||
case SDL_QUIT:
|
||||
Game.running = false;
|
||||
GameEngine.running = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!Game.object_store) {
|
||||
if (!GameEngine.object_store) {
|
||||
break;
|
||||
}
|
||||
|
||||
handle_all_objects_events(Game.object_store, &(Game.event));
|
||||
handle_all_objects_events(GameEngine.object_store, &(GameEngine.event));
|
||||
}
|
||||
}
|
||||
|
||||
void update() {
|
||||
if (!(Game.running) || !(Game.object_store)) {
|
||||
if (!(GameEngine.running) || !(GameEngine.object_store)) {
|
||||
return;
|
||||
}
|
||||
|
||||
update_all_objects(Game.object_store, Game.delta);
|
||||
uint64_t current_time = SDL_GetTicks64();
|
||||
|
||||
// Get the delta time in seconds since the SDL_GetTicks64() returns the
|
||||
// time in milliseconds
|
||||
GameEngine.delta = (current_time - GameEngine.last_time) / 1000.0f;
|
||||
|
||||
update_all_objects(GameEngine.object_store, GameEngine.delta);
|
||||
|
||||
GameEngine.last_time = current_time;
|
||||
}
|
||||
|
||||
void render() {
|
||||
if (!(Game.running)) {
|
||||
if (!(GameEngine.running)) {
|
||||
return;
|
||||
}
|
||||
|
||||
clear_window(&(Game.window));
|
||||
clear_window(&(GameEngine.window));
|
||||
|
||||
if (Game.object_store) {
|
||||
render_all_objects(Game.object_store);
|
||||
if (GameEngine.object_store) {
|
||||
render_all_objects(GameEngine.object_store);
|
||||
}
|
||||
|
||||
swap_render_buffers(&(Game.window));
|
||||
swap_render_buffers(&(GameEngine.window));
|
||||
}
|
||||
|
||||
void close_game() {
|
||||
destroy_asset_manager(Game.asset_manager);
|
||||
Game.asset_manager = NULL;
|
||||
destroy_asset_manager(GameEngine.asset_manager);
|
||||
GameEngine.asset_manager = NULL;
|
||||
|
||||
destroy_object_store(Game.object_store);
|
||||
Game.object_store = NULL;
|
||||
destroy_object_store(GameEngine.object_store);
|
||||
GameEngine.object_store = NULL;
|
||||
|
||||
destroy_window(&(Game.window));
|
||||
destroy_window(&(GameEngine.window));
|
||||
|
||||
Game.delta = 0.0f;
|
||||
GameEngine.last_time = 0.0f;
|
||||
|
||||
Game.running = false;
|
||||
GameEngine.delta = 0.0f;
|
||||
|
||||
GameEngine.running = false;
|
||||
|
||||
IMG_Quit();
|
||||
|
||||
|
||||
Reference in new issue
Block a user