diff --git a/src/FileLoader.cc b/src/FileLoader.cc index d6ce454..9e5bf5f 100644 --- a/src/FileLoader.cc +++ b/src/FileLoader.cc @@ -7,99 +7,96 @@ #include #include -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); +} diff --git a/src/FileLoader.h b/src/FileLoader.h index 807cdcb..f14a72e 100644 --- a/src/FileLoader.h +++ b/src/FileLoader.h @@ -6,45 +6,42 @@ #include #include -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 LineIndexPair; - typedef std::vector LineIndexPairVector; - typedef Ref 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 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 LineIndexPair; + typedef std::vector LineIndexPairVector; + typedef Ref LineIndexPairVectorRef; + void load_buf(size_t size); + uint8_t * m_buf; + int m_line_endings; + LineIndexPairVectorRef m_lines; +}; +typedef Ref FileLoaderRef; #endif diff --git a/src/FileReader.cc b/src/FileReader.cc index 60698fc..999bed6 100644 --- a/src/FileReader.cc +++ b/src/FileReader.cc @@ -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; } diff --git a/src/FileReader.h b/src/FileReader.h index df7200a..b961206 100644 --- a/src/FileReader.h +++ b/src/FileReader.h @@ -4,13 +4,10 @@ #include #include -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 diff --git a/src/Font.cc b/src/Font.cc index 4c46019..5d7fda5 100644 --- a/src/Font.cc +++ b/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; +} diff --git a/src/Font.h b/src/Font.h index 8d71d5f..2f99d59 100644 --- a/src/Font.h +++ b/src/Font.h @@ -8,60 +8,57 @@ #include #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 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 m_glyphs; + int m_advance; + GLTextureRef m_texture; + GLuint m_vbo_id; }; + typedef Ref GlyphRef; - typedef Ref 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 m_glyphs; +}; + +typedef Ref FontRef; #endif diff --git a/src/FontManager.cc b/src/FontManager.cc index 48fc668..6826fe0 100644 --- a/src/FontManager.cc +++ b/src/FontManager.cc @@ -1,42 +1,39 @@ #include "FontManager.h" #include -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 } diff --git a/src/FontManager.h b/src/FontManager.h index 58d1334..1432d98 100644 --- a/src/FontManager.h +++ b/src/FontManager.h @@ -6,20 +6,17 @@ #include #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 FontManagerRef; -} +typedef Ref FontManagerRef; #endif diff --git a/src/GLBuffer.cc b/src/GLBuffer.cc index 0589d03..5abfbc2 100644 --- a/src/GLBuffer.cc +++ b/src/GLBuffer.cc @@ -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; +} diff --git a/src/GLBuffer.h b/src/GLBuffer.h index 87bd243..167c3e3 100644 --- a/src/GLBuffer.h +++ b/src/GLBuffer.h @@ -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 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 GLBufferRef; #endif diff --git a/src/GLProgram.cc b/src/GLProgram.cc index fda1175..13bf8e6 100644 --- a/src/GLProgram.cc +++ b/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; +} diff --git a/src/GLProgram.h b/src/GLProgram.h index e7ba0a9..c382e71 100644 --- a/src/GLProgram.h +++ b/src/GLProgram.h @@ -7,24 +7,21 @@ #include #include -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 m_uniforms; - }; - typedef Ref 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 m_uniforms; +}; +typedef Ref GLProgramRef; #endif diff --git a/src/GLShader.cc b/src/GLShader.cc index d84a036..df07fc3 100644 --- a/src/GLShader.cc +++ b/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; +} diff --git a/src/GLShader.h b/src/GLShader.h index 9598fd4..a6c818f 100644 --- a/src/GLShader.h +++ b/src/GLShader.h @@ -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 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 GLShaderRef; #endif diff --git a/src/GLTexture.cc b/src/GLTexture.cc index c471024..6c0b79a 100644 --- a/src/GLTexture.cc +++ b/src/GLTexture.cc @@ -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; } diff --git a/src/GLTexture.h b/src/GLTexture.h index b724ccd..8ab1520 100644 --- a/src/GLTexture.h +++ b/src/GLTexture.h @@ -5,25 +5,22 @@ #include "gl3w.h" #include -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 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 GLTextureRef; #endif diff --git a/src/GUI.cc b/src/GUI.cc index 2e6bc8a..85a1e0a 100644 --- a/src/GUI.cc +++ b/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); +} diff --git a/src/GUI.h b/src/GUI.h index dd4099b..99e430f 100644 --- a/src/GUI.h +++ b/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 GUIRef; -} + SDL_Window * m_window; + FontManagerRef m_font_manager; + GLProgramRef m_programs[PROGRAM_COUNT]; + GLBufferRef m_rect_vbo; +}; + +typedef Ref GUIRef; #endif diff --git a/src/Path.cc b/src/Path.cc index 1f5acf7..c8e53d2 100644 --- a/src/Path.cc +++ b/src/Path.cc @@ -5,117 +5,114 @@ #include #include -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 Path::dir_entries() - { - std::vector 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 Path::dir_entries() +{ + std::vector 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 = '/'; } } diff --git a/src/Path.h b/src/Path.h index daa2b1b..686770d 100644 --- a/src/Path.h +++ b/src/Path.h @@ -6,26 +6,23 @@ #include #include -namespace jes +class Path; +typedef Ref PathRef; +class Path { - class Path; - typedef Ref 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 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 dir_entries(); + bool read(uint8_t ** buf, size_t * size); +protected: + void clean(); + std::string m_path; +}; #endif diff --git a/src/Ref.h b/src/Ref.h index d183b3d..5b9f676 100644 --- a/src/Ref.h +++ b/src/Ref.h @@ -3,100 +3,97 @@ #include -namespace jes +template +class Ref { - template - class Ref + public: + Ref(); + Ref(T * ptr); + Ref(const Ref & orig); + Ref & operator=(const Ref & orig); + Ref & operator=(T * ptr); + ~Ref(); + 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 & orig); + void destroy(); + + T * m_ptr; + unsigned int * m_ref_count; +}; + +template +Ref::Ref() +{ + m_ptr = NULL; + m_ref_count = NULL; +} + +template +Ref::Ref(T * ptr) +{ + m_ptr = ptr; + m_ref_count = new unsigned int; + *m_ref_count = 1u; +} + +template +Ref::Ref(const Ref & orig) +{ + clone_from(orig); +} + +template +Ref & Ref::operator=(const Ref & orig) +{ + destroy(); + clone_from(orig); + return *this; +} + +template +Ref & Ref::operator=(T * ptr) +{ + destroy(); + m_ptr = ptr; + m_ref_count = new unsigned int; + *m_ref_count = 1u; + return *this; +} + +template +void Ref::clone_from(const Ref & orig) +{ + this->m_ptr = orig.m_ptr; + this->m_ref_count = orig.m_ref_count; + if (m_ref_count != NULL) + (*m_ref_count)++; +} + +template +Ref::~Ref() +{ + destroy(); +} + +template +void Ref::destroy() +{ + if (m_ref_count != NULL) { - public: - Ref(); - Ref(T * ptr); - Ref(const Ref & orig); - Ref & operator=(const Ref & orig); - Ref & operator=(T * ptr); - ~Ref(); - 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 & orig); - void destroy(); - - T * m_ptr; - unsigned int * m_ref_count; - }; - - template - Ref::Ref() - { - m_ptr = NULL; - m_ref_count = NULL; - } - - template - Ref::Ref(T * ptr) - { - m_ptr = ptr; - m_ref_count = new unsigned int; - *m_ref_count = 1u; - } - - template - Ref::Ref(const Ref & orig) - { - clone_from(orig); - } - - template - Ref & Ref::operator=(const Ref & orig) - { - destroy(); - clone_from(orig); - return *this; - } - - template - Ref & Ref::operator=(T * ptr) - { - destroy(); - m_ptr = ptr; - m_ref_count = new unsigned int; - *m_ref_count = 1u; - return *this; - } - - template - void Ref::clone_from(const Ref & orig) - { - this->m_ptr = orig.m_ptr; - this->m_ref_count = orig.m_ref_count; - if (m_ref_count != NULL) - (*m_ref_count)++; - } - - template - Ref::~Ref() - { - destroy(); - } - - template - void Ref::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)--; } } } diff --git a/src/Text.cc b/src/Text.cc index c29c005..5994e2a 100644 --- a/src/Text.cc +++ b/src/Text.cc @@ -1,41 +1,38 @@ #include "Text.h" #include -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; } } diff --git a/src/Text.h b/src/Text.h index 624ba0a..39b286e 100644 --- a/src/Text.h +++ b/src/Text.h @@ -5,32 +5,29 @@ #include "Ref.h" #include -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 TextRef; -} + uint8_t * m_buffer; + size_t m_size; + size_t m_gap_index; + size_t m_gap_size; +}; +typedef Ref TextRef; #endif