From 88073614253d3a9c678848a6e905c3794aa831ca Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 2 Mar 2016 10:31:31 +0100 Subject: [PATCH] the usual suspects --- Makefile | 144 ++++++++++++++++++++++++++++++++++++++++ src/app/error.cpp | 56 ++++++++++++++++ src/app/error.hpp | 42 ++++++++++++ src/app/init.cpp | 88 ++++++++++++++++++++++++ src/app/init.hpp | 64 ++++++++++++++++++ src/app/window.cpp | 48 ++++++++++++++ src/app/window.hpp | 28 ++++++++ src/graphics/shader.cpp | 122 ++++++++++++++++++++++++++++++++++ src/graphics/shader.hpp | 65 ++++++++++++++++++ src/tacos.cpp | 10 +++ tst/test.cpp | 16 +++++ 11 files changed, 683 insertions(+) create mode 100644 Makefile create mode 100644 src/app/error.cpp create mode 100644 src/app/error.hpp create mode 100644 src/app/init.cpp create mode 100644 src/app/init.hpp create mode 100644 src/app/window.cpp create mode 100644 src/app/window.hpp create mode 100644 src/graphics/shader.cpp create mode 100644 src/graphics/shader.hpp create mode 100644 src/tacos.cpp create mode 100644 tst/test.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f491d89 --- /dev/null +++ b/Makefile @@ -0,0 +1,144 @@ +CXX = g++ --std=c++11 +LDXX = g++ + +LIBS = sdl2 SDL2_image SDL2_ttf glew openal freealut + +PKGFLAGS := $(shell pkg-config --cflags $(LIBS)) +PKGLIBS := $(shell pkg-config --libs $(LIBS)) +TESTFLAGS := $(shell pkg-config --cflags cppunit) +TESTLIBS := $(shell pkg-config --libs cppunit) + +CPPFLAGS ?= +CPPFLAGS += $(PKGFLAGS) +CXXFLAGS ?= +CXXFLAGS += -Wall +#CXXFLAGS += -march=native +LDXXFLAGS ?= +LDXXFLAGS += $(PKGLIBS) + +DEBUG_FLAGS = -g3 -O0 +PROFILE_FLAGS = -DNDEBUG -O1 -g3 +RELEASE_FLAGS = -DNDEBUG -O2 -g1 +TEST_FLAGS = -g -O2 -I./src $(TESTFLAGS) + +SOURCE_DIR := src +TEST_SRC_DIR := tst +DEBUG_DIR := build/debug +PROFILE_DIR := build/profile +RELEASE_DIR := build/release +TEST_DIR := build/test +DIR := $(RELEASE_DIR) $(DEBUG_DIR) $(PROFILE_DIR) $(TEST_DIR) build + +#ASSET_DIR := assets +#ASSET_DEP := $(ASSET_DIR)/.git + +LIB_SRC := $(wildcard $(SOURCE_DIR)/*/*.cpp) +BIN_SRC := $(wildcard $(SOURCE_DIR)/*.cpp) +SRC := $(LIB_SRC) $(BIN_SRC) +TEST_SRC := $(wildcard $(TEST_SRC_DIR)/*.cpp) $(wildcard $(TEST_SRC_DIR)/*/*.cpp) +RELEASE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(RELEASE_DIR)/%.o, $(SRC)) +RELEASE_LIB_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(RELEASE_DIR)/%.o, $(LIB_SRC)) +DEBUG_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(DEBUG_DIR)/%.o, $(SRC)) +DEBUG_LIB_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(DEBUG_DIR)/%.o, $(LIB_SRC)) +PROFILE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(PROFILE_DIR)/%.o, $(SRC)) +PROFILE_LIB_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(PROFILE_DIR)/%.o, $(LIB_SRC)) +TEST_OBJ := $(patsubst $(TEST_SRC_DIR)/%.cpp, $(TEST_DIR)/%.o, $(TEST_SRC)) $(patsubst $(SOURCE_DIR)/%.cpp, $(TEST_DIR)/src/%.o, $(LIB_SRC)) +RELEASE_DEP := $(RELEASE_OBJ:.o=.d) +DEBUG_DEP := $(DEBUG_OBJ:.o=.d) +PROFILE_DEP := $(PROFILE_OBJ:.o=.d) +TEST_DEP := $(TEST_OBJ:.o=.d) +RELEASE_BIN := tacos +DEBUG_BIN := tacos.debug +PROFILE_BIN := tacos.profile +TEST_BIN := tacos.test +OBJ := $(RELEASE_OBJ) $(DEBUG_OBJ) $(PROFILE_OBJ) $(TEST_OBJ) +DEP := $(RELEASE_DEP) $(DEBUG_DEP) $(PROFILE_DEP) $(TEST_DEP) +BIN := $(RELEASE_BIN) $(DEBUG_BIN) $(PROFILE_BIN) $(TEST_BIN) + +release: $(RELEASE_BIN) + +all: $(BIN) + +debug: $(DEBUG_BIN) + +profile: $(PROFILE_BIN) + +tests: $(TEST_BIN) + +run: $(ASSET_DEP) tacos + ./tacos + +gdb: $(ASSET_DEP) tacos.debug + gdb ./tacos.debug + +cachegrind: $(ASSET_DEP) tacos.profile + valgrind ./tacos.profile + +callgrind: $(ASSET_DEP) tacos.profile + valgrind --tool=callgrind \ + --branch-sim=yes --cacheuse=yes --cache-sim=yes \ + --collect-bus=yes --collect-systime=yes --collect-jumps=yes \ + --dump-instr=yes --simulate-hwpref=yes --simulate-wb=yes \ + ./tacos.profile + +test: tacos.test + ./tacos.test + +clean: + rm -df $(OBJ) $(DEP) $(DIR) + +distclean: clean + rm -f $(BIN) cachegrind.out.* callgrind.out.* + rm -Rf build + +.PHONY: all release debug profile tests run gdb cachegrind callgrind test clean distclean + +-include $(DEP) + +$(RELEASE_BIN): %: $(RELEASE_DIR)/%.o $(RELEASE_LIB_OBJ) + @echo link: $@ + @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(RELEASE_FLAGS) $^ + +$(DEBUG_BIN): %.debug: $(DEBUG_DIR)/%.o $(DEBUG_LIB_OBJ) + @echo link: $@ + @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(DEBUG_FLAGS) $^ + +$(PROFILE_BIN): %.profile: $(PROFILE_DIR)/%.o $(PROFILE_LIB_OBJ) + @echo link: $@ + @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(PROFILE_FLAGS) $^ + +$(TEST_BIN): $(TEST_OBJ) + @echo link: $@ + @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(TESTLIBS) $(TEST_FLAGS) $^ + +#$(ASSET_DEP): .git/$(shell git symbolic-ref HEAD) +# @git submodule update --init >/dev/null +# @touch $@ + +$(RELEASE_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(RELEASE_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(RELEASE_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + +$(DEBUG_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(DEBUG_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(DEBUG_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + +$(PROFILE_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(PROFILE_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(PROFILE_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + +$(TEST_DIR)/%.o: $(TEST_SRC_DIR)/%.cpp | $(TEST_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(TEST_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + +$(TEST_DIR)/src/%.o: $(SOURCE_DIR)/%.cpp | $(TEST_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(TEST_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + +$(DIR): + @mkdir -p "$@" diff --git a/src/app/error.cpp b/src/app/error.cpp new file mode 100644 index 0000000..c22aeb9 --- /dev/null +++ b/src/app/error.cpp @@ -0,0 +1,56 @@ +#include "error.hpp" + +#include +#include +#include + +using std::runtime_error; +using std::string; + + +namespace { + +const char *sdl_error_flush() { + const char *err = SDL_GetError(); + if (err && *err) { + SDL_ClearError(); + } + return err; +} + +string error_append(string msg, const char *err) { + if (err && *err) { + msg += ": "; + msg += err; + } + return msg; +} + +} + +namespace tacos { + +AlutError::AlutError(const char *msg) +: runtime_error(error_append(msg, alutGetErrorString(alutGetError()))) { + +} + + +GLError::GLError(const char *msg) +: runtime_error(error_append(msg, reinterpret_cast(gluErrorString(glGetError())))) { + +} + + +GLCompileError::GLCompileError(const char *msg, const char *log) +: runtime_error(error_append(msg, reinterpret_cast(gluErrorString(glGetError()))) + '\n' + log) { + +} + + +SDLError::SDLError(const char *msg) +: runtime_error(error_append(msg, sdl_error_flush())) { + +} + +} diff --git a/src/app/error.hpp b/src/app/error.hpp new file mode 100644 index 0000000..93a76bd --- /dev/null +++ b/src/app/error.hpp @@ -0,0 +1,42 @@ +#ifndef TACOS_APP_ERROR_HPP_ +#define TACOS_APP_ERROR_HPP_ + +#include + + +namespace tacos { + +struct AlutError +: public std::runtime_error { + + explicit AlutError(const char *msg); + +}; + + +struct GLError +: public std::runtime_error { + + explicit GLError(const char *msg); + +}; + + +struct GLCompileError +: public std::runtime_error { + + explicit GLCompileError(const char *msg, const char *log); + +}; + + +struct SDLError +: public std::runtime_error { + + explicit SDLError(const char *msg); + +}; + +} + +#endif diff --git a/src/app/init.cpp b/src/app/init.cpp new file mode 100644 index 0000000..146719b --- /dev/null +++ b/src/app/init.cpp @@ -0,0 +1,88 @@ +#include "init.hpp" + +#include "error.hpp" + +#include +#include +#include +#include + + +namespace tacos { + +InitAlut::InitAlut() { + if (!alutInit(nullptr, nullptr)) { + throw AlutError("alutInit(nullptr, nullptr)"); + } +} + +InitAlut::~InitAlut() { + if (!alutExit()) { + throw AlutError("alutExit()"); + } +} + + +InitGL::InitGL(bool double_buffer, int multi_sample) { + if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0) { + throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)"); + } + if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0) { + throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3)"); + } + if (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE) != 0) { + throw SDLError("SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)"); + } + + if (!double_buffer) { + if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0) != 0) { + throw SDLError("SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0)"); + } + } + + if (multi_sample > 1) { + if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) { + throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1)"); + } + if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multi_sample) != 0) { + throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, x)"); + } + } +} + + +InitIMG::InitIMG() { + if (IMG_Init(IMG_INIT_PNG) == 0) { + throw SDLError("IMG_Init(IMG_INIT_PNG)"); + } +} + +InitIMG::~InitIMG() noexcept { + IMG_Quit(); +} + + +InitSDL::InitSDL() { + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + throw SDLError("SDL_Init(SDL_INIT_VIDEO)"); + } + // SDL seems to start out in text input state + SDL_StopTextInput(); +} + +InitSDL::~InitSDL() noexcept { + SDL_Quit(); +} + + +InitTTF::InitTTF() { + if (TTF_Init() != 0) { + throw SDLError("TTF_Init()"); + } +} + +InitTTF::~InitTTF() noexcept { + TTF_Quit(); +} + +} diff --git a/src/app/init.hpp b/src/app/init.hpp new file mode 100644 index 0000000..4c82d08 --- /dev/null +++ b/src/app/init.hpp @@ -0,0 +1,64 @@ +#ifndef TACOS_APP_INIT_HPP_ +#define TACOS_APP_INIT_HPP_ + +namespace tacos { + +struct InitAlut { + + InitAlut(); + ~InitAlut(); + + InitAlut(const InitAlut &) = delete; + InitAlut &operator =(const InitAlut &) = delete; + +}; + +struct InitGL { + + InitGL(bool double_buffer = true, int samples = 1); + +}; + +struct InitIMG { + + InitIMG(); + ~InitIMG() noexcept; + + InitIMG(const InitIMG &) = delete; + InitIMG &operator =(const InitIMG &) = delete; + +}; + +struct InitSDL { + + InitSDL(); + ~InitSDL() noexcept; + + InitSDL(const InitSDL &) = delete; + InitSDL &operator =(const InitSDL &) = delete; + +}; + +struct InitTTF { + + InitTTF(); + ~InitTTF() noexcept; + + InitTTF(const InitTTF &) = delete; + InitTTF &operator =(const InitTTF &) = delete; + +}; + +struct Init { + + InitSDL sdl; + InitGL gl; + InitIMG img; + InitTTF ttf; + InitAlut alut; + +}; + +} + +#endif diff --git a/src/app/window.cpp b/src/app/window.cpp new file mode 100644 index 0000000..ff7a7e1 --- /dev/null +++ b/src/app/window.cpp @@ -0,0 +1,48 @@ +#include "window.hpp" + +#include "error.hpp" + +#include +#include +#include + + +namespace tacos { + +Window::Window() { + window = SDL_CreateWindow( + "tacos", // title + SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, // position + 1440, 900, // size + SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE // flags + ); + if (!window) { + throw SDLError("SDL_CreateWindow"); + } + context = SDL_GL_CreateContext(window); + if (!context) { + SDL_DestroyWindow(window); + throw SDLError("SDL_GL_CreateContext"); + } + glewExperimental = GL_TRUE; + GLenum err = glewInit(); + if (err != GLEW_OK) { + SDL_GL_DeleteContext(context); + SDL_DestroyWindow(window); + std::string msg("glewInit: "); + msg += reinterpret_cast(glewGetErrorString(err)); + throw std::runtime_error(msg); + } +} + +Window::~Window() noexcept { + SDL_GL_DeleteContext(context); + SDL_DestroyWindow(window); +} + +void Window::Flip() noexcept { + SDL_GL_SwapWindow(window); +} + + +} diff --git a/src/app/window.hpp b/src/app/window.hpp new file mode 100644 index 0000000..cc8fc8a --- /dev/null +++ b/src/app/window.hpp @@ -0,0 +1,28 @@ +#ifndef TACOS_APP_WINDOW_HPP_ +#define TACOS_APP_WINDOW_HPP_ + +#include + + +namespace tacos { + +class Window { + +public: + Window(); + ~Window() noexcept; + + Window(const Window &) = delete; + Window &operator =(const Window &) = delete; + + void Flip() noexcept; + +private: + SDL_Window *window; + SDL_GLContext context; + +}; + +} + +#endif diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp new file mode 100644 index 0000000..b82a6c8 --- /dev/null +++ b/src/graphics/shader.cpp @@ -0,0 +1,122 @@ +#include "shader.hpp" + +#include "../app/error.hpp" + +#include +#include +#include +#include + + +namespace tacos { + +Shader Shader::Vertex(const GLchar *source) { + return Shader(GL_VERTEX_SHADER, source); +} + +Shader Shader::Fragment(const GLchar *source) { + return Shader(GL_FRAGMENT_SHADER, source); +} + +Shader::Shader(GLenum type, const GLchar *source) +: shader(glCreateShader(type)) { + if (shader == 0) { + throw GLError("glCreateShader"); + } + const GLchar *sources[] = { source }; + glShaderSource(shader, 1, sources, nullptr); + glCompileShader(shader); + GLint compiled = GL_FALSE; + glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); + if (compiled != GL_TRUE) { + int log_length = 0, max_length = 0; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &max_length); + ++max_length; + std::unique_ptr log(new char[max_length]); + glGetShaderInfoLog(shader, max_length, &log_length, log.get()); + log[log_length] = '\0'; + glDeleteShader(shader); + throw GLCompileError("glCompileShader", log.get()); + } +} + +Shader::~Shader() noexcept { + if (shader != 0) { + glDeleteShader(shader); + } +} + +Shader::Shader(Shader &&other) noexcept +: shader(other.shader) { + other.shader = 0; +} + +Shader &Shader::operator =(Shader &&other) noexcept { + std::swap(shader, other.shader); + return *this; +} + + +Program::Program() +: program(glCreateProgram()) { + if (program == 0) { + throw GLError("glCreateProgram"); + } +} + +Program::~Program() noexcept { + glDeleteProgram(program); +} + +void Program::Attach(const Shader &shader) noexcept { + glAttachShader(program, shader.shader); +} + +void Program::Link() { + glLinkProgram(program); + GLint linked = GL_FALSE; + glGetProgramiv(program, GL_LINK_STATUS, &linked); + if (linked != GL_TRUE) { + int log_length = 0, max_length = 0; + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &max_length); + ++max_length; + std::unique_ptr log(new char[max_length]); + glGetProgramInfoLog(program, max_length, &log_length, log.get()); + log[log_length] = '\0'; + throw GLCompileError("glLinkProgram", log.get()); + } +} + +void Program::Use() noexcept { + glUseProgram(program); +} + +GLint Program::AttributeLocation(const GLchar *name) const noexcept { + return glGetAttribLocation(program, name); +} + +GLint Program::UniformLocation(const GLchar *name) const noexcept { + return glGetUniformLocation(program, name); +} + +void Program::Uniform(GLint loc, GLint val) noexcept { + glUniform1i(loc, val); +} + +void Program::Uniform(GLint loc, float val) noexcept { + glUniform1f(loc, val); +} + +void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept { + glUniform3fv(loc, 1, glm::value_ptr(val)); +} + +void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept { + glUniform4fv(loc, 1, glm::value_ptr(val)); +} + +void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept { + glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val)); +} + +} diff --git a/src/graphics/shader.hpp b/src/graphics/shader.hpp new file mode 100644 index 0000000..32369b2 --- /dev/null +++ b/src/graphics/shader.hpp @@ -0,0 +1,65 @@ +#ifndef TACOS_GRAPHICS_SHADER_HPP_ +#define TACOS_GRAPHICS_SHADER_HPP_ + +#include +#include + + +namespace tacos { + +class Shader { + + friend class Program; + +public: + static Shader Vertex(const GLchar *source); + static Shader Fragment(const GLchar *source); + + ~Shader() noexcept; + + Shader(Shader &&) noexcept; + Shader &operator =(Shader &&) noexcept; + + Shader(const Shader &) = delete; + Shader &operator =(const Shader &) = delete; + +private: + explicit Shader(GLenum type, const GLchar *source); + +private: + GLuint shader; + +}; + + +class Program { + +public: + Program(); + ~Program() noexcept; + + Program(const Program &) = delete; + Program &operator =(const Program &) = delete; + + void Attach(const Shader &) noexcept; + void Link(); + + void Use() noexcept; + + GLint AttributeLocation(const GLchar *name) const noexcept; + GLint UniformLocation(const GLchar *name) const noexcept; + + void Uniform(GLint, GLint) noexcept; + void Uniform(GLint, float) noexcept; + void Uniform(GLint, const glm::vec3 &) noexcept; + void Uniform(GLint, const glm::vec4 &) noexcept; + void Uniform(GLint, const glm::mat4 &) noexcept; + +private: + GLuint program; + +}; + +} + +#endif diff --git a/src/tacos.cpp b/src/tacos.cpp new file mode 100644 index 0000000..7e0976f --- /dev/null +++ b/src/tacos.cpp @@ -0,0 +1,10 @@ +#include "app/init.hpp" +#include "app/window.hpp" + +using namespace tacos; + +int main(int argc, char *argv[]) { + Init init; + Window window; + return 0; +} diff --git a/tst/test.cpp b/tst/test.cpp new file mode 100644 index 0000000..ff1ce71 --- /dev/null +++ b/tst/test.cpp @@ -0,0 +1,16 @@ +#include +#include + +using CppUnit::TestFactoryRegistry; +using CppUnit::TextUi::TestRunner; + + +int main(int, char **) { + TestRunner runner; + TestFactoryRegistry ®istry = TestFactoryRegistry::getRegistry(); + runner.addTest(registry.makeTest()); + runner.run(); + + return 0; + +} -- 2.39.2