dirty redraw and SDL framebuffer example
[windtk] / example_gl.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <GL/glut.h>
4 #include "windtk.h"
5
6 static void display(void);
7 static void reshape(int x, int y);
8 static void keypress(unsigned char key, int x, int y);
9 static void keyrelease(unsigned char key, int x, int y);
10 static void mouse(int bn, int st, int x, int y);
11 static void motion(int x, int y);
12
13 static int gfx_newcol(int r, int g, int b);
14 static void gfx_color(int c);
15 static void gfx_fillrect(struct wt_rect *r);
16 static void gfx_line(int x0, int y0, int x1, int y1);
17
18 static struct wt_graphics gfx = {
19         WT_GFX_NODIRTY,
20         gfx_newcol, gfx_color,
21         gfx_fillrect,
22         gfx_line
23 };
24
25 int main(int argc, char **argv)
26 {
27         glutInit(&argc, argv);
28         glutInitWindowSize(800, 600);
29         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
30         glutCreateWindow("windtk example");
31
32         glutDisplayFunc(display);
33         glutReshapeFunc(reshape);
34         glutKeyboardFunc(keypress);
35         glutKeyboardUpFunc(keyrelease);
36         glutMouseFunc(mouse);
37         glutMotionFunc(motion);
38         glutPassiveMotionFunc(motion);
39
40         glClearColor(0.6, 0.6, 0.6, 1);
41
42         if(wt_init(800, 600, &gfx) == -1) {
43                 return 1;
44         }
45         wt_window(0, "foo", WT_WS_DEFAULT, 100, 100, 200, 200);
46
47         glutMainLoop();
48         return 0;
49 }
50
51 static void display(void)
52 {
53         glClear(GL_COLOR_BUFFER_BIT);
54
55         wt_draw();
56
57         glutSwapBuffers();
58 }
59
60 static void reshape(int x, int y)
61 {
62         glViewport(0, 0, x, y);
63         glMatrixMode(GL_PROJECTION);
64         glLoadIdentity();
65         glOrtho(0, x, y, 0, -1, 1);
66
67         wt_viewport(0, 0, x, y);
68 }
69
70 static void keypress(unsigned char key, int x, int y)
71 {
72         if(key == 27) exit(0);
73
74         wt_inp_key(key, 1);
75 }
76
77 static void keyrelease(unsigned char key, int x, int y)
78 {
79         wt_inp_key(key, 0);
80 }
81
82 static void mouse(int bn, int st, int x, int y)
83 {
84         wt_inp_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
85 }
86
87 static void motion(int x, int y)
88 {
89         wt_inp_motion(x, y);
90 }
91
92 /* ------ graphics callbacks -------- */
93 static unsigned char colors[32][3];
94 static int maxcol;
95
96 static int gfx_newcol(int r, int g, int b)
97 {
98         int i;
99
100         for(i=0; i<maxcol; i++) {
101                 if(colors[i][0] == r && colors[i][1] == g && colors[i][2] == b) {
102                         return i;
103                 }
104         }
105
106         if(maxcol >= 32) return -1;
107
108         colors[maxcol][0] = r;
109         colors[maxcol][1] = g;
110         colors[maxcol][2] = b;
111         return maxcol++;
112 }
113
114 static void gfx_color(int c)
115 {
116         glColor3ubv(colors[c]);
117 }
118
119 static void gfx_fillrect(struct wt_rect *r)
120 {
121         glBegin(GL_QUADS);
122         glVertex2f(r->x, r->y);
123         glVertex2f(r->x + r->w, r->y);
124         glVertex2f(r->x + r->w, r->y + r->h);
125         glVertex2f(r->x, r->y + r->h);
126         glEnd();
127 }
128
129 static void gfx_line(int x0, int y0, int x1, int y1)
130 {
131         glBegin(GL_LINES);
132         glVertex2f(x0, y0);
133         glVertex2f(x1, y1);
134         glEnd();
135 }