File: cpins.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (647 lines) | stat: -rw-r--r-- 15,857 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef __MSW__
# include <windows.h>
#endif
#include "../include/string.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "gw.h"
#include "v3dtex.h"
#include "cpvalues.h"
#include "cpins.h"
#include "cp.h"


void CPInsSetName(CPIns *ins, const char *name);
void CPInsSetPosition(CPIns *ins, float x, float y);
void CPInsSetSize(CPIns *ins, float width, float height);
void CPInsSetTextureBG(CPIns *ins, const char *path);
void CPInsSetTextureFG(CPIns *ins, const char *path);
void CPInsSetResolution(
        CPIns *ins,
        int res_width, int res_height   /* In pixels. */
);
void CPInsSetUpdateInt(CPIns *ins, time_t update_int);
void CPInsSetFunction(
        CPIns *ins, const char *func_name,
        void *func, void *data
);

void CPInsResetTimmers(CPIns *ins, time_t t);
void CPInsChangeValues(CPIns *ins, ControlPanelValues *v);
void CPInsDraw(CPIns *ins);
void CPInsDrawFullScreen(CPIns *ins, const CPRectangle *rect);
void CPInsManage(CPIns *ins, ControlPanelValues *v);
void CPInsRealizeTexture(CPIns *ins);

CPIns *CPInsNew(size_t s, void *cp, int type, const char *name);
void CPInsDelete(CPIns *ins);


#ifndef PI
# define PI     3.14159265
#endif

#define ATOI(s)         (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)         (((s) != NULL) ? atol(s) : 0)
#define ATOF(s)         (((s) != NULL) ? (float)atof(s) : 0.0f)
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)

#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))

#define RADTODEG(r)     ((r) * 180.0 / PI)
#define DEGTORAD(d)     ((d) * PI / 180.0)

#define NORMAL3F(x,y,z) (glNormal3f(    \
                         (GLfloat)(x),  \
                         (GLfloat)(z),  \
                         (GLfloat)-(y)  \
                        ))
#define TEXCOORD2F(x,y) (glTexCoord2f(          \
                         (GLfloat)(x),		\
                         (GLfloat)(y)		\
                        ))
#define VERTEX3F(x,y,z) (glVertex3f(                    \
                         (GLfloat)((x) / 100.0),        \
                         (GLfloat)((z) / 100.0),        \
                         (GLfloat)((y) / -100.0)        \
                        ))
#define ROTATEH(r)      (glRotatef((GLfloat)-RADTODEG(r), \
                         0.0f, 1.0f, 0.0f))
#define ROTATEP(r)      (glRotatef((GLfloat)-RADTODEG(r), \
                         1.0f, 0.0f, 0.0f))
#define ROTATEB(r)      (glRotatef((GLfloat)-RADTODEG(r), \
                         0.0f, 0.0f, 1.0f))
#define TRANSLATE(x,y,z)        (glTranslatef(          \
                         (GLfloat)((x) / 100.0),        \
                         (GLfloat)((z) / 100.0),        \
                         (GLfloat)((y) / -100.0)        \
                        ))


/*
 *	Sets name of instrument.
 */
void CPInsSetName(CPIns *ins, const char *name)
{
        if(ins == NULL)
            return;

	free(ins->name);
	ins->name = STRDUP(name);
}

/*
 *	Set position coefficient of instrument relative to the control
 *	panel's upper left corner.
 */
void CPInsSetPosition(CPIns *ins, float x, float y)
{
	if(ins == NULL)
	    return;

	ins->x = x;
	ins->y = y;
}

/*
 *	Sets the size coefficient of the instrument.
 */
void CPInsSetSize(CPIns *ins, float width, float height)
{
        if(ins == NULL)
            return;

        ins->width = width;
        ins->height = height;
}

/*
 *      Set instrument background texture.
 */
void CPInsSetTextureBG(CPIns *ins, const char *path)
{
        if(ins == NULL)
            return;

        V3DTextureDestroy(ins->tex_bg);
        ins->tex_bg = NULL;

        if((path != NULL) ? (*path == '\0') : GL_TRUE)
            return;

        ins->tex_bg = V3DTextureLoadFromFile2DPreempt(
            path, "instrument_bg_tex",
            V3D_TEX_FORMAT_RGBA
        );
}

/*
 *      Set instrument foreground texture.
 */
void CPInsSetTextureFG(CPIns *ins, const char *path)
{
        if(ins == NULL)
            return;

        V3DTextureDestroy(ins->tex_fg);
        ins->tex_fg = NULL;

        if((path != NULL) ? (*path == '\0') : GL_TRUE)
            return;

        ins->tex_fg = V3DTextureLoadFromFile2DPreempt(
            path, "instrument_fg_tex",
            V3D_TEX_FORMAT_RGBA
        );
}


