This commit is contained in:
Nishi 2026-06-06 22:30:58 +09:00
commit 112a8f568b
19 changed files with 222 additions and 14 deletions

View file

@ -12,7 +12,9 @@ static void cleanup(int sig){
int main(){
fishbone::Client* client;
std::unordered_map<std::string, std::vector<std::string>> param;
fishbone::StringArrayMap param;
param["basedir"] = {"./data", "../data"};
client = new fishbone::Client(param);

28
data/model.obj Normal file
View file

@ -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

BIN
data/model.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

View file

@ -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})

View file

@ -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

View file

@ -8,9 +8,17 @@ namespace fishbone {
};
#include <fishbone/log.h>
#include <fishbone/file.h>
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

View file

@ -16,7 +16,7 @@ namespace fishbone {
namespace fishbone {
class Client : public fishbone::Base {
public:
Client(std::unordered_map<std::string, std::vector<std::string>> param);
Client(fishbone::StringArrayMap param);
~Client();
bool step();

View file

@ -12,6 +12,7 @@ namespace fishbone {
#include <fishbone/window.h>
#include <fishbone/meshldr.h>
#include <fishbone/textureldr.h>
#include <fishbone/math.h>
namespace fishbone {
class Texture {
@ -64,6 +65,18 @@ namespace fishbone {
~Renderer();
void prepare();
void quad(std::vector<struct fishbone::Vector3> vertices, std::vector<struct fishbone::Vector2> texcoords, std::vector<struct fishbone::Vector3> normals) {
std::vector<struct fishbone::Vector3> v;
std::vector<struct fishbone::Vector2> t;
std::vector<struct fishbone::Vector3> n;
v = fishbone::quadToTriangle<struct fishbone::Vector3>(vertices);
t = fishbone::quadToTriangle<struct fishbone::Vector2>(texcoords);
n = fishbone::quadToTriangle<struct fishbone::Vector3>(normals);
this->triangle(v, t, n);
}
void triangle(std::vector<struct fishbone::Vector3> vertices, std::vector<struct fishbone::Vector2> texcoords, std::vector<struct fishbone::Vector3> normals);
};
static float maxDistance = 64;

View file

@ -13,6 +13,7 @@ namespace fishbone {
class File {
std::ifstream ifs;
std::ofstream ofs;
bool opened;
public:
File();

View file

@ -0,0 +1,16 @@
#ifndef __FISHBONE_FILESYSTEM_H__
#define __FISHBONE_FILESYSTEM_H__
#include <fishbone/pre.h>
namespace fishbone {
bool exists(std::string path);
};
/* no includes */
namespace fishbone {
bool exists(std::string path);
};
#endif

View file

@ -15,5 +15,6 @@
#include <fishbone/log.h>
#include <fishbone/math.h>
#include <fishbone/textureldr.h>
#include <fishbone/filesystem.h>
#endif

View file

@ -5,12 +5,35 @@
namespace fishbone {
float cot(float x);
};
template <typename T>
std::vector<T> quadToTriangle(std::vector<T> v);
}; // namespace fishbone
/* no includes */
namespace fishbone {
float cot(float x);
template <typename T>
std::vector<T> quadToTriangle(std::vector<T> v) {
std::vector<T> 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

View file

@ -37,6 +37,8 @@ namespace fishbone {
float y;
float z;
};
using StringArrayMap = std::unordered_map<std::string, std::vector<std::string>>;
}; // namespace fishbone
#endif

52
engine/src/base.cc Normal file
View file

@ -0,0 +1,52 @@
#include <fishbone/foundation.h>
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;
}

View file

@ -3,7 +3,8 @@
fishbone::File* f;
fishbone::Mesh* m;
fishbone::Client::Client(std::unordered_map<std::string, std::vector<std::string>> 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_map<std::string, std::vector<std::string
fishbone::MeshData* md;
fishbone::TextureData* td;
f = new fishbone::File("model.obj", "r");
f = this->open("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;

View file

@ -1,3 +1,5 @@
#include <fishbone/config.h>
#ifdef FISHBONE_DRAW_USE_OPENGL
#include <fishbone/foundation.h>
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::vector<struct fishb
this->texture = 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<struct fishbone::Vector3> vertices, std::vector<struct fishbone::Vector2> texcoords, std::vector<struct fishbone::Vector3> 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

View file

@ -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;
}

11
engine/src/filesystem.cc Normal file
View file

@ -0,0 +1,11 @@
#include <fishbone/foundation.h>
#include <sys/stat.h>
bool fishbone::exists(std::string path) {
struct stat s;
if(stat(path.c_str(), &s) == 0) return true;
return false;
}

1
engine/src/physics.cc Normal file
View file

@ -0,0 +1 @@
#include <fishbone/foundation.h>