-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathpugl_cursor_demo.c
156 lines (126 loc) · 3.73 KB
/
pugl_cursor_demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2012-2020 David Robillard <[email protected]>
// SPDX-License-Identifier: ISC
#include "test/test_utils.h"
#include "pugl/gl.h"
#include "pugl/pugl.h"
#include <stdbool.h>
static const int N_CURSORS = 10;
static const int N_ROWS = 2;
static const int N_COLS = 5;
typedef struct {
PuglWorld* world;
PuglTestOptions opts;
bool quit;
} PuglTestApp;
static void
onConfigure(const PuglSpan width, const PuglSpan height)
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, (int)width, (int)height);
}
static void
onExpose(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.6f, 0.6f, 0.6f);
for (int row = 1; row < N_ROWS; ++row) {
const float y = (float)row * (2.0f / (float)N_ROWS) - 1.0f;
glBegin(GL_LINES);
glVertex2f(-1.0f, y);
glVertex2f(1.0f, y);
glEnd();
}
for (int col = 1; col < N_COLS; ++col) {
const float x = (float)col * (2.0f / (float)N_COLS) - 1.0f;
glBegin(GL_LINES);
glVertex2f(x, -1.0f);
glVertex2f(x, 1.0f);
glEnd();
}
}
static void
onMotion(PuglView* view, double x, double y)
{
const PuglRect frame = puglGetFrame(view);
int row = (int)(y * N_ROWS / frame.height);
int col = (int)(x * N_COLS / frame.width);
row = (row < 0) ? 0 : (row >= N_ROWS) ? (N_ROWS - 1) : row;
col = (col < 0) ? 0 : (col >= N_COLS) ? (N_COLS - 1) : col;
const PuglCursor cursor = (PuglCursor)((row * N_COLS + col) % N_CURSORS);
puglSetCursor(view, cursor);
}
static PuglStatus
onEvent(PuglView* view, const PuglEvent* event)
{
PuglTestApp* app = (PuglTestApp*)puglGetHandle(view);
printEvent(event, "Event: ", app->opts.verbose);
switch (event->type) {
case PUGL_CONFIGURE:
onConfigure(event->configure.width, event->configure.height);
break;
case PUGL_KEY_PRESS:
if (event->key.key == 'q' || event->key.key == PUGL_KEY_ESCAPE) {
app->quit = 1;
}
break;
case PUGL_MOTION:
onMotion(view, event->motion.x, event->motion.y);
break;
case PUGL_EXPOSE:
onExpose();
break;
case PUGL_POINTER_OUT:
puglSetCursor(view, PUGL_CURSOR_ARROW);
break;
case PUGL_CLOSE:
app->quit = 1;
break;
default:
break;
}
return PUGL_SUCCESS;
}
int
main(int argc, char** argv)
{
PuglTestApp app = {0};
app.opts = puglParseTestOptions(&argc, &argv);
if (app.opts.help) {
puglPrintTestUsage(argv[0], "");
return 1;
}
app.world = puglNewWorld(PUGL_PROGRAM, 0);
puglSetWorldHandle(app.world, &app);
puglSetWorldString(app.world, PUGL_CLASS_NAME, "PuglCursorDemo");
PuglView* view = puglNewView(app.world);
puglSetViewString(view, PUGL_WINDOW_TITLE, "Pugl Cursor Demo");
puglSetSizeHint(view, PUGL_DEFAULT_SIZE, 512, 256);
puglSetSizeHint(view, PUGL_MIN_SIZE, 128, 64);
puglSetSizeHint(view, PUGL_MAX_SIZE, 512, 256);
puglSetBackend(view, puglGlBackend());
puglSetViewHint(view, PUGL_CONTEXT_DEBUG, app.opts.errorChecking);
puglSetViewHint(view, PUGL_RESIZABLE, app.opts.resizable);
puglSetViewHint(view, PUGL_SAMPLES, app.opts.samples);
puglSetViewHint(view, PUGL_DOUBLE_BUFFER, app.opts.doubleBuffer);
puglSetViewHint(view, PUGL_SWAP_INTERVAL, app.opts.sync);
puglSetViewHint(view, PUGL_IGNORE_KEY_REPEAT, app.opts.ignoreKeyRepeat);
puglSetHandle(view, &app);
puglSetEventFunc(view, onEvent);
const PuglStatus st = puglRealize(view);
if (st) {
return logError("Failed to create window (%s)\n", puglStrerror(st));
}
puglShow(view, PUGL_SHOW_RAISE);
while (!app.quit) {
puglUpdate(app.world, -1.0);
}
puglFreeView(view);
puglFreeWorld(app.world);
return 0;
}