libresman

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