Skip to content

Commit

Permalink
Engine core and assets loading refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis authored Oct 30, 2023
1 parent 972db16 commit 0576316
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 181 deletions.
79 changes: 79 additions & 0 deletions src/AssetsLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "AssetsLoader.h"
#include "Assets.h"

#include <iostream>

AssetsLoader::AssetsLoader(Assets* assets) : assets(assets) {
}

void AssetsLoader::addLoader(int tag, aloader_func func) {
loaders[tag] = func;
}

void AssetsLoader::add(int tag, const std::string filename, const std::string alias) {
entries.push(aloader_entry{ tag, filename, alias });
}

bool AssetsLoader::hasNext() const {
return !entries.empty();
}

bool AssetsLoader::loadNext() {
const aloader_entry& entry = entries.front();
std::cout << " loading " << entry.filename << " as " << entry.alias << std::endl;
std::cout.flush();
auto found = loaders.find(entry.tag);
if (found == loaders.end()) {
std::cerr << "unknown asset tag " << entry.tag << std::endl;
return false;
}
aloader_func loader = found->second;
bool status = loader(assets, entry.filename, entry.alias);
entries.pop();
return status;
}

#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "graphics/Font.h"

bool _load_shader(Assets* assets, const std::string& filename, const std::string& name) {
Shader* shader = load_shader(filename + ".glslv", filename + ".glslf");
if (shader == nullptr) {
std::cerr << "failed to load shader '" << name << "'" << std::endl;
return false;
}
assets->store(shader, name);
return true;
}

bool _load_texture(Assets* assets, const std::string& filename, const std::string& name) {
Texture* texture = load_texture(filename);
if (texture == nullptr) {
std::cerr << "failed to load texture '" << name << "'" << std::endl;
return false;
}
assets->store(texture, name);
return true;
}

bool _load_font(Assets* assets, const std::string& filename, const std::string& name) {
std::vector<Texture*> pages;
for (size_t i = 0; i <= 4; i++) {
Texture* texture = load_texture(filename + "_" + std::to_string(i) + ".png");
if (texture == nullptr) {
std::cerr << "failed to load bitmap font '" << name << "' (missing page " << std::to_string(i) << ")" << std::endl;
return false;
}
pages.push_back(texture);
}
Font* font = new Font(pages);
assets->store(font, name);
return true;
}

void AssetsLoader::createDefaults(AssetsLoader& loader) {
loader.addLoader(ASSET_SHADER, _load_shader);
loader.addLoader(ASSET_TEXTURE, _load_texture);
loader.addLoader(ASSET_FONT, _load_font);
}
38 changes: 38 additions & 0 deletions src/AssetsLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef SRC_ASSETS_LOADER_H
#define SRC_ASSETS_LOADER_H

#include <string>
#include <functional>
#include <map>
#include <queue>

#define ASSET_TEXTURE 1
#define ASSET_SHADER 2
#define ASSET_FONT 3

class Assets;

typedef std::function<bool(Assets*, const std::string&, const std::string&)> aloader_func;

struct aloader_entry {
int tag;
const std::string filename;
const std::string alias;
};

class AssetsLoader {
Assets* assets;
std::map<int, aloader_func> loaders;
std::queue<aloader_entry> entries;
public:
AssetsLoader(Assets* assets);
void addLoader(int tag, aloader_func func);
void add(int tag, const std::string filename, const std::string alias);

bool hasNext() const;
bool loadNext();

static void createDefaults(AssetsLoader& loader);
};

#endif // SRC_ASSETS_LOADER_H
67 changes: 9 additions & 58 deletions src/declarations.cpp
Original file line number Diff line number Diff line change
@@ -1,70 +1,21 @@
#include "declarations.h"

#include "Assets.h"
#include "graphics/Shader.h"
#include "graphics/Texture.h"
#include "graphics/Font.h"
#include "AssetsLoader.h"
#include "window/Window.h"

#include "voxels/Block.h"

// Shaders, textures
bool _load_shader(Assets* assets, std::string vertex_file, std::string fragment_file, std::string name){
Shader* shader = load_shader(vertex_file, fragment_file);
if (shader == nullptr){
std::cerr << "failed to load shader '" << name << "'" << std::endl;
return false;
}
assets->store(shader, name);
return true;
}

