This commit is contained in:
Nishi 2026-06-05 21:43:30 +09:00
commit 03b8658666
18 changed files with 257 additions and 57 deletions

View file

@ -0,0 +1,17 @@
#ifndef __FISHBONE_BASE_H__
#define __FISHBONE_BASE_H__
#include <fishbone/pre.h>
namespace fishbone {
class Base;
};
#include <fishbone/log.h>
namespace fishbone {
class Base : public fishbone::Logger {
};
}; // namespace fishbone
#endif

View file

@ -11,19 +11,20 @@ namespace fishbone {
#include <fishbone/window.h>
#include <fishbone/mixer.h>
#include <fishbone/draw.h>
#include <fishbone/base.h>
namespace fishbone {
class Client {
class Client : public fishbone::Base {
public:
Client(std::unordered_map<std::string, std::vector<std::string>> param);
~Client();
bool step();
fishbone::Window* window;
fishbone::SoundDriver* sounddrv;
fishbone::Mixer* mixer;
fishbone::Renderer* renderer;
fishbone::Mixer* mixer;
fishbone::Renderer* renderer;
};
}; // namespace fishbone

View file

@ -7,26 +7,34 @@ namespace fishbone {
class Texture;
class Mesh;
class Renderer;
};
}; // namespace fishbone
/* no includes */
#include <fishbone/window.h>
#include <fishbone/meshldr.h>
namespace fishbone {
class Texture {
struct Impl;
Impl* impl;
public:
public:
Texture(fishbone::Renderer* renderer, unsigned char* rgba, int width, int height);
~Texture();
};
class Mesh {
void init(fishbone::Renderer* renderer, std::vector<struct fishbone::Vector3> vertices, std::vector<struct fishbone::Vector2> texcoords, std::vector<struct fishbone::Vector3> normals);
struct Impl;
Impl* impl;
public:
Mesh(fishbone::Renderer* renderer, std::vector<struct fishbone::Vector3> vertices, std::vector<struct fishbone::Vector2> texcoords, std::vector<struct fishbone::Vector3> normals, std::vector<int> elements);
public:
Mesh(fishbone::Renderer* renderer, std::vector<struct fishbone::Vector3> vertices, std::vector<struct fishbone::Vector2> texcoords, std::vector<struct fishbone::Vector3> normals) {
this->init(renderer, vertices, texcoords, normals);
}
Mesh(fishbone::Renderer* renderer, fishbone::MeshData* data) {
this->init(renderer, data->vertices, data->texcoords, data->normals);
}
~Mesh();
void draw();
@ -36,10 +44,10 @@ namespace fishbone {
struct Impl;
Impl* impl;
public:
Renderer();
public:
Renderer(fishbone::Window* window);
~Renderer();
};
};
}; // namespace fishbone
#endif

View file

@ -10,5 +10,8 @@
#include <fishbone/resource.h>
#include <fishbone/draw.h>
#include <fishbone/time.h>
#include <fishbone/base.h>
#include <fishbone/meshldr.h>
#include <fishbone/log.h>
#endif

View file

@ -0,0 +1,35 @@
#ifndef __FISHBONE_LOG_H__
#define __FISHBONE_LOG_H__
#include <fishbone/pre.h>
namespace fishbone {
class Logger;
};
/* no includes */
namespace fishbone {
class Logger {
enum LoggerLevel {
Log = 0,
Info,
Warn,
Error
};
void init(std::string path);
std::ofstream ofs;
public:
Logger();
Logger(std::string path);
~Logger();
void log(std::string arg);
void log(int level, std::string arg);
};
}; // namespace fishbone
#endif

View file

@ -0,0 +1,23 @@
#ifndef __FISHBONE_MESHLDR_H__
#define __FISHBONE_MESHLDR_H__
#include <fishbone/pre.h>
namespace fishbone {
class MeshData;
};
#include <fishbone/file.h>
namespace fishbone {
class MeshData {
public:
MeshData(fishbone::File* file);
std::vector<struct fishbone::Vector3> vertices;
std::vector<struct fishbone::Vector2> texcoords;
std::vector<struct fishbone::Vector3> normals;
};
}; // namespace fishbone
#endif

View file

@ -12,10 +12,10 @@ namespace fishbone {
namespace fishbone {
class Resource {
public:
fishbone::File* open(std::string path);
Resource(std::string path);
~Resource();
fishbone::File* open(std::string path);
};
}; // namespace fishbone

View file

@ -55,12 +55,16 @@ namespace fishbone {
class SoundLoader {
public:
SoundLoader(){};
~SoundLoader(){};
SoundLoader() {};
~SoundLoader() {};
virtual Sound* open(Mixer* mixer, void* buffer, size_t size){return nullptr;};
virtual int read(Sound* sound, short* buffer, size_t frames){return 0;};
virtual void seek(Sound* sound, size_t pos){};
virtual Sound* open(Mixer* mixer, void* buffer, size_t size) {
return nullptr;
};
virtual int read(Sound* sound, short* buffer, size_t frames) {
return 0;
};
virtual void seek(Sound* sound, size_t pos) {};
};
class Sound {

View file

@ -7,7 +7,7 @@ namespace fishbone {
using AccurateClock = std::chrono::time_point<std::chrono::high_resolution_clock, std::chrono::high_resolution_clock::duration>;
fishbone::AccurateClock accurateTime();
};
}; // namespace fishbone
/* no includes */

View file

@ -14,6 +14,6 @@ namespace fishbone {
float y;
float z;
};
};
}; // namespace fishbone
#endif

View file

@ -17,6 +17,7 @@ namespace fishbone {
~Window();
void poll();
void swapBuffer();
};
}; // namespace fishbone

