libresman
view examples/imgthumbs/src/main.c @ 5:bd9b4ff19c93
more stuff
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 08:02:08 +0200 |
parents | a396d43a62f9 |
children | 410c19c735b2 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "opengl.h"
5 #include "resman.h"
6 #include "thumbs.h"
8 static int init(void);
9 static void cleanup(void);
10 static void display(void);
11 /*static void idle(void);*/
12 static void reshape(int x, int y);
13 static void keyb(unsigned char key, int x, int y);
14 static void mouse(int bn, int st, int x, int y);
15 static void motion(int x, int y);
16 static void sball_motion(int x, int y, int z);
17 static struct thumbnail *find_thumb(int x, int y);
19 const char *path = ".";
20 struct resman *texman;
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 glutReshapeFunc(reshape);
44 glutKeyboardFunc(keyb);
45 glutMouseFunc(mouse);
46 glutMotionFunc(motion);
47 glutSpaceballMotionFunc(sball_motion);
49 if(init() == -1) {
50 return 1;
51 }
52 atexit(cleanup);
54 glutMainLoop();
55 return 0;
56 }
58 static int init(void)
59 {
60 glewInit();
62 thumbs = create_thumbs(path);
63 return 0;
64 }
66 static void cleanup(void)
67 {
68 free_thumbs(thumbs);
69 }
71 static void display(void)
72 {
73 glClear(GL_COLOR_BUFFER_BIT);
75 glMatrixMode(GL_MODELVIEW);
76 glLoadIdentity();
78 if(show_thumb) {
79 glEnable(GL_TEXTURE_2D);
80 glBindTexture(GL_TEXTURE_2D, show_thumb->tex);
82 glScalef(show_zoom, show_zoom, 1);
83 glTranslatef(2.0 * show_pan_x, 2.0 * show_pan_y, 0);
84 if(show_thumb->aspect >= win_aspect) {
85 glScalef(1, 1.0 / show_thumb->aspect, 1);
86 } else {
87 glScalef(show_thumb->aspect / win_aspect, 1.0 / win_aspect, 1);
88 }
90 glBegin(GL_QUADS);
91 glColor3f(1, 1, 1);
92 glTexCoord2f(0, 0); glVertex2f(-1, -1);
93 glTexCoord2f(1, 0); glVertex2f(1, -1);
94 glTexCoord2f(1, 1); glVertex2f(1, 1);
95 glTexCoord2f(0, 1); glVertex2f(-1, 1);
96 glEnd();
98 glDisable(GL_TEXTURE_2D);
99 } else {
100 draw_thumbs(thumbs, thumbs_size, pan_y);
101 }
103 glutSwapBuffers();
104 assert(glGetError() == GL_NO_ERROR);
105 }
107 /*
108 static void idle(void)
109 {
110 glutPostRedisplay();
111 }
112 */
114 static void reshape(int x, int y)
115 {
116 win_aspect = (float)x / (float)y;
118 glViewport(0, 0, x, y);
120 glMatrixMode(GL_PROJECTION);
121 glLoadIdentity();
122 glOrtho(-1, 1, 1.0 / win_aspect, -1.0 / win_aspect, -1, 1);
124 win_width = x;
125 win_height = y;
126 }
128 static void keyb(unsigned char key, int x, int y)
129 {
130 switch(key) {
131 case 27:
132 if(show_thumb) {
133 show_thumb = 0;
134 glutPostRedisplay();
135 } else {
136 exit(0);
137 }
138 break;
140 case ' ':
141 show_zoom = 1.0;
142 thumbs_size = 0.25;
143 pan_x = pan_y = show_pan_x = show_pan_y = 0;
144 glutPostRedisplay();
145 break;
146 }
147 }
149 static int bnstate[32];
150 static int prev_x, prev_y;
151 static int click_x[32], click_y[32];
153 static void mouse(int bn, int st, int x, int y)
154 {
155 int bidx = bn - GLUT_LEFT_BUTTON;
156 int state = st == GLUT_DOWN ? 1 : 0;
158 bnstate[bidx] = state;
160 prev_x = x;
161 prev_y = y;
163 if(state) {
164 click_x[bidx] = x;
165 click_y[bidx] = y;
166 } else {
167 int is_drag = abs(x - click_x[bidx]) > 3 || abs(y - click_y[bidx]) > 3;
169 if(bidx == 0) {
170 if(!show_thumb) {
171 if(!is_drag) {
172 struct thumbnail *sel = find_thumb(x, y);
173 if(sel) {
174 show_thumb = sel;
175 show_pan_x = show_pan_y = 0;
176 glutPostRedisplay();
177 }
178 }
179 }
180 } else {
181 if(!is_drag) {
182 show_thumb = 0;
183 glutPostRedisplay();
184 }
185 }
186 }
187 }
189 static void motion(int x, int y)
190 {
191 int dx = x - prev_x;
192 int dy = y - prev_y;
193 prev_x = x;
194 prev_y = y;
196 if(!dx && !dy) return;
198 if(bnstate[0]) {
199 float fdx = dx / (float)win_width;
200 float fdy = dy / (float)win_height / win_aspect;
202 if(show_thumb) {
203 show_pan_x += fdx / show_zoom;
204 show_pan_y += fdy / show_zoom;
205 } else {
206 pan_x += fdx;
207 pan_y += fdy;
208 }
209 glutPostRedisplay();
210 }
212 if(bnstate[2]) {
213 if(show_thumb) {
214 show_zoom -= dy * 0.0075;
215 if(show_zoom <= 0) show_zoom = 0;
216 } else {
217 thumbs_size -= dy * 0.005;
218 if(thumbs_size <= 0.01) thumbs_size = 0.01;
219 }
220 glutPostRedisplay();
221 }
222 }
224 static void sball_motion(int x, int y, int z)
225 {
226 float fx = -x * 0.0004;
227 float fy = z * 0.0004;
228 float fz = -y * 0.0005;
230 if(show_thumb) {
231 show_pan_x += fx / show_zoom;
232 show_pan_y += fy / show_zoom;
233 show_zoom += fz;
234 if(show_zoom <= 0) show_zoom = 0;
235 } else {
236 pan_x += fx;
237 pan_y += fy;
238 thumbs_size += fz;
239 if(thumbs_size <= 0.01) thumbs_size = 0.01;
240 }
241 glutPostRedisplay();
242 }
244 static struct thumbnail *find_thumb(int x, int y)
245 {
246 float fx = (float)x / (float)win_width;
247 float fy = (float)y / (float)win_height / win_aspect;
248 struct thumbnail *node;
250 node = thumbs;
251 while(node) {
252 float nx = node->layout_pos[0];
253 float ny = node->layout_pos[1];
255 if(fx >= nx && fx < nx + node->layout_size[0] &&
256 fy >= ny && fy < ny + node->layout_size[1]) {
257 return node;
258 }
259 node = node->next;
260 }
261 return 0;
262 }