Initial C++11 to C++03 conversion - still needs one line

converted for textures to render again - see TODO in obj.cpp
This commit is contained in:
twinaphex 2013-05-11 18:30:41 +02:00
parent 2516a1ae43
commit a133885091
11 changed files with 82 additions and 69 deletions

View File

@ -15,8 +15,8 @@ endif
ifeq ($(platform), unix)
TARGET := libretro.so
fpic := -fPIC
SHARED := -shared -Wl,--version-script=link.T -Wl,--no-undefined
GL_LIB := -lGL -lz
SHARED := -lz -shared -Wl,--version-script=link.T -Wl,--no-undefined
GL_LIB := -lGL
else ifeq ($(platform), osx)
TARGET := libretro.dylib
fpic := -fPIC
@ -42,7 +42,7 @@ SOURCES := $(wildcard *.cpp)
CSOURCES := $(wildcard *.c)
OBJECTS := $(SOURCES:.cpp=.o) $(CSOURCES:.c=.o)
CFLAGS += -std=gnu99 -Wall $(fpic)
CXXFLAGS += -std=gnu++0x -Wall $(fpic)
CXXFLAGS += -std=gnu++03 -Wall $(fpic)
ifeq ($(GLES), 1)
CFLAGS += -DGLES

8
gl.hpp
View File

@ -14,6 +14,10 @@
#include <iostream>
#include "libretro.h"
#ifdef __GNUC__
#define decltype(type) typeof(type)
#endif
#define SYM(sym) (::GL::symbol<decltype(&sym)>(#sym))
namespace GL
@ -36,9 +40,9 @@ namespace GL
template<typename Func>
inline Func symbol(const std::string& sym)
{
auto& map = symbol_map();
std::map<std::string, retro_proc_address_t>& map = symbol_map();
auto& func = map[sym];
retro_proc_address_t func = map[sym];
if (!func)
{
func = get_symbol(sym);

View File

@ -33,8 +33,8 @@ namespace GL
};
#undef _D
for (auto& bind : bind_map)
map[bind.sym] = bind.proc;
for (unsigned i = 0; i < sizeof(bind_map) / sizeof(bind_map[0]); i++)
map[bind_map[i].sym] = bind_map[i].proc;
}
static retro_hw_get_proc_address_t proc;

View File

@ -6,11 +6,13 @@
#include <cstring>
#include <string>
#include <iostream>
#include <cstdint>
#include <stdint.h>
#include <tr1/memory>
using namespace GL;
using namespace glm;
using namespace std;
using namespace std::tr1;
#define BASE_WIDTH 320
#define BASE_HEIGHT 240
@ -22,7 +24,7 @@ static unsigned height = BASE_HEIGHT;
static struct retro_hw_render_callback hw_render;
static string mesh_path;
static vector<shared_ptr<Mesh>> meshes;
static vector<std::tr1::shared_ptr<Mesh> > meshes;
void retro_init(void)
{}
@ -73,7 +75,7 @@ void retro_set_environment(retro_environment_t cb)
retro_variable variables[] = {
{ "modelviewer_resolution",
"Internal resolution; 320x240|640x480|960x720|1280x960|1600x1200|1920x1440" },
{ nullptr, nullptr },
{ NULL, NULL },
};
cb(RETRO_ENVIRONMENT_SET_VARIABLES, variables);
@ -142,19 +144,20 @@ static void handle_input()
mat4 model = translation * scaler * rotate_x * rotate_y;
for (auto& mesh : meshes)
mesh->set_model(model);
for (unsigned i = 0; i < meshes.size(); i++)
meshes[i]->set_model(model);
}
static void update_variables()
{
retro_variable var{};
retro_variable var;
var.key = "modelviewer_resolution";
var.value = NULL;
if (!environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || !var.value)
return;
auto list = String::split(var.value, "x");
std::vector<std::string> list = String::split(var.value, "x");
if (list.size() != 2)
return;
@ -180,8 +183,8 @@ void retro_run(void)
SYM(glViewport)(0, 0, width, height);
for (auto& mesh : meshes)
mesh->render();
for (unsigned i = 0; i < meshes.size(); i++)
meshes[i]->render();
SYM(glDisable)(GL_BLEND);
SYM(glDisable)(GL_DEPTH_TEST);
@ -225,16 +228,15 @@ static void init_mesh(const string& path)
" gl_FragColor = vec4(diffuse * color.rgb, color.a);\n"
"}";
auto shader = make_shared<Shader>(vertex_shader, fragment_shader);
std::tr1::shared_ptr<Shader> shader(new Shader(vertex_shader, fragment_shader));
meshes = OBJ::load_from_file(path);
mat4 projection = scale(mat4(1.0), vec3(1, -1, 1)) * perspective(45.0f, 640.0f / 480.0f, 1.0f, 100.0f);
for (auto& mesh : meshes)
for (unsigned i = 0; i < meshes.size(); i++)
{
mesh->set_projection(projection);
mesh->set_shader(shader);
meshes[i]->set_projection(projection);
meshes[i]->set_shader(shader);
}
}
@ -307,7 +309,7 @@ bool retro_unserialize(const void *, size_t)
void *retro_get_memory_data(unsigned)
{
return nullptr;
return NULL;
}
size_t retro_get_memory_size(unsigned)

