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
;
22
struct
stopwatch
*
last_timeout
;
23
struct
stopwatch
*
last_ui_poll
;
24
struct
stopwatch
*
last_display
;
25
GLFWwindow *
window
;
26
bool
interactive
;
27
struct
render_options
*
render_options
;
28
bool
save_screenshot
;
29
int
screenshot_count
;
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
37
poll->
ui_poll_timeout
=
ui_poll_timeout
;
38
poll->
display_timeout
=
display_timeout
;
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
;
44
poll->
render_options
=
render_options
;
45
poll->
save_screenshot
=
false
;
46
poll->
screenshot_count
= 0;
47
poll->
filename
=
filename
;
48
poll->
record
=
record
;
49
stopwatch_start
(poll->
last_timeout
);
50
stopwatch_start
(poll->
last_ui_poll
);
51
stopwatch_start
(poll->
last_display
);
52
return
poll;
53
}
54
55
void
poll_delete
(
struct
poll
*
poll
) {
56
stopwatch_delete
(poll->
last_ui_poll
);
57
stopwatch_delete
(poll->
last_display
);
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);
70
stopwatch_reset
(poll->
last_ui_poll
);
71
if
(poll->
interactive
) {
72
interact_reset
();
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
;
82
poll->
render_options
->
weight
=
interact
.
weight
;
83
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
;
87
render_options_set_location
(poll->
render_options
,
interact
.
centerx
,
interact
.
centery
,
interact
.
radius
);
88
debug_message
(
"poll_ui(%p) abort (updated)\n"
, poll);
89
return
poll_abort
;
90
}
91
if
(
interact
.
save_screenshot
) {
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);
103
stopwatch_reset
(poll->
last_display
);
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;
128
stopwatch_reset
(poll->
last_timeout
);
129
}
src
poll.c
Generated on Mon Jan 19 2015 16:18:12 for mightymandel by
1.8.1.2