libresman
view examples/imgthumbs/src/main.c @ 23:f8e5a1491275
win32 file change notification attempt1 (failed)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 13 Feb 2014 13:17:07 +0200 |
parents | bebc065a941f |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <imago2.h>
5 #include "opengl.h"
6 #include "resman.h"
7 #include "thumbs.h"
9 static int init(void);
10 static void cleanup(void);
11 static void display(void);
12 static void idle(void);
13 static void reshape(int x, int y);
14 static void keyb(unsigned char key, int x, int y);
15 static void mouse(int bn, int st, int x, int y);
16 static void motion(int x, int y);
17 static void sball_motion(int x, int y, int z);
18 static struct thumbnail *find_thumb(int x, int y);
20 const char *path = ".";
21 int win_width, win_height;
22 float win_aspect;
23 float pan_x, pan_y;
24 float show_pan_x, show_pan_y;
25 float show_zoom = 1.0;
26 float thumbs_size = 0.25;
28 struct thumbnail *thumbs, *show_thumb;
30 int main(int argc, char **argv)
31 {
32 glutInitWindowSize(1024, 768);
33 glutInit(&argc, argv);
35 if(argv[1]) {
36 path = argv[1];
37 }
39 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
40 glutCreateWindow("imgthumbs");
42 glutDisplayFunc(display);
43 glutIdleFunc(idle);
44 glutReshapeFunc(reshape);
45 glutKeyboardFunc(keyb);
46 glutMouseFunc(mouse);
47 glutMotionFunc(motion);
48 glutSpaceballMotionFunc(sball_motion);
50 if(init() == -1) {
51 return 1;
52 }
53 atexit(cleanup);
55 glutMainLoop();
56 return 0;
57 }
59 static int init(void)
60 {
61 thumbs = create_thumbs(path);
62 return 0;
63 }
65 static void cleanup(void)
66 {
67 free_thumbs(thumbs);
68 }
70 static void display(void)
71 {
72 update_thumbs();
74 glClear(GL_COLOR_BUFFER_BIT);
76 glMatrixMode(GL_MODELVIEW);
77 glLoadIdentity();
79 if(show_thumb) {
80 glEnable(GL_TEXTURE_2D);
81 glBindTexture(GL_TEXTURE_2D, show_thumb->tex);
83 glScalef(show_zoom, show_zoom, 1);
84 glTranslatef(2.0 * show_pan_x, 2.0 * show_pan_y, 0);
85 if(show_thumb->aspect >= win_aspect) {
86 glScalef(1, 1.0 / show_thumb->aspect, 1);
87 } else {
88 glScalef(show_thumb->aspect / win_aspect, 1.0 / win_aspect, 1);
89 }
91 glBegin(GL_QUADS);
92 glColor3f(1, 1, 1);
93 glTexCoord2f(0, 0); glVertex2f(-1, -1);
94 glTexCoord2f(1, 0); glVertex2f(1, -1);
95 glTexCoord2f(1, 1); glVertex2f(1, 1);
96 glTexCoord2f(0, 1); glVertex2f(-1, 1);
97 glEnd();
99 glDisable(GL_TEXTURE_2D);
100 } else {
101 draw_thumbs(thumbs, thumbs_size, pan_y);
102 }
104 glutSwapBuffers();
105 assert(glGetError() == GL_NO_ERROR);
106 }
108 static void idle(void)
109 {
110 glutPostRedisplay();
111 }
113 static void reshape(int x, int y)
114 {
115 win_aspect = (float)x / (float)y;
117 glViewport(0, 0, x, y);
119 glMatrixMode(GL_PROJECTION);
120 glLoadIdentity();
121 glOrtho(-1, 1, 1.0 / win_aspect, -1.0 / win_aspect, -1, 1);
123 win_width = x;
124 win_height = y;
125 }
127 static void keyb(unsigned char key, int x, int y)
128 {
129 switch(key) {
130 case 27:
131 if(show_thumb) {
132 show_thumb = 0;
133 glutPostRedisplay();
134 } else {
135 exit(0);
136 }
137 break;
139 case ' ':
140 show_zoom = 1.0;
141 thumbs_size = 0.25;
142 pan_x = pan_y = show_pan_x = show_pan_y = 0;
143 glutPostRedisplay();
144 break;
145 }
146 }
148 static int bnstate[32];
149 static int prev_x, prev_y;
150 static int click_x[32], click_y[32];
152 static void mouse(int bn, int st, int x, int y)
153 {
154 int bidx = bn - GLUT_LEFT_BUTTON;
155 int state = st == GLUT_DOWN ? 1 : 0;
157 bnstate[bidx] = state;
159 prev_x = x;
160 prev_y = y;
162 if(state) {
163 click_x[bidx] = x;
164 click_y[bidx] = y;
165 } else {
166 int is_drag = abs(x - click_x[bidx]) > 3 || abs(y - click_y[bidx]) > 3;
168 if(bidx == 0) {
169 if(!show_thumb) {
170 if(!is_drag) {
171 struct thumbnail *sel = find_thumb(x, y);
172 if(sel) {
173 show_thumb = sel;
174 show_pan_x = show_pan_y = 0;
175 glutPostRedisplay();
176 }
177 }
178 }
179 } else {
180 if(!is_drag) {
181 show_thumb = 0;
182 glutPostRedisplay();
183 }
184 }
185 }
186 }
188 static void motion(int x, int y)
189 {
190 int dx = x - prev_x;
191 int dy = y - prev_y;
192 prev_x = x;
193 prev_y = y;
195 if(!dx && !dy) return;
197 if(bnstate[0]) {
198 float fdx = dx / (float)win_width;
199 float fdy = dy / (float)win_height / win_aspect;
201 if(show_thumb) {
202 show_pan_x += fdx / show_zoom;
203 show_pan_y += fdy / show_zoom;
204 } else {
205 pan_x += fdx;
206 pan_y += fdy;
207 }
208 glutPostRedisplay();
209 }
211 if(bnstate[2]) {
212 if(show_thumb) {
213 show_zoom -= dy * 0.0075;
214 if(show_zoom <= 0) show_zoom = 0;
215 } else {
216 thumbs_size -= dy * 0.005;
217 if(thumbs_size <= 0.01) thumbs_size = 0.01;
218 }
219 glutPostRedisplay();
220 }
221 }
223 static void sball_motion(int x, int y, int z)
224 {
225 float fx = -x * 0.0004;
226 float fy = z * 0.0004;
227 float fz = -y * 0.0005;
229 if(show_thumb) {
230 show_pan_x += fx / show_zoom;
231 show_pan_y += fy / show_zoom;
232 show_zoom += fz;
233 if(show_zoom <= 0) show_zoom = 0;
234 } else {
235 pan_x += fx;
236 pan_y += fy;
237 thumbs_size += fz;
238 if(thumbs_size <= 0.01) thumbs_size = 0.01;
239 }
240 glutPostRedisplay();
241 }
243 static struct thumbnail *find_thumb(int x, int y)
244 {
245 float fx = (float)x / (float)win_width;
246 float fy = (float)y / (float)win_height / win_aspect;
247 struct thumbnail *node;
249 node = thumbs;
250 while(node) {
251 float nx = node->layout_pos[0];
252 float ny = node->layout_pos[1];
254 if(fx >= nx && fx < nx + node->layout_size[0] &&
255 fy >= ny && fy < ny + node->layout_size[1]) {
256 return node;
257 }
258 node = node->next;
259 }
260 return 0;
261 }