remove "namespace jes" wrapper around C++ content

This commit is contained in:
Josh Holtrop 2014-07-19 12:42:29 -04:00
parent b6c5e9e1d7
commit c3b4c33304
23 changed files with 969 additions and 1038 deletions

View File

@ -7,99 +7,96 @@
#include <unistd.h> #include <unistd.h>
#include <stdint.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_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);
}

View File

@ -6,45 +6,42 @@
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
namespace jes class FileLoader
{ {
class FileLoader public:
enum
{ {
public: LINE_ENDING_LF,
enum LINE_ENDING_CR,
{ LINE_ENDING_CRLF,
LINE_ENDING_LF, LINE_ENDING_COUNT
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;
}; };
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 #endif

View File

@ -10,55 +10,52 @@
#define JES_O_BINARY 0 #define JES_O_BINARY 0
#endif #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; return false;
if (stat(fname, &st) != 0) }
{
return false;
}
if (st.st_size < 0) if (st.st_size < 0)
{ {
return false; 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);
*size = st.st_size;
if (st.st_size == 0)
{
return true; 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;
} }

View File

@ -4,13 +4,10 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.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 #endif

View File

@ -4,109 +4,106 @@
#define round_up_26_6(val) (((val) + 63) >> 6u) #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; FT_Done_Face(m_face);
}
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;
} }
} }
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;
}

View File

@ -8,60 +8,57 @@
#include <unordered_map> #include <unordered_map>
#include "GLTexture.h" #include "GLTexture.h"
namespace jes class Font
{ {
class Font public:
class Glyph
{ {
public: public:
class Glyph Glyph();
{ bool load(FT_Face face, FT_ULong char_code);
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; }
int get_advance() { return m_advance; } int get_advance() { return m_advance; }
int get_baseline_offset() { return m_baseline_offset; } void render()
GlyphRef get_glyph(FT_ULong char_code)
{ {
auto it = m_glyphs.find(char_code); m_texture->bind();
if (it != m_glyphs.end()) glEnableVertexAttribArray(0);
return it->second; glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id);
GlyphRef g = load_glyph(char_code); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
m_glyphs[char_code] = g; glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
return g;
} }
protected: 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; 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 #endif

View File

@ -1,42 +1,39 @@
#include "FontManager.h" #include "FontManager.h"
#include <iostream> #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;
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;
} }
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 */ std::cerr << "Error loading FreeMono font" << std::endl;
return NULL; return false;
#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
} }
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
} }

View File

@ -6,20 +6,17 @@
#include <ft2build.h> #include <ft2build.h>
#include FT_FREETYPE_H #include FT_FREETYPE_H
namespace jes class FontManager
{ {
class FontManager public:
{ bool load();
public: FontRef load_font(const char * fname, size_t size);
bool load(); FontRef get_mono_font() { return m_mono_font; }
FontRef load_font(const char * fname, size_t size); protected:
FontRef get_mono_font() { return m_mono_font; } FT_Library m_ft;
protected: FontRef m_mono_font;
FT_Library m_ft; };
FontRef m_mono_font;
};
typedef Ref<FontManager> FontManagerRef; typedef Ref<FontManager> FontManagerRef;
}
#endif #endif

View File

@ -1,30 +1,27 @@
#include "GLBuffer.h" #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);
{
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;
} }
} }
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;
}

View File

@ -4,21 +4,18 @@
#include "Ref.h" #include "Ref.h"
#include "gl3w.h" #include "gl3w.h"
namespace jes class GLBuffer
{ {
class GLBuffer public:
{ GLBuffer();
public: ~GLBuffer();
GLBuffer(); bool create(GLenum target, GLenum usage, const void *ptr, size_t sz);
~GLBuffer(); GLuint get_id() { return m_id; }
bool create(GLenum target, GLenum usage, const void *ptr, size_t sz); void bind() { glBindBuffer(m_target, m_id); }
GLuint get_id() { return m_id; } protected:
void bind() { glBindBuffer(m_target, m_id); } GLuint m_id;
protected: GLenum m_target;
GLuint m_id; };
GLenum m_target; typedef Ref<GLBuffer> GLBufferRef;
};
typedef Ref<GLBuffer> GLBufferRef;
}
#endif #endif

