libresman

view examples/imgthumbs/src/main.c @ 2:026cdd1737ff

added spaceball controls to the example and needless GLEW dependency
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 31 Jan 2014 03:40:00 +0200
parents 469ce01809bc
children a396d43a62f9
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 glutInit(&argc, argv);
34 if(argv[1]) {
35 path = argv[1];
36 }
38 glutInitWindowSize(800, 600);
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 >= 1.0) {
85 glScalef(1, 1.0 / show_thumb->aspect, 1);
86 } else {
87 glScalef(show_thumb->aspect, 1, 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 exit(0);
134 case ' ':
135 show_zoom = 1.0;
136 thumbs_size = 0.25;
137 pan_x = pan_y = show_pan_x = show_pan_y = 0;
138 glutPostRedisplay();
139 break;
140 }
141 }
143 static int bnstate[32];
144 static int prev_x, prev_y;
145 static int click_x[32], click_y[32];
147 static void mouse(int bn, int st, int x, int y)
148 {
149 int bidx = bn - GLUT_LEFT_BUTTON;
150 int state = st == GLUT_DOWN ? 1 : 0;
152 bnstate[bidx] = state;
154 prev_x = x;
155 prev_y = y;
157 if(state) {
158 click_x[bidx] = x;
159 click_y[bidx] = y;
160 } else {
161 int is_drag = abs(x - click_x[bidx]) > 3 || abs(y - click_y[bidx]) > 3;
163 if(bidx == 0) {
164 if(!show_thumb) {
165 if(!is_drag) {
166 struct thumbnail *sel = find_thumb(x, y);
167 if(sel) {
168 show_thumb = sel;
169 show_pan_x = show_pan_y = 0;
170 glutPostRedisplay();
171 }
172 }
173 }
174 } else {
175 if(!is_drag) {
176 show_thumb = 0;
177 glutPostRedisplay();
178 }
179 }
180 }
181 }
183 static void motion(int x, int y)
184 {
185 int dx = x - prev_x;
186 int dy = y - prev_y;
187 prev_x = x;
188 prev_y = y;
190 if(!dx && !dy) return;
192 if(bnstate[0]) {
193 float fdx = dx / (float)win_width;
194 float fdy = dy / (float)win_height / win_aspect;
196 if(show_thumb) {
197 show_pan_x += fdx / show_zoom;
198 show_pan_y += fdy / show_zoom;
199 } else {
200 pan_x += fdx;
201 pan_y += fdy;
202 }
203 glutPostRedisplay();
204 }
206 if(bnstate[2]) {
207 if(show_thumb) {
208 show_zoom -= dy * 0.0075;
209 if(show_zoom <= 0) show_zoom = 0;
210 } else {
211 thumbs_size -= dy * 0.005;
212 if(thumbs_size <= 0.01) thumbs_size = 0.01;
213 }
214 glutPostRedisplay();
215 }
216 }
218 static void sball_motion(int x, int y, int z)
219 {
220 float fx = -x * 0.0004;
221 float fy = z * 0.0004;
222 float fz = -y * 0.0005;
224 if(show_thumb) {
225 show_pan_x += fx / show_zoom;
226 show_pan_y += fy / show_zoom;
227 show_zoom += fz;
228 if(show_zoom <= 0) show_zoom = 0;
229 } else {
230 pan_x += fx;
231 pan_y += fy;
232 thumbs_size += fz;
233 if(thumbs_size <= 0.01) thumbs_size = 0.01;
234 }
235 glutPostRedisplay();
236 }
238 static struct thumbnail *find_thumb(int x, int y)
239 {
240 float fx = (float)x / (float)win_width;
241 float fy = (float)y / (float)win_height / win_aspect;
242 struct thumbnail *node;
244 node = thumbs;
245 while(node) {
246 float nx = node->layout_pos[0];
247 float ny = node->layout_pos[1];
249 if(fx >= nx && fx < nx + node->layout_size[0] &&
250 fy >= ny && fy < ny + node->layout_size[1]) {
251 return node;
252 }
253 node = node->next;
254 }
255 return 0;
256 }