X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=example_gl.c;fp=example_gl.c;h=17ed974d0b49b35168f004d08f3d3ea8ed3ec454;hb=492a32903dd07d3736bb878e81eb95f351556bac;hp=0000000000000000000000000000000000000000;hpb=526fc352816f21f1cbfa8110262a58224c14d208;p=windtk diff --git a/example_gl.c b/example_gl.c new file mode 100644 index 0000000..17ed974 --- /dev/null +++ b/example_gl.c @@ -0,0 +1,135 @@ +#include +#include +#include +#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= 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(); +}