dirty redraw and SDL framebuffer example
[windtk] / example_gl.c
diff --git a/example_gl.c b/example_gl.c
new file mode 100644 (file)
index 0000000..17ed974
--- /dev/null
@@ -0,0 +1,135 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <GL/glut.h>
+#include "windtk.h"
+
+static void display(void);
+static void reshape(int x, int y);
+static void keypress(unsigned char key, int x, int y);
+static void keyrelease(unsigned char key, int x, int y);
+static void mouse(int bn, int st, int x, int y);
+static void motion(int x, int y);
+
+static int gfx_newcol(int r, int g, int b);
+static void gfx_color(int c);
+static void gfx_fillrect(struct wt_rect *r);
+static void gfx_line(int x0, int y0, int x1, int y1);
+
+static struct wt_graphics gfx = {
+       WT_GFX_NODIRTY,
+       gfx_newcol, gfx_color,
+       gfx_fillrect,
+       gfx_line
+};
+
+int main(int argc, char **argv)
+{
+       glutInit(&argc, argv);
+       glutInitWindowSize(800, 600);
+       glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
+       glutCreateWindow("windtk example");
+
+       glutDisplayFunc(display);
+       glutReshapeFunc(reshape);
+       glutKeyboardFunc(keypress);
+       glutKeyboardUpFunc(keyrelease);
+       glutMouseFunc(mouse);
+       glutMotionFunc(motion);
+       glutPassiveMotionFunc(motion);
+
+       glClearColor(0.6, 0.6, 0.6, 1);
+
+       if(wt_init(800, 600, &gfx) == -1) {
+               return 1;
+       }
+       wt_window(0, "foo", WT_WS_DEFAULT, 100, 100, 200, 200);
+
+       glutMainLoop();
+       return 0;
+}
+
+static void display(void)
+{
+       glClear(GL_COLOR_BUFFER_BIT);
+
+       wt_draw();
+
+       glutSwapBuffers();
+}
+
+static void reshape(int x, int y)
+{
+       glViewport(0, 0, x, y);
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity();
+       glOrtho(0, x, y, 0, -1, 1);
+
+       wt_viewport(0, 0, x, y);
+}
+
+static void keypress(unsigned char key, int x, int y)
+{
+       if(key == 27) exit(0);
+
+       wt_inp_key(key, 1);
+}
+
+static void keyrelease(unsigned char key, int x, int y)
+{
+       wt_inp_key(key, 0);
+}
+
+static void mouse(int bn, int st, int x, int y)
+{
+       wt_inp_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
+}
+
+static void motion(int x, int y)
+{
+       wt_inp_motion(x, y);
+}
+
+/* ------ graphics callbacks -------- */
+static unsigned char colors[32][3];
+static int maxcol;
+
+static int gfx_newcol(int r, int g, int b)
+{
+       int i;
+
+       for(i=0; i<maxcol; i++) {
+               if(colors[i][0] == r && colors[i][1] == g && colors[i][2] == b) {
+                       return i;
+               }
+       }
+
+       if(maxcol >= 32) return -1;
+
+       colors[maxcol][0] = r;
+       colors[maxcol][1] = g;
+       colors[maxcol][2] = b;
+       return maxcol++;
+}
+
+static void gfx_color(int c)
+{
+       glColor3ubv(colors[c]);
+}
+
+static void gfx_fillrect(struct wt_rect *r)
+{
+       glBegin(GL_QUADS);
+       glVertex2f(r->x, r->y);
+       glVertex2f(r->x + r->w, r->y);
+       glVertex2f(r->x + r->w, r->y + r->h);
+       glVertex2f(r->x, r->y + r->h);
+       glEnd();
+}
+
+static void gfx_line(int x0, int y0, int x1, int y1)
+{
+       glBegin(GL_LINES);
+       glVertex2f(x0, y0);
+       glVertex2f(x1, y1);
+       glEnd();
+}