]> git.localhorst.tv Git - blank.git/commitdiff
moved some stuff around
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 11 Feb 2015 23:07:49 +0000 (00:07 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 11 Feb 2015 23:07:49 +0000 (00:07 +0100)
src/app.cpp [new file with mode: 0644]
src/app.hpp [new file with mode: 0644]
src/main.cpp
src/shader.cpp
src/shader.hpp

diff --git a/src/app.cpp b/src/app.cpp
new file mode 100644 (file)
index 0000000..d54e8df
--- /dev/null
@@ -0,0 +1,144 @@
+#include "app.hpp"
+
+#include <iostream>
+#include <stdexcept>
+
+
+namespace {
+
+constexpr GLfloat vtx_coords[] = {
+       -1.0f, -1.0f, -1.0f,
+        1.0f, -1.0f, -1.0f,
+        0.0f,  1.0f, -1.0f,
+};
+
+}
+
+namespace blank {
+
+Application::Application()
+: init_sdl()
+, init_gl()
+, window()
+, ctx(window.CreateContext())
+, init_glew()
+, program()
+, vtx_buf(0)
+, mvp()
+, mvp_handle(0)
+, running(false) {
+       GLContext::EnableVSync();
+       program.LoadShader(
+               GL_VERTEX_SHADER,
+               "#version 330 core\n"
+               "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
+               "uniform mat4 MVP;\n"
+               "void main() {\n"
+                       "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
+                       "gl_Position = MVP * v;\n"
+               "}\n"
+       );
+       program.LoadShader(
+               GL_FRAGMENT_SHADER,
+               "#version 330 core\n"
+               "out vec3 color;\n"
+               "void main() {\n"
+                       "color = vec3(1, 1, 1);\n"
+               "}\n"
+       );
+       program.Link();
+       if (!program.Linked()) {
+               program.Log(std::cerr);
+               throw std::runtime_error("link program");
+       }
+
+       GLuint VertexArrayID;
+       glGenVertexArrays(1, &VertexArrayID);
+       glBindVertexArray(VertexArrayID);
+
+       glGenBuffers(1, &vtx_buf);
+       glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
+       glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
+
+       glm::mat4 projection = glm::perspective(
+               45.0f, // FOV in degrees
+               1.0f,  // aspect ratio
+               0.1f,  // near clip
+               100.0f // far clip
+       );
+       glm::mat4 view = glm::lookAt(
+               glm::vec3(0, 0,  0),  // observer
+               glm::vec3(0, 0, -1), // target
+               glm::vec3(0, 1,  0) // up
+       );
+       glm::mat4 model(1.0f); // identity: no transformation
+       mvp = projection * view * model;
+
+       mvp_handle = program.UniformLocation("MVP");
+
+       glClearColor(0.0, 0.0, 0.0, 1.0);
+}
+
+Application::~Application() {
+
+}
+
+
+void Application::Run() {
+       running = true;
+       Uint32 last = SDL_GetTicks();
+       while (running) {
+               Uint32 now = SDL_GetTicks();
+               int delta = now - last;
+               Loop(delta);
+               last = now;
+       }
+}
+
+void Application::Loop(int dt) {
+       HandleEvents();
+       Render();
+}
+
+
+void Application::HandleEvents() {
+       SDL_Event event;
+       while (SDL_PollEvent(&event)) {
+               switch (event.type) {
+                       case SDL_QUIT:
+                               running = false;
+                               break;
+                       default:
+                               break;
+               }
+       }
+}
+
+void Application::Render() {
+       glClear(GL_COLOR_BUFFER_BIT);
+
+       program.Use();
+
+       glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
+
+       glEnableVertexAttribArray(0);
+       glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
+       glVertexAttribPointer(
+               0,        // attribute 0 (for shader)
+               3,        // size
+               GL_FLOAT, // type
+               GL_FALSE, // normalized
+               0,        // stride
+               nullptr   // offset
+       );
+       glDrawArrays(
+               GL_TRIANGLES, // how
+               0,            // start
+               3             // len
+       );
+       glDisableVertexAttribArray(0);
+
+       window.Flip();
+}
+
+}
diff --git a/src/app.hpp b/src/app.hpp
new file mode 100644 (file)
index 0000000..9ec5f1f
--- /dev/null
@@ -0,0 +1,46 @@
+#ifndef BLANK_APP_HPP_
+#define BLANK_APP_HPP_
+
+#include <glm/glm.hpp>
+#include <glm/gtc/matrix_transform.hpp>
+
+#include "init.hpp"
+#include "shader.hpp"
+
+
+namespace blank {
+
+class Application {
+
+public:
+       Application();
+       ~Application();
+
+       Application(const Application &) = delete;
+       Application &operator =(const Application &) = delete;
+
+       void Run();
+       void Loop(int dt);
+
+       void HandleEvents();
+       void Render();
+
+private:
+       InitSDL init_sdl;
+       InitGL init_gl;
+       Window window;
+       GLContext ctx;
+       InitGLEW init_glew;
+       Program program;
+
+       GLuint vtx_buf;
+       glm::mat4 mvp;
+       GLuint mvp_handle;
+
+       bool running;
+
+};
+
+}
+
+#endif
index 33071f1ef3510f6d088feea27273245097d78493..06771fe8c4b76d1df04ee9292dd92088eb2428d0 100644 (file)
-#include <iostream>
-#include <SDL.h>
-#include <GL/glew.h>
-#include <glm/glm.hpp>
-#include <glm/gtc/matrix_transform.hpp>
+#include "app.hpp"
 
-#include "init.hpp"
-#include "shader.hpp"
-
-using namespace std;
 using namespace blank;
 
 
-constexpr GLfloat vtx_coords[] = {
-       -1.0f, -1.0f, -1.0f,
-        1.0f, -1.0f, -1.0f,
-        0.0f,  1.0f, -1.0f,
-};
-
-
 int main(int argc, char *argv[]) {
 
-       InitSDL init_sdl;
-       InitGL init_gl;
-       Window window;
-
-       GLContext ctx = window.CreateContext();
-       InitGLEW init_glew;
-       GLContext::EnableVSync();
-
-
-       Shader vtx_shader(GL_VERTEX_SHADER);
-       vtx_shader.Source(
-               "#version 330 core\n"
-               "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
-               "uniform mat4 MVP;\n"
-               "void main() {\n"
-                       "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
-                       "gl_Position = MVP * v;\n"
-               "}\n"
-       );
-       vtx_shader.Compile();
-
-       if (!vtx_shader.Compiled()) {
-               cerr << "vertex shader compile error" << endl;
-               vtx_shader.Log(cerr);
-               return 4;
-       }
-
-       Shader frag_shader(GL_FRAGMENT_SHADER);
-       frag_shader.Source(
-               "#version 330 core\n"
-               "out vec3 color;\n"
-               "void main() {\n"
-                       "color = vec3(1, 1, 1);\n"
-               "}\n"
-       );
-       frag_shader.Compile();
-
-       if (!frag_shader.Compiled()) {
-               cerr << "fragment shader compile error" << endl;
-               frag_shader.Log(cerr);
-               return 4;
-       }
-
-
-       Program program;
-       program.Attach(vtx_shader);
-       program.Attach(frag_shader);
-       program.Link();
-
-       if (!program.Linked()) {
-               cerr << "program link error" << endl;
-               program.Log(cerr);
-               return 4;
-       }
-
-
-       GLuint VertexArrayID;
-       glGenVertexArrays(1, &VertexArrayID);
-       glBindVertexArray(VertexArrayID);
-
-
-       GLuint vtx_buf;
-       glGenBuffers(1, &vtx_buf);
-       glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
-       glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
-
-
-       glm::mat4 projection = glm::perspective(
-               45.0f, // FOV in degrees
-               1.0f,  // aspect ratio
-               0.1f,  // near clip
-               100.0f // far clip
-       );
-       glm::mat4 view = glm::lookAt(
-               glm::vec3(0, 0,  0),  // observer
-               glm::vec3(0, 0, -1), // target
-               glm::vec3(0, 1,  0) // up
-       );
-       glm::mat4 model(1.0f); // identity: no transformation
-       glm::mat4 mvp = projection * view * model;
-
-       GLuint mvp_id = program.UniformLocation("MVP");
-
-
-       glClearColor(0.0, 0.0, 0.0, 1.0);
-
-
-       bool running = true;
-       Uint32 last = SDL_GetTicks();
-       while (running) {
-               Uint32 now = SDL_GetTicks();
-               int delta = now - last;
-
-               SDL_Event event;
-               while (SDL_PollEvent(&event)) {
-                       switch (event.type) {
-                               case SDL_QUIT:
-                                       running = false;
-                                       break;
-                               default:
-                                       break;
-                       }
-               }
-
-               glClear(GL_COLOR_BUFFER_BIT);
-
-               program.Use();
-
-               glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp[0][0]);
-
-               glEnableVertexAttribArray(0);
-               glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
-               glVertexAttribPointer(
-                       0,        // attribute 0 (for shader)
-                       3,        // size
-                       GL_FLOAT, // type
-                       GL_FALSE, // normalized
-                       0,        // stride
-                       nullptr   // offset
-               );
-               glDrawArrays(
-                       GL_TRIANGLES, // how
-                       0,            // start
-                       3             // len
-               );
-               glDisableVertexAttribArray(0);
-
-               window.Flip();
-
-               last = now;
-       }
+       Application app;
+       app.Run();
 
        return 0;
 
index 985b96e5faf9981374f776bffe74fbb25d6a0430..c42e6fcaabc272f0c06f4ea4203cad6d1bea6102 100644 (file)
@@ -1,6 +1,7 @@
 #include "shader.hpp"
 
 #include <algorithm>
+#include <iostream>
 #include <memory>
 #include <ostream>
 #include <stdexcept>
@@ -93,6 +94,19 @@ Program::~Program() {
 }
 
 
+const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
+       shaders.emplace_back(type);
+       Shader &shader = shaders.back();
+       shader.Source(src);
+       shader.Compile();
+       if (!shader.Compiled()) {
+               shader.Log(std::cerr);
+               throw std::runtime_error("compile shader");
+       }
+       Attach(shader);
+       return shader;
+}
+
 void Program::Attach(Shader &shader) {
        shader.AttachToProgram(handle);
 }
index c112c533e42109bca7e75ce2839c06f6e82eefa8..aefcffe1f8b54e819f92eb895e3cebfcba9f6cf0 100644 (file)
@@ -2,6 +2,7 @@
 #define BLANK_SHADER_HPP_
 
 #include <iosfwd>
+#include <list>
 #include <GL/glew.h>
 
 
@@ -41,6 +42,7 @@ public:
        Program(const Program &) = delete;
        Program &operator =(const Program &) = delete;
 
+       const Shader &LoadShader(GLenum type, const GLchar *src);
        void Attach(Shader &);
        void Link();
        bool Linked() const;
@@ -52,6 +54,7 @@ public:
 
 private:
        GLuint handle;
+       std::list<Shader> shaders;
 
 };