libresman

view examples/imgthumbs/src/main.c @ 1:469ce01809bc

rudimentary imgthumbs "example program". doesn't use libresman yet
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 31 Jan 2014 03:17:24 +0200
parents
children 026cdd1737ff
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 struct thumbnail *find_thumb(int x, int y);
18 const char *path = ".";
19 struct resman *texman;
20 int win_width, win_height;
21 float win_aspect;
22 float pan_x, pan_y;
23 float show_pan_x, show_pan_y;
24 float show_zoom = 1.0;
25 float thumbs_size = 0.25;
27 struct thumbnail *thumbs, *show_thumb;
29 int main(int argc, char **argv)
30 {
31 glutInit(&argc, argv);
33 if(argv[1]) {
34 path = argv[1];
35 }
37 glutInitWindowSize(800, 600);
38 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
39 glutCreateWindow("imgthumbs");
41 glutDisplayFunc(display);
42 glutReshapeFunc(reshape);
43 glutKeyboardFunc(keyb);
44 glutMouseFunc(mouse);
45 glutMotionFunc(motion);
47 if(init() == -1) {
48 return 1;
49 }
50 atexit(cleanup);
52 glutMainLoop();
53 return 0;
54 }
56 static int init(void)
57 {
58 thumbs = create_thumbs(path);
59 return 0;
60 }
62 static void cleanup(void)
63 {
64 free_thumbs(thumbs);
65 }
67 static void display(void)
68 {
69 glClear(GL_COLOR_BUFFER_BIT);
71 glMatrixMode(GL_MODELVIEW);
72 glLoadIdentity();
74 if(show_thumb) {
75 glEnable(GL_TEXTURE_2D);
76 glBindTexture(GL_TEXTURE_2D, show_thumb->tex);
78 glScalef(show_zoom, show_zoom, 1);
79 glTranslatef(2.0 * show_pan_x, 2.0 * show_pan_y, 0);
80 if(show_thumb->aspect >= 1.0) {
81 glScalef(1, 1.0 / show_thumb->aspect, 1);
82 } else {
83 glScalef(show_thumb->aspect, 1, 1);
84 }
86 glBegin(GL_QUADS);
87 glColor3f(1, 1, 1);
88 glTexCoord2f(0, 0); glVertex2f(-1, -1);
89 glTexCoord2f(1, 0); glVertex2f(1, -1);
90 glTexCoord2f(1, 1); glVertex2f(1, 1);
91 glTexCoord2f(0, 1); glVertex2f(-1, 1);
92 glEnd();
94 glDisable(GL_TEXTURE_2D);
95 } else {
96 draw_thumbs(thumbs, thumbs_size, pan_y);
97 }
99 glutSwapBuffers();
100 assert(glGetError() == GL_NO_ERROR);
101 }
103 /*
104 static void idle(void)
105 {
106 glutPostRedisplay();
107 }
108 */
110 static void reshape(int x, int y)
111 {
112 win_aspect = (float)x / (float)y;
114 glViewport(0, 0, x, y);
116 glMatrixMode(GL_PROJECTION);
117 glLoadIdentity();
118 glOrtho(-1, 1, 1.0 / win_aspect, -1.0 / win_aspect, -1, 1);
120 win_width = x;
121 win_height = y;
122 }
124 static void keyb(unsigned char key, int x, int y)
125 {
126 switch(key) {
127 case 27:
128 exit(0);
130 case ' ':
131 show_zoom = 1.0;
132 thumbs_size = 0.25;
133 pan_x = pan_y = show_pan_x = show_pan_y = 0;
134 glutPostRedisplay();
135 break;
136 }
137 }
139 static int bnstate[32];
140 static int prev_x, prev_y;
141 static int click_x[32], click_y[32];
143 static void mouse(int bn, int st, int x, int y)
144 {
145 int bidx = bn - GLUT_LEFT_BUTTON;
146 int state = st == GLUT_DOWN ? 1 : 0;
148 bnstate[bidx] = state;
150 prev_x = x;
151 prev_y = y;
153 if(state) {
154 click_x[bidx] = x;
155 click_y[bidx] = y;
156 } else {
157 int is_drag = abs(x - click_x[bidx]) > 3 || abs(y - click_y[bidx]) > 3;
159 if(bidx == 0) {
160 if(!show_thumb) {
161 if(!is_drag) {
162 struct thumbnail *sel = find_thumb(x, y);
163 if(sel) {
164 show_thumb = sel;
165 show_pan_x = show_pan_y = 0;
166 glutPostRedisplay();
167 }
168 }
169 }
170 } else {
171 if(!is_drag) {
172 show_thumb = 0;
173 glutPostRedisplay();
174 }
175 }
176 }
177 }
179 static void motion(int x, int y)
180 {
181 int dx = x - prev_x;
182 int dy = y - prev_y;
183 prev_x = x;
184 prev_y = y;
186 if(!dx && !dy) return;
188 if(bnstate[0]) {
189 float fdx = dx / (float)win_width;
190 float fdy = dy / (float)win_height / win_aspect;
192 if(show_thumb) {
193 show_pan_x += fdx / show_zoom;
194 show_pan_y += fdy / show_zoom;
195 } else {
196 pan_x += fdx;
197 pan_y += fdy;
198 }
199 glutPostRedisplay();
200 }
202 if(bnstate[2]) {
203 if(show_thumb) {
204 show_zoom -= dy * 0.0075;
205 if(show_zoom <= 0) show_zoom = 0;
206 } else {
207 thumbs_size -= dy * 0.005;
208 if(thumbs_size <= 0.01) thumbs_size = 0.01;
209 }
210 glutPostRedisplay();
211 }
212 }
214 static struct thumbnail *find_thumb(int x, int y)
215 {
216 float fx = (float)x / (float)win_width;
217 float fy = (float)y / (float)win_height / win_aspect;
218 struct thumbnail *node;
220 node = thumbs;
221 while(node) {
222 float nx = node->layout_pos[0];
223 float ny = node->layout_pos[1];
225 if(fx >= nx && fx < nx + node->layout_size[0] &&
226 fy >= ny && fy < ny + node->layout_size[1]) {
227 return node;
228 }
229 node = node->next;
230 }
231 return 0;
232 }