Files
milsko/examples/gltripaint.c
NishiOwO 24e7ea5430 comment is not accurate anaymore
git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@308 b9cfdab3-6d41-4d17-bbe4-086880011989
2025-10-13 12:41:20 +00:00

124 lines
2.8 KiB
C

/* $Id$ */
#include <Mw/Milsko.h>
#include <Mw/Widget/OpenGL.h>
#include <stdlib.h>
MwWidget opengl;
typedef struct triangle {
int points[3 * 2];
double r;
double g;
double b;
} triangle_t;
triangle_t* t = NULL;
int ct = 0;
int click = 0;
int mx, my;
static void tick(MwWidget handle, void* user, void* call) {
int i;
int w = MwGetInteger(opengl, MwNwidth);
int h = MwGetInteger(opengl, MwNheight);
MwOpenGLMakeCurrent(opengl);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, h, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for(i = 0; i < ct + (click == 2 ? 1 : 0); i++) {
int j;
glBegin(GL_TRIANGLES);
glColor3d(t[i].r, t[i].g, t[i].b);
for(j = 0; j < 3; j++) {
glVertex2d(t[i].points[j * 2 + 0], t[i].points[j * 2 + 1]);
}
glEnd();
}
MwOpenGLSwapBuffer(opengl);
}
static void mouse(MwWidget handle, void* user, void* call) {
MwLLMouse* mouse = call;
if(mouse->button == MwLLMouseLeft) {
t[ct].points[click * 2 + 0] = mouse->point.x;
t[ct].points[click * 2 + 1] = mouse->point.y;
click++;
if(click == 1) {
t[ct].r = rand() % 65536;
t[ct].g = rand() % 65536;
t[ct].b = rand() % 65536;
t[ct].r /= 65535;
t[ct].g /= 65535;
t[ct].b /= 65535;
} else if(click == 2) {
t[ct].points[2 * 2 + 0] = mx;
t[ct].points[2 * 2 + 1] = my;
} else if(click == 3) {
triangle_t* old = t;
int i;
click = 0;
ct++;
t = malloc(sizeof(*t) * (ct + 1));
for(i = 0; i < ct; i++) t[i] = old[i];
free(old);
}
} else if(mouse->button == MwLLMouseRight) {
if(click > 0) {
click = 0;
} else if(ct > 0) {
ct--;
}
}
}
static void mouse_move(MwWidget handle, void* user, void* call) {
MwPoint* point = call;
mx = point->x;
my = point->y;
if(click == 2) {
t[ct].points[2 * 2 + 0] = point->x;
t[ct].points[2 * 2 + 1] = point->y;
}
}
int main() {
MwSizeHints hints;
MwWidget window = MwVaCreateWidget(MwWindowClass, "main", NULL, MwDEFAULT, MwDEFAULT, 640, 480,
MwNtitle, "tripaint",
NULL);
MwWidget viewport = MwCreateWidget(MwViewportClass, "viewport", window, 5, 5, 630, 470 - 16 - 5);
MwVaCreateWidget(MwLabelClass, "label", window, 5, 470 - 16 + 5, 630, 16,
MwNtext, "Press left click to draw triangle, right click to undo",
NULL);
MwViewportSetSize(viewport, 1024, 768);
t = malloc(sizeof(*t));
opengl = MwCreateWidget(MwOpenGLClass, "opengl", MwViewportGetViewport(viewport), 0, 0, 1024, 768);
hints.min_width = hints.max_width = 640;
hints.min_height = hints.max_height = 480;
MwVaApply(window, MwNsizeHints, &hints, NULL);
MwAddUserHandler(window, MwNtickHandler, tick, NULL);
MwAddUserHandler(opengl, MwNmouseDownHandler, mouse, NULL);
MwAddUserHandler(opengl, MwNmouseMoveHandler, mouse_move, NULL);
MwLoop(window);
}