mightymandel v16

GPU-based Mandelbrot set explorer

poll.c
Go to the documentation of this file.
1 // mightymandel -- GPU-based Mandelbrot Set explorer
2 // Copyright (C) 2012,2013,2014,2015 Claude Heiland-Allen
3 // License GPL3+ http://www.gnu.org/licenses/gpl.html
4 
5 #include <GL/glew.h>
6 #include <GLFW/glfw3.h>
7 #include <stdlib.h>
8 
9 #include "logging.h"
10 #include "stopwatch.h"
11 #include "startup.h"
12 #include "render.h"
13 #include "interact.h"
14 #include "poll.h"
15 #include "filename.h"
16 #include "record.h"
17 
18 struct poll {
19  double timeout;
20  double ui_poll_timeout;
21  double display_timeout;
25  GLFWwindow *window;
26  bool interactive;
30  struct filename *filename;
31  struct record *record;
32 };
33 
34 struct poll *poll_new(GLFWwindow *window, bool interactive, struct render_options *render_options, double ui_poll_timeout, double display_timeout, struct filename *filename, struct record *record) {
35  struct poll *poll = malloc(sizeof(struct poll));
36  poll->timeout = 1.0 / 0.0; // infinity
39  poll->last_timeout = stopwatch_new();
40  poll->last_ui_poll = stopwatch_new();
41  poll->last_display = stopwatch_new();
42  poll->window = window;
43  poll->interactive = interactive;
45  poll->save_screenshot = false;
46  poll->screenshot_count = 0;
47  poll->filename = filename;
48  poll->record = record;
52  return poll;
53 }
54 
55 void poll_delete(struct poll *poll) {
58  free(poll);
59 }
60 
61 enum poll_result poll_ui(struct poll *poll, bool should_wait) {
62  debug_message("poll_ui(%p)\n", poll);
63  bool redisplay = false;
64  if (stopwatch_elapsed(poll->last_timeout) > poll->timeout) {
65  debug_message("poll_ui(%p) timeout\n", poll);
66  return poll_timeout;
67  }
68  if (stopwatch_elapsed(poll->last_ui_poll) > poll->ui_poll_timeout) {
69  debug_message("poll_ui(%p) polling UI\n", poll);
71  if (poll->interactive) {
73  }
74  if (should_wait) {
75  glfwWaitEvents();
76  } else {
77  glfwPollEvents();
78  }
79  if (poll->interactive) {
80  redisplay |= poll->render_options->weight != interact.weight;
81  redisplay |= poll->render_options->show_glitches != interact.show_glitches;
84  if (interact.quit) { glfwSetWindowShouldClose(poll->window, GL_TRUE); }
85  if (interact.updated) {
86  interact.updated = false;
88  debug_message("poll_ui(%p) abort (updated)\n", poll);
89  return poll_abort;
90  }
92  poll->save_screenshot = true;
93  redisplay = true;
94  }
95  }
96  if (glfwWindowShouldClose(poll->window)) {
97  debug_message("poll_ui(%p) abort (quit)\n", poll);
98  return poll_abort;
99  }
100  }
101  if (redisplay || stopwatch_elapsed(poll->last_display) > poll->display_timeout) {
102  debug_message("poll_ui(%p) display\n", poll);
104  return poll_display;
105  }
106  debug_message("poll_ui(%p) continue\n", poll);
107  return poll_continue;
108 }
109 
110 struct tiling;
111 struct zoom;
112 char *collect_metadata_to_string(const char *prefix, struct render_options *render_options, struct tiling *tiling, struct zoom *zoom, int pass);
113 
114 void poll_swap_buffers(struct poll *poll) {
115  glfwSwapBuffers(poll->window);
116  if (poll->save_screenshot) {
117  poll->save_screenshot = false;
118  char *comment = collect_metadata_to_string("# ", poll->render_options, 0, 0, -1);
119  char *name = filename_name(poll->filename, "ppm", poll->screenshot_count++, -1, -1);
120  record_do(poll->record, name, poll->render_options->width, poll->render_options->height, comment);
121  free(name);
122  free(comment);
123  }
124 }
125 
126 void poll_set_timeout(struct poll *poll, double timeout) {
127  poll->timeout = timeout;
129 }