/*
 *	Sets the texture resolution of the instrument in pixels.
 */
void CPInsSetResolution(
        CPIns *ins,
        int res_width, int res_height   /* In pixels. */
)
{
	if(ins == NULL)
            return;

	/* Update resolution only if there is change. */
	if((ins->res_width != res_width) ||
	   (ins->res_height != res_height)
	)
	{
	    ins->res_width = res_width;
	    ins->res_height = res_height;

	    /* Destroy texture. */
	    V3DTextureDestroy(ins->tex);
	    ins->tex = NULL;

	    /* Recreate texture if resolution is positive. */
	    if((res_width > 0) && (res_height > 0))
	    {
		u_int8_t *data = (u_int8_t *)malloc(
		    res_width * res_height * 4 * sizeof(u_int8_t)
		);
		ins->tex = V3DTextureLoadFromData2D(
		    (const void *)data,
		    NULL,
		    res_width, res_height,
		    32,
		    V3D_TEX_FORMAT_RGBA,
		    NULL, NULL
		);
		free(data);
		V3DTexturePriority(ins->tex, 1.0);
	    }
	}
}

/*
 *	Sets the instrument update interval in milliseconds.
 */
void CPInsSetUpdateInt(CPIns *ins, time_t update_int)
{
        if(ins == NULL)
            return;

        ins->update_int = update_int;
}

/*
 *	Sets the function pointer func to the function specified by
 *	func_name.
 */
void CPInsSetFunction(
        CPIns *ins, const char *func_name,
        void *func, void *data
)
{
	if((ins == NULL) || (func_name == NULL))
	    return;

        if(!strcasecmp(func_name, "values_changed"))
        {
            ins->values_changed_func_data = data;
            ins->values_changed_func = func;
        }
        else if(!strcasecmp(func_name, "manage"))
        {
            ins->manage_func_data = data;
            ins->manage_func = func;
        }
	else if(!strcasecmp(func_name, "reset_timmers"))
	{
	    ins->reset_timmers_func_data = data;
	    ins->reset_timmers_func = func;
	}
	else if(!strcasecmp(func_name, "delete"))
        {
            ins->delete_func_data = data;
            ins->delete_func = func;
        }
	else
	{
	    fprintf(
		stderr,
"CPInsSetFunction(): Warning:\
 Instrument \"%s\": Unsupported function \"%s\".",
		CPINS_NAME(ins), func_name
	    );
	}
}

/*
 *	Reset timmer of instrument.
 */
void CPInsResetTimmers(CPIns *ins, time_t t)
{
        if(ins == NULL)
            return;

	ins->update_next = t;
        if(ins->reset_timmers_func != NULL)
            ins->reset_timmers_func(ins, t, ins->reset_timmers_func_data);
}

/*
 *	Change values of instrument.
 */
void CPInsChangeValues(CPIns *ins, ControlPanelValues *v)
{
        if((ins == NULL) || (v == NULL))
            return;

        if(ins->values_changed_func != NULL)
	{
	    ControlPanel *cp = CONTROL_PANEL(ins->cp);
	    gw_display_struct *display = CONTROL_PANEL_DISPLAY(cp);
	    int	width = ins->res_width,
		height = ins->res_height;
	    if((display == NULL) || (width <= 1) || (height < 1))
		return;

	    /* Set up GL states for 2d drawing before calling the
	     * "values_changed" function. This is for convience so that
	     * the GL state is all ready for drawing of the texture.
	     */
            glViewport(0, 0, width, height);

            glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
            glPixelStorei(GL_PACK_ALIGNMENT, 1);

            StateGLDisable(&display->state_gl, GL_LIGHTING);
            StateGLDisable(&display->state_gl, GL_BLEND);
            StateGLDisable(&display->state_gl, GL_FOG);
            StateGLDisable(&display->state_gl, GL_DEPTH_TEST);
            StateGLDepthFunc(&display->state_gl, GL_ALWAYS);
            StateGLEnable(&display->state_gl, GL_ALPHA_TEST);
            StateGLAlphaFunc(&display->state_gl, GL_GREATER, 0.5);
            StateGLShadeModel(&display->state_gl, GL_FLAT);
            StateGLEnable(&display->state_gl, GL_TEXTURE_2D);
            StateGLTexEnvI(
                &display->state_gl,
                GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE
            );
            StateGLDisable(&display->state_gl, GL_CULL_FACE);
            StateGLDisable(&display->state_gl, GL_COLOR_MATERIAL);

	    GWOrtho2DCoord(
		display,
		/* Left and right coordinate values. */
                0.0f, (float)MAX(width - 1, 1),
		/* Top and bottom coordinate values. */
                (float)MAX(height - 1, 1), 0.0f
            );

	    ins->values_changed_func(
		ins,
		v,
		ins->values_changed_func_data
	    );
	}
}

