diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-30 21:20:41 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-30 21:20:41 +0100 |
commit | ecf705157258058de5547eaa6905c3eb1968fa59 (patch) | |
tree | df7b909744e0f331692bf0c43cefd9010abb98f3 | |
parent | 370dc005d99d664f74991411f58026f7a90caa1b (diff) | |
download | Arider-ecf705157258058de5547eaa6905c3eb1968fa59.tar.gz Arider-ecf705157258058de5547eaa6905c3eb1968fa59.zip |
Adding shader support
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | CMakeLists.txt | 45 | ||||
-rw-r--r-- | Makefile | 252 | ||||
-rw-r--r-- | config/arider_config.hpp.in | 5 | ||||
-rw-r--r-- | include/arider_config.hpp | 5 | ||||
-rw-r--r-- | include/game.hpp | 32 | ||||
-rw-r--r-- | include/glsl_program.hpp | 24 | ||||
-rw-r--r-- | include/sprite.hpp | 22 | ||||
-rw-r--r-- | res/shaders/color_shading.frag | 8 | ||||
-rw-r--r-- | res/shaders/color_shading.vert | 10 | ||||
-rw-r--r-- | src/arider.cpp | 29 | ||||
-rw-r--r-- | src/game.cpp | 77 | ||||
-rw-r--r-- | src/glsl_program.cpp | 73 | ||||
-rw-r--r-- | src/main.cpp | 7 | ||||
-rw-r--r-- | src/sprite.cpp | 59 |
15 files changed, 624 insertions, 30 deletions
@@ -1,7 +1,13 @@ # general directories to ignore bin/ build/ +CMakeFiles/ # extensions to ignore *.o *.out + +# general files to ignore +CMakeCache.txt +cmake_install.cmake +Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e8c265c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 2.8) +project(arider) + +# set version number +set(ARIDER_MAJOR_VERSION 0) +set(ARIDER_MINOR_VERSION 1) +set(ARIDER_PATCH_VERSION 0) +set(ARIDER_VERSION + ${ARIDER_MAJOR_VERSION}.${ARIDER_MINOR_VERSION}.${ARIDER_PATCH_VERSION}) + +# set standard +set(CMAKE_CXX_STANDARD 14) + +# set include directories +set(ARIDER_INCLUDE_DIR "include/") + +# setting right output directory +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin/") + +# setting up configuration header +configure_file ( + "config/arider_config.hpp.in" + "include/arider_config.hpp" + ) + +# adding my executable +add_executable(${PROJECT_NAME} "src/arider.cpp" "src/game.cpp" "src/sprite.cpp") + +# adding sdl2 library +include(FindPkgConfig) +find_package(OpenGL REQUIRED) +find_package(GLEW REQUIRED) +pkg_search_module(SDL2 REQUIRED sdl2) + +# adding include directories +include_directories(${ARIDER_INCLUDE_DIR} + ${OPENGL_INCLUDE_DIRS} + ${GLEW_INCLUDE_DIRS} + ${SDL2_INCLUDE_DIRS}) + +# link libraries +target_link_libraries(${PROJECT_NAME} + ${OPENGL_LIBRARIES} + ${GLEW_LIBRARIES} + ${SDL2_LIBRARIES}) @@ -1,32 +1,238 @@ -# Make g++ the default compiler -CC := g++ +# CMAKE generated file: DO NOT EDIT! +# Generated by "Unix Makefiles" Generator, CMake Version 3.7 -# Add the compilation arguments -CFLAGS := -g -Wall -Wextra -Wpedantic -std=c++14 +# Default target executed when no arguments are given to make. +default_target: all -# Source files to be compiled -SRCS := $(wildcard src/*.cpp) -OBJS := $(patsubst src/%,build/%,$(SRCS:.cpp=.o)) -INCL := -Iinclude/ +.PHONY : default_target -# Executable name -EXEC := bin/arider +# Allow only one "make -f Makefile2" at a time, but pass parallelism. +.NOTPARALLEL: -# Rules -.PHONY: all -all: $(EXEC) -$(EXEC): $(OBJS) - $(CC) -o $@ $^ +#============================================================================= +# Special targets provided by cmake. -build/%.o: src/%.cpp - $(CC) $(CFLAGS) -c -o $@ $< +# Disable implicit rules so canonical targets will work. +.SUFFIXES: -# Cleans up the directories -.PHONY: clean + +# Remove some rules from gmake that .SUFFIXES does not remove. +SUFFIXES = + +.SUFFIXES: .hpux_make_needs_suffix_list + + +# Suppress display of executed commands. +$(VERBOSE).SILENT: + + +# A target that is always out of date. +cmake_force: + +.PHONY : cmake_force + +#============================================================================= +# Set environment variables for the build. + +# The shell in which to execute make rules. +SHELL = /bin/sh + +# The CMake executable. +CMAKE_COMMAND = /usr/bin/cmake + +# The command to remove a file. +RM = /usr/bin/cmake -E remove -f + +# Escaping for special characters. +EQUALS = = + +# The top-level source directory on which CMake was run. +CMAKE_SOURCE_DIR = /home/yannherklotz/Github/Arider + +# The top-level build directory on which CMake was run. +CMAKE_BINARY_DIR = /home/yannherklotz/Github/Arider + +#============================================================================= +# Targets provided globally by CMake. + +# Special rule for the target rebuild_cache +rebuild_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." + /usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : rebuild_cache + +# Special rule for the target rebuild_cache +rebuild_cache/fast: rebuild_cache + +.PHONY : rebuild_cache/fast + +# Special rule for the target edit_cache +edit_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..." + /usr/bin/ccmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : edit_cache + +# Special rule for the target edit_cache +edit_cache/fast: edit_cache + +.PHONY : edit_cache/fast + +# The main all target +all: cmake_check_build_system + $(CMAKE_COMMAND) -E cmake_progress_start /home/yannherklotz/Github/Arider/CMakeFiles /home/yannherklotz/Github/Arider/CMakeFiles/progress.marks + $(MAKE) -f CMakeFiles/Makefile2 all + $(CMAKE_COMMAND) -E cmake_progress_start /home/yannherklotz/Github/Arider/CMakeFiles 0 +.PHONY : all + +# The main clean target clean: - rm -rf build/ - rm -rf bin/ + $(MAKE) -f CMakeFiles/Makefile2 clean +.PHONY : clean + +# The main clean target +clean/fast: clean + +.PHONY : clean/fast + +# Prepare targets for installation. +preinstall: all + $(MAKE) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall + +# Prepare targets for installation. +preinstall/fast: + $(MAKE) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall/fast + +# clear depends +depend: + $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 +.PHONY : depend + +#============================================================================= +# Target rules for targets named arider + +# Build rule for target. +arider: cmake_check_build_system + $(MAKE) -f CMakeFiles/Makefile2 arider +.PHONY : arider + +# fast build rule for target. +arider/fast: + $(MAKE) -f CMakeFiles/arider.dir/build.make CMakeFiles/arider.dir/build +.PHONY : arider/fast + +src/arider.o: src/arider.cpp.o + +.PHONY : src/arider.o + +# target to build an object file +src/arider.cpp.o: + $(MAKE) -f CMakeFiles/arider.dir/build.make CMakeFiles/arider.dir/src/arider.cpp.o +.PHONY : src/arider.cpp.o + +src/arider.i: src/arider.cpp.i + +.PHONY : src/arider.i + +# target to preprocess a source file +src/arider.cpp.i: + $(MAKE) -f CMakeFiles/arider.dir/build.make CMakeFiles/arider.dir/src/arider.cpp.i +.PHONY : src/arider.cpp.i + +src/arider.s: src/arider.cpp.s + +.PHONY : src/arider.s + +# target to generate assembly for a file +src/arider.cpp.s: + $(MAKE) -f CMakeFiles/arider.dir/build.make CMakeFiles/arider.dir/src/arider.cpp.s +.PHONY : src/arider.cpp.s + +src/game.o: src/game.cpp.o + +.PHONY : src/game.o + +# target to build an object file +src/game.cpp.o: + $(MAKE) -f CMakeFiles/arider.dir/build.make CMakeFiles/arider.dir/src/game.cpp.o +.PHONY : src/game.cpp.o + +src/game.i: src/game.cpp.i + +.PHONY : src/game.i + +# target to preprocess a source file +src/game.cpp.i: + $(MAKE) -f CMakeFiles/arider.dir/build.make CMakeFiles/arider.dir/src/game.cpp.i +.PHONY : src/game.cpp.i + +src/game.s: src/game.cpp.s + +.PHONY : src/game.s + +# target to generate assembly for a file +src/game.cpp.s: + $(MAKE) -f CMakeFiles/arider.dir/build.make CMakeFiles/arider.dir/src/game.cpp.s +.PHONY : src/game.cpp.s + +src/sprite.o: src/sprite.cpp.o + +.PHONY : src/sprite.o + +# target to build an object file +src/sprite.cpp.o: + $(MAKE) -f CMakeFiles/arider.dir/build.make CMakeFiles/arider.dir/src/sprite.cpp.o +.PHONY : src/sprite.cpp.o + +src/sprite.i: src/sprite.cpp.i + +.PHONY : src/sprite.i + +# target to preprocess a source file +src/sprite.cpp.i: + $(MAKE) -f CMakeFiles/arider.dir/build.make CMakeFiles/arider.dir/src/sprite.cpp.i +.PHONY : src/sprite.cpp.i + +src/sprite.s: src/sprite.cpp.s + +.PHONY : src/sprite.s + +# target to generate assembly for a file +src/sprite.cpp.s: + $(MAKE) -f CMakeFiles/arider.dir/build.make CMakeFiles/arider.dir/src/sprite.cpp.s +.PHONY : src/sprite.cpp.s + +# Help Target +help: + @echo "The following are some of the valid targets for this Makefile:" + @echo "... all (the default if no target is provided)" + @echo "... clean" + @echo "... depend" + @echo "... rebuild_cache" + @echo "... edit_cache" + @echo "... arider" + @echo "... src/arider.o" + @echo "... src/arider.i" + @echo "... src/arider.s" + @echo "... src/game.o" + @echo "... src/game.i" + @echo "... src/game.s" + @echo "... src/sprite.o" + @echo "... src/sprite.i" + @echo "... src/sprite.s" +.PHONY : help + + + +#============================================================================= +# Special targets to cleanup operation of make. + +# Special rule to run CMake to check the build system integrity. +# No rule that depends on this can have commands that come from listfiles +# because they might be regenerated. +cmake_check_build_system: + $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 +.PHONY : cmake_check_build_system -# Make necessary directories if they aren't there yet -$(shell mkdir -p bin/ build/) diff --git a/config/arider_config.hpp.in b/config/arider_config.hpp.in new file mode 100644 index 0000000..5674160 --- /dev/null +++ b/config/arider_config.hpp.in @@ -0,0 +1,5 @@ +// the configured options and settings for Arider +#define ARIDER_MAJOR_VERSION @ARIDER_MAJOR_VERSION@ +#define ARIDER_MINOR_VERSION @ARIDER_MINOR_VERSION@ +#define ARIDER_PATCH_VERSION @ARIDER_PATCH_VERSION@ +#define ARIDER_VERSION @ARIDER_VERSION@ diff --git a/include/arider_config.hpp b/include/arider_config.hpp new file mode 100644 index 0000000..9afa2e9 --- /dev/null +++ b/include/arider_config.hpp @@ -0,0 +1,5 @@ +// the configured options and settings for Arider +#define ARIDER_MAJOR_VERSION 0 +#define ARIDER_MINOR_VERSION 1 +#define ARIDER_PATCH_VERSION 0 +#define ARIDER_VERSION 0.1.0 diff --git a/include/game.hpp b/include/game.hpp new file mode 100644 index 0000000..21bcf7c --- /dev/null +++ b/include/game.hpp @@ -0,0 +1,32 @@ +#ifndef GAME_HPP +#define GAME_HPP + +#include <SDL2/SDL.h> +#include <GL/glew.h> + +enum class GameState +{ + PLAY, + EXIT +}; + +class Game +{ +private: + SDL_Window *window_ = nullptr; + int width_ = 1280; + int height_ = 720; + GameState game_state_ = GameState::PLAY; + + void initSystems(); + void processInput(); + void gameLoop(); + void drawGame(); +public: + Game(); + ~Game(); + + void run(); +}; + +#endif diff --git a/include/glsl_program.hpp b/include/glsl_program.hpp new file mode 100644 index 0000000..dd31b34 --- /dev/null +++ b/include/glsl_program.hpp @@ -0,0 +1,24 @@ +#ifndef GLSL_PROGRAM_HPP +#define GLSL_PROGRAM_HPP + +#include <GL/glew.h> +#include <string> + +class GlslProgram +{ +private: + GLuint program_id_ = 0; + GLuint vertex_shader_id_ = 0; + GLuint fragment_shader_id_ = 0; + + void compileShader(const GLuint &shader, const std::string &file_path); +public: + GlslProgram(); + ~GlslProgram(); + + void compileShaders(const std::string &vertex_shader_path, const std::string &fragment_shader_path); + void linkShaders(); +}; + + +#endif diff --git a/include/sprite.hpp b/include/sprite.hpp new file mode 100644 index 0000000..65ea45b --- /dev/null +++ b/include/sprite.hpp @@ -0,0 +1,22 @@ +#ifndef SPRITE_HPP +#define SPRITE_HPP + +#include <GL/glew.h> + +class Sprite +{ +private: + float x_; + float y_; + float width_; + float height_; + GLuint vbo_id_ = 0; +public: + Sprite(); + ~Sprite(); + + void init(float x, float y, float width, float height); + void draw(); +}; + +#endif diff --git a/res/shaders/color_shading.frag b/res/shaders/color_shading.frag new file mode 100644 index 0000000..c5ce8f0 --- /dev/null +++ b/res/shaders/color_shading.frag @@ -0,0 +1,8 @@ +#version 130 + +out vec3 color; + +void main() +{ + color = vec3(1.0, 0.0, 0.0); +} diff --git a/res/shaders/color_shading.vert b/res/shaders/color_shading.vert new file mode 100644 index 0000000..9bb8304 --- /dev/null +++ b/res/shaders/color_shading.vert @@ -0,0 +1,10 @@ +#version 130 + +in vec2 vertex_position; + +void main() +{ + gl_Position.xy = vertex_position; + gl_Position.z = 0.0; + gl_Position.w = 1.0; +} diff --git a/src/arider.cpp b/src/arider.cpp new file mode 100644 index 0000000..0bb2f96 --- /dev/null +++ b/src/arider.cpp @@ -0,0 +1,29 @@ +#include "arider_config.hpp" +#include "game.hpp" + +#include <exception> +#include <iostream> +#include <SDL2/SDL.h> + +int main() +{ + std::cout<<"Arider\nVersion: "<<ARIDER_MAJOR_VERSION<<"."<<ARIDER_MINOR_VERSION + <<"."<<ARIDER_PATCH_VERSION<<std::endl; + + Game game; + + try + { + game.run(); + } + catch(std::exception &e) + { + std::cerr<<"Error : "<<e.what()<<std::endl; + } + catch(...) + { + std::cerr<<"Error : Exception occured"<<std::endl; + } + + return 0; +} diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..196b735 --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,77 @@ +#include "game.hpp" +#include "sprite.hpp" + +#include <stdexcept> + +Game::Game() +{} + +Game::~Game() +{ + SDL_DestroyWindow(window_); + SDL_Quit(); +} + +void Game::initSystems() +{ + if(SDL_Init(SDL_INIT_VIDEO)) + throw std::runtime_error("SDL_Init failed"); + + window_ = SDL_CreateWindow("Arider", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + width_, height_, SDL_WINDOW_OPENGL); + if(window_ == nullptr) + throw std::runtime_error("SDL_CreateWindow failed"); + + SDL_GLContext glContext = SDL_GL_CreateContext(window_); + if(glContext == nullptr) + throw std::runtime_error("SDL_GL_CreateContext failed"); + + GLenum error = glewInit(); + if(error != GLEW_OK) + throw std::runtime_error("glewInit failed"); + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + glClearColor(0.f, 0.f, 1.f, 1.f); +} + +void Game::processInput() +{ + SDL_Event event; + + while(SDL_PollEvent(&event)) + { + switch(event.type) + { + case SDL_QUIT: + game_state_ = GameState::EXIT; + } + } +} + +void Game::gameLoop() +{ + Sprite sprite; + sprite.init(-1.f, -1.f, 1.f, 1.f); + while(game_state_ != GameState::EXIT) + { + processInput(); + sprite.draw(); + drawGame(); + } +} + +void Game::drawGame() +{ + glClearDepth(1.0); + // clears buffer with clear color + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // swap buffer and draw everything to the screen + SDL_GL_SwapWindow(window_); +} + +void Game::run() +{ + initSystems(); + gameLoop(); +} diff --git a/src/glsl_program.cpp b/src/glsl_program.cpp new file mode 100644 index 0000000..2af956e --- /dev/null +++ b/src/glsl_program.cpp @@ -0,0 +1,73 @@ +#include "glsl_program.hpp" + +#include <fstream> +#include <stdexcept> +#include <vector> + +GlslProgram::GlslProgram() +{} + +GlslProgram::~GlslProgram() +{ + if(fragment_shader_id_ != 0) + glDeleteShader(fragment_shader_id_); + + if(vertex_shader_id_ != 0) + glDeleteShader(vertex_shader_id_); + + if(program_id_ != 0) + glDeleteProgram(program_id_); +} + +void GlslProgram::compileShader(const GLuint &shader, const std::string &file_path) +{ + std::ifstream file(file_path); + if(!file.is_open()) + throw std::runtime_error("Failed to open '"+file_path+"'"); + + std::string content = ""; + std::string line; + + while(std::getline(file, line)) + content+=line+"\n"; + file.close(); + + const char *vertex_char = content.c_str(); + glShaderSource(shader, 1, &vertex_char, nullptr); + glCompileShader(shader); + + GLint is_compiled = 0; + glGetShaderiv(shader, GL_COMPILE_STATUS, &is_compiled); + + if(is_compiled == GL_FALSE) + { + GLint max_length = 0; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &max_length); + + std::vector<char> error_log(max_length); + glGetShaderInfoLog(shader, max_length, &max_length, &error_log[0]); + std::string error_log_str(&error_log[0]); + + throw std::runtime_error("Couldn't compile "+file_path+" : "+error_log_str); + } +} + +void GlslProgram::compileShaders(const std::string &vertex_shader_path, const std::string &fragment_shader_path) +{ + // create the program that will be run on GPU + program_id_ = glCreateProgram(); + + vertex_shader_id_ = glCreateShader(GL_VERTEX_SHADER); + if(vertex_shader_id_ == 0) + throw std::runtime_error("Vertex shader failed to be created"); + + fragment_shader_id_ = glCreateShader(GL_VERTEX_SHADER); + if(fragment_shader_id_ == 0) + throw std::runtime_error("Fragment shader failed to be created"); + + std::string vertex_shader_content; + std::string fragment_shader_content; + + compileShader(vertex_shader_id_, vertex_shader_path); + compileShader(fragment_shader_id_, fragment_shader_path); +} diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index 5260233..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include <cstdio> - -int main(int, char**) -{ - printf("Hello World\n"); - return 0; -} diff --git a/src/sprite.cpp b/src/sprite.cpp new file mode 100644 index 0000000..34d7864 --- /dev/null +++ b/src/sprite.cpp @@ -0,0 +1,59 @@ +#include "sprite.hpp" + +Sprite::Sprite() +{} + +Sprite::~Sprite() +{ + if(vbo_id_ != 0) + glDeleteBuffers(1, &vbo_id_); +} + +void Sprite::init(float x, float y, float width, float height) +{ + x_ = x; + y_ = y; + width_ = width; + height_ = height; + + if(vbo_id_ == 0) + { + glGenBuffers(1, &vbo_id_); + } + + float vertex_data[12]; + + vertex_data[0] = x+width; + vertex_data[1] = y+height; + + vertex_data[2] = x; + vertex_data[3] = y+height; + + vertex_data[4] = x; + vertex_data[5] = y; + + vertex_data[6] = x+width; + vertex_data[7] = y; + + vertex_data[8] = x; + vertex_data[9] = y; + + vertex_data[10] = x+width; + vertex_data[11] = y+height; + + glBindBuffer(GL_ARRAY_BUFFER, vbo_id_); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +void Sprite::draw() +{ + glBindBuffer(GL_ARRAY_BUFFER, vbo_id_); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); + glDrawArrays(GL_TRIANGLES, 0, 6); + + glDisableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} |