View File

@ -4,82 +4,79 @@
using namespace std; using namespace std;
namespace jes GLProgram::GLProgram()
{ {
GLProgram::GLProgram() m_id = glCreateProgram();
if (m_id == 0u)
{ {
m_id = glCreateProgram(); cerr << "Error allocating GL program object" << endl;
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;
} }
} }
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;
}

View File

@ -7,24 +7,21 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace jes class GLProgram
{ {
class GLProgram public:
{ GLProgram();
public: ~GLProgram();
GLProgram(); GLProgram & attach_shader(GLShaderRef shader);
~GLProgram(); GLProgram & bind_attribute(const char * attribute, GLuint index);
GLProgram & attach_shader(GLShaderRef shader); bool link();
GLProgram & bind_attribute(const char * attribute, GLuint index); GLuint get_id() { return m_id; }
bool link(); void use() { glUseProgram(m_id); }
GLuint get_id() { return m_id; } GLint get_uniform(const std::string & uniform_name);
void use() { glUseProgram(m_id); } protected:
GLint get_uniform(const std::string & uniform_name); GLuint m_id;
protected: std::map<std::string, GLint> m_uniforms;
GLuint m_id; };
std::map<std::string, GLint> m_uniforms; typedef Ref<GLProgram> GLProgramRef;
};
typedef Ref<GLProgram> GLProgramRef;
}
#endif #endif

View File

@ -3,70 +3,67 @@
using namespace std; using namespace std;
namespace jes GLShader::~GLShader()
{ {
GLShader::~GLShader() if (m_id > 0)
{ {
if (m_id > 0) glDeleteShader(m_id);
{
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;
} }
} }
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;
}

View File

@ -5,21 +5,18 @@
#include "Path.h" #include "Path.h"
#include "gl3w.h" #include "gl3w.h"
namespace jes class GLShader
{ {
class GLShader public:
{ GLShader() { m_id = 0u; }
public: ~GLShader();
GLShader() { m_id = 0u; } bool create(GLenum shader_type, const char *source, size_t size);
~GLShader(); bool create(GLenum shader_type, PathRef path);
bool create(GLenum shader_type, const char *source, size_t size); GLuint get_id() { return m_id; }
bool create(GLenum shader_type, PathRef path); bool valid() { return m_id > 0; }
GLuint get_id() { return m_id; } protected:
bool valid() { return m_id > 0; } GLuint m_id;
protected: };
GLuint m_id; typedef Ref<GLShader> GLShaderRef;
};
typedef Ref<GLShader> GLShaderRef;
}
#endif #endif

View File

@ -4,41 +4,38 @@
using namespace std; using namespace std;
namespace jes GLTexture::GLTexture()
{ {
GLTexture::GLTexture() glGenTextures(1, &m_id);
{ m_width = 0u;
glGenTextures(1, &m_id); m_height = 0u;
m_width = 0u; }
m_height = 0u;
} GLTexture::~GLTexture()
{
GLTexture::~GLTexture() glDeleteTextures(1, &m_id);
{ }
glDeleteTextures(1, &m_id);
} uint32_t GLTexture::next_power_of_2(uint32_t n)
{
uint32_t GLTexture::next_power_of_2(uint32_t n) n--;
{ n |= n >> 1u;
n--; n |= n >> 2u;
n |= n >> 1u; n |= n >> 4u;
n |= n >> 2u; n |= n >> 8u;
n |= n >> 4u; n |= n >> 16u;
n |= n >> 8u; n++;
n |= n >> 16u; return n;
n++; }
return n;
} void GLTexture::create(unsigned int width, unsigned int height)
{
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);
m_width = width = next_power_of_2(width); uint8_t * d = new uint8_t[width * height];
m_height = height = next_power_of_2(height); memset(d, 0, width * height);
uint8_t * d = new uint8_t[width * height]; bind();
memset(d, 0, width * height); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
bind(); glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, d);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); delete[] d;
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, d);
delete[] d;
}
} }

