aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/tinygl-0.4-ugfx/examples/nanox.c
blob: 14e5d4f1c7ec07be0b225ea5e758f908f9d0f7f5 (plain)
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
/*
 * Demonstration program for Nano-X graphics.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MWINCLUDECOLORS
#include <microwin/nano-X.h>
#include <GL/gl.h> 
#include <GL/nglx.h> 
#include "ui.h"

static	GR_WINDOW_ID	w1;		/* id for large window */
static	GR_GC_ID	gc1;		/* graphics context for text */

void errorcatcher();			/* routine to handle errors */

void tkSwapBuffers(void)
{
    nglXSwapBuffers(w1);
}

int
ui_loop(int argc,char **argv, const char *name)
{
	GR_EVENT	event;		/* current event */
	GR_IMAGE_ID	id = 0;
        NGLXContext cx;
        int width, height, k;

	if (GrOpen() < 0) {
		fprintf(stderr, "cannot open graphics\n");
		exit(1);
	}
	
        width = 400;
        height = 300;

	GrSetErrorHandler(errorcatcher);

	w1 = GrNewWindow(GR_ROOT_WINDOW_ID, 10, 10, width, height, 4, BLACK, WHITE);

	GrSelectEvents(w1, GR_EVENT_MASK_CLOSE_REQ|GR_EVENT_MASK_EXPOSURE|GR_EVENT_MASK_KEY_DOWN);

	GrMapWindow(w1);

	gc1 = GrNewGC();

	GrSetGCForeground(gc1, WHITE);

        cx = nglXCreateContext(NULL, 0);
        nglXMakeCurrent(w1, cx);
        
        init();
        reshape(width, height);

	while (1) {
            GrCheckNextEvent(&event);
            switch(event.type) {
            case GR_EVENT_TYPE_CLOSE_REQ:
                GrFreeImage(id);
                GrClose();
                exit(0);
            case GR_EVENT_TYPE_EXPOSURE:
                break;
            case GR_EVENT_TYPE_KEY_DOWN:
                {
                    GR_EVENT_KEYSTROKE *kp = &event.keystroke;
                    /* XXX: nanoX special keys are totally bugged ! */
                    switch(kp->ch) {
                    case 81:
                        k = KEY_LEFT;
                        break;
                    case 83:
                        k = KEY_RIGHT;
                        break;
                    case 82:
                        k = KEY_UP;
                        break;
                    case 84:
                        k = KEY_DOWN;
                        break;
                    default:
                        k = kp->ch;
                        break;
                    }
                    key(k, 0);
                }
                break;
            default:
                idle();
                break;
            }
        }

	return 0;
}


/*
 * Here on an unrecoverable error.
 */
void
errorcatcher(code, name, id)
	GR_ERROR	code;		/* error code */
	GR_FUNC_NAME	name;		/* function name which failed */
	GR_ID		id;		/* resource id */
{
	GrClose();
	fprintf(stderr, "DEMO ERROR: code %d, function %s, resource id %d\n",
		code, name, id);
	exit(1);
}