start by copying windtk and dropping dirty rects and indexed colors
[anigui] / test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <GL/glut.h>
4 #include "anigui.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 void gfx_color(int r, int g, int b);
14 static void gfx_fillrect(struct ag_rect *r);
15 static void gfx_line(int x0, int y0, int x1, int y1);
16
17 static struct ag_graphics gfx = {
18         0,
19         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         glClearColor(0.6, 0.6, 0.6, 1);
40
41         if(ag_init(800, 600, &gfx) == -1) {
42                 return 1;
43         }
44         ag_window(0, "foo", AG_WS_DEFAULT, 100, 100, 200, 200);
45
46         glutMainLoop();
47         return 0;
48 }
49
50 static void display(void)
51 {
52         glClear(GL_COLOR_BUFFER_BIT);
53
54         ag_draw();
55
56         glutSwapBuffers();
57 }
58
59 static void reshape(int x, int y)
60 {
61         glViewport(0, 0, x, y);
62         glMatrixMode(GL_PROJECTION);
63         glLoadIdentity();
64         glOrtho(0, x, y, 0, -1, 1);
65
66         ag_viewport(0, 0, x, y);
67 }
68
69 static void keypress(unsigned char key, int x, int y)
70 {
71         if(key == 27) exit(0);
72
73         ag_inp_key(key, 1);
74 }
75
76 static void keyrelease(unsigned char key, int x, int y)
77 {
78         ag_inp_key(key, 0);
79 }
80
81 static void mouse(int bn, int st, int x, int y)
82 {
83         ag_inp_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
84 }
85
86 static void motion(int x, int y)
87 {
88         ag_inp_motion(x, y);
89 }
90
91 /* ------ graphics callbacks -------- */
92 static void gfx_color(int r, int g, int b)
93 {
94         glColor3ub(r, g, b);
95 }
96
97 static void gfx_fillrect(struct ag_rect *r)
98 {
99         glBegin(GL_QUADS);
100         glVertex2f(r->x, r->y);
101         glVertex2f(r->x + r->w, r->y);
102         glVertex2f(r->x + r->w, r->y + r->h);
103         glVertex2f(r->x, r->y + r->h);
104         glEnd();
105 }
106
107 static void gfx_line(int x0, int y0, int x1, int y1)
108 {
109         glBegin(GL_LINES);
110         glVertex2f(x0, y0);
111         glVertex2f(x1, y1);
112         glEnd();
113 }