From 17edc0a9d68982c07b3f537333c59da2271f76a7 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 13 Oct 2015 21:38:24 -0400 Subject: [PATCH] redraw on a timer; rotate quad --- app.cc | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/app.cc b/app.cc index 6a180ea..26bec2d 100644 --- a/app.cc +++ b/app.cc @@ -67,7 +67,6 @@ bool init(void) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat))); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(6 * sizeof(GLfloat))); - modelview = glm::translate(glm::mat4(1.0), glm::vec3(0, 0, -2)); projection = glm::perspective(60.0f, (float)WIDTH / (float)HEIGHT, 0.1f, 1000.0f); for (int i = 0; i < 16; i++) @@ -106,12 +105,13 @@ bool init(void) return true; } -void display(SDL_Window * window) +static void draw_cube() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glm::mat4 modelview_backup = modelview; program->use(); cube_array->bind(); + glUniform4f(uniforms.ambient, 1.0, 1.0, 1.0, 1.0); glUniform4f(uniforms.specular, 1.0, 1.0, 1.0, 1.0); glUniform1f(uniforms.shininess, 150.0); @@ -119,13 +119,30 @@ void display(SDL_Window * window) glUniformMatrix4fv(uniforms.modelview, 1, GL_FALSE, &modelview[0][0]); glUniformMatrix4fv(uniforms.projection, 1, GL_FALSE, &projection[0][0]); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); +} + +void display(SDL_Window * window) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + modelview = glm::translate(glm::mat4(1.0), glm::vec3(0, 0, -2)); + modelview = glm::rotate(modelview, (float)(SDL_GetTicks() / 1000.0), glm::vec3(0, 1, 0)); + + draw_cube(); SDL_GL_SwapWindow(window); } +static SDL_Event UpdateEvent; + +static Uint32 UpdateCallback(Uint32 interval, void * param) +{ + SDL_PushEvent(&UpdateEvent); + return interval; +} + int main(int argc, char *argv[]) { - if (SDL_Init(SDL_INIT_VIDEO)) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) { printf("Failed to initialize SDL!\n"); return 1; @@ -164,6 +181,9 @@ int main(int argc, char *argv[]) return 2; } + UpdateEvent.type = SDL_USEREVENT; + UpdateEvent.user.code = 0; + SDL_AddTimer(25, UpdateCallback, NULL); display(window); SDL_Event event; while (SDL_WaitEvent(&event)) @@ -177,5 +197,9 @@ int main(int argc, char *argv[]) if (event.key.keysym.sym == SDLK_RETURN) display(window); } + else if (event.type == SDL_USEREVENT) + { + display(window); + } } }