hemoglobinuria boob
This commit is contained in:
parent
dfd36ac93d
commit
d4efb11dd1
28 changed files with 8 additions and 965 deletions
|
@ -22,8 +22,8 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS
|
|||
#find_package(SDL2_image REQUIRED)
|
||||
|
||||
file(GLOB_RECURSE client_src
|
||||
"src/common/*.hpp"
|
||||
"src/common/*.cpp"
|
||||
#"src/common/*.hpp"
|
||||
#"src/common/*.cpp"
|
||||
"src/client/*.hpp"
|
||||
"src/client/*.cpp"
|
||||
)
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
#ifndef SOSC_CLIENT_COMMON_H
|
||||
#define SOSC_CLIENT_COMMON_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#ifdef SOSC_DEBUG
|
||||
#define SOSC_RESOURCE_PATH (std::string("../resources/client/"))
|
||||
#else
|
||||
#define SOSC_RESOURCE_PATH (std::string("resources/"))
|
||||
#endif
|
||||
|
||||
#define SOSC_RESC(X) (SOSC_RESOURCE_PATH + std::string(X))
|
||||
|
||||
#endif
|
|
@ -1,115 +1,12 @@
|
|||
#include <SDL.h>
|
||||
#include <GLM/glm.hpp>
|
||||
#include <GLM/gtc/matrix_transform.hpp>
|
||||
#include <GL/glew.h>
|
||||
#include <emscripten.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "ui/font.hpp"
|
||||
#include "shaders/test.hpp"
|
||||
struct {
|
||||
SDL_Window* window;
|
||||
} _ctx;
|
||||
|
||||
void setColor(float r, float g, float b, SDL_Window* window) {
|
||||
glClearColor(r, g, b, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
||||
|
||||
void setupOrthoMode() {
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
}
|
||||
|
||||
void draw();
|
||||
|
||||
sosc::ui::Text text;
|
||||
SDL_Window* window;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
using namespace sosc;
|
||||
|
||||
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
printf(SDL_GetError());
|
||||
return -1;
|
||||
}
|
||||
atexit(SDL_Quit);
|
||||
|
||||
window = SDL_CreateWindow(
|
||||
"SockScape Client",
|
||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||
640, 480,
|
||||
SDL_WINDOW_OPENGL
|
||||
);
|
||||
|
||||
SDL_GL_LoadLibrary(nullptr);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
|
||||
SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
|
||||
auto ctx = SDL_GL_CreateContext(window);
|
||||
if(ctx == nullptr)
|
||||
return -1;
|
||||
|
||||
#ifndef __APPLE__
|
||||
if(glewInit() != GLEW_OK)
|
||||
return -1;
|
||||
#endif
|
||||
|
||||
ui::font_init_subsystem(window);
|
||||
ui::Font scapeFont(
|
||||
SOSC_RESC("fonts/scape.bmp"),
|
||||
SOSC_RESC("fonts/scape.dat")
|
||||
);
|
||||
ui::font_set_default(&scapeFont);
|
||||
|
||||
text = ui::Text(75, glm::vec4(1, 0, 0, 1), "test", 100, 100, 400);
|
||||
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
emscripten_set_main_loop(draw, 30, 1);
|
||||
|
||||
/*ui::font_deinit_subsystem();
|
||||
SDL_GL_DeleteContext(ctx);
|
||||
SDL_DestroyWindow(window);*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void draw() {
|
||||
using namespace sosc;
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClearColor(.25, .25, .25, 1);
|
||||
|
||||
text.Render();
|
||||
|
||||
SDL_GL_SwapWindow(window);
|
||||
|
||||
SDL_Event event;
|
||||
while(SDL_PollEvent(&event)) {
|
||||
/*if(event.type == SDL_QUIT)
|
||||
running = false;*/
|
||||
|
||||
if(event.type == SDL_WINDOWEVENT &&
|
||||
event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
|
||||
{
|
||||
glViewport(0, 0, event.window.data1, event.window.data2);
|
||||
ui::font_window_changed(window);
|
||||
}
|
||||
|
||||
if(event.type == SDL_KEYDOWN) {
|
||||
switch(event.key.keysym.sym) {
|
||||
/*case SDLK_ESCAPE:
|
||||
running = false;
|
||||
break;*/
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
#include "_shader.hpp"
|
||||
|
||||
sosc::shdr::Shader::Shader() : loaded(false) {}
|
||||
|
||||
bool sosc::shdr::Shader::Load() {
|
||||
if(this->loaded)
|
||||
return true;
|
||||
|
||||
program = glCreateProgram();
|
||||
PrepareLoad();
|
||||
|
||||
this->loaded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void sosc::shdr::Shader::Start() const {
|
||||
if(!this->loaded)
|
||||
return;
|
||||
|
||||
glUseProgram(this->program);
|
||||
}
|
||||
|
||||
void sosc::shdr::Shader::Stop() const {
|
||||
if(!this->loaded)
|
||||
return;
|
||||
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
void sosc::shdr::Shader::Unload() {
|
||||
if(!this->loaded)
|
||||
return;
|
||||
|
||||
PrepareUnload();
|
||||
glDeleteProgram(this->program);
|
||||
this->loaded = false;
|
||||
}
|
||||
|
||||
void sosc::shdr::Shader::AttachSource
|
||||
(const std::string& fileName, GLuint shaderType)
|
||||
{
|
||||
if(this->loaded)
|
||||
return;
|
||||
|
||||
GLuint shader = glCreateShader(shaderType);
|
||||
|
||||
std::ifstream file(fileName);
|
||||
std::stringstream ss;
|
||||
ss << file.rdbuf();
|
||||
std::string source = ss.str();
|
||||
|
||||
const char* src = source.c_str();
|
||||
glShaderSource(shader, 1, (const GLchar**)&src, nullptr);
|
||||
glCompileShader(shader);
|
||||
|
||||
GLint tmpBuf;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &tmpBuf);
|
||||
if(tmpBuf == GL_FALSE) {
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &tmpBuf);
|
||||
|
||||
auto msg = new char[tmpBuf];
|
||||
glGetShaderInfoLog(shader, tmpBuf, nullptr, msg);
|
||||
throw ShaderCompilationException(fileName, msg);
|
||||
}
|
||||
|
||||
glAttachShader(this->program, shader);
|
||||
this->shaders.push_back(shader);
|
||||
}
|
||||
|
||||
void sosc::shdr::Shader::LinkProgram() {
|
||||
glLinkProgram(this->program);
|
||||
for(const auto& shader : this->shaders)
|
||||
glDeleteShader(shader);
|
||||
}
|
||||
|
||||
void sosc::shdr::Shader::LoadUniforms(std::vector<std::string> names) {
|
||||
if(this->loaded)
|
||||
return;
|
||||
|
||||
GLint id;
|
||||
this->locations.clear();
|
||||
for(const auto& name : names) {
|
||||
if((id = glGetUniformLocation(this->program, name.c_str())) == -1)
|
||||
0;//throw ShaderUniformException(name);
|
||||
|
||||
this->locations.push_back(id);
|
||||
}
|
||||
}
|
|
@ -1,96 +0,0 @@
|
|||
#ifndef SOSC_SHADER_CORE_H
|
||||
#define SOSC_SHADER_CORE_H
|
||||
|
||||
#include "common.hpp"
|
||||
#include <GL/glew.h>
|
||||
#include <vector>
|
||||
#include <exception>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
namespace sosc {
|
||||
namespace shdr {
|
||||
struct ShaderLoadingException : public std::exception {
|
||||
public:
|
||||
ShaderLoadingException(const std::string& errorInfo) {
|
||||
this->errorInfo = errorInfo;
|
||||
}
|
||||
|
||||
const char* what() noexcept {
|
||||
return this->errorInfo.c_str();
|
||||
}
|
||||
private:
|
||||
std::string errorInfo;
|
||||
};
|
||||
|
||||
struct ShaderCompilationException : public std::exception {
|
||||
public:
|
||||
ShaderCompilationException
|
||||
(const std::string& shaderName, const std::string& errorInfo)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Error in '" << shaderName << "': " << errorInfo;
|
||||
this->errorInfo = ss.str();
|
||||
}
|
||||
|
||||
const char* what() noexcept {
|
||||
return this->errorInfo.c_str();
|
||||
}
|
||||
private:
|
||||
std::string errorInfo;
|
||||
};
|
||||
|
||||
struct ShaderUniformException : public std::exception {
|
||||
public:
|
||||
explicit ShaderUniformException(const std::string& uniformName) {
|
||||
std::stringstream ss;
|
||||
ss << "Could not find uniform '" << uniformName << "'.";
|
||||
this->errorInfo = ss.str();
|
||||
}
|
||||
|
||||
const char* what() noexcept {
|
||||
return this->errorInfo.c_str();
|
||||
}
|
||||
private:
|
||||
std::string errorInfo;
|
||||
};
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
Shader();
|
||||
|
||||
bool Load();
|
||||
void Start() const;
|
||||
|
||||
inline GLint operator[] (std::vector<GLint>::size_type index) {
|
||||
if(index >= locations.size())
|
||||
return -1;
|
||||
|
||||
return locations[index];
|
||||
}
|
||||
|
||||
inline GLint Uniform(std::vector<GLint>::size_type index) {
|
||||
if(index >= locations.size())
|
||||
return -1;
|
||||
|
||||
return locations[index];
|
||||
}
|
||||
|
||||
void Stop() const;
|
||||
void Unload();
|
||||
protected:
|
||||
virtual void PrepareLoad() = 0;
|
||||
virtual void PrepareUnload() {};
|
||||
|
||||
void AttachSource(const std::string& fileName, GLuint shaderType);
|
||||
void LinkProgram();
|
||||
void LoadUniforms(std::vector<std::string> names);
|
||||
private:
|
||||
std::vector<GLint> locations;
|
||||
std::vector<GLuint> shaders;
|
||||
GLuint program;
|
||||
bool loaded;
|
||||
};
|
||||
}}
|
||||
|
||||
#endif
|
|
@ -1,45 +0,0 @@
|
|||
#ifndef SOSC_SHADER_TEST_H
|
||||
#define SOSC_SHADER_TEST_H
|
||||
|
||||
#include <SDL.h>
|
||||
#include <GLM/glm.hpp>
|
||||
#include <GLM/gtc/type_ptr.hpp>
|
||||
#include "common.hpp"
|
||||
#include "_shader.hpp"
|
||||
|
||||
namespace sosc {
|
||||
namespace shdr {
|
||||
class TestShader : public Shader {
|
||||
public:
|
||||
enum Uniforms {
|
||||
ORTHO_MATRIX = 0,
|
||||
GRAPHIC_SAMPLER
|
||||
};
|
||||
|
||||
void UpdateWindow(SDL_Window* window) {
|
||||
this->Start();
|
||||
|
||||
int width, height;
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
glm::mat4 orthoMatrix = glm::ortho(0, width, height, 0);
|
||||
glUniformMatrix4fv(
|
||||
(*this)[ORTHO_MATRIX], 1, GL_FALSE, glm::value_ptr(orthoMatrix)
|
||||
);
|
||||
|
||||
this->Stop();
|
||||
}
|
||||
protected:
|
||||
void PrepareLoad() override {
|
||||
this->AttachSource(SOSC_RESC("shaders/test.vert"), GL_VERTEX_SHADER);
|
||||
this->AttachSource(SOSC_RESC("shaders/test.frag"), GL_VERTEX_SHADER);
|
||||
|
||||
this->LinkProgram();
|
||||
this->LoadUniforms({
|
||||
"orthoMatrix",
|
||||
"graphicSampler"
|
||||
});
|
||||
}
|
||||
};
|
||||
}}
|
||||
|
||||
#endif
|
|
@ -1,413 +0,0 @@
|
|||
#include "font.hpp"
|
||||
|
||||
#define LILEND_UNPACK(X,N) \
|
||||
(((uint32_t)(X)[N]) | \
|
||||
((((uint32_t)(X)[(N)+1])) << 8u) | \
|
||||
((((uint32_t)(X)[(N)+2])) << 16u) | \
|
||||
((((uint32_t)(X)[(N)+3])) << 24u))
|
||||
|
||||
namespace sosc {
|
||||
namespace ui {
|
||||
class FontShader;
|
||||
}}
|
||||
|
||||
// STATE STRUCT //
|
||||
|
||||
static struct {
|
||||
sosc::ui::FontShader* shader;
|
||||
sosc::ui::Font* default_font;
|
||||
} _font_ctx;
|
||||
|
||||
// FONT SHADER CLASS //
|
||||
|
||||
namespace sosc {
|
||||
namespace ui {
|
||||
class FontShader : public sosc::shdr::Shader {
|
||||
public:
|
||||
enum Uniforms {
|
||||
ORTHO_MATRIX = 0,
|
||||
TRANSLATION_MATRIX,
|
||||
FONT_BITMAP,
|
||||
FONT_COLOR
|
||||
};
|
||||
|
||||
void UpdateWindow(SDL_Window *window) {
|
||||
this->Start();
|
||||
|
||||
int width, height;
|
||||
SDL_GetWindowSize(window, &width, &height);
|
||||
glm::mat4 orthoMatrix =
|
||||
glm::ortho(0.f, (float)width, (float)height, 0.f);
|
||||
glUniformMatrix4fv(
|
||||
(*this)[ORTHO_MATRIX], 1, GL_FALSE,
|
||||
glm::value_ptr(orthoMatrix)
|
||||
);
|
||||
|
||||
this->Stop();
|
||||
}
|
||||
|
||||
protected:
|
||||
void PrepareLoad() override {
|
||||
this->AttachSource(
|
||||
SOSC_RESC("shaders/font/font.vert"), GL_VERTEX_SHADER
|
||||
);
|
||||
this->AttachSource(
|
||||
SOSC_RESC("shaders/font/font.frag"), GL_FRAGMENT_SHADER
|
||||
);
|
||||
|
||||
this->LinkProgram();
|
||||
this->LoadUniforms({
|
||||
"orthoMatrix",
|
||||
"transMatrix",
|
||||
"fontBitmap",
|
||||
"fontColor"
|
||||
});
|
||||
}
|
||||
};
|
||||
}}
|
||||
|
||||
// SUBSYSTEM FUNCS //
|
||||
|
||||
void sosc::ui::font_init_subsystem(SDL_Window* window) {
|
||||
_font_ctx.shader = new FontShader();
|
||||
_font_ctx.shader->Load();
|
||||
_font_ctx.shader->UpdateWindow(window);
|
||||
_font_ctx.default_font = nullptr;
|
||||
}
|
||||
|
||||
void sosc::ui::font_set_default(Font *font) {
|
||||
_font_ctx.default_font = font;
|
||||
}
|
||||
|
||||
void sosc::ui::font_window_changed(SDL_Window* window) {
|
||||
_font_ctx.shader->UpdateWindow(window);
|
||||
}
|
||||
|
||||
void sosc::ui::font_deinit_subsystem() {
|
||||
_font_ctx.shader->Unload();
|
||||
delete _font_ctx.shader;
|
||||
}
|
||||
|
||||
// FONT CLASS //
|
||||
|
||||
sosc::ui::Font::Font
|
||||
(const std::string& bitmapPath, const std::string& dataPath,
|
||||
bool useNearest)
|
||||
{
|
||||
this->loaded = false;
|
||||
if(!this->Load(bitmapPath, dataPath))
|
||||
throw FontException(bitmapPath, dataPath);
|
||||
}
|
||||
|
||||
bool sosc::ui::Font::Load
|
||||
(const std::string& bitmapPath, const std::string& dataPath,
|
||||
bool useNearest)
|
||||
{
|
||||
if(this->loaded)
|
||||
this->Unload();
|
||||
|
||||
SDL_RWops* rwop = SDL_RWFromFile(bitmapPath.c_str(), "rb");
|
||||
SDL_Surface* image = SDL_LoadBMP_RW(rwop, 1);
|
||||
if(!image)
|
||||
return false;
|
||||
|
||||
char buffer[0x111];
|
||||
std::ifstream dataFile(dataPath, std::ios::in | std::ios::binary);
|
||||
if(!dataFile.is_open())
|
||||
return false;
|
||||
dataFile.read(buffer, 0x111);
|
||||
dataFile.close();
|
||||
std::string data(buffer, 0x111);
|
||||
if(buffer[0x10] != 0)
|
||||
return false;
|
||||
|
||||
this->width = (uint32_t)image->w;
|
||||
this->height = (uint32_t)image->h;
|
||||
|
||||
this->cell_width = LILEND_UNPACK(buffer, 0x08);
|
||||
this->cell_height = LILEND_UNPACK(buffer, 0x0C);
|
||||
|
||||
for(int i = 0; i < 256; ++i) {
|
||||
auto glyph = &this->glyphs[i];
|
||||
auto width = (uint8_t)buffer[0x11 + i];
|
||||
|
||||
glyph->width = (double)width / (double)this->cell_width;
|
||||
|
||||
int x = (this->cell_width * i) % this->width;
|
||||
int y = ((this->cell_width * i) / this->width) * this->cell_height;
|
||||
|
||||
glyph->top_left = glm::vec2(
|
||||
(double)x / (double)this->width,
|
||||
1 - (double)y / (double)this->height
|
||||
);
|
||||
glyph->top_right = glm::vec2(
|
||||
(double)(x + width) / (double)this->width,
|
||||
1 - (double)y / (double)this->height
|
||||
);
|
||||
glyph->bottom_left = glm::vec2(
|
||||
(double)x / (double)this->width,
|
||||
1 - (double)(y + this->cell_height) / (double)this->height
|
||||
);
|
||||
glyph->bottom_right = glm::vec2(
|
||||
(double)(x + width) / (double)this->width,
|
||||
1 - (double)(y + this->cell_height) / (double)this->height
|
||||
);
|
||||
}
|
||||
|
||||
glGenTextures(1, &this->texture);
|
||||
glBindTexture(GL_TEXTURE_2D, this->texture);
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, 3,
|
||||
this->width, this->height, 0,
|
||||
GL_BGR,
|
||||
GL_UNSIGNED_BYTE, image->pixels
|
||||
);
|
||||
|
||||
glTexParameteri(
|
||||
GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||
useNearest ? GL_NEAREST : GL_LINEAR
|
||||
);
|
||||
glTexParameteri(
|
||||
GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
||||
useNearest ? GL_NEAREST : GL_LINEAR
|
||||
);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
SDL_FreeSurface(image);
|
||||
this->loaded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void sosc::ui::Font::BindBitmap() const {
|
||||
if(!this->loaded)
|
||||
return;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, this->texture);
|
||||
}
|
||||
|
||||
void sosc::ui::Font::UnbindBitmap() const {
|
||||
if(!this->loaded)
|
||||
return;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void sosc::ui::Font::Unload() {
|
||||
glDeleteTextures(1, &this->texture);
|
||||
this->loaded = false;
|
||||
}
|
||||
|
||||
// TEXT CLASS //
|
||||
|
||||
sosc::ui::Text::Text() {
|
||||
this->font = _font_ctx.default_font;
|
||||
this->font_size = 0;
|
||||
this->vertices = nullptr;
|
||||
this->tex_coords = nullptr;
|
||||
|
||||
glGenVertexArrays(1, &this->vao);
|
||||
glGenBuffers(2, this->vbos);
|
||||
}
|
||||
|
||||
sosc::ui::Text::Text
|
||||
(sosc::ui::Font *font, uint32_t size, const glm::vec4& color) : Text()
|
||||
{
|
||||
this->font = font;
|
||||
this->font_size = size;
|
||||
this->font_color = color;
|
||||
}
|
||||
|
||||
sosc::ui::Text::Text(uint32_t size, const glm::vec4& color,
|
||||
const std::string &text, uint32_t x, uint32_t y, uint32_t w) : Text()
|
||||
{
|
||||
this->Set(size, color, text, x, y, w);
|
||||
}
|
||||
|
||||
sosc::ui::Text::Text
|
||||
(sosc::ui::Font *font, uint32_t size, const glm::vec4& color,
|
||||
const std::string &text, uint32_t x, uint32_t y, uint32_t w) : Text()
|
||||
{
|
||||
this->font = font;
|
||||
this->Set(size, color, text, x, y, w);
|
||||
}
|
||||
|
||||
void sosc::ui::Text::Set(uint32_t size, const glm::vec4& color,
|
||||
const std::string &text, uint32_t x, uint32_t y, uint32_t w)
|
||||
{
|
||||
this->font_size = size;
|
||||
this->font_color = color;
|
||||
this->text = text;
|
||||
if(w != 0)
|
||||
this->wrap_width = w;
|
||||
this->trans_matrix = glm::translate(glm::mat4(1.f), glm::vec3(x, y, 0.f));
|
||||
this->Redraw();
|
||||
}
|
||||
|
||||
void sosc::ui::Text::SetFont(Font *font, uint32_t size) {
|
||||
this->font = font;
|
||||
if(size != 0)
|
||||
this->font_size = size;
|
||||
this->Redraw();
|
||||
}
|
||||
|
||||
void sosc::ui::Text::SetFontSize(uint32_t size) {
|
||||
this->font_size = size;
|
||||
this->Redraw();
|
||||
}
|
||||
|
||||
void sosc::ui::Text::SetFontColor(const glm::vec4 &color) {
|
||||
this->font_color = color;
|
||||
}
|
||||
|
||||
void sosc::ui::Text::SetText(const std::string &text) {
|
||||
this->text = text;
|
||||
this->Redraw();
|
||||
}
|
||||
|
||||
void sosc::ui::Text::SetPosition(uint32_t x, uint32_t y) {
|
||||
this->trans_matrix = glm::translate(glm::mat4(1.f), glm::vec3(x, y, 0.f));
|
||||
}
|
||||
|
||||
void sosc::ui::Text::SetWrapWidth(uint32_t w) {
|
||||
this->wrap_width = w;
|
||||
this->Redraw();
|
||||
}
|
||||
|
||||
uint32_t sosc::ui::Text::GetHeight() const {
|
||||
return this->GetLineCount() * this->font_size;
|
||||
}
|
||||
|
||||
uint32_t sosc::ui::Text::GetLineCount() const {
|
||||
if(this->wrap_width == 0)
|
||||
return 1;
|
||||
else {
|
||||
uint32_t count = 1, topleft_x = 0;
|
||||
for(const auto c : this->text) {
|
||||
auto width = (uint32_t)((*this->font)[c].width * this->font_size);
|
||||
|
||||
if(topleft_x + width > this->wrap_width) {
|
||||
++count;
|
||||
topleft_x = 0;
|
||||
}
|
||||
|
||||
topleft_x += width;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
void sosc::ui::Text::Render() {
|
||||
auto shdr = _font_ctx.shader;
|
||||
|
||||
shdr->Start();
|
||||
glUniformMatrix4fv(
|
||||
(*shdr)[shdr->TRANSLATION_MATRIX],
|
||||
1, GL_FALSE,
|
||||
glm::value_ptr(this->trans_matrix)
|
||||
);
|
||||
glUniform4f(
|
||||
(*shdr)[shdr->FONT_COLOR],
|
||||
this->font_color.r, this->font_color.g,
|
||||
this->font_color.b, this->font_color.a
|
||||
);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
this->font->BindBitmap();
|
||||
|
||||
glBindVertexArray(this->vao);
|
||||
glDrawArrays(GL_TRIANGLES, 0, this->vertex_count);
|
||||
glBindVertexArray(0);
|
||||
|
||||
this->font->UnbindBitmap();
|
||||
shdr->Stop();
|
||||
}
|
||||
|
||||
void sosc::ui::Text::Destroy() {
|
||||
glDeleteBuffers(2, this->vbos);
|
||||
glDeleteVertexArrays(1, &this->vao);
|
||||
|
||||
delete[] this->vertices;
|
||||
delete[] this->tex_coords;
|
||||
}
|
||||
|
||||
void sosc::ui::Text::Redraw() {
|
||||
this->vertex_count = (GLuint)(6 * this->text.length());
|
||||
|
||||
delete[] this->vertices;
|
||||
this->vertices = new float[this->vertex_count * 2];
|
||||
|
||||
delete[]this->tex_coords;
|
||||
this->tex_coords = new float[this->vertex_count * 2];
|
||||
|
||||
uint32_t top_x = 0, top_y = 0;
|
||||
for(int i = 0; i < this->text.length(); ++i) {
|
||||
auto glyph = (*this->font)[this->text[i]];
|
||||
uint32_t width = (uint32_t)(this->font_size * glyph.width),
|
||||
height = this->font_size;
|
||||
|
||||
if(top_x + width > this->wrap_width && this->wrap_width != 0) {
|
||||
top_x = 0;
|
||||
top_y += height;
|
||||
}
|
||||
|
||||
/// TRIANGLE 1 ///
|
||||
// TOP LEFT
|
||||
this->vertices[i*12] = top_x;
|
||||
this->vertices[i*12 + 1] = top_y;
|
||||
this->tex_coords[i*12] = glyph.top_left.x;
|
||||
this->tex_coords[i*12 + 1] = glyph.top_left.y;
|
||||
// TOP RIGHT
|
||||
this->vertices[i*12 + 2] = top_x + width;
|
||||
this->vertices[i*12 + 3] = top_y;
|
||||
this->tex_coords[i*12 + 2] = glyph.top_right.x;
|
||||
this->tex_coords[i*12 + 3] = glyph.top_right.y;
|
||||
// BOTTOM LEFT
|
||||
this->vertices[i*12 + 4] = top_x;
|
||||
this->vertices[i*12 + 5] = top_y + height;
|
||||
this->tex_coords[i*12 + 4] = glyph.bottom_left.x;
|
||||
this->tex_coords[i*12 + 5] = glyph.bottom_left.y;
|
||||
|
||||
/// TRIANGLE 2 ///
|
||||
// BOTTOM LEFT
|
||||
this->vertices[i*12 + 6] = top_x;
|
||||
this->vertices[i*12 + 7] = top_y + height;
|
||||
this->tex_coords[i*12 + 6] = glyph.bottom_left.x;
|
||||
this->tex_coords[i*12 + 7] = glyph.bottom_left.y;
|
||||
// TOP RIGHT
|
||||
this->vertices[i*12 + 8] = top_x + width;
|
||||
this->vertices[i*12 + 9] = top_y;
|
||||
this->tex_coords[i*12 + 8] = glyph.top_right.x;
|
||||
this->tex_coords[i*12 + 9] = glyph.top_right.y;
|
||||
// BOTTOM RIGHT
|
||||
this->vertices[i*12 + 10] = top_x + width;
|
||||
this->vertices[i*12 + 11] = top_y + height;
|
||||
this->tex_coords[i*12 + 10] = glyph.bottom_right.x;
|
||||
this->tex_coords[i*12 + 11] = glyph.bottom_right.y;
|
||||
|
||||
top_x += width;
|
||||
}
|
||||
|
||||
glBindVertexArray(this->vao);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->vbos[0]);
|
||||
glBufferData(
|
||||
GL_ARRAY_BUFFER,
|
||||
this->vertex_count * 2 * sizeof(float),
|
||||
this->vertices,
|
||||
GL_STATIC_DRAW
|
||||
);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
|
||||
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this->vbos[1]);
|
||||
glBufferData(
|
||||
GL_ARRAY_BUFFER,
|
||||
this->vertex_count * 2 * sizeof(float),
|
||||
this->tex_coords,
|
||||
GL_STATIC_DRAW
|
||||
);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
|
@ -1,120 +0,0 @@
|
|||
#ifndef SOSC_UI_FONT_H
|
||||
#define SOSC_UI_FONT_H
|
||||
|
||||
#include <SDL.h>
|
||||
#include <GL/glew.h>
|
||||
#include <GLM/glm.hpp>
|
||||
#include <GLM/gtc/matrix_transform.hpp>
|
||||
#include <GLM/gtc/type_ptr.hpp>
|
||||
#include <SDL_image.h>
|
||||
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include "ui/texture.hpp"
|
||||
#include "shaders/_shader.hpp"
|
||||
|
||||
namespace sosc {
|
||||
namespace ui {
|
||||
class Font;
|
||||
void font_init_subsystem(SDL_Window* window);
|
||||
void font_set_default(sosc::ui::Font* font);
|
||||
void font_window_changed(SDL_Window* window);
|
||||
void font_deinit_subsystem();
|
||||
|
||||
class FontException : public std::exception {
|
||||
public:
|
||||
explicit FontException
|
||||
(const std::string& bitmapPath, const std::string& dataPath)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "Could not load bitmap file '" << bitmapPath << "' "
|
||||
<< "and/or data file '" << dataPath << "'.";
|
||||
|
||||
this->errorInfo = ss.str();
|
||||
}
|
||||
|
||||
const std::string& what() noexcept {
|
||||
return this->errorInfo;
|
||||
}
|
||||
private:
|
||||
std::string errorInfo;
|
||||
};
|
||||
|
||||
class Font {
|
||||
public:
|
||||
struct glyph_t {
|
||||
double width;
|
||||
glm::vec2
|
||||
top_left,
|
||||
top_right,
|
||||
bottom_left,
|
||||
bottom_right;
|
||||
};
|
||||
|
||||
Font() : loaded(false) {}
|
||||
Font(const std::string& bitmapPath,
|
||||
const std::string& dataPath, bool useNearest = true);
|
||||
bool Load(const std::string& bitmapPath,
|
||||
const std::string& dataPath, bool useNearest = true);
|
||||
|
||||
inline const glyph_t& operator[] (char c) const {
|
||||
return this->glyphs[(uint8_t)c];
|
||||
}
|
||||
|
||||
void Unload();
|
||||
private:
|
||||
void BindBitmap() const;
|
||||
void UnbindBitmap() const;
|
||||
|
||||
bool loaded;
|
||||
GLuint texture;
|
||||
uint32_t
|
||||
width, height,
|
||||
cell_width, cell_height;
|
||||
glyph_t glyphs[256];
|
||||
|
||||
friend class Text;
|
||||
};
|
||||
|
||||
class Text {
|
||||
public:
|
||||
Text();
|
||||
Text(Font* font, uint32_t size, const glm::vec4& color);
|
||||
Text(uint32_t size, const glm::vec4& color, const std::string& text,
|
||||
uint32_t x, uint32_t y, uint32_t w = 0);
|
||||
Text(Font* font, uint32_t size, const glm::vec4& color,
|
||||
const std::string& text, uint32_t x, uint32_t y, uint32_t w = 0);
|
||||
|
||||
void Set(uint32_t size, const glm::vec4& color, const std::string& text,
|
||||
uint32_t x, uint32_t y, uint32_t w = 0);
|
||||
void SetFont(Font* font, uint32_t size = 0);
|
||||
void SetFontSize(uint32_t size);
|
||||
void SetFontColor(const glm::vec4& color);
|
||||
void SetText(const std::string& text);
|
||||
void SetPosition(uint32_t x, uint32_t y);
|
||||
void SetWrapWidth(uint32_t w);
|
||||
|
||||
uint32_t GetHeight() const;
|
||||
uint32_t GetLineCount() const;
|
||||
|
||||
void Render();
|
||||
|
||||
void Destroy();
|
||||
private:
|
||||
void Redraw();
|
||||
|
||||
Font* font;
|
||||
glm::vec4 font_color;
|
||||
uint32_t font_size,
|
||||
wrap_width;
|
||||
std::string text;
|
||||
glm::mat4 trans_matrix;
|
||||
|
||||
GLsizei vertex_count;
|
||||
float* vertices;
|
||||
float* tex_coords;
|
||||
GLuint vao, vbos[2];
|
||||
};
|
||||
}}
|
||||
|
||||
#endif
|
|
@ -1,2 +0,0 @@
|
|||
#include "texture.hpp"
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
#ifndef SOSC_UI_TEXTURE_H
|
||||
#define SOSC_UI_TEXTURE_H
|
||||
|
||||
#include <SDL.h>
|
||||
#include <GL/glew.h>
|
||||
#include "utils/time.hpp"
|
||||
|
||||
#include <iterator>
|
||||
#include <forward_list>
|
||||
#include <chrono>
|
||||
|
||||
namespace sosc {
|
||||
namespace ui {
|
||||
class Texture {
|
||||
public:
|
||||
|
||||
private:
|
||||
sosc::time last_render;
|
||||
std::forward_list<GLuint> texture_ids;
|
||||
std::forward_list<GLuint>::iterator texture_id_iter;
|
||||
};
|
||||
}}
|
||||
|
||||
#endif
|
|
@ -1,2 +0,0 @@
|
|||
#include "texture_array.hpp"
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
#ifndef SOSC_UI_TEXTURE_ARR_H
|
||||
#define SOSC_UI_TEXTURE_ARR_H
|
||||
|
||||
#include <SDL.h>
|
||||
#include <GL/glew.h>
|
||||
#include "utils/time.hpp"
|
||||
|
||||
#include <iterator>
|
||||
#include <forward_list>
|
||||
#include <chrono>
|
||||
|
||||
namespace sosc {
|
||||
namespace ui {
|
||||
class TextureArray {
|
||||
public:
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
}}
|
||||
|
||||
#endif
|
|
@ -1,4 +0,0 @@
|
|||
//
|
||||
// Created by alec on 8/28/2018.
|
||||
//
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
#ifndef SOSC_VIEW_ORTHO_H
|
||||
#define SOSC_VIEW_ORTHO_H
|
||||
|
||||
#include <SDL.h>
|
||||
#include <GL/glew.h>
|
||||
#include <GLM/glm.hpp>
|
||||
#include <GLM/gtc/matrix_transform.hpp>
|
||||
|
||||
void setup_ortho_mode();
|
||||
|
||||
|
||||
#endif
|
|
@ -1 +0,0 @@
|
|||
#include "projection.hpp"
|
|
@ -1,11 +0,0 @@
|
|||
#ifndef SOSC_VIEW_PROJECTION_H
|
||||
#define SOSC_VIEW_PROJECTION_H
|
||||
|
||||
#include <SDL.h>
|
||||
#include <GL/glew.h>
|
||||
#include <GLM/glm.hpp>
|
||||
#include <GLM/gtc/matrix_transform.hpp>
|
||||
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue