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,26 +7,24 @@
#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()
{
FileLoader::~FileLoader()
{
if (m_buf != NULL)
{
delete[] m_buf;
m_buf = NULL;
}
}
}
bool FileLoader::load(const char * fname)
{
bool FileLoader::load(const char * fname)
{
size_t size;
if (!FileReader::load(fname, &m_buf, &size))
@ -37,10 +35,10 @@ namespace jes
load_buf(size);
return true;
}
}
void FileLoader::load_buf(size_t size)
{
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;
@ -96,10 +94,9 @@ namespace jes
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);
}
}
TextRef FileLoader::get_line(unsigned int line_no)
{
return new Text((*m_lines)[line_no].first, (*m_lines)[line_no].second);
}

View File

@ -6,11 +6,9 @@
#include <stdint.h>
#include <vector>
namespace jes
class FileLoader
{
class FileLoader
{
public:
public:
enum
{
LINE_ENDING_LF,
@ -35,7 +33,7 @@ namespace jes
}
TextRef get_line(unsigned int line_no);
int get_line_endings() { return m_line_endings; }
protected:
protected:
typedef std::pair<const uint8_t *, size_t> LineIndexPair;
typedef std::vector<LineIndexPair> LineIndexPairVector;
typedef Ref<LineIndexPairVector> LineIndexPairVectorRef;
@ -43,8 +41,7 @@ namespace jes
uint8_t * m_buf;
int m_line_endings;
LineIndexPairVectorRef m_lines;
};
typedef Ref<FileLoader> FileLoaderRef;
}
};
typedef Ref<FileLoader> FileLoaderRef;
#endif

View File

@ -10,10 +10,8 @@
#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)
{
@ -60,5 +58,4 @@ namespace jes
close(fd);
return true;
}
}

View File

@ -4,13 +4,10 @@
#include <stdint.h>
#include <stdlib.h>
namespace jes
class FileReader
{
class FileReader
{
public:
public:
static bool load(const char * fname, uint8_t ** buf, size_t * size);
};
}
};
#endif

View File

@ -4,23 +4,21 @@
#define round_up_26_6(val) (((val) + 63) >> 6u)
namespace jes
Font::Font()
{
Font::Font()
{
m_loaded = false;
}
}
Font::~Font()
{
Font::~Font()
{
if (m_loaded)
{
FT_Done_Face(m_face);
}
}
}
bool Font::load(FT_Library ft, const char * fname, size_t size)
{
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;
@ -46,23 +44,23 @@ namespace jes
m_loaded = true;
return true;
}
}
Font::GlyphRef Font::load_glyph(FT_ULong char_code)
{
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()
{
Font::Glyph::Glyph()
{
m_loaded = false;
}
}
bool Font::Glyph::load(FT_Face face, FT_ULong char_code)
{
bool Font::Glyph::load(FT_Face face, FT_ULong char_code)
{
if (FT_Load_Char(face, char_code, FT_LOAD_RENDER) != 0)
return false;
@ -108,5 +106,4 @@ namespace jes
m_loaded = true;
return true;
}
}

View File

@ -8,11 +8,9 @@
#include <unordered_map>
#include "GLTexture.h"
namespace jes
class Font
{
class Font
{
public:
public:
class Glyph
{
public:
@ -50,7 +48,7 @@ namespace jes
m_glyphs[char_code] = g;
return g;
}
protected:
protected:
GlyphRef load_glyph(FT_ULong char_code);
FT_Face m_face;
@ -59,9 +57,8 @@ namespace jes
int m_baseline_offset;
bool m_loaded;
std::unordered_map<FT_ULong, GlyphRef> m_glyphs;
};
};
typedef Ref<Font> FontRef;
}
typedef Ref<Font> FontRef;
#endif

View File

@ -1,10 +1,8 @@
#include "FontManager.h"
#include <iostream>
namespace jes
bool FontManager::load()
{
bool FontManager::load()
{
if (FT_Init_FreeType(&m_ft) != 0)
{
std::cerr << "Error initializing FreeType" << std::endl;
@ -21,10 +19,10 @@ namespace jes
}
return true;
}
}
FontRef FontManager::load_font(const char * fname, size_t size)
{
FontRef FontManager::load_font(const char * fname, size_t size)
{
/* TODO */
return NULL;
#if 0
@ -38,5 +36,4 @@ namespace jes
return font;
#endif
}
}

View File

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

View File

@ -1,22 +1,20 @@
#include "GLBuffer.h"
namespace jes
GLBuffer::GLBuffer()
{
GLBuffer::GLBuffer()
{
m_id = 0;
}
}
GLBuffer::~GLBuffer()
{
GLBuffer::~GLBuffer()
{
if (m_id > 0)
{
glDeleteBuffers(1, &m_id);
}
}
}
bool GLBuffer::create(GLenum target, GLenum usage, const void *ptr, size_t sz)
{
bool GLBuffer::create(GLenum target, GLenum usage, const void *ptr, size_t sz)
{
m_target = target;
glGenBuffers(1, &m_id);
if (m_id > 0)
@ -26,5 +24,4 @@ namespace jes
return true;
}
return false;
}
}

View File

@ -4,10 +4,8 @@
#include "Ref.h"
#include "gl3w.h"
namespace jes
class GLBuffer
{
class GLBuffer
{
public:
GLBuffer();
~GLBuffer();
@ -17,8 +15,7 @@ namespace jes
protected:
GLuint m_id;
GLenum m_target;
};
typedef Ref<GLBuffer> GLBufferRef;
}
};
typedef Ref<GLBuffer> GLBufferRef;
#endif

View File

@ -4,45 +4,43 @@
using namespace std;
namespace jes
GLProgram::GLProgram()
{
GLProgram::GLProgram()
{
m_id = glCreateProgram();
if (m_id == 0u)
{
cerr << "Error allocating GL program object" << endl;
}
}
}
GLProgram::~GLProgram()
{
GLProgram::~GLProgram()
{
if (m_id > 0u)
{
glDeleteProgram(m_id);
}
}
}
GLProgram & GLProgram::attach_shader(GLShaderRef shader)
{
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)
{
GLProgram & GLProgram::bind_attribute(const char * attribute, GLuint index)
{
if (m_id > 0u)
{
glBindAttribLocation(m_id, index, attribute);
}
return *this;
}
}
bool GLProgram::link()
{
bool GLProgram::link()
{
if (m_id == 0u)
return false;
@ -65,10 +63,10 @@ namespace jes
}
return true;
}
}
GLint GLProgram::get_uniform(const std::string & uniform_name)
{
GLint GLProgram::get_uniform(const std::string & uniform_name)
{
if (m_id > 0u)
{
auto it = m_uniforms.find(uniform_name);
@ -81,5 +79,4 @@ namespace jes
return loc;
}
return -1;
}
}

View File

@ -7,11 +7,9 @@
#include <string>
#include <vector>
namespace jes
class GLProgram
{
class GLProgram
{
public:
public:
GLProgram();
~GLProgram();
GLProgram & attach_shader(GLShaderRef shader);
@ -20,11 +18,10 @@ namespace jes
GLuint get_id() { return m_id; }
void use() { glUseProgram(m_id); }
GLint get_uniform(const std::string & uniform_name);
protected:
protected:
GLuint m_id;
std::map<std::string, GLint> m_uniforms;
};
typedef Ref<GLProgram> GLProgramRef;
}
};
typedef Ref<GLProgram> GLProgramRef;
#endif

View File

@ -3,18 +3,16 @@
using namespace std;
namespace jes
GLShader::~GLShader()
{
GLShader::~GLShader()
{
if (m_id > 0)
{
glDeleteShader(m_id);
}
}
}
bool GLShader::create(GLenum shader_type, const char *source, size_t size)
{
bool GLShader::create(GLenum shader_type, const char *source, size_t size)
{
GLint status;
m_id = glCreateShader(shader_type);
@ -54,10 +52,10 @@ namespace jes
glDeleteShader(m_id);
}
return false;
}
}
bool GLShader::create(GLenum shader_type, PathRef path)
{
bool GLShader::create(GLenum shader_type, PathRef path)
{
uint8_t * file;
size_t size;
if (!path->read(&file, &size))
@ -68,5 +66,4 @@ namespace jes
bool result = create(shader_type, (const char *)file, size);
delete[] file;
return result;
}
}

View File

@ -5,21 +5,18 @@
#include "Path.h"
#include "gl3w.h"
namespace jes
class GLShader
{
class GLShader
{
public:
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:
protected:
GLuint m_id;
};
typedef Ref<GLShader> GLShaderRef;
}
};
typedef Ref<GLShader> GLShaderRef;
#endif

View File

@ -4,22 +4,20 @@
using namespace std;
namespace jes
GLTexture::GLTexture()
{
GLTexture::GLTexture()
{
glGenTextures(1, &m_id);
m_width = 0u;
m_height = 0u;
}
}
GLTexture::~GLTexture()
{
GLTexture::~GLTexture()
{
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 >> 2u;
@ -28,10 +26,10 @@ namespace jes
n |= n >> 16u;
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);
uint8_t * d = new uint8_t[width * height];
@ -40,5 +38,4 @@ namespace jes
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, d);
delete[] d;
}
}

View File

@ -5,11 +5,9 @@
#include "gl3w.h"
#include <stdint.h>
namespace jes
class GLTexture
{
class GLTexture
{
public:
public:
GLTexture();
~GLTexture();
GLuint get_id() { return m_id; }
@ -18,12 +16,11 @@ namespace jes
void create(unsigned int width, unsigned int height);
unsigned int get_width() { return m_width; }
unsigned int get_height() { return m_height; }
protected:
protected:
GLuint m_id;
unsigned int m_width;
unsigned int m_height;
};
typedef Ref<GLTexture> GLTextureRef;
}
};
typedef Ref<GLTexture> GLTextureRef;
#endif

View File