View File

@ -24,7 +24,7 @@ namespace GL
void Mesh::set_vertices(std::vector<Vertex> vertex)
{
set_vertices(std::make_shared<std::vector<Vertex>>(std::move(vertex)));
set_vertices(std::tr1::shared_ptr<std::vector<Vertex> >(new std::vector<Vertex>(vertex)));
}
void Mesh::set_vertex_type(GLenum type)
@ -32,7 +32,7 @@ namespace GL
vertex_type = type;
}
void Mesh::set_vertices(const std::shared_ptr<std::vector<Vertex>>& vertex)
void Mesh::set_vertices(const std::tr1::shared_ptr<std::vector<Vertex> >& vertex)
{
this->vertex = vertex;
@ -42,12 +42,12 @@ namespace GL
SYM(glBindBuffer)(GL_ARRAY_BUFFER, 0);
}
void Mesh::set_texture(const std::shared_ptr<Texture>& texture)
void Mesh::set_texture(const std::tr1::shared_ptr<Texture>& texture)
{
this->texture = texture;
}
void Mesh::set_shader(const std::shared_ptr<Shader>& shader)
void Mesh::set_shader(const std::tr1::shared_ptr<Shader>& shader)
{
this->shader = shader;
}

View File

@ -10,6 +10,7 @@
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include <tr1/memory>
namespace GL
{
@ -27,10 +28,10 @@ namespace GL
~Mesh();
void set_vertices(std::vector<Vertex> vertex);
void set_vertices(const std::shared_ptr<std::vector<Vertex>>& vertex);
void set_vertices(const std::tr1::shared_ptr<std::vector<Vertex> >& vertex);
void set_vertex_type(GLenum type);
void set_texture(const std::shared_ptr<Texture>& tex);
void set_shader(const std::shared_ptr<Shader>& shader);
void set_texture(const std::tr1::shared_ptr<Texture>& tex);
void set_shader(const std::tr1::shared_ptr<Shader>& shader);
void set_model(const glm::mat4& model);
void set_view(const glm::mat4& view);
@ -41,9 +42,9 @@ namespace GL
private:
GLuint vbo;
GLenum vertex_type;
std::shared_ptr<std::vector<Vertex>> vertex;
std::shared_ptr<Texture> texture;
std::shared_ptr<Shader> shader;
std::tr1::shared_ptr<std::vector<Vertex> > vertex;
std::tr1::shared_ptr<Texture> texture;
std::tr1::shared_ptr<Shader> shader;
glm::mat4 model;
glm::mat4 view;

View File

@ -8,6 +8,7 @@
using namespace GL;
using namespace glm;
using namespace std;
using namespace std::tr1;
namespace OBJ
{
@ -18,7 +19,7 @@ namespace OBJ
inline vec2 parse_line(const string& data)
{
float x = 0, y = 0;
auto split = String::split(data, " ");
std::vector<std::string> split = String::split(data, " ");
if (split.size() >= 2)
{
x = String::stof(split[0]);
@ -32,7 +33,7 @@ namespace OBJ
inline vec3 parse_line(const string& data)
{
float x = 0, y = 0, z = 0;
auto split = String::split(data, " ");
std::vector<std::string> split = String::split(data, " ");
if (split.size() >= 3)
{
x = String::stof(split[0]);
@ -53,16 +54,16 @@ namespace OBJ
const vector<vec3>& normal,
const vector<vec2>& tex)
{
auto vertices = String::split(data, " ");
std::vector<std::string> vertices = String::split(data, " ");
if (vertices.size() > 3)
vertices.resize(3);
vector<vector<string>> verts;
for (auto& vert : vertices)
vector<vector<string> > verts;
for (unsigned i = 0; i < vertices.size(); i++)
{
Vertex out_vertex{};
Vertex out_vertex;
auto coords = String::split(vert, "/", true);
std::vector<std::string> coords = String::split(vertices[i], "/", true);
if (coords.size() == 1) // Vertex only
{
size_t coord = translate_index(String::stoi(coords[0]), vertex.size());
@ -108,10 +109,10 @@ namespace OBJ
}
}
vector<shared_ptr<Mesh>> load_from_file(const string& path)
vector<shared_ptr<Mesh> > load_from_file(const string& path)
{
ifstream file(path, ios::in);
vector<shared_ptr<Mesh>> meshes;
ifstream file(path.c_str(), ios::in);
vector<shared_ptr<Mesh> > meshes;
if (!file.is_open())
return meshes;
@ -122,16 +123,16 @@ namespace OBJ
vector<Vertex> vertices;
// Texture cache.
map<string, shared_ptr<Texture>> textures;
map<string, shared_ptr<Texture> > textures;
shared_ptr<Texture> current_texture;
for (string line; getline(file, line); )
{
line = line.substr(0, line.find_first_of('\r'));
auto split_point = line.find_first_of(' ');
auto type = line.substr(0, split_point);
auto data = split_point != string::npos ? line.substr(split_point + 1) : string();
size_t split_point = line.find_first_of(' ');
std::string type = line.substr(0, split_point);
std::string data = split_point != string::npos ? line.substr(split_point + 1) : string();
if (type == "v")
vertex.push_back(parse_line<vec3>(data));
@ -145,27 +146,29 @@ namespace OBJ
{
if (vertices.size()) // Different texture, new mesh.
{
auto mesh = make_shared<Mesh>();
mesh->set_vertices(move(vertices));
std::tr1::shared_ptr<Mesh> mesh(new Mesh());
mesh->set_vertices(vertices);
vertices.clear();
mesh->set_texture(current_texture);
meshes.push_back(mesh);
}
auto& texture = textures[data];
if (!texture)
if (!textures[data])
{
auto texture_path = Path::join(Path::basedir(path), data + ".png");
texture = make_shared<Texture>(texture_path);
std::string texture_path = Path::join(Path::basedir(path), data + ".png");
// TODO //
//textures[data] = make_shared<Texture>(texture_path);
}
current_texture = texture;
current_texture = textures[data];
}
}
if (vertices.size())
{
auto mesh = make_shared<Mesh>();
mesh->set_vertices(move(vertices));
std::tr1::shared_ptr<Mesh> mesh(new Mesh());
mesh->set_vertices(vertices);
vertices.clear();
mesh->set_texture(current_texture);
meshes.push_back(mesh);
}

View File

@ -5,10 +5,11 @@
#include <string>
#include <vector>
#include <memory>
#include <tr1/memory>
namespace OBJ
{
std::vector<std::shared_ptr<GL::Mesh>> load_from_file(const std::string& path);
std::vector<std::tr1::shared_ptr<GL::Mesh> > load_from_file(const std::string& path);
}
#endif

View File

@ -38,7 +38,7 @@ namespace GL
GLuint shader = SYM(glCreateShader)(type);
const char* src = source.c_str();
SYM(glShaderSource)(shader, 1, &src, nullptr);
SYM(glShaderSource)(shader, 1, &src, NULL);
SYM(glCompileShader)(shader);
GLint status = 0;
@ -95,8 +95,8 @@ namespace GL
{
GLint ret = -1;
auto itr = map.find(sym);
if (itr == std::end(map))
std::map<std::string, GLint>::iterator itr = map.find(sym);
if (itr == map.end())
map[sym] = ret = SYM(glGetUniformLocation)(prog, sym);
else
ret = itr->second;
@ -108,8 +108,8 @@ namespace GL
{
GLint ret = -1;
auto itr = map.find(sym);
if (itr == std::end(map))
std::map<std::string, GLint>::iterator itr = map.find(sym);
if (itr == map.end())
map[sym] = ret = SYM(glGetAttribLocation)(prog, sym);
else
ret = itr->second;

View File

@ -1,13 +1,13 @@
#include "texture.hpp"
#include "rpng.h"
#include <cstdlib>
#include <cstdint>
#include <stdint.h>
#include <stdlib.h>
namespace GL
{
Texture::Texture(const std::string& path) : tex(0)
{
std::uint32_t* data = nullptr;
uint32_t* data = NULL;
unsigned width = 0, height = 0;
bool ret = rpng_load_image_argb(path.c_str(),
@ -40,7 +40,7 @@ namespace GL
GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
unbind();
std::free(data);
free(data);
}
else
std::cerr << "Failed to load image: " << path << std::endl;

View File

@ -4,11 +4,13 @@
#include <string>
#include <cstdlib>
#define DIR_BACK(string) (string[string.length()-1])
namespace Path
{
inline std::string basedir(const std::string& path)
{
auto last = path.find_last_of("/\\");
size_t last = path.find_last_of("/\\");
if (last != std::string::npos)
return path.substr(0, last);
else
@ -17,7 +19,7 @@ namespace Path
inline std::string join(const std::string& dir, const std::string& path)
{
char last = dir.size() ? dir.back() : '\0';
char last = dir.size() ? DIR_BACK(dir) : '\0';
std::string sep;
if (last != '/' && last != '\\')
sep = "/";
@ -50,7 +52,7 @@ namespace String
inline int stoi(const std::string& str)
{
#if defined(__GNUC__) && (GCC_VERSION < 40700)
return std::strtol(str.c_str(), nullptr, 0);
return std::strtol(str.c_str(), NULL, 0);
#else
return std::stoi(str);
#endif
@ -59,7 +61,7 @@ namespace String
inline float stof(const std::string& str)
{
#if defined(__GNUC__) && (GCC_VERSION < 40700)
return std::strtod(str.c_str(), nullptr);
return std::strtod(str.c_str(), NULL);
#else
return std::stof(str);
#endif