]> git.localhorst.tv Git - blank.git/blob - src/app.cpp
add SDL2_image library
[blank.git] / src / app.cpp
1 #include "app.hpp"
2
3 #include <iostream>
4 #include <stdexcept>
5
6
7 namespace {
8
9 constexpr GLfloat vtx_coords[] = {
10         -1.0f, -1.0f, -5.0f,
11          1.0f, -1.0f, -5.0f,
12          0.0f,  1.0f, -5.0f,
13 };
14
15 }
16
17 namespace blank {
18
19 Application::Application()
20 : init_sdl()
21 , init_img()
22 , init_gl()
23 , window()
24 , ctx(window.CreateContext())
25 , init_glew()
26 , program()
27 , cam()
28 , vtx_buf(0)
29 , mvp_handle(0)
30 , running(false) {
31         GLContext::EnableVSync();
32         program.LoadShader(
33                 GL_VERTEX_SHADER,
34                 "#version 330 core\n"
35                 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
36                 "uniform mat4 MVP;\n"
37                 "void main() {\n"
38                         "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
39                         "gl_Position = MVP * v;\n"
40                 "}\n"
41         );
42         program.LoadShader(
43                 GL_FRAGMENT_SHADER,
44                 "#version 330 core\n"
45                 "out vec3 color;\n"
46                 "void main() {\n"
47                         "color = vec3(1, 1, 1);\n"
48                 "}\n"
49         );
50         program.Link();
51         if (!program.Linked()) {
52                 program.Log(std::cerr);
53                 throw std::runtime_error("link program");
54         }
55
56         GLuint VertexArrayID;
57         glGenVertexArrays(1, &VertexArrayID);
58         glBindVertexArray(VertexArrayID);
59
60         glGenBuffers(1, &vtx_buf);
61         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
62         glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
63
64         mvp_handle = program.UniformLocation("MVP");
65
66         glClearColor(0.0, 0.0, 0.0, 1.0);
67 }
68
69 Application::~Application() {
70
71 }
72
73
74 void Application::Run() {
75         running = true;
76         Uint32 last = SDL_GetTicks();
77         while (running) {
78                 Uint32 now = SDL_GetTicks();
79                 int delta = now - last;
80                 Loop(delta);
81                 last = now;
82         }
83 }
84
85 void Application::Loop(int dt) {
86         HandleEvents();
87         Render();
88 }
89
90
91 void Application::HandleEvents() {
92         SDL_Event event;
93         while (SDL_PollEvent(&event)) {
94                 switch (event.type) {
95                         case SDL_QUIT:
96                                 running = false;
97                                 break;
98                         case SDL_WINDOWEVENT:
99                                 switch (event.window.event) {
100                                         case SDL_WINDOWEVENT_RESIZED:
101                                                 cam.Viewport(event.window.data1, event.window.data2);
102                                                 break;
103                                         default:
104                                                 break;
105                                 }
106                                 break;
107                         default:
108                                 break;
109                 }
110         }
111 }
112
113 void Application::Render() {
114         glClear(GL_COLOR_BUFFER_BIT);
115
116         program.Use();
117
118         glm::mat4 model(1.0f); // identity: no transformation
119         glm::mat4 mvp(cam.MakeMVP(model));
120         glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
121
122         glEnableVertexAttribArray(0);
123         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
124         glVertexAttribPointer(
125                 0,        // attribute 0 (for shader)
126                 3,        // size
127                 GL_FLOAT, // type
128                 GL_FALSE, // normalized
129                 0,        // stride
130                 nullptr   // offset
131         );
132         glDrawArrays(
133                 GL_TRIANGLES, // how
134                 0,            // start
135                 3             // len
136         );
137         glDisableVertexAttribArray(0);
138
139         window.Flip();
140 }
141
142 }