From: Daniel Karbach Date: Sat, 4 Nov 2017 14:59:49 +0000 (+0100) Subject: some stolen stuff X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=b999aef37fd70e6a776fb1eb787fbcec0156c660;p=blobs.git some stolen stuff --- diff --git a/.gitignore b/.gitignore index 1377554..ed4e379 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,10 @@ *.swp +*.swo +blobs +blobs.cover +blobs.debug +blobs.profile +blobs.test +build +cachegrind.out.* +callgrind.out.* diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c4a1c42 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "assets"] + path = assets + url = https://git.localhorst.tv/repo/blobs-assets.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a098252 --- /dev/null +++ b/Makefile @@ -0,0 +1,236 @@ +CXX = g++ --std=c++11 +LDXX = g++ +CPPCHECK = cppcheck -q --std=c++11 \ + --enable=warning,style,performance,portability,unusedFunction,missingInclude \ + --error-exitcode=1 + +LIBS = sdl2 SDL2_image SDL2_net SDL2_ttf glew openal freealut zlib + +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) + +# source +SOURCE_DIR := src +TEST_SRC_DIR := tst + +# build configurations +# cover: +# coverage reporting +# for use with gcov +# debug: +# unoptimized and maximally annotated +# for use with gdb +# profile: +# somewhat optimized and maximally annotated +# for use with valgrind +# release: +# optimized, without debugging instructions and minimally +# annotated (mainly for stack traces) +# for use with people +# tests: +# same flags as release, but with main replaced by cppunit +# test runner and tests (from tst dir) built in + +COVER_FLAGS = -g -O0 --coverage -I$(SOURCE_DIR) $(TESTFLAGS) +DEBUG_FLAGS = -g3 -O0 +PROFILE_FLAGS = -DNDEBUG -O1 -g3 -DBLOBS_PROFILING +RELEASE_FLAGS = -DNDEBUG -O2 -g1 +TEST_FLAGS = -g -O2 -I$(SOURCE_DIR) $(TESTFLAGS) + +# destination +COVER_DIR := build/cover +DEBUG_DIR := build/debug +PROFILE_DIR := build/profile +RELEASE_DIR := build/release +TEST_DIR := build/test + +DIR := $(RELEASE_DIR) $(COVER_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) + +COVER_OBJ := $(patsubst $(TEST_SRC_DIR)/%.cpp, $(COVER_DIR)/%.o, $(TEST_SRC)) $(patsubst $(SOURCE_DIR)/%.cpp, $(COVER_DIR)/src/%.o, $(LIB_SRC)) +COVER_DEP := $(COVER_OBJ:.o=.d) +COVER_BIN := blobs.cover + +DEBUG_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(DEBUG_DIR)/%.o, $(SRC)) +DEBUG_LIB_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(DEBUG_DIR)/%.o, $(LIB_SRC)) +DEBUG_DEP := $(DEBUG_OBJ:.o=.d) +DEBUG_BIN := blobs.debug + +PROFILE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(PROFILE_DIR)/%.o, $(SRC)) +PROFILE_LIB_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(PROFILE_DIR)/%.o, $(LIB_SRC)) +PROFILE_DEP := $(PROFILE_OBJ:.o=.d) +PROFILE_BIN := blobs.profile + +RELEASE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(RELEASE_DIR)/%.o, $(SRC)) +RELEASE_LIB_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(RELEASE_DIR)/%.o, $(LIB_SRC)) +RELEASE_DEP := $(RELEASE_OBJ:.o=.d) +RELEASE_BIN := blobs + +TEST_OBJ := $(patsubst $(TEST_SRC_DIR)/%.cpp, $(TEST_DIR)/%.o, $(TEST_SRC)) $(patsubst $(SOURCE_DIR)/%.cpp, $(TEST_DIR)/src/%.o, $(LIB_SRC)) +TEST_DEP := $(TEST_OBJ:.o=.d) +TEST_BIN := blobs.test + +OBJ := $(COVER_OBJ) $(DEBUG_OBJ) $(PROFILE_OBJ) $(RELEASE_OBJ) $(TEST_OBJ) +DEP := $(COVER_DEP) $(DEBUG_DEP) $(PROFILE_DEP) $(RELEASE_DEP) $(TEST_DEP) +BIN := $(COVER_BIN) $(DEBUG_BIN) $(PROFILE_BIN) $(RELEASE_BIN) $(TEST_BIN) + +release: $(RELEASE_BIN) + +info: + @echo "CXX: $(CXX)" + @echo "LDXX: $(LDXX)" + @echo + @echo "LIBS: $(LIBS)" + @echo + @echo "CPPFLAGS: $(CPPFLAGS)" + @echo "CXXFLAGS: $(CXXFLAGS)" + @echo "LDXXFLAGS: $(LDXXFLAGS)" + @echo "TESTFLAGS: $(TESTFLAGS)" + @echo "TESTLIBS: $(TESTLIBS)" + @echo + @-lsb_release -a + @git --version + @g++ --version + +all: $(BIN) + +cover: $(COVER_BIN) + +debug: $(DEBUG_BIN) + +profile: $(PROFILE_BIN) + +tests: $(TEST_BIN) + +run: $(ASSET_DEP) blobs + ./blobs + +gdb: $(ASSET_DEP) blobs.debug + gdb ./blobs.debug + +cachegrind: $(ASSET_DEP) blobs.profile + valgrind ./blobs.profile + +callgrind: $(ASSET_DEP) blobs.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 \ + ./blobs.profile + +test: blobs.test + @echo run: blobs.test + @./blobs.test + +coverage: blobs.cover + @echo run: blobs.cover + @./blobs.cover + +codecov: coverage + @echo run: codecov.io + @bash -c 'bash <(curl -s https://codecov.io/bash) -Z' + +lint: + @echo lint: source + @$(CPPCHECK) $(SOURCE_DIR) + @echo lint: tests + @$(CPPCHECK) -I $(SOURCE_DIR) $(TEST_SRC_DIR) + +clean: + rm -f $(OBJ) + rm -f $(DEP) + find build -type d -empty -delete + +distclean: clean + rm -f $(BIN) cachegrind.out.* callgrind.out.* + rm -Rf build client-saves saves + +.PHONY: all release cover debug profile tests run gdb cachegrind callgrind test coverage codecov lint clean distclean + +-include $(DEP) + + +$(COVER_BIN): $(COVER_OBJ) + @echo link: $@ + @$(LDXX) $(CXXFLAGS) $^ -o $@ $(LDXXFLAGS) $(TESTLIBS) $(COVER_FLAGS) + +$(COVER_DIR)/%.o: $(TEST_SRC_DIR)/%.cpp | $(COVER_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(COVER_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + +$(COVER_DIR)/src/%.o: $(SOURCE_DIR)/%.cpp | $(COVER_DIR) + @mkdir -p "$(@D)" + @echo compile: $@ + @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(COVER_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< + + +$(DEBUG_BIN): %.debug: $(DEBUG_DIR)/%.o $(DEBUG_LIB_OBJ) + @echo link: $@ + @$(LDXX) $(CXXFLAGS) $^ -o $@ $(LDXXFLAGS) $(DEBUG_FLAGS) + +$(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_BIN): %.profile: $(PROFILE_DIR)/%.o $(PROFILE_LIB_OBJ) + @echo link: $@ + @$(LDXX) $(CXXFLAGS) $^ -o $@ $(LDXXFLAGS) $(PROFILE_FLAGS) + +$(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"$@" $< + + +$(RELEASE_BIN): %: $(RELEASE_DIR)/%.o $(RELEASE_LIB_OBJ) + @echo link: $@ + @$(LDXX) $(CXXFLAGS) $^ -o $@ $(LDXXFLAGS) $(RELEASE_FLAGS) + +$(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"$@" $< + + +$(TEST_BIN): $(TEST_OBJ) + @echo link: $@ + @$(LDXX) $(CXXFLAGS) $^ -o $@ $(LDXXFLAGS) $(TESTLIBS) $(TEST_FLAGS) + +$(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"$@" $< + + +$(ASSET_DEP): .git/$(shell git symbolic-ref HEAD 2>/dev/null || echo HEAD) + @echo fetch: assets + @git submodule update --init >/dev/null + @touch $@ + +$(DIR): + @mkdir -p "$@" diff --git a/assets b/assets new file mode 160000 index 0000000..4494180 --- /dev/null +++ b/assets @@ -0,0 +1 @@ +Subproject commit 44941807e0e738083d2e2e79aeae3f06b0f7fcc2 diff --git a/src/app/init.cpp b/src/app/init.cpp new file mode 100644 index 0000000..34e0393 --- /dev/null +++ b/src/app/init.cpp @@ -0,0 +1,265 @@ +#include "init.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace { + +std::string sdl_error_append(std::string msg) { + const char *error = SDL_GetError(); + if (*error != '\0') { + msg += ": "; + msg += error; + SDL_ClearError(); + } + return msg; +} + +std::string net_error_append(std::string msg) { + const char *error = SDLNet_GetError(); + if (*error != '\0') { + msg += ": "; + msg += error; + } + return msg; +} + +std::string alut_error_append(ALenum num, std::string msg) { + const char *error = alutGetErrorString(num); + if (*error != '\0') { + msg += ": "; + msg += error; + } + return msg; +} + +} + +namespace blobs { + +AlutError::AlutError(ALenum num) +: std::runtime_error(alutGetErrorString(num)) { + +} + +AlutError::AlutError(ALenum num, const std::string &msg) +: std::runtime_error(alut_error_append(num, msg)) { + +} + + +NetError::NetError() +: std::runtime_error(SDLNet_GetError()) { + +} + +NetError::NetError(const std::string &msg) +: std::runtime_error(net_error_append(msg)) { + +} + + +SDLError::SDLError() +: std::runtime_error(SDL_GetError()) { + +} + +SDLError::SDLError(const std::string &msg) +: std::runtime_error(sdl_error_append(msg)) { + +} + + +InitSDL::InitSDL() { + if (SDL_Init(SDL_INIT_EVENTS) != 0) { + throw SDLError("SDL_Init(SDL_INIT_EVENTS)"); + } +} + +InitSDL::~InitSDL() { + SDL_Quit(); +} + + +InitVideo::InitVideo() { + if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) { + throw SDLError("SDL_InitSubSystem(SDL_INIT_VIDEO)"); + } + // SDL seems to start out in text input state + SDL_StopTextInput(); +} + +InitVideo::~InitVideo() { + SDL_QuitSubSystem(SDL_INIT_VIDEO); +} + + +InitIMG::InitIMG() { + if (IMG_Init(IMG_INIT_PNG) == 0) { + throw SDLError("IMG_Init(IMG_INIT_PNG)"); + } +} + +InitIMG::~InitIMG() { + IMG_Quit(); +} + + +InitNet::InitNet() { + if (SDLNet_Init() != 0) { + throw SDLError("SDLNet_Init()"); + } +} + +InitNet::~InitNet() { + SDLNet_Quit(); +} + + +InitTTF::InitTTF() { + if (TTF_Init() != 0) { + throw SDLError("TTF_Init()"); + } +} + +InitTTF::~InitTTF() { + TTF_Quit(); +} + + +InitAL::InitAL() { + if (!alutInit(nullptr, nullptr)) { + throw AlutError(alutGetError(), "alutInit"); + } +} + +InitAL::~InitAL() { + if (!alutExit()) { + AlutError e(alutGetError(), "alutExit"); + std::cerr << "error: " << e.what() << std::endl; + } +} + + +InitGL::InitGL(bool double_buffer, int sample_size) { + 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 (sample_size > 1) { + if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0) { + throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS)"); + } + if (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, sample_size) != 0) { + throw SDLError("SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES)"); + } + } +} + + +Window::Window() +: handle(SDL_CreateWindow( + "blobs", + SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + 960, 600, + SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE +)) { + if (!handle) { + throw SDLError("SDL_CreateWindow"); + } +} + +Window::~Window() { + SDL_DestroyWindow(handle); +} + +void Window::GrabInput() { + SDL_SetWindowGrab(handle, SDL_TRUE); +} + +void Window::ReleaseInput() { + SDL_SetWindowGrab(handle, SDL_FALSE); +} + +void Window::GrabMouse() { + if (SDL_SetRelativeMouseMode(SDL_TRUE) != 0) { + throw SDLError("SDL_SetRelativeMouseMode"); + } +} + +void Window::ReleaseMouse() { + if (SDL_SetRelativeMouseMode(SDL_FALSE) != 0) { + throw SDLError("SDL_SetRelativeMouseMode"); + } +} + +void Window::Flip() { + SDL_GL_SwapWindow(handle); +} + + +GLContext::GLContext(SDL_Window *win) +: ctx(SDL_GL_CreateContext(win)) { + if (!ctx) { + throw SDLError("SDL_GL_CreateContext"); + } +} + +GLContext::~GLContext() { + SDL_GL_DeleteContext(ctx); +} + + +InitGLEW::InitGLEW() { + glewExperimental = GL_TRUE; + GLenum glew_err = glewInit(); + if (glew_err != GLEW_OK) { + std::string msg("glewInit: "); + const GLubyte *errBegin = glewGetErrorString(glew_err); + const GLubyte *errEnd = errBegin; + while (*errEnd != '\0') { + ++errEnd; + } + msg.append(errBegin, errEnd); + throw std::runtime_error(msg); + } +} + + +InitHeadless::InitHeadless() +: init_sdl() +, init_net() { + +} + +Init::Init(bool double_buffer, int sample_size) +: init_video() +, init_img() +, init_ttf() +, init_gl(double_buffer, sample_size) +, window() +, ctx(window.Handle()) +, init_glew() { + +} + +} diff --git a/src/app/init.hpp b/src/app/init.hpp new file mode 100644 index 0000000..ba263c4 --- /dev/null +++ b/src/app/init.hpp @@ -0,0 +1,200 @@ +#ifndef BLOBS_APP_INIT_HPP_ +#define BLOBS_APP_INIT_HPP_ + +#include +#include +#include +#include + + +namespace blobs { + +class AlutError +: public std::runtime_error { + +public: + explicit AlutError(ALenum); + AlutError(ALenum, const std::string &); + +}; + +class SDLError +: public std::runtime_error { + +public: + SDLError(); + explicit SDLError(const std::string &); + +}; + +class NetError +: public std::runtime_error { + +public: + NetError(); + explicit NetError(const std::string &); + +}; + + +class InitSDL { + +public: + InitSDL(); + ~InitSDL(); + + InitSDL(const InitSDL &) = delete; + InitSDL &operator =(const InitSDL &) = delete; + +}; + + +class InitVideo { + +public: + InitVideo(); + ~InitVideo(); + + InitVideo(const InitVideo &) = delete; + InitVideo &operator =(const InitVideo &) = delete; + +}; + + +class InitIMG { + +public: + InitIMG(); + ~InitIMG(); + + InitIMG(const InitIMG &) = delete; + InitIMG &operator =(const InitIMG &) = delete; + +}; + + +class InitNet { + +public: + InitNet(); + ~InitNet(); + + InitNet(const InitNet &) = delete; + InitNet &operator =(const InitNet &) = delete; + +}; + + +class InitTTF { + +public: + InitTTF(); + ~InitTTF(); + + InitTTF(const InitTTF &) = delete; + InitTTF &operator =(const InitTTF &) = delete; + +}; + + +class InitAL { + +public: + InitAL(); + ~InitAL(); + + InitAL(const InitAL &) = delete; + InitAL &operator =(const InitAL &) = delete; + +}; + + +class InitGL { + +public: + explicit InitGL(bool double_buffer = true, int sample_size = 1); + + InitGL(const InitGL &) = delete; + InitGL &operator =(const InitGL &) = delete; + +}; + + +class Window { + +public: + Window(); + ~Window(); + + Window(const Window &) = delete; + Window &operator =(const Window &) = delete; + + void GrabInput(); + void ReleaseInput(); + + void GrabMouse(); + void ReleaseMouse(); + + SDL_Window *Handle() { return handle; } + + void Flip(); + +private: + SDL_Window *handle; + +}; + + +class GLContext { + +public: + explicit GLContext(SDL_Window *); + ~GLContext(); + + GLContext(const GLContext &) = delete; + GLContext &operator =(const GLContext &) = delete; + +private: + SDL_GLContext ctx; + +}; + + +class InitGLEW { + +public: + InitGLEW(); + + InitGLEW(const InitGLEW &) = delete; + InitGLEW &operator =(const InitGLEW &) = delete; + +}; + + +struct InitHeadless { + + InitHeadless(); + + InitSDL init_sdl; + InitNet init_net; + +}; + +struct Init { + + Init(bool double_buffer = true, int sample_size = 1); + + InitVideo init_video; + InitIMG init_img; + InitTTF init_ttf; + InitAL init_al; + InitGL init_gl; + Window window; + GLContext ctx; + InitGLEW init_glew; + +}; + +} + +#endif diff --git a/src/blobs.cpp b/src/blobs.cpp new file mode 100644 index 0000000..30571ac --- /dev/null +++ b/src/blobs.cpp @@ -0,0 +1,10 @@ +#include "app/init.hpp" + +#include +#include + +using namespace blobs; + +int main(int argc, char *argv[]) { + Init init; +}