/*
 *	Redraws the instrument in 3d.
 */
void CPInsDraw(CPIns *ins)
{
	GLfloat x, y, width, height;
	ControlPanel *cp;
        gw_display_struct *display;
        v3d_texture_ref_struct *tex;
        if(ins == NULL)
            return;

	cp = CONTROL_PANEL(ins->cp);
	display = CONTROL_PANEL_DISPLAY(cp);
	if(display == NULL)
	    return;

	if(GL_TRUE)
	{
	    /* Calculate position and size (in centimeters) relative to
	     * the control panel's upper left corner.
	     */
	    x = ins->x * cp->width;
	    y = ins->y * cp->height;
	    width = ins->width * cp->width;
	    height = ins->height * cp->height;

	    /* Begin drawing instrument. */
	    glPushMatrix();

	    TRANSLATE(x, -y, 0.0);

	    tex = ins->tex;
	    if(tex != NULL)
	    {
		glColor3f(1.0f, 1.0f, 1.0f);
		V3DTextureSelect(tex);
	    }
	    else
	    {
		glColor3f(0.0f, 0.0f, 0.0f);
	    }
	    glBegin(GL_QUADS);
	    {
		NORMAL3F(0.0, 0.0, 1.0);
		TEXCOORD2F(0, 0);
		VERTEX3F(0.0, 0.0, 0.0);
		TEXCOORD2F(0, 1);
		VERTEX3F(0.0, -height, 0.0);
		TEXCOORD2F(1, 1);
		VERTEX3F(width, -height, 0.0);
		TEXCOORD2F(1, 0);
		VERTEX3F(width, 0.0, 0.0);
	    }
	    glEnd();

	    glPopMatrix();
	}
}

/*
 *      Redraws the instrument in 2d with respect to the given
 *	rectangle indicating the size and position of the control panel
 *	in window coordinates..
 */
void CPInsDrawFullScreen(CPIns *ins, const CPRectangle *rect)
{
	GLfloat x, y, width, height;
        ControlPanel *cp;
        gw_display_struct *display;
        v3d_texture_ref_struct *tex;
        if((ins == NULL) || (rect == NULL))
            return;

        cp = CONTROL_PANEL(ins->cp);
        display = CONTROL_PANEL_DISPLAY(cp);
        if(display == NULL)
            return;

        if(GL_TRUE)
        {
            /* Calculate position and size (in window coordinates)
	     * relative to the control panel's upper left corner.
             */
            x = rect->x + (ins->x * rect->width);
            y = rect->y + (ins->y * rect->height);
            width = ins->width * rect->width;
            height = ins->height * rect->height;

            tex = ins->tex;
            if(tex != NULL)
            {
                glColor3f(1.0f, 1.0f, 1.0f);
                V3DTextureSelect(tex);
            }
            else
            {
                glColor3f(0.0f, 0.0f, 0.0f);
            }
            glBegin(GL_QUADS);
            {
		glTexCoord2f(0, 0);
		glVertex2f(x, y);
                glTexCoord2f(0, 1);
                glVertex2f(x, y + height);
                glTexCoord2f(1, 1);
                glVertex2f(x + width, y + height);
                glTexCoord2f(1, 0);
                glVertex2f(x + width, y);
            }
            glEnd();
	}
}

/*
 *	Manages the instrument, this should be called once per loop.
 *
 *	Only the members current_time and time_compensation in v are
 *	used.
 */
void CPInsManage(CPIns *ins, ControlPanelValues *v)
{
	if((ins == NULL) || (v == NULL))
	    return;

        if(ins->manage_func != NULL)
            ins->manage_func(ins, v, ins->manage_func_data);
}

/*
 *	Gets the texture from the drawn up back buffer of the GL frame 
 *	buffer.
 */
