libresman

annotate examples/imgthumbs/src/main.c @ 10:4d18498a0078

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