remove "namespace jes" wrapper around C++ content
This commit is contained in:
parent
b6c5e9e1d7
commit
c3b4c33304
@ -7,99 +7,96 @@
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace jes
|
||||
FileLoader::FileLoader()
|
||||
{
|
||||
FileLoader::FileLoader()
|
||||
m_buf = NULL;
|
||||
m_line_endings = LINE_ENDING_COUNT;
|
||||
m_lines = NULL;
|
||||
}
|
||||
|
||||
FileLoader::~FileLoader()
|
||||
{
|
||||
if (m_buf != NULL)
|
||||
{
|
||||
delete[] m_buf;
|
||||
m_buf = NULL;
|
||||
m_line_endings = LINE_ENDING_COUNT;
|
||||
m_lines = NULL;
|
||||
}
|
||||
|
||||
FileLoader::~FileLoader()
|
||||
{
|
||||
if (m_buf != NULL)
|
||||
{
|
||||
delete[] m_buf;
|
||||
m_buf = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileLoader::load(const char * fname)
|
||||
{
|
||||
size_t size;
|
||||
|
||||
if (!FileReader::load(fname, &m_buf, &size))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
load_buf(size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileLoader::load_buf(size_t size)
|
||||
{
|
||||
LineIndexPairVectorRef lines[LINE_ENDING_COUNT];
|
||||
size_t line_start[LINE_ENDING_COUNT] = {0};
|
||||
unsigned int n_cr = 0;
|
||||
unsigned int n_lf = 0;
|
||||
bool crlf = true;
|
||||
for (size_t i = 0; i < LINE_ENDING_COUNT; i++)
|
||||
{
|
||||
lines[i] = new LineIndexPairVector();
|
||||
}
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
if (m_buf[i] == '\r')
|
||||
{
|
||||
lines[LINE_ENDING_CR]->push_back(LineIndexPair(&m_buf[line_start[LINE_ENDING_CR]], i - line_start[LINE_ENDING_CR]));
|
||||
n_cr++;
|
||||
line_start[LINE_ENDING_CR] = i + 1;
|
||||
if (crlf)
|
||||
{
|
||||
if ((i < (size - 1)) && (m_buf[i + 1] == '\n'))
|
||||
{
|
||||
lines[LINE_ENDING_CRLF]->push_back(LineIndexPair(&m_buf[line_start[LINE_ENDING_CRLF]], i - line_start[LINE_ENDING_CRLF]));
|
||||
n_lf++;
|
||||
i++;
|
||||
line_start[LINE_ENDING_CRLF] = i + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
crlf = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (m_buf[i] == '\n')
|
||||
{
|
||||
lines[LINE_ENDING_LF]->push_back(LineIndexPair(&m_buf[line_start[LINE_ENDING_LF]], i - line_start[LINE_ENDING_LF]));
|
||||
crlf = false;
|
||||
n_lf++;
|
||||
line_start[LINE_ENDING_LF] = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (crlf && (n_lf > 0u))
|
||||
{
|
||||
m_line_endings = LINE_ENDING_CRLF;
|
||||
m_lines = lines[LINE_ENDING_CRLF];
|
||||
}
|
||||
else if ((n_cr > 0u) && (n_lf == 0u))
|
||||
{
|
||||
m_line_endings = LINE_ENDING_CR;
|
||||
m_lines = lines[LINE_ENDING_CR];
|
||||
}
|
||||
else
|
||||
{
|
||||
m_line_endings = LINE_ENDING_LF;
|
||||
m_lines = lines[LINE_ENDING_LF];
|
||||
}
|
||||
}
|
||||
|
||||
TextRef FileLoader::get_line(unsigned int line_no)
|
||||
{
|
||||
return new Text((*m_lines)[line_no].first, (*m_lines)[line_no].second);
|
||||
}
|
||||
}
|
||||
|
||||
bool FileLoader::load(const char * fname)
|
||||
{
|
||||
size_t size;
|
||||
|
||||
if (!FileReader::load(fname, &m_buf, &size))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
load_buf(size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileLoader::load_buf(size_t size)
|
||||
{
|
||||
LineIndexPairVectorRef lines[LINE_ENDING_COUNT];
|
||||
size_t line_start[LINE_ENDING_COUNT] = {0};
|
||||
unsigned int n_cr = 0;
|
||||
unsigned int n_lf = 0;
|
||||
bool crlf = true;
|
||||
for (size_t i = 0; i < LINE_ENDING_COUNT; i++)
|
||||
{
|
||||
lines[i] = new LineIndexPairVector();
|
||||
}
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
if (m_buf[i] == '\r')
|
||||
{
|
||||
lines[LINE_ENDING_CR]->push_back(LineIndexPair(&m_buf[line_start[LINE_ENDING_CR]], i - line_start[LINE_ENDING_CR]));
|
||||
n_cr++;
|
||||
line_start[LINE_ENDING_CR] = i + 1;
|
||||
if (crlf)
|
||||
{
|
||||
if ((i < (size - 1)) && (m_buf[i + 1] == '\n'))
|
||||
{
|
||||
lines[LINE_ENDING_CRLF]->push_back(LineIndexPair(&m_buf[line_start[LINE_ENDING_CRLF]], i - line_start[LINE_ENDING_CRLF]));
|
||||
n_lf++;
|
||||
i++;
|
||||
line_start[LINE_ENDING_CRLF] = i + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
crlf = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (m_buf[i] == '\n')
|
||||
{
|
||||
lines[LINE_ENDING_LF]->push_back(LineIndexPair(&m_buf[line_start[LINE_ENDING_LF]], i - line_start[LINE_ENDING_LF]));
|
||||
crlf = false;
|
||||
n_lf++;
|
||||
line_start[LINE_ENDING_LF] = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (crlf && (n_lf > 0u))
|
||||
{
|
||||
m_line_endings = LINE_ENDING_CRLF;
|
||||
m_lines = lines[LINE_ENDING_CRLF];
|
||||
}
|
||||
else if ((n_cr > 0u) && (n_lf == 0u))
|
||||
{
|
||||
m_line_endings = LINE_ENDING_CR;
|
||||
m_lines = lines[LINE_ENDING_CR];
|
||||
}
|
||||
else
|
||||
{
|
||||
m_line_endings = LINE_ENDING_LF;
|
||||
m_lines = lines[LINE_ENDING_LF];
|
||||
}
|
||||
}
|
||||
|
||||
TextRef FileLoader::get_line(unsigned int line_no)
|
||||
{
|
||||
return new Text((*m_lines)[line_no].first, (*m_lines)[line_no].second);
|
||||
}
|
||||
|
@ -6,45 +6,42 @@
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
|
||||
namespace jes
|
||||
class FileLoader
|
||||
{
|
||||
class FileLoader
|
||||
public:
|
||||
enum
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
LINE_ENDING_LF,
|
||||
LINE_ENDING_CR,
|
||||
LINE_ENDING_CRLF,
|
||||
LINE_ENDING_COUNT
|
||||
};
|
||||
|
||||
FileLoader();
|
||||
~FileLoader();
|
||||
bool load(const char * fname);
|
||||
unsigned int num_lines()
|
||||
{
|
||||
if (m_lines == NULL)
|
||||
{
|
||||
return 0u;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_lines->size();
|
||||
}
|
||||
}
|
||||
TextRef get_line(unsigned int line_no);
|
||||
int get_line_endings() { return m_line_endings; }
|
||||
protected:
|
||||
typedef std::pair<const uint8_t *, size_t> LineIndexPair;
|
||||
typedef std::vector<LineIndexPair> LineIndexPairVector;
|
||||
typedef Ref<LineIndexPairVector> LineIndexPairVectorRef;
|
||||
void load_buf(size_t size);
|
||||
uint8_t * m_buf;
|
||||
int m_line_endings;
|
||||
LineIndexPairVectorRef m_lines;
|
||||
LINE_ENDING_LF,
|
||||
LINE_ENDING_CR,
|
||||
LINE_ENDING_CRLF,
|
||||
LINE_ENDING_COUNT
|
||||
};
|
||||
typedef Ref<FileLoader> FileLoaderRef;
|
||||
}
|
||||
|
||||
FileLoader();
|
||||
~FileLoader();
|
||||
bool load(const char * fname);
|
||||
unsigned int num_lines()
|
||||
{
|
||||
if (m_lines == NULL)
|
||||
{
|
||||
return 0u;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_lines->size();
|
||||
}
|
||||
}
|
||||
TextRef get_line(unsigned int line_no);
|
||||
int get_line_endings() { return m_line_endings; }
|
||||
protected:
|
||||
typedef std::pair<const uint8_t *, size_t> LineIndexPair;
|
||||
typedef std::vector<LineIndexPair> LineIndexPairVector;
|
||||
typedef Ref<LineIndexPairVector> LineIndexPairVectorRef;
|
||||
void load_buf(size_t size);
|
||||
uint8_t * m_buf;
|
||||
int m_line_endings;
|
||||
LineIndexPairVectorRef m_lines;
|
||||
};
|
||||
typedef Ref<FileLoader> FileLoaderRef;
|
||||
|
||||
#endif
|
||||
|
@ -10,55 +10,52 @@
|
||||
#define JES_O_BINARY 0
|
||||
#endif
|
||||
|
||||
namespace jes
|
||||
bool FileReader::load(const char * fname, uint8_t ** buf, size_t * size)
|
||||
{
|
||||
bool FileReader::load(const char * fname, uint8_t ** buf, size_t * size)
|
||||
struct stat st;
|
||||
if (stat(fname, &st) != 0)
|
||||
{
|
||||
struct stat st;
|
||||
if (stat(fname, &st) != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (st.st_size < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
*size = st.st_size;
|
||||
if (st.st_size == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
int fd = open(fname, O_RDONLY | JES_O_BINARY);
|
||||
if (fd < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
*buf = new uint8_t[st.st_size];
|
||||
|
||||
off_t n_bytes_read = 0u;
|
||||
for (;;)
|
||||
{
|
||||
off_t rd_size = read(fd, &(*buf)[n_bytes_read], st.st_size - n_bytes_read);
|
||||
if (rd_size <= 0)
|
||||
break;
|
||||
n_bytes_read += rd_size;
|
||||
if (n_bytes_read >= st.st_size)
|
||||
break;
|
||||
}
|
||||
if (n_bytes_read != st.st_size)
|
||||
{
|
||||
delete[] *buf;
|
||||
*buf = NULL;
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
if (st.st_size < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
*size = st.st_size;
|
||||
if (st.st_size == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
int fd = open(fname, O_RDONLY | JES_O_BINARY);
|
||||
if (fd < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
*buf = new uint8_t[st.st_size];
|
||||
|
||||
off_t n_bytes_read = 0u;
|
||||
for (;;)
|
||||
{
|
||||
off_t rd_size = read(fd, &(*buf)[n_bytes_read], st.st_size - n_bytes_read);
|
||||
if (rd_size <= 0)
|
||||
break;
|
||||
n_bytes_read += rd_size;
|
||||
if (n_bytes_read >= st.st_size)
|
||||
break;
|
||||
}
|
||||
if (n_bytes_read != st.st_size)
|
||||
{
|
||||
delete[] *buf;
|
||||
*buf = NULL;
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -4,13 +4,10 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace jes
|
||||
class FileReader
|
||||
{
|
||||
class FileReader
|
||||
{
|
||||
public:
|
||||
static bool load(const char * fname, uint8_t ** buf, size_t * size);
|
||||
};
|
||||
}
|
||||
public:
|
||||
static bool load(const char * fname, uint8_t ** buf, size_t * size);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
201
src/Font.cc
201
src/Font.cc
@ -4,109 +4,106 @@
|
||||
|
||||
#define round_up_26_6(val) (((val) + 63) >> 6u)
|
||||
|
||||
namespace jes
|
||||
Font::Font()
|
||||
{
|
||||
Font::Font()
|
||||
m_loaded = false;
|
||||
}
|
||||
|
||||
Font::~Font()
|
||||
{
|
||||
if (m_loaded)
|
||||
{
|
||||
m_loaded = false;
|
||||
}
|
||||
|
||||
Font::~Font()
|
||||
{
|
||||
if (m_loaded)
|
||||
{
|
||||
FT_Done_Face(m_face);
|
||||
}
|
||||
}
|
||||
|
||||
bool Font::load(FT_Library ft, const char * fname, size_t size)
|
||||
{
|
||||
if (FT_New_Face(ft, fname, 0, &m_face) != 0)
|
||||
{
|
||||
std::cerr << "Could not load font " << fname << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
FT_Set_Pixel_Sizes(m_face, 0, size);
|
||||
|
||||
GlyphRef glyph = load_glyph('g');
|
||||
if (glyph == NULL)
|
||||
{
|
||||
std::cerr << "Could not load 'g' glyph from font" << fname << std::endl;
|
||||
return false;
|
||||
}
|
||||
m_glyphs['g'] = glyph;
|
||||
|
||||
m_advance = glyph->get_advance();
|
||||
m_line_height = round_up_26_6(m_face->size->metrics.height);
|
||||
#if 0
|
||||
m_baseline_offset = round_up_26_6((m_face->size->metrics.height - (m_face->size->metrics.ascender - m_face->size->metrics.descender)) / 2 - m_face->size->metrics.descender);
|
||||
#endif
|
||||
m_baseline_offset = 1 - m_face->glyph->bitmap_top + m_face->glyph->bitmap.rows;
|
||||
|
||||
m_loaded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
Font::GlyphRef Font::load_glyph(FT_ULong char_code)
|
||||
{
|
||||
GlyphRef glyph = new Glyph();
|
||||
if (glyph->load(m_face, char_code))
|
||||
return glyph;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Font::Glyph::Glyph()
|
||||
{
|
||||
m_loaded = false;
|
||||
}
|
||||
|
||||
bool Font::Glyph::load(FT_Face face, FT_ULong char_code)
|
||||
{
|
||||
if (FT_Load_Char(face, char_code, FT_LOAD_RENDER) != 0)
|
||||
return false;
|
||||
|
||||
int width = face->glyph->bitmap.width;
|
||||
int height = face->glyph->bitmap.rows;
|
||||
int left = face->glyph->bitmap_left;
|
||||
int top = face->glyph->bitmap_top;
|
||||
m_advance = round_up_26_6(face->glyph->advance.x);
|
||||
uint8_t * texture = new uint8_t[width * height];
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
memcpy(&texture[width * i],
|
||||
&face->glyph->bitmap.buffer[width * (height - i - 1)],
|
||||
width);
|
||||
}
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_texture = new GLTexture();
|
||||
m_texture->create(width, height);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_ALPHA, GL_UNSIGNED_BYTE, texture);
|
||||
delete[] texture;
|
||||
|
||||
float s_max = width / (float)m_texture->get_width();
|
||||
float t_max = height / (float)m_texture->get_height();
|
||||
GLfloat box[4][4] = {
|
||||
{(GLfloat)left, (GLfloat)(top - height),
|
||||
0.0, 0.0},
|
||||
{(GLfloat)(left + width), (GLfloat)(top - height),
|
||||
s_max, 0.0},
|
||||
{(GLfloat)left, (GLfloat)top,
|
||||
0.0, t_max},
|
||||
{(GLfloat)(left + width), (GLfloat)top,
|
||||
s_max, t_max},
|
||||
};
|
||||
glGenBuffers(1, &m_vbo_id);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(box), box, GL_STATIC_DRAW);
|
||||
|
||||
m_loaded = true;
|
||||
return true;
|
||||
FT_Done_Face(m_face);
|
||||
}
|
||||
}
|
||||
|
||||
bool Font::load(FT_Library ft, const char * fname, size_t size)
|
||||
{
|
||||
if (FT_New_Face(ft, fname, 0, &m_face) != 0)
|
||||
{
|
||||
std::cerr << "Could not load font " << fname << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
FT_Set_Pixel_Sizes(m_face, 0, size);
|
||||
|
||||
GlyphRef glyph = load_glyph('g');
|
||||
if (glyph == NULL)
|
||||
{
|
||||
std::cerr << "Could not load 'g' glyph from font" << fname << std::endl;
|
||||
return false;
|
||||
}
|
||||
m_glyphs['g'] = glyph;
|
||||
|
||||
m_advance = glyph->get_advance();
|
||||
m_line_height = round_up_26_6(m_face->size->metrics.height);
|
||||
#if 0
|
||||
m_baseline_offset = round_up_26_6((m_face->size->metrics.height - (m_face->size->metrics.ascender - m_face->size->metrics.descender)) / 2 - m_face->size->metrics.descender);
|
||||
#endif
|
||||
m_baseline_offset = 1 - m_face->glyph->bitmap_top + m_face->glyph->bitmap.rows;
|
||||
|
||||
m_loaded = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
Font::GlyphRef Font::load_glyph(FT_ULong char_code)
|
||||
{
|
||||
GlyphRef glyph = new Glyph();
|
||||
if (glyph->load(m_face, char_code))
|
||||
return glyph;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Font::Glyph::Glyph()
|
||||
{
|
||||
m_loaded = false;
|
||||
}
|
||||
|
||||
bool Font::Glyph::load(FT_Face face, FT_ULong char_code)
|
||||
{
|
||||
if (FT_Load_Char(face, char_code, FT_LOAD_RENDER) != 0)
|
||||
return false;
|
||||
|
||||
int width = face->glyph->bitmap.width;
|
||||
int height = face->glyph->bitmap.rows;
|
||||
int left = face->glyph->bitmap_left;
|
||||
int top = face->glyph->bitmap_top;
|
||||
m_advance = round_up_26_6(face->glyph->advance.x);
|
||||
uint8_t * texture = new uint8_t[width * height];
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
memcpy(&texture[width * i],
|
||||
&face->glyph->bitmap.buffer[width * (height - i - 1)],
|
||||
width);
|
||||
}
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_texture = new GLTexture();
|
||||
m_texture->create(width, height);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_ALPHA, GL_UNSIGNED_BYTE, texture);
|
||||
delete[] texture;
|
||||
|
||||
float s_max = width / (float)m_texture->get_width();
|
||||
float t_max = height / (float)m_texture->get_height();
|
||||
GLfloat box[4][4] = {
|
||||
{(GLfloat)left, (GLfloat)(top - height),
|
||||
0.0, 0.0},
|
||||
{(GLfloat)(left + width), (GLfloat)(top - height),
|
||||
s_max, 0.0},
|
||||
{(GLfloat)left, (GLfloat)top,
|
||||
0.0, t_max},
|
||||
{(GLfloat)(left + width), (GLfloat)top,
|
||||
s_max, t_max},
|
||||
};
|
||||
glGenBuffers(1, &m_vbo_id);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(box), box, GL_STATIC_DRAW);
|
||||
|
||||
m_loaded = true;
|
||||
return true;
|
||||
}
|
||||
|
87
src/Font.h
87
src/Font.h
@ -8,60 +8,57 @@
|
||||
#include <unordered_map>
|
||||
#include "GLTexture.h"
|
||||
|
||||
namespace jes
|
||||
class Font
|
||||
{
|
||||
class Font
|
||||
public:
|
||||
class Glyph
|
||||
{
|
||||
public:
|
||||
class Glyph
|
||||
{
|
||||
public:
|
||||
Glyph();
|
||||
bool load(FT_Face face, FT_ULong char_code);
|
||||
int get_advance() { return m_advance; }
|
||||
void render()
|
||||
{
|
||||
m_texture->bind();
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id);
|
||||
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
protected:
|
||||
bool m_loaded;
|
||||
int m_advance;
|
||||
GLTextureRef m_texture;
|
||||
GLuint m_vbo_id;
|
||||
};
|
||||
typedef Ref<Glyph> GlyphRef;
|
||||
|
||||
Font();
|
||||
~Font();
|
||||
bool load(FT_Library ft, const char * fname, size_t size);
|
||||
int get_line_height() { return m_line_height; }
|
||||
Glyph();
|
||||
bool load(FT_Face face, FT_ULong char_code);
|
||||
int get_advance() { return m_advance; }
|
||||
int get_baseline_offset() { return m_baseline_offset; }
|
||||
GlyphRef get_glyph(FT_ULong char_code)
|
||||
void render()
|
||||
{
|
||||
auto it = m_glyphs.find(char_code);
|
||||
if (it != m_glyphs.end())
|
||||
return it->second;
|
||||
GlyphRef g = load_glyph(char_code);
|
||||
m_glyphs[char_code] = g;
|
||||
return g;
|
||||
m_texture->bind();
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id);
|
||||
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
protected:
|
||||
GlyphRef load_glyph(FT_ULong char_code);
|
||||
|
||||
FT_Face m_face;
|
||||
int m_line_height;
|
||||
int m_advance;
|
||||
int m_baseline_offset;
|
||||
bool m_loaded;
|
||||
std::unordered_map<FT_ULong, GlyphRef> m_glyphs;
|
||||
int m_advance;
|
||||
GLTextureRef m_texture;
|
||||
GLuint m_vbo_id;
|
||||
};
|
||||
typedef Ref<Glyph> GlyphRef;
|
||||
|
||||
typedef Ref<Font> FontRef;
|
||||
}
|
||||
Font();
|
||||
~Font();
|
||||
bool load(FT_Library ft, const char * fname, size_t size);
|
||||
int get_line_height() { return m_line_height; }
|
||||
int get_advance() { return m_advance; }
|
||||
int get_baseline_offset() { return m_baseline_offset; }
|
||||
GlyphRef get_glyph(FT_ULong char_code)
|
||||
{
|
||||
auto it = m_glyphs.find(char_code);
|
||||
if (it != m_glyphs.end())
|
||||
return it->second;
|
||||
GlyphRef g = load_glyph(char_code);
|
||||
m_glyphs[char_code] = g;
|
||||
return g;
|
||||
}
|
||||
protected:
|
||||
GlyphRef load_glyph(FT_ULong char_code);
|
||||
|
||||
FT_Face m_face;
|
||||
int m_line_height;
|
||||
int m_advance;
|
||||
int m_baseline_offset;
|
||||
bool m_loaded;
|
||||
std::unordered_map<FT_ULong, GlyphRef> m_glyphs;
|
||||
};
|
||||
|
||||
typedef Ref<Font> FontRef;
|
||||
|
||||
#endif
|
||||
|
@ -1,42 +1,39 @@
|
||||
#include "FontManager.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace jes
|
||||
bool FontManager::load()
|
||||
{
|
||||
bool FontManager::load()
|
||||
if (FT_Init_FreeType(&m_ft) != 0)
|
||||
{
|
||||
if (FT_Init_FreeType(&m_ft) != 0)
|
||||
{
|
||||
std::cerr << "Error initializing FreeType" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Load failsafe font */
|
||||
m_mono_font = load_font("FreeMono", 20);
|
||||
|
||||
if (m_mono_font == NULL)
|
||||
{
|
||||
std::cerr << "Error loading FreeMono font" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
std::cerr << "Error initializing FreeType" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
FontRef FontManager::load_font(const char * fname, size_t size)
|
||||
/* Load failsafe font */
|
||||
m_mono_font = load_font("FreeMono", 20);
|
||||
|
||||
if (m_mono_font == NULL)
|
||||
{
|
||||
/* TODO */
|
||||
return NULL;
|
||||
#if 0
|
||||
PathRef font_path = Runtime::locate(Runtime::FONT, fname);
|
||||
if (font_path == NULL)
|
||||
return NULL;
|
||||
|
||||
FontRef font = new Font();
|
||||
if (!font->load(m_ft, font_path->to_s().c_str(), size))
|
||||
return NULL;
|
||||
|
||||
return font;
|
||||
#endif
|
||||
std::cerr << "Error loading FreeMono font" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
FontRef FontManager::load_font(const char * fname, size_t size)
|
||||
{
|
||||
/* TODO */
|
||||
return NULL;
|
||||
#if 0
|
||||
PathRef font_path = Runtime::locate(Runtime::FONT, fname);
|
||||
if (font_path == NULL)
|
||||
return NULL;
|
||||
|
||||
FontRef font = new Font();
|
||||
if (!font->load(m_ft, font_path->to_s().c_str(), size))
|
||||
return NULL;
|
||||
|
||||
return font;
|
||||
#endif
|
||||
}
|
||||
|
@ -6,20 +6,17 @@
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
namespace jes
|
||||
class FontManager
|
||||
{
|
||||
class FontManager
|
||||
{
|
||||
public:
|
||||
bool load();
|
||||
FontRef load_font(const char * fname, size_t size);
|
||||
FontRef get_mono_font() { return m_mono_font; }
|
||||
protected:
|
||||
FT_Library m_ft;
|
||||
FontRef m_mono_font;
|
||||
};
|
||||
public:
|
||||
bool load();
|
||||
FontRef load_font(const char * fname, size_t size);
|
||||
FontRef get_mono_font() { return m_mono_font; }
|
||||
protected:
|
||||
FT_Library m_ft;
|
||||
FontRef m_mono_font;
|
||||
};
|
||||
|
||||
typedef Ref<FontManager> FontManagerRef;
|
||||
}
|
||||
typedef Ref<FontManager> FontManagerRef;
|
||||
|
||||
#endif
|
||||
|
@ -1,30 +1,27 @@
|
||||
#include "GLBuffer.h"
|
||||
|
||||
namespace jes
|
||||
GLBuffer::GLBuffer()
|
||||
{
|
||||
GLBuffer::GLBuffer()
|
||||
{
|
||||
m_id = 0;
|
||||
}
|
||||
m_id = 0;
|
||||
}
|
||||
|
||||
GLBuffer::~GLBuffer()
|
||||
GLBuffer::~GLBuffer()
|
||||
{
|
||||
if (m_id > 0)
|
||||
{
|
||||
if (m_id > 0)
|
||||
{
|
||||
glDeleteBuffers(1, &m_id);
|
||||
}
|
||||
}
|
||||
|
||||
bool GLBuffer::create(GLenum target, GLenum usage, const void *ptr, size_t sz)
|
||||
{
|
||||
m_target = target;
|
||||
glGenBuffers(1, &m_id);
|
||||
if (m_id > 0)
|
||||
{
|
||||
bind();
|
||||
glBufferData(target, sz, ptr, usage);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
glDeleteBuffers(1, &m_id);
|
||||
}
|
||||
}
|
||||
|
||||
bool GLBuffer::create(GLenum target, GLenum usage, const void *ptr, size_t sz)
|
||||
{
|
||||
m_target = target;
|
||||
glGenBuffers(1, &m_id);
|
||||
if (m_id > 0)
|
||||
{
|
||||
bind();
|
||||
glBufferData(target, sz, ptr, usage);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -4,21 +4,18 @@
|
||||
#include "Ref.h"
|
||||
#include "gl3w.h"
|
||||
|
||||
namespace jes
|
||||
class GLBuffer
|
||||
{
|
||||
class GLBuffer
|
||||
{
|
||||
public:
|
||||
GLBuffer();
|
||||
~GLBuffer();
|
||||
bool create(GLenum target, GLenum usage, const void *ptr, size_t sz);
|
||||
GLuint get_id() { return m_id; }
|
||||
void bind() { glBindBuffer(m_target, m_id); }
|
||||
protected:
|
||||
GLuint m_id;
|
||||
GLenum m_target;
|
||||
};
|
||||
typedef Ref<GLBuffer> GLBufferRef;
|
||||
}
|
||||
public:
|
||||
GLBuffer();
|
||||
~GLBuffer();
|
||||
bool create(GLenum target, GLenum usage, const void *ptr, size_t sz);
|
||||
GLuint get_id() { return m_id; }
|
||||
void bind() { glBindBuffer(m_target, m_id); }
|
||||
protected:
|
||||
GLuint m_id;
|
||||
GLenum m_target;
|
||||
};
|
||||
typedef Ref<GLBuffer> GLBufferRef;
|
||||
|
||||
#endif
|
||||
|
147
src/GLProgram.cc
147
src/GLProgram.cc
@ -4,82 +4,79 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace jes
|
||||
GLProgram::GLProgram()
|
||||
{
|
||||
GLProgram::GLProgram()
|
||||
m_id = glCreateProgram();
|
||||
if (m_id == 0u)
|
||||
{
|
||||
m_id = glCreateProgram();
|
||||
if (m_id == 0u)
|
||||
{
|
||||
cerr << "Error allocating GL program object" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
GLProgram::~GLProgram()
|
||||
{
|
||||
if (m_id > 0u)
|
||||
{
|
||||
glDeleteProgram(m_id);
|
||||
}
|
||||
}
|
||||
|
||||
GLProgram & GLProgram::attach_shader(GLShaderRef shader)
|
||||
{
|
||||
if (m_id > 0u)
|
||||
{
|
||||
glAttachShader(m_id, shader->get_id());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
GLProgram & GLProgram::bind_attribute(const char * attribute, GLuint index)
|
||||
{
|
||||
if (m_id > 0u)
|
||||
{
|
||||
glBindAttribLocation(m_id, index, attribute);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool GLProgram::link()
|
||||
{
|
||||
if (m_id == 0u)
|
||||
return false;
|
||||
|
||||
glLinkProgram(m_id);
|
||||
|
||||
GLint link_status;
|
||||
glGetProgramiv(m_id, GL_LINK_STATUS, &link_status);
|
||||
if (link_status != GL_TRUE)
|
||||
{
|
||||
GLint log_length;
|
||||
glGetProgramiv(m_id, GL_INFO_LOG_LENGTH, &log_length);
|
||||
if (log_length > 0)
|
||||
{
|
||||
char *log = new char[log_length];
|
||||
glGetProgramInfoLog(m_id, log_length, &log_length, log);
|
||||
cerr << "Program log:" << endl << log << endl;
|
||||
delete[] log;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
GLint GLProgram::get_uniform(const std::string & uniform_name)
|
||||
{
|
||||
if (m_id > 0u)
|
||||
{
|
||||
auto it = m_uniforms.find(uniform_name);
|
||||
if (it != m_uniforms.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
GLint loc = glGetUniformLocation(m_id, uniform_name.c_str());
|
||||
m_uniforms[uniform_name] = loc;
|
||||
return loc;
|
||||
}
|
||||
return -1;
|
||||
cerr << "Error allocating GL program object" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
GLProgram::~GLProgram()
|
||||
{
|
||||
if (m_id > 0u)
|
||||
{
|
||||
glDeleteProgram(m_id);
|
||||
}
|
||||
}
|
||||
|
||||
GLProgram & GLProgram::attach_shader(GLShaderRef shader)
|
||||
{
|
||||
if (m_id > 0u)
|
||||
{
|
||||
glAttachShader(m_id, shader->get_id());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
GLProgram & GLProgram::bind_attribute(const char * attribute, GLuint index)
|
||||
{
|
||||
if (m_id > 0u)
|
||||
{
|
||||
glBindAttribLocation(m_id, index, attribute);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool GLProgram::link()
|
||||
{
|
||||
if (m_id == 0u)
|
||||
return false;
|
||||
|
||||
glLinkProgram(m_id);
|
||||
|
||||
GLint link_status;
|
||||
glGetProgramiv(m_id, GL_LINK_STATUS, &link_status);
|
||||
if (link_status != GL_TRUE)
|
||||
{
|
||||
GLint log_length;
|
||||
glGetProgramiv(m_id, GL_INFO_LOG_LENGTH, &log_length);
|
||||
if (log_length > 0)
|
||||
{
|
||||
char *log = new char[log_length];
|
||||
glGetProgramInfoLog(m_id, log_length, &log_length, log);
|
||||
cerr << "Program log:" << endl << log << endl;
|
||||
delete[] log;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
GLint GLProgram::get_uniform(const std::string & uniform_name)
|
||||
{
|
||||
if (m_id > 0u)
|
||||
{
|
||||
auto it = m_uniforms.find(uniform_name);
|
||||
if (it != m_uniforms.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
GLint loc = glGetUniformLocation(m_id, uniform_name.c_str());
|
||||
m_uniforms[uniform_name] = loc;
|
||||
return loc;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
@ -7,24 +7,21 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace jes
|
||||
class GLProgram
|
||||
{
|
||||
class GLProgram
|
||||
{
|
||||
public:
|
||||
GLProgram();
|
||||
~GLProgram();
|
||||
GLProgram & attach_shader(GLShaderRef shader);
|
||||
GLProgram & bind_attribute(const char * attribute, GLuint index);
|
||||
bool link();
|
||||
GLuint get_id() { return m_id; }
|
||||
void use() { glUseProgram(m_id); }
|
||||
GLint get_uniform(const std::string & uniform_name);
|
||||
protected:
|
||||
GLuint m_id;
|
||||
std::map<std::string, GLint> m_uniforms;
|
||||
};
|
||||
typedef Ref<GLProgram> GLProgramRef;
|
||||
}
|
||||
public:
|
||||
GLProgram();
|
||||
~GLProgram();
|
||||
GLProgram & attach_shader(GLShaderRef shader);
|
||||
GLProgram & bind_attribute(const char * attribute, GLuint index);
|
||||
bool link();
|
||||
GLuint get_id() { return m_id; }
|
||||
void use() { glUseProgram(m_id); }
|
||||
GLint get_uniform(const std::string & uniform_name);
|
||||
protected:
|
||||
GLuint m_id;
|
||||
std::map<std::string, GLint> m_uniforms;
|
||||
};
|
||||
typedef Ref<GLProgram> GLProgramRef;
|
||||
|
||||
#endif
|
||||
|
123
src/GLShader.cc
123
src/GLShader.cc
@ -3,70 +3,67 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace jes
|
||||
GLShader::~GLShader()
|
||||
{
|
||||
GLShader::~GLShader()
|
||||
if (m_id > 0)
|
||||
{
|
||||
if (m_id > 0)
|
||||
{
|
||||
glDeleteShader(m_id);
|
||||
}
|
||||
}
|
||||
|
||||
bool GLShader::create(GLenum shader_type, const char *source, size_t size)
|
||||
{
|
||||
GLint status;
|
||||
|
||||
m_id = glCreateShader(shader_type);
|
||||
if (m_id > 0)
|
||||
{
|
||||
GLint length = size;
|
||||
glShaderSource(m_id, 1, &source, &length);
|
||||
|
||||
glCompileShader(m_id);
|
||||
|
||||
glGetShaderiv(m_id, GL_COMPILE_STATUS, &status);
|
||||
if (status == GL_TRUE)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
GLint log_length;
|
||||
cerr << "Error compiling ";
|
||||
switch (shader_type)
|
||||
{
|
||||
case GL_VERTEX_SHADER:
|
||||
cerr << "vertex";
|
||||
break;
|
||||
case GL_FRAGMENT_SHADER:
|
||||
cerr << "fragment";
|
||||
break;
|
||||
}
|
||||
cerr << " shader" << endl;
|
||||
glGetShaderiv(m_id, GL_INFO_LOG_LENGTH, &log_length);
|
||||
if (log_length > 0)
|
||||
{
|
||||
char * log = new char[log_length];
|
||||
glGetShaderInfoLog(m_id, log_length, &log_length, log);
|
||||
cerr << "Shader Log:" << endl << log << endl;
|
||||
delete[] log;
|
||||
}
|
||||
glDeleteShader(m_id);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GLShader::create(GLenum shader_type, PathRef path)
|
||||
{
|
||||
uint8_t * file;
|
||||
size_t size;
|
||||
if (!path->read(&file, &size))
|
||||
return false;
|
||||
if (size == 0u)
|
||||
return false;
|
||||
|
||||
bool result = create(shader_type, (const char *)file, size);
|
||||
delete[] file;
|
||||
return result;
|
||||
glDeleteShader(m_id);
|
||||
}
|
||||
}
|
||||
|
||||
bool GLShader::create(GLenum shader_type, const char *source, size_t size)
|
||||
{
|
||||
GLint status;
|
||||
|
||||
m_id = glCreateShader(shader_type);
|
||||
if (m_id > 0)
|
||||
{
|
||||
GLint length = size;
|
||||
glShaderSource(m_id, 1, &source, &length);
|
||||
|
||||
glCompileShader(m_id);
|
||||
|
||||
glGetShaderiv(m_id, GL_COMPILE_STATUS, &status);
|
||||
if (status == GL_TRUE)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
GLint log_length;
|
||||
cerr << "Error compiling ";
|
||||
switch (shader_type)
|
||||
{
|
||||
case GL_VERTEX_SHADER:
|
||||
cerr << "vertex";
|
||||
break;
|
||||
case GL_FRAGMENT_SHADER:
|
||||
cerr << "fragment";
|
||||
break;
|
||||
}
|
||||
cerr << " shader" << endl;
|
||||
glGetShaderiv(m_id, GL_INFO_LOG_LENGTH, &log_length);
|
||||
if (log_length > 0)
|
||||
{
|
||||
char * log = new char[log_length];
|
||||
glGetShaderInfoLog(m_id, log_length, &log_length, log);
|
||||
cerr << "Shader Log:" << endl << log << endl;
|
||||
delete[] log;
|
||||
}
|
||||
glDeleteShader(m_id);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GLShader::create(GLenum shader_type, PathRef path)
|
||||
{
|
||||
uint8_t * file;
|
||||
size_t size;
|
||||
if (!path->read(&file, &size))
|
||||
return false;
|
||||
if (size == 0u)
|
||||
return false;
|
||||
|
||||
bool result = create(shader_type, (const char *)file, size);
|
||||
delete[] file;
|
||||
return result;
|
||||
}
|
||||
|
@ -5,21 +5,18 @@
|
||||
#include "Path.h"
|
||||
#include "gl3w.h"
|
||||
|
||||
namespace jes
|
||||
class GLShader
|
||||
{
|
||||
class GLShader
|
||||
{
|
||||
public:
|
||||
GLShader() { m_id = 0u; }
|
||||
~GLShader();
|
||||
bool create(GLenum shader_type, const char *source, size_t size);
|
||||
bool create(GLenum shader_type, PathRef path);
|
||||
GLuint get_id() { return m_id; }
|
||||
bool valid() { return m_id > 0; }
|
||||
protected:
|
||||
GLuint m_id;
|
||||
};
|
||||
typedef Ref<GLShader> GLShaderRef;
|
||||
}
|
||||
public:
|
||||
GLShader() { m_id = 0u; }
|
||||
~GLShader();
|
||||
bool create(GLenum shader_type, const char *source, size_t size);
|
||||
bool create(GLenum shader_type, PathRef path);
|
||||
GLuint get_id() { return m_id; }
|
||||
bool valid() { return m_id > 0; }
|
||||
protected:
|
||||
GLuint m_id;
|
||||
};
|
||||
typedef Ref<GLShader> GLShaderRef;
|
||||
|
||||
#endif
|
||||
|
@ -4,41 +4,38 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace jes
|
||||
GLTexture::GLTexture()
|
||||
{
|
||||
GLTexture::GLTexture()
|
||||
{
|
||||
glGenTextures(1, &m_id);
|
||||
m_width = 0u;
|
||||
m_height = 0u;
|
||||
}
|
||||
|
||||
GLTexture::~GLTexture()
|
||||
{
|
||||
glDeleteTextures(1, &m_id);
|
||||
}
|
||||
|
||||
uint32_t GLTexture::next_power_of_2(uint32_t n)
|
||||
{
|
||||
n--;
|
||||
n |= n >> 1u;
|
||||
n |= n >> 2u;
|
||||
n |= n >> 4u;
|
||||
n |= n >> 8u;
|
||||
n |= n >> 16u;
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
void GLTexture::create(unsigned int width, unsigned int height)
|
||||
{
|
||||
m_width = width = next_power_of_2(width);
|
||||
m_height = height = next_power_of_2(height);
|
||||
uint8_t * d = new uint8_t[width * height];
|
||||
memset(d, 0, width * height);
|
||||
bind();
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, d);
|
||||
delete[] d;
|
||||
}
|
||||
glGenTextures(1, &m_id);
|
||||
m_width = 0u;
|
||||
m_height = 0u;
|
||||
}
|
||||
|
||||
GLTexture::~GLTexture()
|
||||
{
|
||||
glDeleteTextures(1, &m_id);
|
||||
}
|
||||
|
||||
uint32_t GLTexture::next_power_of_2(uint32_t n)
|
||||
{
|
||||
n--;
|
||||
n |= n >> 1u;
|
||||
n |= n >> 2u;
|
||||
n |= n >> 4u;
|
||||
n |= n >> 8u;
|
||||
n |= n >> 16u;
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
void GLTexture::create(unsigned int width, unsigned int height)
|
||||
{
|
||||
m_width = width = next_power_of_2(width);
|
||||
m_height = height = next_power_of_2(height);
|
||||
uint8_t * d = new uint8_t[width * height];
|
||||
memset(d, 0, width * height);
|
||||
bind();
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, d);
|
||||
delete[] d;
|
||||
}
|
||||
|
@ -5,25 +5,22 @@
|
||||
#include "gl3w.h"
|
||||
#include <stdint.h>
|
||||
|
||||
namespace jes
|
||||
class GLTexture
|
||||
{
|
||||
class GLTexture
|
||||
{
|
||||
public:
|
||||
GLTexture();
|
||||
~GLTexture();
|
||||
GLuint get_id() { return m_id; }
|
||||
void bind() { glBindTexture(GL_TEXTURE_2D, m_id); }
|
||||
static uint32_t next_power_of_2(uint32_t n);
|
||||
void create(unsigned int width, unsigned int height);
|
||||
unsigned int get_width() { return m_width; }
|
||||
unsigned int get_height() { return m_height; }
|
||||
protected:
|
||||
GLuint m_id;
|
||||
unsigned int m_width;
|
||||
unsigned int m_height;
|
||||
};
|
||||
typedef Ref<GLTexture> GLTextureRef;
|
||||
}
|
||||
public:
|
||||
GLTexture();
|
||||
~GLTexture();
|
||||
GLuint get_id() { return m_id; }
|
||||
void bind() { glBindTexture(GL_TEXTURE_2D, m_id); }
|
||||
static uint32_t next_power_of_2(uint32_t n);
|
||||
void create(unsigned int width, unsigned int height);
|
||||
unsigned int get_width() { return m_width; }
|
||||
unsigned int get_height() { return m_height; }
|
||||
protected:
|
||||
GLuint m_id;
|
||||
unsigned int m_width;
|
||||
unsigned int m_height;
|
||||
};
|
||||
typedef Ref<GLTexture> GLTextureRef;
|
||||
|
||||
#endif
|
||||
|
237
src/GUI.cc
237
src/GUI.cc
@ -5,147 +5,144 @@
|
||||
#define WIDTH 500
|
||||
#define HEIGHT 500
|
||||
|
||||
namespace jes
|
||||
bool GUI::load()
|
||||
{
|
||||
bool GUI::load()
|
||||
glClearColor (0.0, 0.0, 0.0, 0.0);
|
||||
|
||||
m_font_manager = new FontManager();
|
||||
|
||||
if (!m_font_manager->load())
|
||||
return false;
|
||||
|
||||
if (!load_shaders())
|
||||
return false;
|
||||
|
||||
if (!load_buffers())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GUI::load_shaders()
|
||||
{
|
||||
static const char * shader_sources[PROGRAM_COUNT][2] = {
|
||||
{"text.v.glsl", "text.f.glsl"},
|
||||
{"basic.v.glsl", "basic.f.glsl"},
|
||||
{"rect.v.glsl", "basic.f.glsl"},
|
||||
};
|
||||
|
||||
for (int i = 0; i < PROGRAM_COUNT; i++)
|
||||
{
|
||||
glClearColor (0.0, 0.0, 0.0, 0.0);
|
||||
PathRef shader_paths[2];
|
||||
GLShaderRef shaders[2];
|
||||
|
||||
m_font_manager = new FontManager();
|
||||
m_programs[i] = new GLProgram();
|
||||
|
||||
if (!m_font_manager->load())
|
||||
return false;
|
||||
|
||||
if (!load_shaders())
|
||||
return false;
|
||||
|
||||
if (!load_buffers())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GUI::load_shaders()
|
||||
{
|
||||
static const char * shader_sources[PROGRAM_COUNT][2] = {
|
||||
{"text.v.glsl", "text.f.glsl"},
|
||||
{"basic.v.glsl", "basic.f.glsl"},
|
||||
{"rect.v.glsl", "basic.f.glsl"},
|
||||
};
|
||||
|
||||
for (int i = 0; i < PROGRAM_COUNT; i++)
|
||||
for (int j = 0; j < 2; j++)
|
||||
{
|
||||
PathRef shader_paths[2];
|
||||
GLShaderRef shaders[2];
|
||||
|
||||
m_programs[i] = new GLProgram();
|
||||
|
||||
for (int j = 0; j < 2; j++)
|
||||
{
|
||||
/* TODO */
|
||||
shader_paths[j] = NULL;
|
||||
/* TODO */
|
||||
shader_paths[j] = NULL;
|
||||
#if 0
|
||||
shader_paths[j] = Runtime::locate(Runtime::SHADER, shader_sources[i][j]);
|
||||
shader_paths[j] = Runtime::locate(Runtime::SHADER, shader_sources[i][j]);
|
||||
#endif
|
||||
if (shader_paths[j] == NULL)
|
||||
{
|
||||
std::cerr << "Could not find shader source " << shader_sources[i][j] << std::endl;
|
||||
return false;
|
||||
}
|
||||
shaders[j] = new GLShader();
|
||||
if (!shaders[j]->create(j == 0 ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER, shader_paths[j]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_programs[i]->attach_shader(shaders[j]);
|
||||
}
|
||||
if (!m_programs[i]->link())
|
||||
if (shader_paths[j] == NULL)
|
||||
{
|
||||
std::cerr << "Could not find shader source " << shader_sources[i][j] << std::endl;
|
||||
return false;
|
||||
}
|
||||
shaders[j] = new GLShader();
|
||||
if (!shaders[j]->create(j == 0 ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER, shader_paths[j]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_programs[i]->attach_shader(shaders[j]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GUI::load_buffers()
|
||||
{
|
||||
static const GLint rect_coords[4][2] = {
|
||||
{0, 0},
|
||||
{1, 0},
|
||||
{0, 1},
|
||||
{1, 1},
|
||||
};
|
||||
|
||||
m_rect_vbo = new GLBuffer();
|
||||
if (!m_rect_vbo->create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, &rect_coords, sizeof(rect_coords)))
|
||||
{
|
||||
std::cerr << "Failed to create rectangle VBO" << std::endl;
|
||||
if (!m_programs[i]->link())
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int GUI::run()
|
||||
{
|
||||
resize();
|
||||
draw();
|
||||
return true;
|
||||
}
|
||||
|
||||
SDL_Event event;
|
||||
while (SDL_WaitEvent(&event))
|
||||
bool GUI::load_buffers()
|
||||
{
|
||||
static const GLint rect_coords[4][2] = {
|
||||
{0, 0},
|
||||
{1, 0},
|
||||
{0, 1},
|
||||
{1, 1},
|
||||
};
|
||||
|
||||
m_rect_vbo = new GLBuffer();
|
||||
if (!m_rect_vbo->create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, &rect_coords, sizeof(rect_coords)))
|
||||
{
|
||||
std::cerr << "Failed to create rectangle VBO" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int GUI::run()
|
||||
{
|
||||
resize();
|
||||
draw();
|
||||
|
||||
SDL_Event event;
|
||||
while (SDL_WaitEvent(&event))
|
||||
{
|
||||
if (event.type == SDL_QUIT)
|
||||
break;
|
||||
else if (event.type == SDL_KEYDOWN)
|
||||
{
|
||||
if (event.type == SDL_QUIT)
|
||||
if (event.key.keysym.sym == SDLK_ESCAPE)
|
||||
break;
|
||||
else if (event.type == SDL_KEYDOWN)
|
||||
{
|
||||
if (event.key.keysym.sym == SDLK_ESCAPE)
|
||||
break;
|
||||
}
|
||||
else if (event.type == SDL_WINDOWEVENT)
|
||||
{
|
||||
switch (event.window.event)
|
||||
{
|
||||
case SDL_WINDOWEVENT_EXPOSED:
|
||||
draw();
|
||||
break;
|
||||
case SDL_WINDOWEVENT_RESIZED:
|
||||
resize();
|
||||
draw();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GUI::resize()
|
||||
{
|
||||
GLint viewport_size[2];
|
||||
SDL_GetWindowSize(m_window, &viewport_size[0], &viewport_size[1]);
|
||||
glViewport(0, 0, viewport_size[0], viewport_size[1]);
|
||||
for (int i = 0; i < PROGRAM_COUNT; i++)
|
||||
else if (event.type == SDL_WINDOWEVENT)
|
||||
{
|
||||
m_programs[i]->use();
|
||||
glUniform2iv(m_programs[i]->get_uniform("viewport_size"), 1, &viewport_size[0]);
|
||||
switch (event.window.event)
|
||||
{
|
||||
case SDL_WINDOWEVENT_EXPOSED:
|
||||
draw();
|
||||
break;
|
||||
case SDL_WINDOWEVENT_RESIZED:
|
||||
resize();
|
||||
draw();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GUI::draw()
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
SDL_GL_SwapWindow(m_window);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GUI::draw_rect(int x, int y, int width, int height, float r, float g, float b, float a)
|
||||
void GUI::resize()
|
||||
{
|
||||
GLint viewport_size[2];
|
||||
SDL_GetWindowSize(m_window, &viewport_size[0], &viewport_size[1]);
|
||||
glViewport(0, 0, viewport_size[0], viewport_size[1]);
|
||||
for (int i = 0; i < PROGRAM_COUNT; i++)
|
||||
{
|
||||
m_programs[PROGRAM_RECT]->use();
|
||||
glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("position"), x, y);
|
||||
glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("size"), width, height);
|
||||
glUniform4f(m_programs[PROGRAM_RECT]->get_uniform("color"), r, g, b, a);
|
||||
m_rect_vbo->bind();
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribIPointer(0, 2, GL_INT, 0, 0);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
m_programs[i]->use();
|
||||
glUniform2iv(m_programs[i]->get_uniform("viewport_size"), 1, &viewport_size[0]);
|
||||
}
|
||||
}
|
||||
|
||||
void GUI::draw()
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
SDL_GL_SwapWindow(m_window);
|
||||
}
|
||||
|
||||
void GUI::draw_rect(int x, int y, int width, int height, float r, float g, float b, float a)
|
||||
{
|
||||
m_programs[PROGRAM_RECT]->use();
|
||||
glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("position"), x, y);
|
||||
glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("size"), width, height);
|
||||
glUniform4f(m_programs[PROGRAM_RECT]->get_uniform("color"), r, g, b, a);
|
||||
m_rect_vbo->bind();
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribIPointer(0, 2, GL_INT, 0, 0);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
|
47
src/GUI.h
47
src/GUI.h
@ -7,34 +7,31 @@
|
||||
#include "GLProgram.h"
|
||||
#include "GLBuffer.h"
|
||||
|
||||
namespace jes
|
||||
class GUI
|
||||
{
|
||||
class GUI
|
||||
public:
|
||||
enum
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
PROGRAM_TEXT,
|
||||
PROGRAM_BASIC,
|
||||
PROGRAM_RECT,
|
||||
PROGRAM_COUNT,
|
||||
};
|
||||
bool load();
|
||||
int run();
|
||||
protected:
|
||||
bool load_shaders();
|
||||
bool load_buffers();
|
||||
void resize();
|
||||
void draw();
|
||||
void draw_rect(int x, int y, int width, int height, float r, float g, float b, float a);
|
||||
|
||||
SDL_Window * m_window;
|
||||
FontManagerRef m_font_manager;
|
||||
GLProgramRef m_programs[PROGRAM_COUNT];
|
||||
GLBufferRef m_rect_vbo;
|
||||
PROGRAM_TEXT,
|
||||
PROGRAM_BASIC,
|
||||
PROGRAM_RECT,
|
||||
PROGRAM_COUNT,
|
||||
};
|
||||
bool load();
|
||||
int run();
|
||||
protected:
|
||||
bool load_shaders();
|
||||
bool load_buffers();
|
||||
void resize();
|
||||
void draw();
|
||||
void draw_rect(int x, int y, int width, int height, float r, float g, float b, float a);
|
||||
|
||||
typedef Ref<GUI> GUIRef;
|
||||
}
|
||||
SDL_Window * m_window;
|
||||
FontManagerRef m_font_manager;
|
||||
GLProgramRef m_programs[PROGRAM_COUNT];
|
||||
GLBufferRef m_rect_vbo;
|
||||
};
|
||||
|
||||
typedef Ref<GUI> GUIRef;
|
||||
|
||||
#endif
|
||||
|
187
src/Path.cc
187
src/Path.cc
@ -5,117 +5,114 @@
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
|
||||
namespace jes
|
||||
Path::Path(const char * path)
|
||||
{
|
||||
Path::Path(const char * path)
|
||||
{
|
||||
m_path = path;
|
||||
clean();
|
||||
}
|
||||
m_path = path;
|
||||
clean();
|
||||
}
|
||||
|
||||
Path::Path(const std::string & path)
|
||||
{
|
||||
m_path = path;
|
||||
clean();
|
||||
}
|
||||
Path::Path(const std::string & path)
|
||||
{
|
||||
m_path = path;
|
||||
clean();
|
||||
}
|
||||
|
||||
PathRef Path::dirname()
|
||||
PathRef Path::dirname()
|
||||
{
|
||||
if (m_path.size() == 0)
|
||||
return new Path("");
|
||||
size_t i = m_path.size() - 1;
|
||||
while (m_path[i] == '/')
|
||||
{
|
||||
if (m_path.size() == 0)
|
||||
return new Path("");
|
||||
size_t i = m_path.size() - 1;
|
||||
while (m_path[i] == '/')
|
||||
{
|
||||
if (i == 0)
|
||||
return new Path("/");
|
||||
i--;
|
||||
}
|
||||
while (m_path[i] != '/')
|
||||
{
|
||||
if (i == 0)
|
||||
return new Path(".");
|
||||
i--;
|
||||
}
|
||||
while (m_path[i] == '/')
|
||||
{
|
||||
if (i == 0)
|
||||
return new Path("/");
|
||||
i--;
|
||||
}
|
||||
return new Path(std::string(m_path, 0, i + 1));
|
||||
if (i == 0)
|
||||
return new Path("/");
|
||||
i--;
|
||||
}
|
||||
|
||||
PathRef Path::ext(const std::string & new_ext)
|
||||
while (m_path[i] != '/')
|
||||
{
|
||||
if (m_path.size() == 0)
|
||||
return new Path("");
|
||||
size_t i = m_path.size() - 1;
|
||||
while (m_path[i] != '.')
|
||||
{
|
||||
if ((i == 0) || (m_path[i] == '/'))
|
||||
{
|
||||
i = m_path.size();
|
||||
break;
|
||||
}
|
||||
i--;
|
||||
}
|
||||
if (new_ext.size() == 0)
|
||||
{
|
||||
return new Path(std::string(m_path, 0, i));
|
||||
}
|
||||
else if (new_ext[0] == '.')
|
||||
{
|
||||
return new Path(std::string(m_path, 0, i) + new_ext);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Path(std::string(m_path, 0, i) + "." + new_ext);
|
||||
}
|
||||
if (i == 0)
|
||||
return new Path(".");
|
||||
i--;
|
||||
}
|
||||
|
||||
PathRef Path::join(const Path & other)
|
||||
while (m_path[i] == '/')
|
||||
{
|
||||
if (m_path.size() > 0 && *m_path.rbegin() == '/')
|
||||
return new Path(m_path + other.m_path);
|
||||
else
|
||||
return new Path(m_path + '/' + other.m_path);
|
||||
if (i == 0)
|
||||
return new Path("/");
|
||||
i--;
|
||||
}
|
||||
return new Path(std::string(m_path, 0, i + 1));
|
||||
}
|
||||
|
||||
bool Path::exists()
|
||||
PathRef Path::ext(const std::string & new_ext)
|
||||
{
|
||||
if (m_path.size() == 0)
|
||||
return new Path("");
|
||||
size_t i = m_path.size() - 1;
|
||||
while (m_path[i] != '.')
|
||||
{
|
||||
struct stat st;
|
||||
return stat(m_path.c_str(), &st) == 0;
|
||||
}
|
||||
|
||||
std::vector<std::string> Path::dir_entries()
|
||||
{
|
||||
std::vector<std::string> rv;
|
||||
DIR * dir = opendir(m_path.c_str());
|
||||
if (dir != NULL)
|
||||
if ((i == 0) || (m_path[i] == '/'))
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
struct dirent * de = readdir(dir);
|
||||
if (de == NULL)
|
||||
break;
|
||||
rv.push_back(std::string(de->d_name));
|
||||
}
|
||||
closedir(dir);
|
||||
i = m_path.size();
|
||||
break;
|
||||
}
|
||||
return rv;
|
||||
i--;
|
||||
}
|
||||
|
||||
bool Path::read(uint8_t ** buf, size_t * size)
|
||||
if (new_ext.size() == 0)
|
||||
{
|
||||
return FileReader::load(m_path.c_str(), buf, size);
|
||||
return new Path(std::string(m_path, 0, i));
|
||||
}
|
||||
|
||||
void Path::clean()
|
||||
else if (new_ext[0] == '.')
|
||||
{
|
||||
for (char & c : m_path)
|
||||
{
|
||||
if (c == '\\')
|
||||
c = '/';
|
||||
}
|
||||
return new Path(std::string(m_path, 0, i) + new_ext);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Path(std::string(m_path, 0, i) + "." + new_ext);
|
||||
}
|
||||
}
|
||||
|
||||
PathRef Path::join(const Path & other)
|
||||
{
|
||||
if (m_path.size() > 0 && *m_path.rbegin() == '/')
|
||||
return new Path(m_path + other.m_path);
|
||||
else
|
||||
return new Path(m_path + '/' + other.m_path);
|
||||
}
|
||||
|
||||
bool Path::exists()
|
||||
{
|
||||
struct stat st;
|
||||
return stat(m_path.c_str(), &st) == 0;
|
||||
}
|
||||
|
||||
std::vector<std::string> Path::dir_entries()
|
||||
{
|
||||
std::vector<std::string> rv;
|
||||
DIR * dir = opendir(m_path.c_str());
|
||||
if (dir != NULL)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
struct dirent * de = readdir(dir);
|
||||
if (de == NULL)
|
||||
break;
|
||||
rv.push_back(std::string(de->d_name));
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
bool Path::read(uint8_t ** buf, size_t * size)
|
||||
{
|
||||
return FileReader::load(m_path.c_str(), buf, size);
|
||||
}
|
||||
|
||||
void Path::clean()
|
||||
{
|
||||
for (char & c : m_path)
|
||||
{
|
||||
if (c == '\\')
|
||||
c = '/';
|
||||
}
|
||||
}
|
||||
|
37
src/Path.h
37
src/Path.h
@ -6,26 +6,23 @@
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace jes
|
||||
class Path;
|
||||
typedef Ref<Path> PathRef;
|
||||
class Path
|
||||
{
|
||||
class Path;
|
||||
typedef Ref<Path> PathRef;
|
||||
class Path
|
||||
{
|
||||
public:
|
||||
Path(const char * path);
|
||||
Path(const std::string & path);
|
||||
PathRef dirname();
|
||||
PathRef ext(const std::string & new_ext);
|
||||
PathRef join(const Path & other);
|
||||
const std::string & to_s() { return m_path; }
|
||||
bool exists();
|
||||
std::vector<std::string> dir_entries();
|
||||
bool read(uint8_t ** buf, size_t * size);
|
||||
protected:
|
||||
void clean();
|
||||
std::string m_path;
|
||||
};
|
||||
}
|
||||
public:
|
||||
Path(const char * path);
|
||||
Path(const std::string & path);
|
||||
PathRef dirname();
|
||||
PathRef ext(const std::string & new_ext);
|
||||
PathRef join(const Path & other);
|
||||
const std::string & to_s() { return m_path; }
|
||||
bool exists();
|
||||
std::vector<std::string> dir_entries();
|
||||
bool read(uint8_t ** buf, size_t * size);
|
||||
protected:
|
||||
void clean();
|
||||
std::string m_path;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
179
src/Ref.h
179
src/Ref.h
@ -3,100 +3,97 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace jes
|
||||
template <typename T>
|
||||
class Ref
|
||||
{
|
||||
template <typename T>
|
||||
class Ref
|
||||
public:
|
||||
Ref<T>();
|
||||
Ref<T>(T * ptr);
|
||||
Ref<T>(const Ref<T> & orig);
|
||||
Ref<T> & operator=(const Ref<T> & orig);
|
||||
Ref<T> & operator=(T * ptr);
|
||||
~Ref<T>();
|
||||
T & operator*() const { return *m_ptr; }
|
||||
T * operator->() const { return m_ptr; }
|
||||
bool is_null() const { return m_ptr == NULL; }
|
||||
bool operator==(const T * ptr) { return m_ptr == ptr; }
|
||||
bool operator!=(const T * ptr) { return m_ptr != ptr; }
|
||||
|
||||
private:
|
||||
void clone_from(const Ref<T> & orig);
|
||||
void destroy();
|
||||
|
||||
T * m_ptr;
|
||||
unsigned int * m_ref_count;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Ref<T>::Ref()
|
||||
{
|
||||
m_ptr = NULL;
|
||||
m_ref_count = NULL;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Ref<T>::Ref(T * ptr)
|
||||
{
|
||||
m_ptr = ptr;
|
||||
m_ref_count = new unsigned int;
|
||||
*m_ref_count = 1u;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Ref<T>::Ref(const Ref<T> & orig)
|
||||
{
|
||||
clone_from(orig);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Ref<T> & Ref<T>::operator=(const Ref<T> & orig)
|
||||
{
|
||||
destroy();
|
||||
clone_from(orig);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Ref<T> & Ref<T>::operator=(T * ptr)
|
||||
{
|
||||
destroy();
|
||||
m_ptr = ptr;
|
||||
m_ref_count = new unsigned int;
|
||||
*m_ref_count = 1u;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Ref<T>::clone_from(const Ref<T> & orig)
|
||||
{
|
||||
this->m_ptr = orig.m_ptr;
|
||||
this->m_ref_count = orig.m_ref_count;
|
||||
if (m_ref_count != NULL)
|
||||
(*m_ref_count)++;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Ref<T>::~Ref()
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Ref<T>::destroy()
|
||||
{
|
||||
if (m_ref_count != NULL)
|
||||
{
|
||||
public:
|
||||
Ref<T>();
|
||||
Ref<T>(T * ptr);
|
||||
Ref<T>(const Ref<T> & orig);
|
||||
Ref<T> & operator=(const Ref<T> & orig);
|
||||
Ref<T> & operator=(T * ptr);
|
||||
~Ref<T>();
|
||||
T & operator*() const { return *m_ptr; }
|
||||
T * operator->() const { return m_ptr; }
|
||||
bool is_null() const { return m_ptr == NULL; }
|
||||
bool operator==(const T * ptr) { return m_ptr == ptr; }
|
||||
bool operator!=(const T * ptr) { return m_ptr != ptr; }
|
||||
|
||||
private:
|
||||
void clone_from(const Ref<T> & orig);
|
||||
void destroy();
|
||||
|
||||
T * m_ptr;
|
||||
unsigned int * m_ref_count;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Ref<T>::Ref()
|
||||
{
|
||||
m_ptr = NULL;
|
||||
m_ref_count = NULL;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Ref<T>::Ref(T * ptr)
|
||||
{
|
||||
m_ptr = ptr;
|
||||
m_ref_count = new unsigned int;
|
||||
*m_ref_count = 1u;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Ref<T>::Ref(const Ref<T> & orig)
|
||||
{
|
||||
clone_from(orig);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Ref<T> & Ref<T>::operator=(const Ref<T> & orig)
|
||||
{
|
||||
destroy();
|
||||
clone_from(orig);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Ref<T> & Ref<T>::operator=(T * ptr)
|
||||
{
|
||||
destroy();
|
||||
m_ptr = ptr;
|
||||
m_ref_count = new unsigned int;
|
||||
*m_ref_count = 1u;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Ref<T>::clone_from(const Ref<T> & orig)
|
||||
{
|
||||
this->m_ptr = orig.m_ptr;
|
||||
this->m_ref_count = orig.m_ref_count;
|
||||
if (m_ref_count != NULL)
|
||||
(*m_ref_count)++;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Ref<T>::~Ref()
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Ref<T>::destroy()
|
||||
{
|
||||
if (m_ref_count != NULL)
|
||||
if (*m_ref_count <= 1u)
|
||||
{
|
||||
if (*m_ref_count <= 1u)
|
||||
{
|
||||
delete m_ptr;
|
||||
delete m_ref_count;
|
||||
}
|
||||
else
|
||||
{
|
||||
(*m_ref_count)--;
|
||||
}
|
||||
delete m_ptr;
|
||||
delete m_ref_count;
|
||||
}
|
||||
else
|
||||
{
|
||||
(*m_ref_count)--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
55
src/Text.cc
55
src/Text.cc
@ -1,41 +1,38 @@
|
||||
#include "Text.h"
|
||||
#include <string.h>
|
||||
|
||||
namespace jes
|
||||
Text::Text()
|
||||
{
|
||||
Text::Text()
|
||||
{
|
||||
m_buffer = NULL;
|
||||
m_size = 0;
|
||||
m_gap_index = 0;
|
||||
m_gap_size = 0;
|
||||
}
|
||||
m_buffer = NULL;
|
||||
m_size = 0;
|
||||
m_gap_index = 0;
|
||||
m_gap_size = 0;
|
||||
}
|
||||
|
||||
Text::Text(const uint8_t buf[], size_t size)
|
||||
{
|
||||
m_buffer = new uint8_t[size];
|
||||
m_size = size;
|
||||
m_gap_index = size;
|
||||
m_gap_size = 0;
|
||||
memcpy(m_buffer, &buf[0], size);
|
||||
}
|
||||
Text::Text(const uint8_t buf[], size_t size)
|
||||
{
|
||||
m_buffer = new uint8_t[size];
|
||||
m_size = size;
|
||||
m_gap_index = size;
|
||||
m_gap_size = 0;
|
||||
memcpy(m_buffer, &buf[0], size);
|
||||
}
|
||||
|
||||
std::string Text::to_s()
|
||||
std::string Text::to_s()
|
||||
{
|
||||
if (m_buffer == NULL)
|
||||
{
|
||||
if (m_buffer == NULL)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
compact();
|
||||
return std::string((const char *)m_buffer, m_size - m_gap_size);
|
||||
return "";
|
||||
}
|
||||
compact();
|
||||
return std::string((const char *)m_buffer, m_size - m_gap_size);
|
||||
}
|
||||
|
||||
void Text::compact()
|
||||
void Text::compact()
|
||||
{
|
||||
if ((m_gap_index < m_size - m_gap_size) && (m_gap_size > 0))
|
||||
{
|
||||
if ((m_gap_index < m_size - m_gap_size) && (m_gap_size > 0))
|
||||
{
|
||||
memmove(&m_buffer[m_gap_index], &m_buffer[m_gap_index + m_gap_size], m_size - m_gap_index);
|
||||
m_gap_index = m_size - m_gap_size;
|
||||
}
|
||||
memmove(&m_buffer[m_gap_index], &m_buffer[m_gap_index + m_gap_size], m_size - m_gap_index);
|
||||
m_gap_index = m_size - m_gap_size;
|
||||
}
|
||||
}
|
||||
|
41
src/Text.h
41
src/Text.h
@ -5,32 +5,29 @@
|
||||
#include "Ref.h"
|
||||
#include <string>
|
||||
|
||||
namespace jes
|
||||
class Text
|
||||
{
|
||||
class Text
|
||||
public:
|
||||
typedef char Character;
|
||||
Text();
|
||||
Text(const uint8_t buf[], size_t size);
|
||||
size_t get_size() { return m_size - m_gap_size; }
|
||||
|
||||
Character operator[](size_t index)
|
||||
{
|
||||
public:
|
||||
typedef char Character;
|
||||
Text();
|
||||
Text(const uint8_t buf[], size_t size);
|
||||
size_t get_size() { return m_size - m_gap_size; }
|
||||
return (Character)(m_buffer[index < m_gap_index ? index : index + m_gap_size]);
|
||||
}
|
||||
|
||||
Character operator[](size_t index)
|
||||
{
|
||||
return (Character)(m_buffer[index < m_gap_index ? index : index + m_gap_size]);
|
||||
}
|
||||
std::string to_s();
|
||||
|
||||
std::string to_s();
|
||||
protected:
|
||||
void compact();
|
||||
|
||||
protected:
|
||||
void compact();
|
||||
|
||||
uint8_t * m_buffer;
|
||||
size_t m_size;
|
||||
size_t m_gap_index;
|
||||
size_t m_gap_size;
|
||||
};
|
||||
typedef Ref<Text> TextRef;
|
||||
}
|
||||
uint8_t * m_buffer;
|
||||
size_t m_size;
|
||||
size_t m_gap_index;
|
||||
size_t m_gap_size;
|
||||
};
|
||||
typedef Ref<Text> TextRef;
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user