bool _load_texture(Assets* assets, std::string filename, std::string name){
Texture* texture = load_texture(filename);
if (texture == nullptr){
std::cerr << "failed to load texture '" << name << "'" << std::endl;
return false;
}
assets->store(texture, name);
return true;
}
void initialize_assets(AssetsLoader* loader) {
loader->add(ASSET_SHADER, "res/main", "main");
loader->add(ASSET_SHADER, "res/crosshair", "crosshair");
loader->add(ASSET_SHADER, "res/lines", "lines");
loader->add(ASSET_SHADER, "res/ui", "ui");

bool _load_font(Assets* assets, std::string filename, std::string name){
std::vector<Texture*> pages;
for (size_t i = 0; i <= 4; i++){
Texture* texture = load_texture(filename+"_"+std::to_string(i)+".png");
if (texture == nullptr){
std::cerr << "failed to load bitmap font '" << name << "' (missing page " << std::to_string(i) << ")" << std::endl;
return false;
}
pages.push_back(texture);
}
Font* font = new Font(pages);
assets->store(font, name);
return true;
}
loader->add(ASSET_TEXTURE, "res/block.png", "block");
loader->add(ASSET_TEXTURE, "res/slot.png", "slot");

int initialize_assets(Assets* assets) {
#define LOAD_SHADER(VERTEX, FRAGMENT, NAME) \
if (!_load_shader(assets, VERTEX, FRAGMENT, NAME))\
return 1;
#define LOAD_TEXTURE(FILENAME, NAME) \
if (!_load_texture(assets, FILENAME, NAME))\
return 1;
#define LOAD_FONT(FILENAME, NAME) \
if (!_load_font(assets, FILENAME, NAME))\
return 1;

LOAD_SHADER("res/main.glslv", "res/main.glslf", "main");
LOAD_SHADER("res/crosshair.glslv", "res/crosshair.glslf", "crosshair");
LOAD_SHADER("res/lines.glslv", "res/lines.glslf", "lines");
LOAD_SHADER("res/ui.glslv", "res/ui.glslf", "ui");

LOAD_TEXTURE("res/block.png", "block");
LOAD_TEXTURE("res/slot.png", "slot");

LOAD_FONT("res/font", "normal");
return 0;
loader->add(ASSET_FONT, "res/font", "normal");
}

// All in-game definitions (blocks, items, etc..)
Expand Down
4 changes: 2 additions & 2 deletions src/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#define BLOCK_METAL 15
#define BLOCK_RUST 16

class Assets;
class AssetsLoader;

int initialize_assets(Assets* assets);
void initialize_assets(AssetsLoader* loader);
void setup_definitions();

#endif // DECLARATIONS_H
Expand Down
42 changes: 42 additions & 0 deletions src/hud_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@


HudRenderer::HudRenderer() {
// float vertices[] = {
// // x y
// -0.01f,-0.01f,
// 0.01f, 0.01f,

// -0.01f, 0.01f,
// 0.01f,-0.01f,
// };
// int attrs[] = {
// 2, 0 //null terminator
// };
// crosshair = new Mesh(vertices, 4, attrs);

batch = new Batch2D(1024);
uicamera = new Camera(glm::vec3(), Window::height / 1.0f);
uicamera->perspective = false;
Expand Down Expand Up @@ -66,6 +79,7 @@ void HudRenderer::draw(Level* level, Assets* assets){

// Chosen block preview
Texture* blocks = assets->getTexture("block");
Texture* sprite = assets->getTexture("slot");

batch->texture(nullptr);
batch->color = vec4(1.0f);
Expand All @@ -76,6 +90,13 @@ void HudRenderer::draw(Level* level, Assets* assets){
batch->line(Window::width/2-5, Window::height/2-5, Window::width/2+5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
batch->line(Window::width/2+5, Window::height/2-5, Window::width/2-5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
}

// batch->texture(sprite);
// batch->sprite(Window::width/2-32, uicamera->fov - 80, 64, 64, 16, 0, vec4(1.0f));
// batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
// 0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f,
// 0.55f, 0.55f, 0.55f,
// 0.45f, 0.45f, 0.45f, 0.7f, 0.7f, 0.7f, 2);
batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f,
0.7f, 0.7f, 0.7f,
Expand Down Expand Up @@ -146,6 +167,7 @@ void HudRenderer::draw(Level* level, Assets* assets){
for (uint i = 1; i < count; i++) {
x = xs + step * ((i-1) % (inv_w / step));
y = ys + step * ((i-1) / (inv_w / step));
// batch->rect(x-2, y-2, size+4, size+4);
batch->rect(x-2, y-2, size+4, size+4,
0.45f, 0.45f, 0.45f, 0.55f, 0.55f, 0.55f,
0.7f, 0.7f, 0.7f,
Expand All @@ -156,6 +178,13 @@ void HudRenderer::draw(Level* level, Assets* assets){
0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 2);
}

// batch->color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
// for (unsigned i = 1; i < count; i++) {
// x = xs + step * ((i-1) % (inv_w / step));
// y = ys + step * ((i-1) / (inv_w / step));
// batch->rect(x, y, size, size);
// }

//front
batch->texture(blocks);
for (uint i = 1; i < count; i++) {
Expand All @@ -171,8 +200,10 @@ void HudRenderer::draw(Level* level, Assets* assets){
if (Events::jclicked(GLFW_MOUSE_BUTTON_LEFT)) {
player->choosenBlock = i;
}
// size = 50;
} else
{
// size = 48;
tint = vec4(1.0f);
}

Expand All @@ -183,4 +214,15 @@ void HudRenderer::draw(Level* level, Assets* assets){
}
}
}

// batch->render();

if (Events::_cursor_locked && !level->player->debug){
// Shader* crosshairShader = assets->getShader("crosshair");
// crosshairShader->use();
// crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width);
// crosshairShader->uniform1f("u_scale", 1.0f / ((float)Window::height / 1000.0f));
// glLineWidth(2.0f);
// crosshair->draw(GL_LINES);
}
}
Loading

0 comments on commit 0576316

Please sign in to comment.