rayfract

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/imtk/draw.c	Mon Jul 31 18:58:56 2023 +0300
     1.3 @@ -0,0 +1,268 @@
     1.4 +#include <string.h>
     1.5 +#include <math.h>
     1.6 +#include <assert.h>
     1.7 +#include "draw.h"
     1.8 +#include "imtk.h"
     1.9 +
    1.10 +#define COLOR_MASK	0xff
    1.11 +
    1.12 +/* default colors, can be changed with imtk_set_color */
    1.13 +static float colors[][4] = {
    1.14 +	{0.0, 0.0, 0.0, 1.0},		/* text color */
    1.15 +	{0.75, 0.75, 0.75, 1.0},	/* top color */
    1.16 +	{0.56, 0.56, 0.56, 1.0},	/* bot color */
    1.17 +	{0.9, 0.9, 0.9, 1.0},		/* lit bevel */
    1.18 +	{0.3, 0.3, 0.3, 1.0},		/* shadowed bevel */
    1.19 +	{0.8, 0.25, 0.18, 1.0},		/* cursor color */
    1.20 +	{0.68, 0.85, 1.3, 1.0},		/* selection color */
    1.21 +	{0.75, 0.1, 0.095, 1.0}		/* check color */
    1.22 +};
    1.23 +
    1.24 +static float focus_factor = 1.15;
    1.25 +static float press_factor = 0.8;
    1.26 +static float alpha = 1.0;
    1.27 +static float bevel = 1.0;
    1.28 +
    1.29 +void imtk_set_color(unsigned int col, float r, float g, float b, float a)
    1.30 +{
    1.31 +	int idx = col & COLOR_MASK;
    1.32 +	assert(idx >= 0 && idx < sizeof colors / sizeof *colors);
    1.33 +
    1.34 +	colors[idx][0] = r;
    1.35 +	colors[idx][1] = g;
    1.36 +	colors[idx][2] = b;
    1.37 +	colors[idx][3] = a;
    1.38 +}
    1.39 +
    1.40 +float *imtk_get_color(unsigned int col)
    1.41 +{
    1.42 +	static float ret[4];
    1.43 +	int idx = col & COLOR_MASK;
    1.44 +
    1.45 +	memcpy(ret, colors + idx, sizeof ret);
    1.46 +	if(col & IMTK_FOCUS_BIT) {
    1.47 +		ret[0] *= focus_factor;
    1.48 +		ret[1] *= focus_factor;
    1.49 +		ret[2] *= focus_factor;
    1.50 +	}
    1.51 +	if(col & IMTK_PRESS_BIT) {
    1.52 +		ret[0] *= press_factor;
    1.53 +		ret[1] *= press_factor;
    1.54 +		ret[2] *= press_factor;
    1.55 +	}
    1.56 +	if(col & IMTK_SEL_BIT) {
    1.57 +		ret[0] *= colors[IMTK_SELECTION_COLOR][0];
    1.58 +		ret[1] *= colors[IMTK_SELECTION_COLOR][1];
    1.59 +		ret[2] *= colors[IMTK_SELECTION_COLOR][2];
    1.60 +	}
    1.61 +	ret[3] *= alpha;
    1.62 +	return ret;
    1.63 +}
    1.64 +
    1.65 +void imtk_set_alpha(float a)
    1.66 +{
    1.67 +	alpha = a;
    1.68 +}
    1.69 +
    1.70 +float imtk_get_alpha(void)
    1.71 +{
    1.72 +	return alpha;
    1.73 +}
    1.74 +
    1.75 +void imtk_set_bevel_width(float b)
    1.76 +{
    1.77 +	bevel = b;
    1.78 +}
    1.79 +
    1.80 +float imtk_get_bevel_width(void)
    1.81 +{
    1.82 +	return bevel;
    1.83 +}
    1.84 +
    1.85 +void imtk_set_focus_factor(float fact)
    1.86 +{
    1.87 +	focus_factor = fact;
    1.88 +}
    1.89 +
    1.90 +float imtk_get_focus_factor(void)
    1.91 +{
    1.92 +	return focus_factor;
    1.93 +}
    1.94 +
    1.95 +void imtk_set_press_factor(float fact)
    1.96 +{
    1.97 +	press_factor = fact;
    1.98 +}
    1.99 +
   1.100 +float imtk_get_press_factor(void)
   1.101 +{
   1.102 +	return press_factor;
   1.103 +}
   1.104 +
   1.105 +void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot)
   1.106 +{
   1.107 +	glBegin(GL_QUADS);
   1.108 +	if(ctop) {
   1.109 +		glColor4fv(ctop);
   1.110 +	}
   1.111 +	glVertex2f(x, y);
   1.112 +	glVertex2f(x + w, y);
   1.113 +
   1.114 +	if(cbot) {
   1.115 +		glColor4fv(cbot);
   1.116 +	}
   1.117 +	glVertex2f(x + w, y + h);
   1.118 +	glVertex2f(x, y + h);
   1.119 +	glEnd();
   1.120 +}
   1.121 +
   1.122 +void imtk_draw_frame(int x, int y, int w, int h, int style)
   1.123 +{
   1.124 +	float tcol[4], bcol[4];
   1.125 +	float b = imtk_get_bevel_width();
   1.126 +
   1.127 +	if(!b) {
   1.128 +		return;
   1.129 +	}
   1.130 +
   1.131 +	x -= b;
   1.132 +	y -= b;
   1.133 +	w += b * 2;
   1.134 +	h += b * 2;
   1.135 +
   1.136 +	switch(style) {
   1.137 +	case FRAME_INSET:
   1.138 +		memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
   1.139 +		memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
   1.140 +		break;
   1.141 +
   1.142 +	case FRAME_OUTSET:
   1.143 +	default:
   1.144 +		memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
   1.145 +		memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
   1.146 +	}
   1.147 +
   1.148 +	glBegin(GL_QUADS);
   1.149 +	glColor4fv(tcol);
   1.150 +	glVertex2f(x, y);
   1.151 +	glVertex2f(x + b, y + b);
   1.152 +	glVertex2f(x + w - b, y + b);
   1.153 +	glVertex2f(x + w, y);
   1.154 +
   1.155 +	glVertex2f(x + b, y + b);
   1.156 +	glVertex2f(x, y);
   1.157 +	glVertex2f(x, y + h);
   1.158 +	glVertex2f(x + b, y + h - b);
   1.159 +
   1.160 +	glColor4fv(bcol);
   1.161 +	glVertex2f(x + b, y + h - b);
   1.162 +	glVertex2f(x + w - b, y + h - b);
   1.163 +	glVertex2f(x + w, y + h);
   1.164 +	glVertex2f(x, y + h);
   1.165 +
   1.166 +	glVertex2f(x + w - b, y + b);
   1.167 +	glVertex2f(x + w, y);
   1.168 +	glVertex2f(x + w, y + h);
   1.169 +	glVertex2f(x + w - b, y + h - b);
   1.170 +	glEnd();
   1.171 +}
   1.172 +
   1.173 +void imtk_draw_disc(int x, int y, float rad, int subdiv, float *ctop, float *cbot)
   1.174 +{
   1.175 +	int i;
   1.176 +	float t, dtheta, theta = 0.0;
   1.177 +	float color[4];
   1.178 +	float cx = (float)x;
   1.179 +	float cy = (float)y;
   1.180 +
   1.181 +	subdiv += 3;
   1.182 +	dtheta = 2.0 * M_PI / subdiv;
   1.183 +
   1.184 +	color[0] = (ctop[0] + cbot[0]) * 0.5;
   1.185 +	color[1] = (ctop[1] + cbot[1]) * 0.5;
   1.186 +	color[2] = (ctop[2] + cbot[2]) * 0.5;
   1.187 +	color[3] = (ctop[3] + cbot[3]) * 0.5;
   1.188 +
   1.189 +	glBegin(GL_TRIANGLE_FAN);
   1.190 +	glColor4fv(color);
   1.191 +	glVertex2f(cx, cy);
   1.192 +
   1.193 +	for(i=0; i<=subdiv; i++) {
   1.194 +		float vx, vy;
   1.195 +
   1.196 +		vx = cos(theta);
   1.197 +		vy = sin(theta);
   1.198 +		theta += dtheta;
   1.199 +
   1.200 +		t = (vy + 1.0) / 2.0;
   1.201 +		color[0] = ctop[0] + (cbot[0] - ctop[0]) * t;
   1.202 +		color[1] = ctop[1] + (cbot[1] - ctop[1]) * t;
   1.203 +		color[2] = ctop[2] + (cbot[2] - ctop[2]) * t;
   1.204 +		color[3] = ctop[3] + (cbot[3] - ctop[3]) * t;
   1.205 +
   1.206 +		glColor4fv(color);
   1.207 +		glVertex2f(cx + vx * rad, cy + vy * rad);
   1.208 +	}
   1.209 +	glEnd();
   1.210 +}
   1.211 +
   1.212 +void imtk_draw_disc_frame(int x, int y, float inner, float outer, int subdiv, int style)
   1.213 +{
   1.214 +	int i;
   1.215 +	float t, dtheta, theta = 0.0;
   1.216 +	float color[4], tcol[4], bcol[4];
   1.217 +	float cx = (float)x;
   1.218 +	float cy = (float)y;
   1.219 +
   1.220 +	switch(style) {
   1.221 +	case FRAME_INSET:
   1.222 +		memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
   1.223 +		memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
   1.224 +		break;
   1.225 +
   1.226 +	case FRAME_OUTSET:
   1.227 +	default:
   1.228 +		memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
   1.229 +		memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
   1.230 +	}
   1.231 +
   1.232 +	subdiv += 3;
   1.233 +	dtheta = 2.0 * M_PI / subdiv;
   1.234 +
   1.235 +	glBegin(GL_QUAD_STRIP);
   1.236 +
   1.237 +	for(i=0; i<=subdiv; i++) {
   1.238 +		float vx, vy;
   1.239 +
   1.240 +		vx = cos(theta);
   1.241 +		vy = sin(theta);
   1.242 +
   1.243 +		t = (vy + 1.0) / 2.0;
   1.244 +		color[0] = tcol[0] + (bcol[0] - tcol[0]) * t;
   1.245 +		color[1] = tcol[1] + (bcol[1] - tcol[1]) * t;
   1.246 +		color[2] = tcol[2] + (bcol[2] - tcol[2]) * t;
   1.247 +		color[3] = tcol[3] + (bcol[3] - tcol[3]) * t;
   1.248 +
   1.249 +		vx = cos(theta - M_PI / 4.0);
   1.250 +		vy = sin(theta - M_PI / 4.0);
   1.251 +		theta += dtheta;
   1.252 +
   1.253 +		glColor4fv(color);
   1.254 +		glVertex2f(cx + vx * inner, cy + vy * inner);
   1.255 +		glVertex2f(cx + vx * outer, cy + vy * outer);
   1.256 +	}
   1.257 +	glEnd();
   1.258 +}
   1.259 +
   1.260 +void imtk_draw_string(int x, int y, const char *str)
   1.261 +{
   1.262 +	glRasterPos2i(x, y);
   1.263 +	while(*str) {
   1.264 +		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
   1.265 +	}
   1.266 +}
   1.267 +
   1.268 +int imtk_string_size(const char *str)
   1.269 +{
   1.270 +	return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
   1.271 +}