libresman
view examples/imgthumbs/src/main.c @ 6:410c19c735b2
- removed the glew dependency
- initial thread pool implementation
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 03 Feb 2014 05:22:09 +0200 |
parents | eadf99551730 |
children | bebc065a941f |
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 thumbs = create_thumbs(path);
61 return 0;
62 }
64 static void cleanup(void)
65 {
66 free_thumbs(thumbs);
67 }
69 static void display(void)
70 {
71 glClear(GL_COLOR_BUFFER_BIT);
73 glMatrixMode(GL_MODELVIEW);
74 glLoadIdentity();
76 if(show_thumb) {
77 glEnable(GL_TEXTURE_2D);
78 glBindTexture(GL_TEXTURE_2D, show_thumb->tex);
80 glScalef(show_zoom, show_zoom, 1);
81 glTranslatef(2.0 * show_pan_x, 2.0 * show_pan_y, 0);
82 if(show_thumb->aspect >= win_aspect) {
83 glScalef(1, 1.0 / show_thumb->aspect, 1);
84 } else {
85 glScalef(show_thumb->aspect / win_aspect, 1.0 / win_aspect, 1);
86 }
88 glBegin(GL_QUADS);
89 glColor3f(1, 1, 1);
90 glTexCoord2f(0, 0); glVertex2f(-1, -1);
91 glTexCoord2f(1, 0); glVertex2f(1, -1);
92 glTexCoord2f(1, 1); glVertex2f(1, 1);
93 glTexCoord2f(0, 1); glVertex2f(-1, 1);
94 glEnd();
96 glDisable(GL_TEXTURE_2D);
97 } else {
98 draw_thumbs(thumbs, thumbs_size, pan_y);
99 }
101 glutSwapBuffers();
102 assert(glGetError() == GL_NO_ERROR);
103 }
105 /*
106 static void idle(void)
107 {
108 glutPostRedisplay();
109 }
110 */
112 static void reshape(int x, int y)
113 {
114 win_aspect = (float)x / (float)y;
116 glViewport(0, 0, x, y);
118 glMatrixMode(GL_PROJECTION);
119 glLoadIdentity();
120 glOrtho(-1, 1, 1.0 / win_aspect, -1.0 / win_aspect, -1, 1);
122 win_width = x;
123 win_height = y;
124 }
126 static void keyb(unsigned char key, int x, int y)
127 {
128 switch(key) {
129 case 27:
130 if(show_thumb) {
131 show_thumb = 0;
132 glutPostRedisplay();
133 } else {
134 exit(0);
135 }
136 break;
138 case ' ':
139 show_zoom = 1.0;
140 thumbs_size = 0.25;
141 pan_x = pan_y = show_pan_x = show_pan_y = 0;
142 glutPostRedisplay();
143 break;
144 }
145 }
147 static int bnstate[32];
148 static int prev_x, prev_y;
149 static int click_x[32], click_y[32];
151 static void mouse(int bn, int st, int x, int y)
152 {
153 int bidx = bn - GLUT_LEFT_BUTTON;
154 int state = st == GLUT_DOWN ? 1 : 0;
156 bnstate[bidx] = state;
158 prev_x = x;
159 prev_y = y;
161 if(state) {
162 click_x[bidx] = x;
163 click_y[bidx] = y;
164 } else {
165 int is_drag = abs(x - click_x[bidx]) > 3 || abs(y - click_y[bidx]) > 3;
167 if(bidx == 0) {
168 if(!show_thumb) {
169 if(!is_drag) {
170 struct thumbnail *sel = find_thumb(x, y);
171 if(sel) {
172 show_thumb = sel;
173 show_pan_x = show_pan_y = 0;
174 glutPostRedisplay();
175 }
176 }
177 }
178 } else {
179 if(!is_drag) {
180 show_thumb = 0;
181 glutPostRedisplay();
182 }
183 }
184 }
185 }
187 static void motion(int x, int y)
188 {
189 int dx = x - prev_x;
190 int dy = y - prev_y;
191 prev_x = x;
192 prev_y = y;
194 if(!dx && !dy) return;
196 if(bnstate[0]) {
197 float fdx = dx / (float)win_width;
198 float fdy = dy / (float)win_height / win_aspect;
200 if(show_thumb) {
201 show_pan_x += fdx / show_zoom;
202 show_pan_y += fdy / show_zoom;
203 } else {
204 pan_x += fdx;
205 pan_y += fdy;
206 }
207 glutPostRedisplay();
208 }
210 if(bnstate[2]) {
211 if(show_thumb) {
212 show_zoom -= dy * 0.0075;
213 if(show_zoom <= 0) show_zoom = 0;
214 } else {
215 thumbs_size -= dy * 0.005;
216 if(thumbs_size <= 0.01) thumbs_size = 0.01;
217 }
218 glutPostRedisplay();
219 }
220 }
222 static void sball_motion(int x, int y, int z)
223 {
224 float fx = -x * 0.0004;
225 float fy = z * 0.0004;
226 float fz = -y * 0.0005;
228 if(show_thumb) {
229 show_pan_x += fx / show_zoom;
230 show_pan_y += fy / show_zoom;
231 show_zoom += fz;
232 if(show_zoom <= 0) show_zoom = 0;
233 } else {
234 pan_x += fx;
235 pan_y += fy;
236 thumbs_size += fz;
237 if(thumbs_size <= 0.01) thumbs_size = 0.01;
238 }
239 glutPostRedisplay();
240 }
242 static struct thumbnail *find_thumb(int x, int y)
243 {
244 float fx = (float)x / (float)win_width;
245 float fy = (float)y / (float)win_height / win_aspect;
246 struct thumbnail *node;
248 node = thumbs;
249 while(node) {
250 float nx = node->layout_pos[0];
251 float ny = node->layout_pos[1];
253 if(fx >= nx && fx < nx + node->layout_size[0] &&
254 fy >= ny && fy < ny + node->layout_size[1]) {
255 return node;
256 }
257 node = node->next;
258 }
259 return 0;
260 }