imtk

view src/imtk.c @ 22:17f5ff624da3

minor fixes in the layout system
author John Tsiombikas <nuclear@siggraph.org>
date Sat, 30 Apr 2011 06:12:51 +0300
parents 3138c38433e2
children 4c2b3e281409
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <assert.h>
6 #ifndef __APPLE__
7 #include <GL/glut.h>
8 #else
9 #include <GLUT/glut.h>
10 #endif
11 #include "imtk.h"
12 #include "state.h"
13 #include "draw.h"
16 void imtk_post_redisplay(void)
17 {
18 glutPostRedisplay();
19 }
21 void imtk_begin(void)
22 {
23 int width, height;
25 glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
27 glDisable(GL_DEPTH_TEST);
28 glDisable(GL_STENCIL_TEST);
29 glDisable(GL_ALPHA_TEST);
30 glDisable(GL_TEXTURE_1D);
31 glDisable(GL_TEXTURE_2D);
32 glDisable(GL_CULL_FACE);
33 glDisable(GL_SCISSOR_TEST);
34 glDisable(GL_LIGHTING);
35 glEnable(GL_BLEND);
36 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
39 imtk_get_viewport(&width, &height);
41 glMatrixMode(GL_PROJECTION);
42 glPushMatrix();
43 glLoadIdentity();
44 glTranslatef(-1, 1, 0);
45 glScalef(2.0 / width, -2.0 / height, 1.0);
47 glMatrixMode(GL_MODELVIEW);
48 glPushMatrix();
49 glLoadIdentity();
50 }
52 void imtk_end(void)
53 {
54 glMatrixMode(GL_PROJECTION);
55 glPopMatrix();
56 glMatrixMode(GL_MODELVIEW);
57 glPopMatrix();
59 glPopAttrib();
60 }
62 void imtk_label(const char *str, int x, int y)
63 {
64 if(x == IMTK_AUTO || y == IMTK_AUTO) {
65 imtk_layout_get_pos(&x, &y);
66 }
68 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
69 imtk_draw_string(x, y + 14, str);
70 imtk_layout_advance(imtk_string_size(str), 12);
71 }
74 static int box[4], span[4];
75 static int spacing;
76 static int layout;
78 void imtk_layout_start(int x, int y, int sp, int dir)
79 {
80 box[0] = span[0] = x;
81 box[1] = span[1] = y;
82 box[2] = box[3] = span[2] = span[3] = 0;
83 spacing = sp;
84 layout = dir;
85 }
87 void imtk_layout_dir(int dir)
88 {
89 layout = dir;
90 if(dir == IMTK_VERTICAL) {
91 imtk_layout_newline();
92 }
93 }
95 void imtk_layout_advance(int width, int height)
96 {
97 int max_span_y, max_box_y;
99 span[2] += width + spacing;
101 if(height > span[3]) {
102 span[3] = height;
103 }
105 max_span_y = span[1] + span[3];
106 max_box_y = box[1] + box[3];
108 if(max_span_y > max_box_y) {
109 box[3] = max_span_y - box[1];
110 }
111 if(span[2] > box[2]) {
112 box[2] = span[2];
113 }
115 if(layout == IMTK_VERTICAL) {
116 imtk_layout_newline();
117 }
118 }
120 void imtk_layout_newline(void)
121 {
122 span[0] = box[0];
123 span[1] = box[1] + box[3] + spacing;
124 span[2] = span[3] = 0;
125 }
127 void imtk_layout_get_pos(int *x, int *y)
128 {
129 *x = span[0] + span[2];
130 *y = span[1];
131 }
133 void imtk_layout_get_bounds(int *bbox)
134 {
135 memcpy(bbox, box, sizeof box);
136 }