my mom is cool

This commit is contained in:
malloc 2018-08-30 16:38:58 -05:00
parent 31b4c520cd
commit 71cc1e55fc
5 changed files with 139 additions and 34 deletions

View file

@ -3,6 +3,7 @@ project(sockscape)
string(COMPARE EQUAL ${CMAKE_BUILD_TYPE} Debug _CMP) string(COMPARE EQUAL ${CMAKE_BUILD_TYPE} Debug _CMP)
if(_CMP) if(_CMP)
set(CMAKE_VERBOSE_MAKEFILE ON)
add_definitions("-DSOSC_DEBUG") add_definitions("-DSOSC_DEBUG")
endif() endif()
@ -18,7 +19,6 @@ find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED) find_package(GLEW REQUIRED)
find_package(SDL2 REQUIRED) find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED) find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)
file(GLOB_RECURSE client_src file(GLOB_RECURSE client_src
"src/common/*.hpp" "src/common/*.hpp"
@ -35,14 +35,12 @@ target_include_directories(client
PRIVATE ${OPENGL_INCLUDE_DIR} PRIVATE ${OPENGL_INCLUDE_DIR}
PRIVATE ${GLEW_INCLUDE_DIR} PRIVATE ${GLEW_INCLUDE_DIR}
PRIVATE ${SDL2_INCLUDE_DIR} PRIVATE ${SDL2_INCLUDE_DIR}
PRIVATE ${SDL2_IMAGE_INCLUDE_DIR} PRIVATE ${SDL2_IMAGE_INCLUDE_DIR})
PRIVATE ${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(client target_link_libraries(client
${OPENGL_LIBRARIES} ${OPENGL_LIBRARIES}
${GLEW_LIBRARY} ${GLEW_LIBRARY}
${SDL2_LIBRARY} ${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})
${SDL2_TTF_LIBRARIES})
install(TARGETS client RUNTIME DESTINATION bin/client) install(TARGETS client RUNTIME DESTINATION bin/client)
## SERVER BUILD ## ## SERVER BUILD ##

View file

@ -1,5 +1,5 @@
#version 330 core #version 330 core
layout (location = 0) in vec4 aScreenCoords; layout (location = 0) in vec2 aScreenCoords;
layout (location = 1) in vec2 aTexCoords; layout (location = 1) in vec2 aTexCoords;
out vec2 texCoords; out vec2 texCoords;
@ -8,6 +8,6 @@ uniform mat4 transMatrix;
uniform mat4 orthoMatrix; uniform mat4 orthoMatrix;
void main() { void main() {
gl_Position = orthoMatrix * aScreenCoords; gl_Position = orthoMatrix * transMatrix * vec4(aScreenCoords, 0.0, 1.0);
texCoords = aTexCoords; texCoords = aTexCoords;
} }

View file

@ -1,7 +1,7 @@
#include <SDL.h> #include <SDL.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#define GL3_PROTOTYPES 1 #define GLEW_STATIC
#include <GL/glew.h> #include <GL/glew.h>
#include "ui/font.hpp" #include "ui/font.hpp"
@ -33,23 +33,32 @@ int main(int argc, char* argv[]) {
SDL_WINDOW_OPENGL SDL_WINDOW_OPENGL
); );
auto ctx = SDL_GL_CreateContext(window); SDL_GL_LoadLibrary(nullptr);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetSwapInterval(1); SDL_GL_SetSwapInterval(1);
auto ctx = SDL_GL_CreateContext(window);
if(ctx == nullptr)
return -1;
#ifndef __APPLE__ #ifndef __APPLE__
glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK)
glewInit(); return -1;
#endif #endif
auto a = glGetString(GL_VENDOR);
auto b = glGetString(GL_RENDERER);
auto c = glGetString(GL_VERSION);
auto d = glGetString(GL_SHADING_LANGUAGE_VERSION);
ui::font_init_subsystem(window); ui::font_init_subsystem(window);
ui::Font scapeFont( ui::Font scapeFont(
SOSC_RESC("fonts/scape.bmp"), SOSC_RESC("fonts/scape.bmp"),
@ -57,11 +66,13 @@ int main(int argc, char* argv[]) {
); );
ui::font_set_default(&scapeFont); ui::font_set_default(&scapeFont);
ui::Text text(32, glm::vec4(1, 0, 0, 1), "test text", 100, 100);
bool running = true; bool running = true;
while(running) { while(running) {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
text.Render();
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);

View file

@ -10,7 +10,7 @@
namespace sosc { namespace sosc {
namespace ui { namespace ui {
static class FontShader : public sosc::shdr::Shader { class FontShader : public sosc::shdr::Shader {
public: public:
enum Uniforms { enum Uniforms {
ORTHO_MATRIX = 0, ORTHO_MATRIX = 0,
@ -61,7 +61,7 @@ static struct {
// SUBSYSTEM FUNCS // // SUBSYSTEM FUNCS //
bool sosc::ui::font_init_subsystem(SDL_Window* window) { void sosc::ui::font_init_subsystem(SDL_Window* window) {
_font_ctx.shader.Load(); _font_ctx.shader.Load();
_font_ctx.shader.UpdateWindow(window); _font_ctx.shader.UpdateWindow(window);
_font_ctx.default_font = nullptr; _font_ctx.default_font = nullptr;
@ -153,6 +153,7 @@ bool sosc::ui::Font::Load
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
this->loaded = true; this->loaded = true;
return true;
} }
void sosc::ui::Font::BindBitmap() { void sosc::ui::Font::BindBitmap() {
@ -183,33 +184,36 @@ sosc::ui::Text::Text() {
this->font_size = 0; this->font_size = 0;
glGenVertexArrays(1, &this->vao); glGenVertexArrays(1, &this->vao);
glBindVertexArray(this->vao);
glGenBuffers(2, this->vbos); glGenBuffers(2, this->vbos);
glBindVertexArray(0);
} }
sosc::ui::Text::Text(sosc::ui::Font *font, uint32_t size) : Text() { sosc::ui::Text::Text
(sosc::ui::Font *font, uint32_t size, const glm::vec4& color) : Text()
{
this->font = font; this->font = font;
this->font_size = size; this->font_size = size;
this->font_color = color;
} }
sosc::ui::Text::Text(uint32_t size, const std::string &text, sosc::ui::Text::Text(uint32_t size, const glm::vec4& color,
uint32_t x, uint32_t y, uint32_t w) : Text() const std::string &text, uint32_t x, uint32_t y, uint32_t w) : Text()
{ {
this->Set(size, text, x, y, w); this->Set(size, color, text, x, y, w);
} }
sosc::ui::Text::Text(sosc::ui::Font *font, uint32_t size, 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() const std::string &text, uint32_t x, uint32_t y, uint32_t w) : Text()
{ {
this->font = font; this->font = font;
this->Set(size, text, x, y, w); this->Set(size, color, text, x, y, w);
} }
void sosc::ui::Text::Set void sosc::ui::Text::Set(uint32_t size, const glm::vec4& color,
(uint32_t size, const std::string &text, uint32_t x, uint32_t y, uint32_t w) const std::string &text, uint32_t x, uint32_t y, uint32_t w)
{ {
this->font_size = size; this->font_size = size;
this->font_color = color;
this->text = text; this->text = text;
if(w != 0) if(w != 0)
this->wrap_width = w; this->wrap_width = w;
@ -229,6 +233,10 @@ void sosc::ui::Text::SetFontSize(uint32_t size) {
this->Redraw(); this->Redraw();
} }
void sosc::ui::Text::SetFontColor(const glm::vec4 &color) {
this->font_color = color;
}
void sosc::ui::Text::SetText(const std::string &text) { void sosc::ui::Text::SetText(const std::string &text) {
this->text = text; this->text = text;
this->Redraw(); this->Redraw();
@ -244,9 +252,29 @@ void sosc::ui::Text::SetWrapWidth(uint32_t w) {
} }
void sosc::ui::Text::Render() { void sosc::ui::Text::Render() {
auto shdr = &_font_ctx.shader;
_font_ctx.shader.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); glBindVertexArray(this->vao);
glDrawArrays(GL_TRIANGLES, 0, this->vertex_count); glDrawArrays(GL_TRIANGLES, 0, this->vertex_count);
glBindVertexArray(0); glBindVertexArray(0);
this->font->UnbindBitmap();
_font_ctx.shader.Stop();
} }
void sosc::ui::Text::Destroy() { void sosc::ui::Text::Destroy() {
@ -255,13 +283,79 @@ void sosc::ui::Text::Destroy() {
} }
void sosc::ui::Text::Redraw() { void sosc::ui::Text::Redraw() {
this->vertex_count = ; this->vertex_count = (GLuint)(6 * this->text.length());
auto vertices = new float[this->vertex_count * 2];
auto tex_coords = new float[this->vertex_count * 2];
uint32_t line_width = 0, top_x = 0, top_y = 0; uint32_t line_width = 0, top_x = 0, top_y = 0;
for(const auto c : this->text) { for(int i = 0; i < this->text.length(); ++i) {
auto glyph = (*this->font)[c]; auto glyph = (*this->font)[this->text[i]];
uint32_t width = (uint32_t)(this->font_size * glyph.width), uint32_t width = (uint32_t)(this->font_size * glyph.width),
height = this->font_size; 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
vertices[i*12] = top_x;
vertices[i*12 + 1] = top_y;
tex_coords[i*12] = glyph.top_left.x;
tex_coords[i*12 + 1] = glyph.top_left.y;
// TOP RIGHT
vertices[i*12 + 2] = top_x + width;
vertices[i*12 + 3] = top_y;
tex_coords[i*12 + 2] = glyph.top_right.x;
tex_coords[i*12 + 3] = glyph.top_right.y;
// BOTTOM LEFT
vertices[i*12 + 4] = top_x;
vertices[i*12 + 5] = top_y + height;
tex_coords[i*12 + 4] = glyph.bottom_left.x;
tex_coords[i*12 + 5] = glyph.bottom_left.y;
/// TRIANGLE 2 ///
// BOTTOM LEFT
vertices[i*12 + 6] = top_x;
vertices[i*12 + 7] = top_y + height;
tex_coords[i*12 + 6] = glyph.bottom_left.x;
tex_coords[i*12 + 7] = glyph.bottom_left.y;
// TOP RIGHT
vertices[i*12 + 8] = top_x + width;
vertices[i*12 + 9] = top_y;
tex_coords[i*12 + 8] = glyph.top_right.x;
tex_coords[i*12 + 9] = glyph.top_right.y;
// BOTTOM RIGHT
vertices[i*12 + 10] = top_x + width;
vertices[i*12 + 11] = top_y + height;
tex_coords[i*12 + 10] = glyph.bottom_right.x;
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),
vertices,
GL_STATIC_DRAW
);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, this->vbos[1]);
glBufferData(
GL_ARRAY_BUFFER,
this->vertex_count * 2 * sizeof(float),
vertices,
GL_STATIC_DRAW
);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
}
glBindVertexArray(0);
} }

View file

@ -16,7 +16,7 @@
namespace sosc { namespace sosc {
namespace ui { namespace ui {
class Font; class Font;
bool font_init_subsystem(SDL_Window* window); void font_init_subsystem(SDL_Window* window);
void font_set_default(sosc::ui::Font* font); void font_set_default(sosc::ui::Font* font);
void font_window_changed(SDL_Window* window); void font_window_changed(SDL_Window* window);
void font_deinit_subsystem(); void font_deinit_subsystem();
@ -78,16 +78,17 @@ private:
class Text { class Text {
public: public:
Text(); Text();
Text(Font* font, uint32_t size); Text(Font* font, uint32_t size, const glm::vec4& color);
Text(uint32_t size, const std::string& text, 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 std::string& text,
uint32_t x, uint32_t y, uint32_t w = 0); 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 std::string& text, void Set(uint32_t size, const glm::vec4& color, const std::string& text,
uint32_t x, uint32_t y, uint32_t w = 0); uint32_t x, uint32_t y, uint32_t w = 0);
void SetFont(Font* font, uint32_t size = 0); void SetFont(Font* font, uint32_t size = 0);
void SetFontSize(uint32_t size); void SetFontSize(uint32_t size);
void SetFontColor(const glm::vec4& color);
void SetText(const std::string& text); void SetText(const std::string& text);
void SetPosition(uint32_t x, uint32_t y); void SetPosition(uint32_t x, uint32_t y);
void SetWrapWidth(uint32_t w); void SetWrapWidth(uint32_t w);
@ -99,6 +100,7 @@ private:
void Redraw(); void Redraw();
Font* font; Font* font;
glm::vec4 font_color;
uint32_t font_size, uint32_t font_size,
wrap_width; wrap_width;
std::string text; std::string text;