View file

@ -1,18 +1,33 @@
#include <fishbone/foundation.h>
fishbone::Client::Client(std::unordered_map<std::string, std::vector<std::string>> param){
this->window = new fishbone::Window("Fishbone", 800, 600);
fishbone::File* f;
fishbone::MeshData* md;
fishbone::Mesh* m;
fishbone::Client::Client(std::unordered_map<std::string, std::vector<std::string>> param) {
this->window = new fishbone::Window("Fishbone", 800, 600);
this->sounddrv = new fishbone::SoundDriver();
this->mixer = &this->sounddrv->mixer;
this->renderer = new fishbone::Renderer();
this->mixer = &this->sounddrv->mixer;
this->renderer = new fishbone::Renderer(this->window);
f = new fishbone::File("model.obj", "r");
md = new fishbone::MeshData(f);
delete f;
m = new fishbone::Mesh(this->renderer, md);
delete md;
}
fishbone::Client::~Client(){
fishbone::Client::~Client() {
delete this->renderer;
delete this->sounddrv;
delete this->window;
}
bool fishbone::Client::step(){
bool fishbone::Client::step() {
m->draw();
this->window->swapBuffer();
return true;
}

View file

@ -4,9 +4,9 @@ struct fishbone::Texture::Impl {
GLuint texture;
};
fishbone::Texture::Texture(fishbone::Renderer* renderer, unsigned char* rgba, int width, int height){
fishbone::Texture::Texture(fishbone::Renderer* renderer, unsigned char* rgba, int width, int height) {
this->impl = new struct fishbone::Texture::Impl();
glGenTextures(1, &this->impl->texture);
glBindTexture(GL_TEXTURE_2D, this->impl->texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
@ -15,7 +15,7 @@ fishbone::Texture::Texture(fishbone::Renderer* renderer, unsigned char* rgba, in
glBindTexture(GL_TEXTURE_2D, 0);
}
fishbone::Texture::~Texture(){
fishbone::Texture::~Texture() {
glDeleteTextures(1, &this->impl->texture);
delete this->impl;
@ -25,28 +25,24 @@ struct fishbone::Mesh::Impl {
GLuint v_vbo;
GLuint t_vbo;
GLuint n_vbo;
GLuint e_vbo;
float* vertices;
float* texcoords;
float* normals;
int* elements;
int count;
};
fishbone::Mesh::Mesh(fishbone::Renderer* renderer, std::vector<struct fishbone::Vector3> vertices, std::vector<struct fishbone::Vector2> texcoords, std::vector<struct fishbone::Vector3> normals, std::vector<int> elements){
this->impl = new struct fishbone::Mesh::Impl();
this->impl->vertices = new float[vertices.size() * 3];
void fishbone::Mesh::init(fishbone::Renderer* renderer, std::vector<struct fishbone::Vector3> vertices, std::vector<struct fishbone::Vector2> texcoords, std::vector<struct fishbone::Vector3> normals) {
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->elements = new int[elements.size()];
this->impl->count = elements.size();
this->impl->normals = new float[normals.size() * 3];
this->impl->count = vertices.size();
glGenBuffers(1, &this->impl->v_vbo);
glGenBuffers(1, &this->impl->t_vbo);
glGenBuffers(1, &this->impl->n_vbo);
glGenBuffers(1, &this->impl->e_vbo);
glBindBuffer(GL_ARRAY_BUFFER, this->impl->v_vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float) * 3, this->impl->vertices, GL_STATIC_DRAW);
@ -58,27 +54,20 @@ fishbone::Mesh::Mesh(fishbone::Renderer* renderer, std::vector<struct fishbone::
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float) * 3, this->impl->normals, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->impl->e_vbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(int), this->impl->elements, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
fishbone::Mesh::~Mesh(){
glDeleteBuffers(1, &this->impl->e_vbo);
fishbone::Mesh::~Mesh() {
glDeleteBuffers(1, &this->impl->n_vbo);
glDeleteBuffers(1, &this->impl->t_vbo);
glDeleteBuffers(1, &this->impl->v_vbo);
delete[] this->impl->elements;
delete[] this->impl->normals;
delete[] this->impl->texcoords;
delete[] this->impl->vertices;
delete this->impl;
}
void fishbone::Mesh::draw(){
void fishbone::Mesh::draw() {
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
@ -92,9 +81,8 @@ void fishbone::Mesh::draw(){
glBindBuffer(GL_ARRAY_BUFFER, this->impl->n_vbo);
glNormalPointer(GL_FLOAT, 0, nullptr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->impl->e_vbo);
glEnable(GL_TEXTURE_2D);
glDrawElements(GL_TRIANGLES, this->impl->count, GL_UNSIGNED_INT, nullptr);
glDrawArrays(GL_TRIANGLES, 0, this->impl->count);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_NORMAL_ARRAY);
@ -102,8 +90,8 @@ void fishbone::Mesh::draw(){
glDisableClientState(GL_VERTEX_ARRAY);
}
fishbone::Renderer::Renderer(){
fishbone::Renderer::Renderer(fishbone::Window* window) {
}
fishbone::Renderer::~Renderer(){
fishbone::Renderer::~Renderer() {
}

View file

@ -1,6 +1,6 @@
#include <fishbone/file.h>
fishbone::File::File(){
fishbone::File::File() {
this->good = true;
this->size = 0;
}

25
engine/src/log.cc Normal file
View file

@ -0,0 +1,25 @@
#include <fishbone/foundation.h>
void fishbone::Logger::init(std::string path) {
this->ofs = std::ofstream(path, std::ios_base::app);
}
fishbone::Logger::Logger() {
this->init("log.txt");
}
fishbone::Logger::Logger(std::string path) {
this->init(path);
}
fishbone::Logger::~Logger() {
this->ofs.close();
}
void fishbone::Logger::log(std::string arg) {
this->log(fishbone::Logger::Info, arg);
}
void fishbone::Logger::log(int level, std::string arg) {
this->ofs << "" + arg << std::endl;
}

76
engine/src/meshldr.cc Normal file
View file

@ -0,0 +1,76 @@
#include <fishbone/foundation.h>
#include <iostream>
static std::vector<int> parseFace(std::string s) {
std::vector<int> v;
std::string n = "";
for(int i = 0; i < s.size(); i++) {
if(s[i] == '/') {
v.push_back(std::stoi(n));
n = "";
} else {
n += s[i];
}
}
if(n.size() > 0) v.push_back(std::stoi(n));
return v;
}
fishbone::MeshData::MeshData(fishbone::File* f) {
char c;
std::string l = "";
std::vector<struct fishbone::Vector3> v;
std::vector<struct fishbone::Vector2> vt;
std::vector<struct fishbone::Vector3> vn;
fishbone::Vector2 e2 = {0, 0};
fishbone::Vector3 e3 = {0, 0, 0};
while(f->read(&c, 1) == 1) {
if(c == '\n') {
if(l.size() == 0) {
} else if(l[0] == '#') {
} else {
std::vector<std::string> vs;
std::string n = "";
for(int i = 0; i < l.size(); i++) {
if(l[i] == ' ') {
vs.push_back(n);
n = "";
} else {
n += l[i];
}
}
if(n.size() > 0) vs.push_back(n);
if(vs[0] == "v") {
fishbone::Vector3 nv = {std::stof(vs[1]), std::stof(vs[2]), std::stof(vs[3])};
v.push_back(nv);
} else if(vs[0] == "vt") {
fishbone::Vector2 nv = {std::stof(vs[1]), std::stof(vs[2])};
vt.push_back(nv);
} else if(vs[0] == "vn") {
fishbone::Vector3 nv = {std::stof(vs[1]), std::stof(vs[2]), std::stof(vs[3])};
vn.push_back(nv);
} else if(vs[0] == "f") {
for(int i = 1; i < 4; i++) {
std::vector<int> ind = parseFace(vs[i]);
this->vertices.push_back(v[ind[0]]);
this->texcoords.push_back(ind.size() >= 2 ? vt[ind[1]] : e2);
this->normals.push_back(ind.size() >= 3 ? vn[ind[2]] : e3);
}
}
}
l = "";
} else if(c != '\r') {
l += c;
}
}
}

View file

@ -1,5 +1,5 @@
#include <fishbone/foundation.h>
fishbone::AccurateClock fishbone::accurateTime(){
fishbone::AccurateClock fishbone::accurateTime() {
return std::chrono::high_resolution_clock::now();
}

View file

@ -28,10 +28,14 @@ fishbone::Window::~Window() {
delete this->impl;
}
void fishbone::Window::poll(){
void fishbone::Window::poll() {
SDL_Event ev;
while(SDL_PollEvent(&ev)){
while(SDL_PollEvent(&ev)) {
}
}
void fishbone::Window::swapBuffer() {
SDL_GL_SwapWindow(this->impl->window);
}
#endif