textured boob

This commit is contained in:
malloc 2018-08-28 16:45:49 -05:00
parent d29f25bc86
commit 699c9aa4ad
8 changed files with 165 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 MiB

Binary file not shown.

79
src/client/ui/text.cpp Normal file
View file

@ -0,0 +1,79 @@
#include "text.hpp"
// FONT CLASS //
bool sosc::ui::Font::Load
(const std::string& filePath, const std::string& dataPath)
{
if(this->loaded)
this->Unload();
SDL_RWops* rwop = SDL_RWFromFile(filePath.c_str(), "rb");
this->image = SDL_LoadBMP_RW(rwop, 1);
if(!this->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)this->image->w;
this->height = (uint32_t)this->image->h;
this->cell_width = sosc::net::ntohv<uint32_t>(data, 0x08);
this->cell_height = sosc::net::ntohv<uint32_t>(data, 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, GL_RGBA,
this->width, this->height, 0,
(this->image->format->BytesPerPixel == 4 ? GL_RGBA : GL_RGB),
GL_UNSIGNED_BYTE, this->image->pixels
);
glBindTexture(GL_TEXTURE_2D, 0);
this->loaded = true;
}
void sosc::ui::Font::Unload() {
glDeleteTextures(1, &this->texture);
SDL_FreeSurface(this->image);
this->loaded = false;
}
// TEXT CLASS //

58
src/client/ui/text.hpp Normal file
View file

@ -0,0 +1,58 @@
#ifndef SOSC_UI_TEXT_H
#define SOSC_UI_TEXT_H
#include <SDL.h>
#include <GL/glew.h>
#include <GLM/glm.hpp>
#include <SDL_image.h>
#include <string>
#include <fstream>
#include "utils/net.hpp"
namespace sosc {
namespace ui {
class Font {
public:
struct glyph_t {
double width;
glm::vec2
top_left,
top_right,
bottom_left,
bottom_right;
};
Font() : loaded(false) {}
bool Load(const std::string& bitmapPath, const std::string& dataPath);
inline const glyph_t& operator[] (char c) {
return this->glyphs[(uint8_t)c];
}
void Unload();
private:
bool loaded;
GLuint texture;
SDL_Surface* image;
uint32_t
width, height,
cell_width, cell_height;
glyph_t glyphs[256];
};
class Text {
public:
Text(Font* font) : loaded(false) {}
private:
Font* font;
GLuint vao, vbo[2];
bool loaded;
};
}}
#endif

View file

@ -0,0 +1,4 @@
//
// Created by alec on 8/28/2018.
//

View file

@ -0,0 +1,12 @@
#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

View file

@ -0,0 +1 @@
#include "projection.hpp"

View file

@ -0,0 +1,11 @@
#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