View File

@ -5,25 +5,22 @@
#include "gl3w.h" #include "gl3w.h"
#include <stdint.h> #include <stdint.h>
namespace jes class GLTexture
{ {
class GLTexture public:
{ GLTexture();
public: ~GLTexture();
GLTexture(); GLuint get_id() { return m_id; }
~GLTexture(); void bind() { glBindTexture(GL_TEXTURE_2D, m_id); }
GLuint get_id() { return m_id; } static uint32_t next_power_of_2(uint32_t n);
void bind() { glBindTexture(GL_TEXTURE_2D, m_id); } void create(unsigned int width, unsigned int height);
static uint32_t next_power_of_2(uint32_t n); unsigned int get_width() { return m_width; }
void create(unsigned int width, unsigned int height); unsigned int get_height() { return m_height; }
unsigned int get_width() { return m_width; } protected:
unsigned int get_height() { return m_height; } GLuint m_id;
protected: unsigned int m_width;
GLuint m_id; unsigned int m_height;
unsigned int m_width; };
unsigned int m_height; typedef Ref<GLTexture> GLTextureRef;
};
typedef Ref<GLTexture> GLTextureRef;
}
#endif #endif

View File

@ -5,147 +5,144 @@
#define WIDTH 500 #define WIDTH 500
#define HEIGHT 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()) for (int j = 0; j < 2; j++)
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++)
{ {
PathRef shader_paths[2]; /* TODO */
GLShaderRef shaders[2]; shader_paths[j] = NULL;
m_programs[i] = new GLProgram();
for (int j = 0; j < 2; j++)
{
/* TODO */
shader_paths[j] = NULL;
#if 0 #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 #endif
if (shader_paths[j] == NULL) if (shader_paths[j] == NULL)
{ {
std::cerr << "Could not find shader source " << shader_sources[i][j] << std::endl; 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())
return false; 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())
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;
return false; return false;
}
return true;
} }
int GUI::run() return true;
{ }
resize();
draw();
SDL_Event event; bool GUI::load_buffers()
while (SDL_WaitEvent(&event)) {
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; 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;
}
}
} }
else if (event.type == SDL_WINDOWEVENT)
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++)
{ {
m_programs[i]->use(); switch (event.window.event)
glUniform2iv(m_programs[i]->get_uniform("viewport_size"), 1, &viewport_size[0]); {
case SDL_WINDOWEVENT_EXPOSED:
draw();
break;
case SDL_WINDOWEVENT_RESIZED:
resize();
draw();
break;
}
} }
} }
void GUI::draw() return 0;
{ }
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) 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(); m_programs[i]->use();
glUniform2i(m_programs[PROGRAM_RECT]->get_uniform("position"), x, y); glUniform2iv(m_programs[i]->get_uniform("viewport_size"), 1, &viewport_size[0]);
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);
} }
} }
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);
}

View File

@ -7,34 +7,31 @@
#include "GLProgram.h" #include "GLProgram.h"
#include "GLBuffer.h" #include "GLBuffer.h"
namespace jes class GUI
{ {
class GUI public:
enum
{ {
public: PROGRAM_TEXT,
enum PROGRAM_BASIC,
{ PROGRAM_RECT,
PROGRAM_TEXT, PROGRAM_COUNT,
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;
}; };
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 #endif

View File

@ -5,117 +5,114 @@
#include <unistd.h> #include <unistd.h>
#include <dirent.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) Path::Path(const std::string & path)
{ {
m_path = path; m_path = path;
clean(); 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) if (i == 0)
return new Path(""); return new Path("/");
size_t i = m_path.size() - 1; i--;
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));
} }
while (m_path[i] != '/')
PathRef Path::ext(const std::string & new_ext)
{ {
if (m_path.size() == 0) if (i == 0)
return new Path(""); return new Path(".");
size_t i = m_path.size() - 1; i--;
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);
}
} }
while (m_path[i] == '/')
PathRef Path::join(const Path & other)
{ {
if (m_path.size() > 0 && *m_path.rbegin() == '/') if (i == 0)
return new Path(m_path + other.m_path); return new Path("/");
else i--;
return new Path(m_path + '/' + other.m_path);
} }
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; if ((i == 0) || (m_path[i] == '/'))
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 (;;) i = m_path.size();
{ break;
struct dirent * de = readdir(dir);
if (de == NULL)
break;
rv.push_back(std::string(de->d_name));
}
closedir(dir);
} }
return rv; i--;
} }
if (new_ext.size() == 0)
bool Path::read(uint8_t ** buf, size_t * size)
{ {
return FileReader::load(m_path.c_str(), buf, size); return new Path(std::string(m_path, 0, i));
} }
else if (new_ext[0] == '.')
void Path::clean()
{ {
for (char & c : m_path) return new Path(std::string(m_path, 0, i) + new_ext);
{ }
if (c == '\\') else
c = '/'; {
} 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 = '/';
} }
} }

