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