aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-03-30 21:20:41 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-03-30 21:20:41 +0100
commitecf705157258058de5547eaa6905c3eb1968fa59 (patch)
treedf7b909744e0f331692bf0c43cefd9010abb98f3 /src
parent370dc005d99d664f74991411f58026f7a90caa1b (diff)
downloadArider-ecf705157258058de5547eaa6905c3eb1968fa59.tar.gz
Arider-ecf705157258058de5547eaa6905c3eb1968fa59.zip
Adding shader support
Diffstat (limited to 'src')
-rw-r--r--src/arider.cpp29
-rw-r--r--src/game.cpp77
-rw-r--r--src/glsl_program.cpp73
-rw-r--r--src/main.cpp7
-rw-r--r--src/sprite.cpp59
5 files changed, 238 insertions, 7 deletions
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);
+}