View File

@ -6,26 +6,23 @@
#include <vector> #include <vector>
#include <stdint.h> #include <stdint.h>
namespace jes class Path;
typedef Ref<Path> PathRef;
class Path
{ {
class Path; public:
typedef Ref<Path> PathRef; Path(const char * path);
class Path Path(const std::string & path);
{ PathRef dirname();
public: PathRef ext(const std::string & new_ext);
Path(const char * path); PathRef join(const Path & other);
Path(const std::string & path); const std::string & to_s() { return m_path; }
PathRef dirname(); bool exists();
PathRef ext(const std::string & new_ext); std::vector<std::string> dir_entries();
PathRef join(const Path & other); bool read(uint8_t ** buf, size_t * size);
const std::string & to_s() { return m_path; } protected:
bool exists(); void clean();
std::vector<std::string> dir_entries(); std::string m_path;
bool read(uint8_t ** buf, size_t * size); };
protected:
void clean();
std::string m_path;
};
}
#endif #endif

179
src/Ref.h
View File

@ -3,100 +3,97 @@
#include <stdlib.h> #include <stdlib.h>
namespace jes template <typename T>
class Ref
{ {
template <typename T> public:
class Ref 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: if (*m_ref_count <= 1u)
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) delete m_ptr;
{ delete m_ref_count;
delete m_ptr; }
delete m_ref_count; else
} {
else (*m_ref_count)--;
{
(*m_ref_count)--;
}
} }
} }
} }

View File

@ -1,41 +1,38 @@
#include "Text.h" #include "Text.h"
#include <string.h> #include <string.h>
namespace jes Text::Text()
{ {
Text::Text() m_buffer = NULL;
{ m_size = 0;
m_buffer = NULL; m_gap_index = 0;
m_size = 0; m_gap_size = 0;
m_gap_index = 0; }
m_gap_size = 0;
}
Text::Text(const uint8_t buf[], size_t size) Text::Text(const uint8_t buf[], size_t size)
{ {
m_buffer = new uint8_t[size]; m_buffer = new uint8_t[size];
m_size = size; m_size = size;
m_gap_index = size; m_gap_index = size;
m_gap_size = 0; m_gap_size = 0;
memcpy(m_buffer, &buf[0], size); 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 "";
{
return "";
}
compact();
return std::string((const char *)m_buffer, m_size - m_gap_size);
} }
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;
}
} }
} }

View File

@ -5,32 +5,29 @@
#include "Ref.h" #include "Ref.h"
#include <string> #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: return (Character)(m_buffer[index < m_gap_index ? index : index + m_gap_size]);
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) std::string to_s();
{
return (Character)(m_buffer[index < m_gap_index ? index : index + m_gap_size]);
}
std::string to_s(); protected:
void compact();
protected: uint8_t * m_buffer;
void compact(); size_t m_size;
size_t m_gap_index;
uint8_t * m_buffer; size_t m_gap_size;
size_t m_size; };
size_t m_gap_index; typedef Ref<Text> TextRef;
size_t m_gap_size;
};
typedef Ref<Text> TextRef;
}
#endif #endif