libresman

view examples/imgthumbs/src/main.c @ 13:a42888d26839

bit more progress...
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 08 Feb 2014 07:41:39 +0200
parents 410c19c735b2
children 0a789208498d
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(glutPostRedisplay);
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 /*
109 static void idle(void)
110 {
111 glutPostRedisplay();
112 }
113 */
115 static void reshape(int x, int y)
116 {
117 win_aspect = (float)x / (float)y;
119 glViewport(0, 0, x, y);
121 glMatrixMode(GL_PROJECTION);
122 glLoadIdentity();
123 glOrtho(-1, 1, 1.0 / win_aspect, -1.0 / win_aspect, -1, 1);
125 win_width = x;
126 win_height = y;
127 }
129 static void keyb(unsigned char key, int x, int y)
130 {
131 switch(key) {
132 case 27:
133 if(show_thumb) {
134 show_thumb = 0;
135 glutPostRedisplay();
136 } else {
137 exit(0);
138 }
139 break;
141 case ' ':
142 show_zoom = 1.0;
143 thumbs_size = 0.25;
144 pan_x = pan_y = show_pan_x = show_pan_y = 0;
145 glutPostRedisplay();
146 break;
147 }
148 }
150 static int bnstate[32];
151 static int prev_x, prev_y;
152 static int click_x[32], click_y[32];
154 static void mouse(int bn, int st, int x, int y)
155 {
156 int bidx = bn - GLUT_LEFT_BUTTON;
157 int state = st == GLUT_DOWN ? 1 : 0;
159 bnstate[bidx] = state;
161 prev_x = x;
162 prev_y = y;
164 if(state) {
165 click_x[bidx] = x;
166 click_y[bidx] = y;
167 } else {
168 int is_drag = abs(x - click_x[bidx]) > 3 || abs(y - click_y[bidx]) > 3;
170 if(bidx == 0) {
171 if(!show_thumb) {
172 if(!is_drag) {
173 struct thumbnail *sel = find_thumb(x, y);
174 if(sel) {
175 show_thumb = sel;
176 show_pan_x = show_pan_y = 0;
177 glutPostRedisplay();
178 }
179 }
180 }
181 } else {
182 if(!is_drag) {
183 show_thumb = 0;
184 glutPostRedisplay();
185 }
186 }
187 }
188 }
190 static void motion(int x, int y)
191 {
192 int dx = x - prev_x;
193 int dy = y - prev_y;
194 prev_x = x;
195 prev_y = y;
197 if(!dx && !dy) return;
199 if(bnstate[0]) {
200 float fdx = dx / (float)win_width;
201 float fdy = dy / (float)win_height / win_aspect;
203 if(show_thumb) {
204 show_pan_x += fdx / show_zoom;
205 show_pan_y += fdy / show_zoom;
206 } else {
207 pan_x += fdx;
208 pan_y += fdy;
209 }
210 glutPostRedisplay();
211 }
213 if(bnstate[2]) {
214 if(show_thumb) {
215 show_zoom -= dy * 0.0075;
216 if(show_zoom <= 0) show_zoom = 0;
217 } else {
218 thumbs_size -= dy * 0.005;
219 if(thumbs_size <= 0.01) thumbs_size = 0.01;
220 }
221 glutPostRedisplay();
222 }
223 }
225 static void sball_motion(int x, int y, int z)
226 {
227 float fx = -x * 0.0004;
228 float fy = z * 0.0004;
229 float fz = -y * 0.0005;
231 if(show_thumb) {
232 show_pan_x += fx / show_zoom;
233 show_pan_y += fy / show_zoom;
234 show_zoom += fz;
235 if(show_zoom <= 0) show_zoom = 0;
236 } else {
237 pan_x += fx;
238 pan_y += fy;
239 thumbs_size += fz;
240 if(thumbs_size <= 0.01) thumbs_size = 0.01;
241 }
242 glutPostRedisplay();
243 }
245 static struct thumbnail *find_thumb(int x, int y)
246 {
247 float fx = (float)x / (float)win_width;
248 float fy = (float)y / (float)win_height / win_aspect;
249 struct thumbnail *node;
251 node = thumbs;
252 while(node) {
253 float nx = node->layout_pos[0];
254 float ny = node->layout_pos[1];
256 if(fx >= nx && fx < nx + node->layout_size[0] &&
257 fy >= ny && fy < ny + node->layout_size[1]) {
258 return node;
259 }
260 node = node->next;
261 }
262 return 0;
263 }