void CPInsRealizeTexture(CPIns *ins)
{
	int x, y, xoffset, yoffset;
	int res_width, res_height;
        ControlPanel *cp;
        gw_display_struct *display;
        v3d_texture_ref_struct *tex;
        if(ins == NULL)
            return;

        cp = CONTROL_PANEL(ins->cp);
        display = CONTROL_PANEL_DISPLAY(cp);
        if(display == NULL)
            return;

	res_width = ins->res_width;
	res_height = ins->res_height;

	/* Get instrument's texture, recreate it as needed. */
	tex = ins->tex;
	if(tex == NULL)
	{
	    CPInsSetResolution(ins, 0, 0);
	    CPInsSetResolution(ins, res_width, res_height);
	    tex = ins->tex;
	}
	if(tex == NULL)
	    return;

	/* Texture size must match resolution size. */
	if((res_width != tex->width) ||
	   (res_height != tex->height)
	)
	    return;

	/* Select instrument texture. */
        StateGLEnable(&display->state_gl, GL_TEXTURE_2D);
	V3DTextureSelect(tex);

	/* Read back drawn frame buffer's alpha channel. */
	if(display->has_double_buffer)
	    glReadBuffer(GL_BACK);
	else
	    glReadBuffer(GL_FRONT);

	/* Texture position. */
	xoffset = 0;
	yoffset = 0;

	/* Frame buffer position. */
	x = 0;
	y = 0;

	/* Copy frame buffer contents to texture. */
	glCopyTexSubImage2D(
	    GL_TEXTURE_2D,
	    0,		/* level-of-detail. */
	    xoffset, yoffset,
	    x, y,
	    res_width, res_height
	);
}


/*
 *	Creates a new instrument.
 *
 *	The size s must be sizeof(CPIns) or greater.
 */
CPIns *CPInsNew(size_t s, void *cp, int type, const char *name)
{
	CPColor *c;
	CPIns *ins;

	if(s < sizeof(CPIns))
	{
	    fprintf(
		stderr,
"CPInsNew(): Error: Instrument \"%s\" size %i is less than required size %i.\n",
		name, s, sizeof(CPIns)
	    );
	    return(NULL);
	}

	ins = CPINS(calloc(1, s));
	if(ins == NULL)
	    return(NULL);

	ins->type = type;
	ins->cp = cp;
	ins->name = STRDUP(name);

	ins->res_width = 64;
	ins->res_height = 64;
	ins->tex = NULL;

	ins->tex_bg = NULL;
	ins->tex_fg = NULL;

	c = &ins->color_bg[CP_COLOR_STATE_NORMAL];
	c->r = 0.3f;
	c->g = 0.3f;
	c->b = 0.3f;
        c = &ins->color_bg[CP_COLOR_STATE_DIM];
        c->r = 0.15f;
        c->g = 0.15f;
        c->b = 0.15f;
        c = &ins->color_bg[CP_COLOR_STATE_DARK];
        c->r = 0.0f;
        c->g = 0.0f;
        c->b = 0.0f;
        c = &ins->color_bg[CP_COLOR_STATE_LIGHTED];
        c->r = 0.3f;
        c->g = 0.05f;
        c->b = 0.0f;

        c = &ins->color_fg[CP_COLOR_STATE_NORMAL];
        c->r = 0.8f;
        c->g = 0.8f;
        c->b = 0.8f;
        c = &ins->color_fg[CP_COLOR_STATE_DIM];
        c->r = 0.4f;
        c->g = 0.4f;
        c->b = 0.4f;
        c = &ins->color_fg[CP_COLOR_STATE_DARK];
        c->r = 0.1f;
        c->g = 0.1f;
        c->b = 0.1f;
        c = &ins->color_fg[CP_COLOR_STATE_LIGHTED];
        c->r = 0.8f;
        c->g = 0.3f;
        c->b = 0.0f;

        c = &ins->color_text[CP_COLOR_STATE_NORMAL];
        c->r = 0.8f;
        c->g = 0.8f;
        c->b = 0.8f;
        c = &ins->color_text[CP_COLOR_STATE_DIM];
        c->r = 0.4f;
        c->g = 0.4f;
        c->b = 0.4f;
        c = &ins->color_text[CP_COLOR_STATE_DARK];
        c->r = 0.1f;
        c->g = 0.1f;
        c->b = 0.1f;
        c = &ins->color_text[CP_COLOR_STATE_LIGHTED];
        c->r = 0.8f;
        c->g = 0.3f;
        c->b = 0.0f;


	return(ins);
}

/*
 *	Deletes the instrument.
 */
void CPInsDelete(CPIns *ins)
{
        if(ins == NULL)
            return;

	/* Call delete function to delete the instrument's
	 * substructures and other resources not accessable from
	 * this module.
	 */
	if(ins->delete_func != NULL)
	    ins->delete_func(ins, ins->delete_func_data);
	else if(!CPINS_IS_INS(ins))
	    fprintf(
		stderr,
"CPInsDelete(): Warning: Instrument \"%s\": \"delete\" function not set.\n",
		CPINS_NAME(ins)
	    );

	/* Begin deleting instrument. */
        V3DTextureDestroy(ins->tex_bg);
        V3DTextureDestroy(ins->tex_fg);
	V3DTextureDestroy(ins->tex);
	free(ins->name);
	free(ins);
}