@ -5,10 +5,8 @@
#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();
@ -23,10 +21,10 @@ namespace jes
return false;
return true;
}
}
bool GUI::load_shaders()
{
bool GUI::load_shaders()
{
static const char * shader_sources[PROGRAM_COUNT][2] = {
{"text.v.glsl", "text.f.glsl"},
{"basic.v.glsl", "basic.f.glsl"},
@ -65,10 +63,10 @@ namespace jes
}
return true;
}
}
bool GUI::load_buffers()
{
bool GUI::load_buffers()
{
static const GLint rect_coords[4][2] = {
{0, 0},
{1, 0},
@ -84,10 +82,10 @@ namespace jes
}
return true;
}
}
int GUI::run()
{
int GUI::run()
{
resize();
draw();
@ -117,10 +115,10 @@ namespace jes
}
return 0;
}
}
void GUI::resize()
{
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]);
@ -129,16 +127,16 @@ namespace jes
m_programs[i]->use();
glUniform2iv(m_programs[i]->get_uniform("viewport_size"), 1, &viewport_size[0]);
}
}
}
void GUI::draw()
{
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)
{
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);
@ -147,5 +145,4 @@ namespace jes
glEnableVertexAttribArray(0);
glVertexAttribIPointer(0, 2, GL_INT, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
}

View File

@ -7,11 +7,9 @@
#include "GLProgram.h"
#include "GLBuffer.h"
namespace jes
class GUI
{
class GUI
{
public:
public:
enum
{
PROGRAM_TEXT,
@ -21,7 +19,7 @@ namespace jes
};
bool load();
int run();
protected:
protected:
bool load_shaders();
bool load_buffers();
void resize();
@ -32,9 +30,8 @@ namespace jes
FontManagerRef m_font_manager;
GLProgramRef m_programs[PROGRAM_COUNT];
GLBufferRef m_rect_vbo;
};
};
typedef Ref<GUI> GUIRef;
}
typedef Ref<GUI> GUIRef;
#endif

View File

@ -5,22 +5,20 @@
#include <unistd.h>
#include <dirent.h>
namespace jes
Path::Path(const char * path)
{
Path::Path(const char * path)
{
m_path = path;
clean();
}
}
Path::Path(const std::string & path)
{
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;
@ -43,10 +41,10 @@ namespace jes
i--;
}
return new Path(std::string(m_path, 0, i + 1));
}
}
PathRef Path::ext(const std::string & new_ext)
{
PathRef Path::ext(const std::string & new_ext)
{
if (m_path.size() == 0)
return new Path("");
size_t i = m_path.size() - 1;
@ -71,24 +69,24 @@ namespace jes
{
return new Path(std::string(m_path, 0, i) + "." + new_ext);
}
}
}
PathRef Path::join(const Path & other)
{
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()
{
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> Path::dir_entries()
{
std::vector<std::string> rv;
DIR * dir = opendir(m_path.c_str());
if (dir != NULL)
@ -103,19 +101,18 @@ namespace jes
closedir(dir);
}
return rv;
}
}
bool Path::read(uint8_t ** buf, size_t * size)
{
bool Path::read(uint8_t ** buf, size_t * size)
{
return FileReader::load(m_path.c_str(), buf, size);
}
}
void Path::clean()
{
void Path::clean()
{
for (char & c : m_path)
{
if (c == '\\')
c = '/';
}
}
}

View File

@ -6,13 +6,11 @@
#include <vector>
#include <stdint.h>
namespace jes
class Path;
typedef Ref<Path> PathRef;
class Path
{
class Path;
typedef Ref<Path> PathRef;
class Path
{
public:
public:
Path(const char * path);
Path(const std::string & path);
PathRef dirname();
@ -22,10 +20,9 @@ namespace jes
bool exists();
std::vector<std::string> dir_entries();
bool read(uint8_t ** buf, size_t * size);
protected:
protected:
void clean();
std::string m_path;
};
}
};
#endif

View File

@ -3,11 +3,9 @@
#include <stdlib.h>
namespace jes
template <typename T>
class Ref
{
template <typename T>
class Ref
{
public:
Ref<T>();
Ref<T>(T * ptr);
@ -27,65 +25,65 @@ namespace jes
T * m_ptr;
unsigned int * m_ref_count;
};
};
template <typename T>
Ref<T>::Ref()
{
template <typename T>
Ref<T>::Ref()
{
m_ptr = NULL;
m_ref_count = NULL;
}
}
template <typename T>
Ref<T>::Ref(T * ptr)
{
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)
{
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)
{
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)
{
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)
{
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()
{
template <typename T>
Ref<T>::~Ref()
{
destroy();
}
}
template <typename T>
void Ref<T>::destroy()
{
template <typename T>
void Ref<T>::destroy()
{
if (m_ref_count != NULL)
{
if (*m_ref_count <= 1u)
@ -98,7 +96,6 @@ namespace jes
(*m_ref_count)--;
}
}
}
}
#endif

View File

@ -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;
}
}
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_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)
{
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))
{
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,11 +5,9 @@
#include "Ref.h"
#include <string>
namespace jes
class Text
{
class Text
{
public:
public:
typedef char Character;
Text();
Text(const uint8_t buf[], size_t size);
@ -22,15 +20,14 @@ namespace jes
std::string to_s();
protected:
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;
}
};
typedef Ref<Text> TextRef;
#endif