diff --git a/client/main.cc b/client/main.cc index f46336b..174db3d 100644 --- a/client/main.cc +++ b/client/main.cc @@ -12,7 +12,9 @@ static void cleanup(int sig){ int main(){ fishbone::Client* client; - std::unordered_map> param; + fishbone::StringArrayMap param; + + param["basedir"] = {"./data", "../data"}; client = new fishbone::Client(param); diff --git a/data/model.obj b/data/model.obj new file mode 100644 index 0000000..18e8c39 --- /dev/null +++ b/data/model.obj @@ -0,0 +1,28 @@ +# Blender 5.0.1 +# www.blender.org +v -0.500000 -0.500000 0.500000 +v -0.500000 0.500000 0.500000 +v -0.500000 -0.500000 -0.500000 +v -0.500000 0.500000 -0.500000 +v 0.500000 -0.500000 0.500000 +v 0.500000 0.500000 0.500000 +v 0.500000 -0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -0.0000 -1.0000 -0.0000 +vn -0.0000 1.0000 -0.0000 +vt 0.000000 0.000000 +vt 0.000000 1.000000 +vt 1.000000 1.000000 +vt 1.000000 0.000000 +s 0 +usemtl Material +f 1/1/1 2/2/1 4/3/1 3/4/1 +f 3/1/2 4/2/2 8/3/2 7/4/2 +f 7/4/3 8/3/3 6/2/3 5/1/3 +f 5/4/4 6/3/4 2/2/4 1/1/4 +f 3/2/5 7/3/5 5/4/5 1/1/5 +f 8/3/6 4/2/6 2/1/6 6/4/6 diff --git a/data/model.png b/data/model.png new file mode 100644 index 0000000..1ce3321 Binary files /dev/null and b/data/model.png differ diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 4e7fbbd..acdc2e7 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -37,6 +37,7 @@ pkg_add(fishbone sdl2 REQUIRED) set(FISHBONE_SOUND_USE_SDL2 ON) set(FISHBONE_WINDOW_USE_SDL2 ON) set(FISHBONE_THREAD_USE_SDL2 ON) +set(FISHBONE_DRAW_USE_OPENGL ON) configure_file(config.h.in fishbone/config.h) target_include_directories(fishbone PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/engine/config.h.in b/engine/config.h.in index b9e25db..c42dc21 100644 --- a/engine/config.h.in +++ b/engine/config.h.in @@ -4,5 +4,6 @@ #cmakedefine FISHBONE_SOUND_USE_SDL2 #cmakedefine FISHBONE_WINDOW_USE_SDL2 #cmakedefine FISHBONE_THREAD_USE_SDL2 +#cmakedefine FISHBONE_DRAW_USE_OPENGL #endif diff --git a/engine/include/fishbone/base.h b/engine/include/fishbone/base.h index 9268b24..11910c5 100644 --- a/engine/include/fishbone/base.h +++ b/engine/include/fishbone/base.h @@ -8,9 +8,17 @@ namespace fishbone { }; #include +#include namespace fishbone { class Base : public fishbone::Logger { + fishbone::StringArrayMap mountpoints; + + public: + Base(fishbone::StringArrayMap param); + + void mount(std::string name, std::string path); + fishbone::File* open(std::string path, std::string mode); }; }; // namespace fishbone diff --git a/engine/include/fishbone/client.h b/engine/include/fishbone/client.h index 4777589..c420fb3 100644 --- a/engine/include/fishbone/client.h +++ b/engine/include/fishbone/client.h @@ -16,7 +16,7 @@ namespace fishbone { namespace fishbone { class Client : public fishbone::Base { public: - Client(std::unordered_map> param); + Client(fishbone::StringArrayMap param); ~Client(); bool step(); diff --git a/engine/include/fishbone/draw.h b/engine/include/fishbone/draw.h index 755b941..5d52a2d 100644 --- a/engine/include/fishbone/draw.h +++ b/engine/include/fishbone/draw.h @@ -12,6 +12,7 @@ namespace fishbone { #include #include #include +#include namespace fishbone { class Texture { @@ -64,6 +65,18 @@ namespace fishbone { ~Renderer(); void prepare(); + void quad(std::vector vertices, std::vector texcoords, std::vector normals) { + std::vector v; + std::vector t; + std::vector n; + + v = fishbone::quadToTriangle(vertices); + t = fishbone::quadToTriangle(texcoords); + n = fishbone::quadToTriangle(normals); + + this->triangle(v, t, n); + } + void triangle(std::vector vertices, std::vector texcoords, std::vector normals); }; static float maxDistance = 64; diff --git a/engine/include/fishbone/file.h b/engine/include/fishbone/file.h index 16b7d8b..ff5e840 100644 --- a/engine/include/fishbone/file.h +++ b/engine/include/fishbone/file.h @@ -13,6 +13,7 @@ namespace fishbone { class File { std::ifstream ifs; std::ofstream ofs; + bool opened; public: File(); diff --git a/engine/include/fishbone/filesystem.h b/engine/include/fishbone/filesystem.h new file mode 100644 index 0000000..fa1ded1 --- /dev/null +++ b/engine/include/fishbone/filesystem.h @@ -0,0 +1,16 @@ +#ifndef __FISHBONE_FILESYSTEM_H__ +#define __FISHBONE_FILESYSTEM_H__ + +#include + +namespace fishbone { + bool exists(std::string path); +}; + +/* no includes */ + +namespace fishbone { + bool exists(std::string path); +}; + +#endif diff --git a/engine/include/fishbone/foundation.h b/engine/include/fishbone/foundation.h index 27f53fb..d2b7bcd 100644 --- a/engine/include/fishbone/foundation.h +++ b/engine/include/fishbone/foundation.h @@ -15,5 +15,6 @@ #include #include #include +#include #endif diff --git a/engine/include/fishbone/math.h b/engine/include/fishbone/math.h index 0b1bb83..838640f 100644 --- a/engine/include/fishbone/math.h +++ b/engine/include/fishbone/math.h @@ -5,12 +5,35 @@ namespace fishbone { float cot(float x); -}; + template + std::vector quadToTriangle(std::vector v); +}; // namespace fishbone /* no includes */ namespace fishbone { float cot(float x); + template + std::vector quadToTriangle(std::vector v) { + std::vector r; + + /** + * 1-4 + * |\| + * 2-3 + */ + for(int i = 0; i < v.size(); i += 4) { + r.push_back(v[i + 0]); + r.push_back(v[i + 1]); + r.push_back(v[i + 2]); + + r.push_back(v[i + 2]); + r.push_back(v[i + 3]); + r.push_back(v[i + 0]); + } + + return r; + } static double pi = 3.14159265358979323846; }; // namespace fishbone diff --git a/engine/include/fishbone/types.h b/engine/include/fishbone/types.h index 62a3039..30c15f6 100644 --- a/engine/include/fishbone/types.h +++ b/engine/include/fishbone/types.h @@ -37,6 +37,8 @@ namespace fishbone { float y; float z; }; + + using StringArrayMap = std::unordered_map>; }; // namespace fishbone #endif diff --git a/engine/src/base.cc b/engine/src/base.cc new file mode 100644 index 0000000..f657a79 --- /dev/null +++ b/engine/src/base.cc @@ -0,0 +1,52 @@ +#include + +fishbone::Base::Base(fishbone::StringArrayMap param) { + for(auto i = param.begin(); i != param.end(); i++) { + if(i->first == "basedir") { + for(int j = 0; j < i->second.size(); j++) { + this->mount("base", i->second[j]); + } + } + } +} + +void fishbone::Base::mount(std::string name, std::string path) { + if(this->mountpoints.find(name) == this->mountpoints.end()) this->mountpoints[name] = {}; + + this->mountpoints[name].push_back(path); +} + +fishbone::File* fishbone::Base::open(std::string path, std::string mode) { + fishbone::File* f = nullptr; + + for(int i = 0; i < path.size() - 1; i++) { + if(path.substr(i, 2) == ":/") { + std::string p = path.substr(i + 1); + std::string k = path.substr(0, i); + + if(this->mountpoints.find(k) != this->mountpoints.end()) { + for(int j = 0; j < this->mountpoints[k].size(); j++) { + f = new fishbone::File(this->mountpoints[k][j] + p, mode); + + if(!f->good()) { + delete f; + f = nullptr; + } + + if(f != nullptr && f->good()) break; + } + } + + break; + } + } + + if(f == nullptr) f = new fishbone::File(path, mode); + + if(f != nullptr && !f->good()) { + delete f; + f = nullptr; + } + + return f; +} diff --git a/engine/src/client.cc b/engine/src/client.cc index 28d9822..08f64a1 100644 --- a/engine/src/client.cc +++ b/engine/src/client.cc @@ -3,7 +3,8 @@ fishbone::File* f; fishbone::Mesh* m; -fishbone::Client::Client(std::unordered_map> param) { +fishbone::Client::Client(fishbone::StringArrayMap param) + : fishbone::Base::Base(param) { this->window = new fishbone::Window("Fishbone", 800, 600); this->sounddrv = new fishbone::SoundDriver(); this->mixer = &this->sounddrv->mixer; @@ -12,11 +13,11 @@ fishbone::Client::Client(std::unordered_mapopen("base:/model.obj", "r"); md = new fishbone::MeshData(f); delete f; - f = new fishbone::File("model.png", "r"); + f = this->open("base:/model.png", "r"); td = new fishbone::TextureData(f); delete f; diff --git a/engine/src/draw/opengl.cc b/engine/src/draw/opengl.cc index 1566a1c..0270d2a 100644 --- a/engine/src/draw/opengl.cc +++ b/engine/src/draw/opengl.cc @@ -1,3 +1,5 @@ +#include +#ifdef FISHBONE_DRAW_USE_OPENGL #include struct fishbone::Texture::Impl { @@ -36,9 +38,9 @@ struct fishbone::Mesh::Impl { GLuint t_vbo; GLuint n_vbo; - float* vertices; - float* texcoords; - float* normals; + GLfloat* vertices; + GLfloat* texcoords; + GLfloat* normals; int count; }; @@ -47,9 +49,9 @@ void fishbone::Mesh::init(fishbone::Renderer* renderer, std::vectortexture = nullptr; this->impl = new struct fishbone::Mesh::Impl(); - this->impl->vertices = new float[vertices.size() * 3]; - this->impl->texcoords = new float[texcoords.size() * 2]; - this->impl->normals = new float[normals.size() * 3]; + this->impl->vertices = new GLfloat[vertices.size() * 3]; + this->impl->texcoords = new GLfloat[texcoords.size() * 2]; + this->impl->normals = new GLfloat[normals.size() * 3]; this->impl->count = vertices.size(); glGenBuffers(1, &this->impl->v_vbo); @@ -233,3 +235,45 @@ void fishbone::Renderer::prepare() { glLoadIdentity(); lookAt({4, 4, 4}, {0, 0, 0}); } + +void fishbone::Renderer::triangle(std::vector vertices, std::vector texcoords, std::vector normals) { + GLfloat* v = new GLfloat[vertices.size() * 3]; + GLfloat* t = new GLfloat[texcoords.size() * 2]; + GLfloat* n = new GLfloat[normals.size() * 3]; + + for(int i = 0; i < vertices.size(); i++) { + v[i * 3 + 0] = vertices[i].x; + v[i * 3 + 1] = vertices[i].y; + v[i * 3 + 2] = vertices[i].z; + } + + for(int i = 0; i < texcoords.size(); i++) { + t[i * 2 + 0] = texcoords[i].x; + t[i * 2 + 1] = texcoords[i].y; + } + + for(int i = 0; i < normals.size(); i++) { + n[i * 3 + 0] = normals[i].x; + n[i * 3 + 1] = normals[i].y; + n[i * 3 + 2] = normals[i].z; + } + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + + glVertexPointer(3, GL_FLOAT, 0, v); + glTexCoordPointer(2, GL_FLOAT, 0, t); + glNormalPointer(GL_FLOAT, 0, n); + + glDrawArrays(GL_TRIANGLES, 0, vertices.size()); + + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + delete[] n; + delete[] t; + delete[] v; +} +#endif diff --git a/engine/src/file.cc b/engine/src/file.cc index 505456f..2c7f35c 100644 --- a/engine/src/file.cc +++ b/engine/src/file.cc @@ -5,7 +5,8 @@ fishbone::File::File() { } fishbone::File::File(std::string path, std::string type) { - this->size = 0; + this->size = 0; + this->opened = false; if(type == "r") { this->ifs = std::ifstream(path, std::ios::binary); @@ -22,6 +23,8 @@ fishbone::File::File(std::string path, std::string type) { } else { return; } + + this->opened = true; } fishbone::File::~File() { @@ -46,6 +49,6 @@ size_t fishbone::File::write(const void* buffer, size_t size) { } bool fishbone::File::good() { - if(this->ifs.good() || this->ofs.good()) return true; + if(this->opened) return true; return false; } diff --git a/engine/src/filesystem.cc b/engine/src/filesystem.cc new file mode 100644 index 0000000..dbdd7d9 --- /dev/null +++ b/engine/src/filesystem.cc @@ -0,0 +1,11 @@ +#include + +#include + +bool fishbone::exists(std::string path) { + struct stat s; + + if(stat(path.c_str(), &s) == 0) return true; + + return false; +} diff --git a/engine/src/physics.cc b/engine/src/physics.cc new file mode 100644 index 0000000..10e9255 --- /dev/null +++ b/engine/src/physics.cc @@ -0,0 +1 @@ +#include