initial commit
[windtk] / example.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         gfx_newcol, gfx_color,
20         gfx_fillrect,
21         gfx_line
22 };
23
24 int main(int argc, char **argv)
25 {
26         glutInit(&argc, argv);
27         glutInitWindowSize(800, 600);
28         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
29         glutCreateWindow("windtk example");
30
31         glutDisplayFunc(display);
32         glutReshapeFunc(reshape);
33         glutKeyboardFunc(keypress);
34         glutKeyboardUpFunc(keyrelease);
35         glutMouseFunc(mouse);
36         glutMotionFunc(motion);
37         glutPassiveMotionFunc(motion);
38
39         if(wt_init(800, 600, &gfx)) {
40                 return 1;
41         }
42
43         glutMainLoop();
44         return 0;
45 }
46
47 static void display(void)
48 {
49         glClear(GL_COLOR_BUFFER_BIT);
50
51         glutSwapBuffers();
52 }
53
54 static void reshape(int x, int y)
55 {
56         glViewport(0, 0, x, y);
57         glMatrixMode(GL_PROJECTION);
58         glLoadIdentity();
59         glOrtho(0, x, y, 0, -1, 1);
60
61         wt_viewport(0, 0, x, y);
62 }
63
64 static void keypress(unsigned char key, int x, int y)
65 {
66         if(key == 27) exit(0);
67
68         wt_inp_key(key, 1);
69 }
70
71 static void keyrelease(unsigned char key, int x, int y)
72 {
73         wt_inp_key(key, 0);
74 }
75
76 static void mouse(int bn, int st, int x, int y)
77 {
78         wt_inp_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
79 }
80
81 static void motion(int x, int y)
82 {
83         wt_inp_motion(x, y);
84 }
85
86 /* ------ graphics callbacks -------- */
87 static float colors[32][3];
88 static int maxcol;
89
90 static int gfx_newcol(int r, int g, int b)
91 {
92         if(maxcol >= 32) return -1;
93         colors[maxcol][0] = r / 255.0f;
94         colors[maxcol][1] = g / 255.0f;
95         colors[maxcol][2] = b / 255.0f;
96         return maxcol++;
97 }
98
99 static void gfx_color(int c)
100 {
101         glColor3fv(colors[c]);
102 }
103
104 static void gfx_fillrect(struct wt_rect *r)
105 {
106         glBegin(GL_QUADS);
107         glVertex2f(r->x, r->y);
108         glVertex2f(r->x + r->width, r->y);
109         glVertex2f(r->x + r->width, r->y + r->height);
110         glVertex2f(r->x, r->y + r->height);
111         glEnd();
112 }
113
114 static void gfx_line(int x0, int y0, int x1, int y1)
115 {
116         glBegin(GL_LINES);
117         glVertex2f(x0, y0);
118         glVertex2f(x1, y1);
119         glEnd();
120 }