rayfract

annotate src/imtk/draw.c @ 10:1496aae2e7d4

- simplified build by including dependences in the source tree - added make dep tracking - added mingw cross-build rules - added readme & licence
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 31 Jul 2023 18:58:56 +0300
parents
children
rev   line source
nuclear@10 1 #include <string.h>
nuclear@10 2 #include <math.h>
nuclear@10 3 #include <assert.h>
nuclear@10 4 #include "draw.h"
nuclear@10 5 #include "imtk.h"
nuclear@10 6
nuclear@10 7 #define COLOR_MASK 0xff
nuclear@10 8
nuclear@10 9 /* default colors, can be changed with imtk_set_color */
nuclear@10 10 static float colors[][4] = {
nuclear@10 11 {0.0, 0.0, 0.0, 1.0}, /* text color */
nuclear@10 12 {0.75, 0.75, 0.75, 1.0}, /* top color */
nuclear@10 13 {0.56, 0.56, 0.56, 1.0}, /* bot color */
nuclear@10 14 {0.9, 0.9, 0.9, 1.0}, /* lit bevel */
nuclear@10 15 {0.3, 0.3, 0.3, 1.0}, /* shadowed bevel */
nuclear@10 16 {0.8, 0.25, 0.18, 1.0}, /* cursor color */
nuclear@10 17 {0.68, 0.85, 1.3, 1.0}, /* selection color */
nuclear@10 18 {0.75, 0.1, 0.095, 1.0} /* check color */
nuclear@10 19 };
nuclear@10 20
nuclear@10 21 static float focus_factor = 1.15;
nuclear@10 22 static float press_factor = 0.8;
nuclear@10 23 static float alpha = 1.0;
nuclear@10 24 static float bevel = 1.0;
nuclear@10 25
nuclear@10 26 void imtk_set_color(unsigned int col, float r, float g, float b, float a)
nuclear@10 27 {
nuclear@10 28 int idx = col & COLOR_MASK;
nuclear@10 29 assert(idx >= 0 && idx < sizeof colors / sizeof *colors);
nuclear@10 30
nuclear@10 31 colors[idx][0] = r;
nuclear@10 32 colors[idx][1] = g;
nuclear@10 33 colors[idx][2] = b;
nuclear@10 34 colors[idx][3] = a;
nuclear@10 35 }
nuclear@10 36
nuclear@10 37 float *imtk_get_color(unsigned int col)
nuclear@10 38 {
nuclear@10 39 static float ret[4];
nuclear@10 40 int idx = col & COLOR_MASK;
nuclear@10 41
nuclear@10 42 memcpy(ret, colors + idx, sizeof ret);
nuclear@10 43 if(col & IMTK_FOCUS_BIT) {
nuclear@10 44 ret[0] *= focus_factor;
nuclear@10 45 ret[1] *= focus_factor;
nuclear@10 46 ret[2] *= focus_factor;
nuclear@10 47 }
nuclear@10 48 if(col & IMTK_PRESS_BIT) {
nuclear@10 49 ret[0] *= press_factor;
nuclear@10 50 ret[1] *= press_factor;
nuclear@10 51 ret[2] *= press_factor;
nuclear@10 52 }
nuclear@10 53 if(col & IMTK_SEL_BIT) {
nuclear@10 54 ret[0] *= colors[IMTK_SELECTION_COLOR][0];
nuclear@10 55 ret[1] *= colors[IMTK_SELECTION_COLOR][1];
nuclear@10 56 ret[2] *= colors[IMTK_SELECTION_COLOR][2];
nuclear@10 57 }
nuclear@10 58 ret[3] *= alpha;
nuclear@10 59 return ret;
nuclear@10 60 }
nuclear@10 61
nuclear@10 62 void imtk_set_alpha(float a)
nuclear@10 63 {
nuclear@10 64 alpha = a;
nuclear@10 65 }
nuclear@10 66
nuclear@10 67 float imtk_get_alpha(void)
nuclear@10 68 {
nuclear@10 69 return alpha;
nuclear@10 70 }
nuclear@10 71
nuclear@10 72 void imtk_set_bevel_width(float b)
nuclear@10 73 {
nuclear@10 74 bevel = b;
nuclear@10 75 }
nuclear@10 76
nuclear@10 77 float imtk_get_bevel_width(void)
nuclear@10 78 {
nuclear@10 79 return bevel;
nuclear@10 80 }
nuclear@10 81
nuclear@10 82 void imtk_set_focus_factor(float fact)
nuclear@10 83 {
nuclear@10 84 focus_factor = fact;
nuclear@10 85 }
nuclear@10 86
nuclear@10 87 float imtk_get_focus_factor(void)
nuclear@10 88 {
nuclear@10 89 return focus_factor;
nuclear@10 90 }
nuclear@10 91
nuclear@10 92 void imtk_set_press_factor(float fact)
nuclear@10 93 {
nuclear@10 94 press_factor = fact;
nuclear@10 95 }
nuclear@10 96
nuclear@10 97 float imtk_get_press_factor(void)
nuclear@10 98 {
nuclear@10 99 return press_factor;
nuclear@10 100 }
nuclear@10 101
nuclear@10 102 void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot)
nuclear@10 103 {
nuclear@10 104 glBegin(GL_QUADS);
nuclear@10 105 if(ctop) {
nuclear@10 106 glColor4fv(ctop);
nuclear@10 107 }
nuclear@10 108 glVertex2f(x, y);
nuclear@10 109 glVertex2f(x + w, y);
nuclear@10 110
nuclear@10 111 if(cbot) {
nuclear@10 112 glColor4fv(cbot);
nuclear@10 113 }
nuclear@10 114 glVertex2f(x + w, y + h);
nuclear@10 115 glVertex2f(x, y + h);
nuclear@10 116 glEnd();
nuclear@10 117 }
nuclear@10 118
nuclear@10 119 void imtk_draw_frame(int x, int y, int w, int h, int style)
nuclear@10 120 {
nuclear@10 121 float tcol[4], bcol[4];
nuclear@10 122 float b = imtk_get_bevel_width();
nuclear@10 123
nuclear@10 124 if(!b) {
nuclear@10 125 return;
nuclear@10 126 }
nuclear@10 127
nuclear@10 128 x -= b;
nuclear@10 129 y -= b;
nuclear@10 130 w += b * 2;
nuclear@10 131 h += b * 2;
nuclear@10 132
nuclear@10 133 switch(style) {
nuclear@10 134 case FRAME_INSET:
nuclear@10 135 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
nuclear@10 136 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
nuclear@10 137 break;
nuclear@10 138
nuclear@10 139 case FRAME_OUTSET:
nuclear@10 140 default:
nuclear@10 141 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
nuclear@10 142 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
nuclear@10 143 }
nuclear@10 144
nuclear@10 145 glBegin(GL_QUADS);
nuclear@10 146 glColor4fv(tcol);
nuclear@10 147 glVertex2f(x, y);
nuclear@10 148 glVertex2f(x + b, y + b);
nuclear@10 149 glVertex2f(x + w - b, y + b);
nuclear@10 150 glVertex2f(x + w, y);
nuclear@10 151
nuclear@10 152 glVertex2f(x + b, y + b);
nuclear@10 153 glVertex2f(x, y);
nuclear@10 154 glVertex2f(x, y + h);
nuclear@10 155 glVertex2f(x + b, y + h - b);
nuclear@10 156
nuclear@10 157 glColor4fv(bcol);
nuclear@10 158 glVertex2f(x + b, y + h - b);
nuclear@10 159 glVertex2f(x + w - b, y + h - b);
nuclear@10 160 glVertex2f(x + w, y + h);
nuclear@10 161 glVertex2f(x, y + h);
nuclear@10 162
nuclear@10 163 glVertex2f(x + w - b, y + b);
nuclear@10 164 glVertex2f(x + w, y);
nuclear@10 165 glVertex2f(x + w, y + h);
nuclear@10 166 glVertex2f(x + w - b, y + h - b);
nuclear@10 167 glEnd();
nuclear@10 168 }
nuclear@10 169
nuclear@10 170 void imtk_draw_disc(int x, int y, float rad, int subdiv, float *ctop, float *cbot)
nuclear@10 171 {
nuclear@10 172 int i;
nuclear@10 173 float t, dtheta, theta = 0.0;
nuclear@10 174 float color[4];
nuclear@10 175 float cx = (float)x;
nuclear@10 176 float cy = (float)y;
nuclear@10 177
nuclear@10 178 subdiv += 3;
nuclear@10 179 dtheta = 2.0 * M_PI / subdiv;
nuclear@10 180
nuclear@10 181 color[0] = (ctop[0] + cbot[0]) * 0.5;
nuclear@10 182 color[1] = (ctop[1] + cbot[1]) * 0.5;
nuclear@10 183 color[2] = (ctop[2] + cbot[2]) * 0.5;
nuclear@10 184 color[3] = (ctop[3] + cbot[3]) * 0.5;
nuclear@10 185
nuclear@10 186 glBegin(GL_TRIANGLE_FAN);
nuclear@10 187 glColor4fv(color);
nuclear@10 188 glVertex2f(cx, cy);
nuclear@10 189
nuclear@10 190 for(i=0; i<=subdiv; i++) {
nuclear@10 191 float vx, vy;
nuclear@10 192
nuclear@10 193 vx = cos(theta);
nuclear@10 194 vy = sin(theta);
nuclear@10 195 theta += dtheta;
nuclear@10 196
nuclear@10 197 t = (vy + 1.0) / 2.0;
nuclear@10 198 color[0] = ctop[0] + (cbot[0] - ctop[0]) * t;
nuclear@10 199 color[1] = ctop[1] + (cbot[1] - ctop[1]) * t;
nuclear@10 200 color[2] = ctop[2] + (cbot[2] - ctop[2]) * t;
nuclear@10 201 color[3] = ctop[3] + (cbot[3] - ctop[3]) * t;
nuclear@10 202
nuclear@10 203 glColor4fv(color);
nuclear@10 204 glVertex2f(cx + vx * rad, cy + vy * rad);
nuclear@10 205 }
nuclear@10 206 glEnd();
nuclear@10 207 }
nuclear@10 208
nuclear@10 209 void imtk_draw_disc_frame(int x, int y, float inner, float outer, int subdiv, int style)
nuclear@10 210 {
nuclear@10 211 int i;
nuclear@10 212 float t, dtheta, theta = 0.0;
nuclear@10 213 float color[4], tcol[4], bcol[4];
nuclear@10 214 float cx = (float)x;
nuclear@10 215 float cy = (float)y;
nuclear@10 216
nuclear@10 217 switch(style) {
nuclear@10 218 case FRAME_INSET:
nuclear@10 219 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
nuclear@10 220 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
nuclear@10 221 break;
nuclear@10 222
nuclear@10 223 case FRAME_OUTSET:
nuclear@10 224 default:
nuclear@10 225 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
nuclear@10 226 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
nuclear@10 227 }
nuclear@10 228
nuclear@10 229 subdiv += 3;
nuclear@10 230 dtheta = 2.0 * M_PI / subdiv;
nuclear@10 231
nuclear@10 232 glBegin(GL_QUAD_STRIP);
nuclear@10 233
nuclear@10 234 for(i=0; i<=subdiv; i++) {
nuclear@10 235 float vx, vy;
nuclear@10 236
nuclear@10 237 vx = cos(theta);
nuclear@10 238 vy = sin(theta);
nuclear@10 239
nuclear@10 240 t = (vy + 1.0) / 2.0;
nuclear@10 241 color[0] = tcol[0] + (bcol[0] - tcol[0]) * t;
nuclear@10 242 color[1] = tcol[1] + (bcol[1] - tcol[1]) * t;
nuclear@10 243 color[2] = tcol[2] + (bcol[2] - tcol[2]) * t;
nuclear@10 244 color[3] = tcol[3] + (bcol[3] - tcol[3]) * t;
nuclear@10 245
nuclear@10 246 vx = cos(theta - M_PI / 4.0);
nuclear@10 247 vy = sin(theta - M_PI / 4.0);
nuclear@10 248 theta += dtheta;
nuclear@10 249
nuclear@10 250 glColor4fv(color);
nuclear@10 251 glVertex2f(cx + vx * inner, cy + vy * inner);
nuclear@10 252 glVertex2f(cx + vx * outer, cy + vy * outer);
nuclear@10 253 }
nuclear@10 254 glEnd();
nuclear@10 255 }
nuclear@10 256
nuclear@10 257 void imtk_draw_string(int x, int y, const char *str)
nuclear@10 258 {
nuclear@10 259 glRasterPos2i(x, y);
nuclear@10 260 while(*str) {
nuclear@10 261 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
nuclear@10 262 }
nuclear@10 263 }
nuclear@10 264
nuclear@10 265 int imtk_string_size(const char *str)
nuclear@10 266 {
nuclear@10 267 return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
nuclear@10 268 }