better layout

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@343 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
NishiOwO
2025-10-15 13:58:56 +00:00
parent d3a951abd5
commit ba9b6750d5
19 changed files with 10 additions and 8 deletions

159
examples/gldemos/boing.c Normal file
View File

@@ -0,0 +1,159 @@
/* $Id$ */
#define TITLE "boing"
#include "glutlayer.c"
/*
* Bouncing ball demo.
*
* This program is in the public domain
*
* Brian Paul
*/
#include <math.h>
#include <stdlib.h>
#define COS(X) cos((X) * 3.14159 / 180.0)
#define SIN(X) sin((X) * 3.14159 / 180.0)
#define RED 1.0, 0.0, 0.0
#define WHITE 1.0, 1.0, 1.0
#define CYAN 0.0, 1.0, 1.0
GLuint Ball;
GLenum Mode;
GLfloat Zrot = 0.0, Zstep = 6.0;
GLfloat Xpos = 0.0, Ypos = 1.0;
GLfloat Xvel = 0.2, Yvel = 0.0;
GLfloat Xmin = -4.0, Xmax = 4.0;
GLfloat Ymin = -3.8, Ymax = 4.0;
GLfloat G = -0.1;
static GLuint make_ball(void) {
GLuint list;
GLfloat a, b;
GLfloat da = 18.0, db = 18.0;
GLfloat radius = 1.0;
GLuint color;
GLfloat x, y, z;
list = glGenLists(1);
glNewList(list, GL_COMPILE);
color = 0;
for(a = -90.0; a + da <= 90.0; a += da) {
glBegin(GL_QUAD_STRIP);
for(b = 0.0; b <= 360.0; b += db) {
if(color) {
glColor3f(RED);
} else {
glColor3f(WHITE);
}
x = COS(b) * COS(a);
y = SIN(b) * COS(a);
z = SIN(a);
glVertex3f(x, y, z);
x = radius * COS(b) * COS(a + da);
y = radius * SIN(b) * COS(a + da);
z = radius * SIN(a + da);
glVertex3f(x, y, z);
color = 1 - color;
}
glEnd();
}
glEndList();
return list;
}
static void reshape(int width, int height) {
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-6.0, 6.0, -6.0, 6.0, -6.0, 6.0);
glMatrixMode(GL_MODELVIEW);
}
static void draw(void) {
GLint i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(CYAN);
glBegin(GL_LINES);
for(i = -5; i <= 5; i++) {
glVertex2i(i, -5);
glVertex2i(i, 5);
}
for(i = -5; i <= 5; i++) {
glVertex2i(-5, i);
glVertex2i(5, i);
}
for(i = -5; i <= 5; i++) {
glVertex2i(i, -5);
glVertex2f(i * 1.15, -5.9);
}
glVertex2f(-5.3, -5.35);
glVertex2f(5.3, -5.35);
glVertex2f(-5.75, -5.9);
glVertex2f(5.75, -5.9);
glEnd();
glPushMatrix();
glTranslatef(Xpos, Ypos, 0.0);
glScalef(2.0, 2.0, 2.0);
glRotatef(8.0, 0.0, 0.0, 1.0);
glRotatef(90.0, 1.0, 0.0, 0.0);
glRotatef(Zrot, 0.0, 0.0, 1.0);
glCallList(Ball);
glPopMatrix();
glFlush();
}
static void idle(void) {
static float vel0 = -100.0;
Zrot += Zstep;
Xpos += Xvel;
if(Xpos >= Xmax) {
Xpos = Xmax;
Xvel = -Xvel;
Zstep = -Zstep;
}
if(Xpos <= Xmin) {
Xpos = Xmin;
Xvel = -Xvel;
Zstep = -Zstep;
}
Ypos += Yvel;
Yvel += G;
if(Ypos < Ymin) {
Ypos = Ymin;
if(vel0 == -100.0)
vel0 = fabs(Yvel);
Yvel = vel0;
}
}
static void init(void) {
Ball = make_ball();
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glDisable(GL_DITHER);
glShadeModel(GL_FLAT);
}
static void key(int k) {
(void)k;
}

160
examples/gldemos/clock.c Normal file
View File

@@ -0,0 +1,160 @@
/* $Id$ */
#include <Mw/Milsko.h>
#include <Mw/Widget/OpenGL.h>
#include <time.h>
MwWidget window, opengl, ldate, ltime;
time_t last = 0;
struct tm* tm;
int br, bg, bb;
int fr, fg, fb;
double deg2rad(double deg) {
return (360.0 - deg) / 180.0 * M_PI;
}
void tick(MwWidget handle, void* user, void* call) {
time_t t = time(NULL);
int i;
double rad;
int render = 0;
int w = MwGetInteger(opengl, MwNwidth);
int h = MwGetInteger(opengl, MwNheight);
if(last != t) {
char* wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char* mon[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char buf[512];
tm = localtime(&t);
sprintf(buf, "%s %02d %s", mon[tm->tm_mon], tm->tm_mday, wday[tm->tm_wday]);
MwSetText(ldate, MwNtext, buf);
sprintf(buf, "%02d:%02d %s", tm->tm_hour % 12, tm->tm_min, tm->tm_hour >= 12 ? "PM" : "AM");
MwSetText(ltime, MwNtext, buf);
render = 1;
last = t;
}
MwOpenGLMakeCurrent(opengl);
glClearColor(br / 255.0, bg / 255.0, bb / 255.0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, 0, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLineWidth(2);
for(i = 0; i < 12; i++) {
rad = deg2rad(30.0 * i);
glPointSize((i % 3) ? 2 : 5);
glBegin(GL_POINTS);
glColor3f(fr / 255.0, fg / 255.0, fb / 255.0);
glVertex2f(cos(rad) * 0.85, sin(rad) * 0.85);
glEnd();
}
rad = deg2rad((tm->tm_hour % 12) * 30 + (tm->tm_min / 2) - 90);
glBegin(GL_LINES);
glVertex2f(0, 0);
glVertex2f(cos(rad) * 0.3, sin(rad) * 0.3);
glEnd();
rad = deg2rad(tm->tm_min * 6 - 90);
glBegin(GL_LINES);
glVertex2f(0, 0);
glVertex2f(cos(rad) * 0.5, sin(rad) * 0.5);
glEnd();
if(render && w > 0 && h > 0) {
unsigned char* buffer = malloc(w * h * 4);
MwLLPixmap px;
int j;
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
for(i = 0; i < h / 2; i++) {
for(j = 0; j < w * 4; j++) {
unsigned char b = buffer[i * w * 4 + j];
buffer[i * w * 4 + j] = buffer[(h - i - 1) * w * 4 + j];
buffer[(h - i - 1) * w * 4 + j] = b;
}
}
px = MwLoadRaw(window, buffer, w, h);
MwVaApply(window,
MwNiconPixmap, px,
NULL);
MwLLDestroyPixmap(px);
free(buffer);
}
MwOpenGLSwapBuffer(opengl);
}
void resize(MwWidget handle, void* user, void* call) {
int w = MwGetInteger(handle, MwNwidth);
int h = MwGetInteger(handle, MwNheight);
MwVaApply(opengl,
MwNwidth, w / 5 * 2,
MwNheight, h,
NULL);
MwVaApply(ldate,
MwNx, w / 5 * 2,
MwNwidth, w - w / 5 * 2,
MwNheight, h / 2,
NULL);
MwVaApply(ltime,
MwNx, w / 5 * 2,
MwNy, h / 2,
MwNwidth, w - w / 5 * 2,
MwNheight, h / 2,
NULL);
glViewport(0, 0, w / 5 * 2, h);
}
int main() {
MwLLColor bgcolor, fgcolor;
window = MwVaCreateWidget(MwWindowClass, "main", NULL, 0, 0, 250, 100,
MwNtitle, "clock",
NULL);
bgcolor = MwParseColor(window, MwGetText(window, MwNbackground));
MwGetColor(bgcolor, &br, &bg, &bb);
fgcolor = MwParseColor(window, MwGetText(window, MwNforeground));
MwGetColor(fgcolor, &fr, &fg, &fb);
opengl = MwCreateWidget(MwOpenGLClass, "clock", window, 0, 0, 100, 100);
ldate = MwVaCreateWidget(MwLabelClass, "date", window, 100, 0, 150, 50,
MwNbold, 1,
NULL);
ltime = MwVaCreateWidget(MwLabelClass, "time", window, 100, 50, 150, 50,
MwNbold, 1,
NULL);
MwAddUserHandler(window, MwNtickHandler, tick, NULL);
MwAddUserHandler(window, MwNresizeHandler, resize, NULL);
resize(window, NULL, NULL);
tick(window, NULL, NULL);
MwLoop(window);
MwLLFreeColor(fgcolor);
MwLLFreeColor(bgcolor);
}

79
examples/gldemos/cube.c Normal file
View File

@@ -0,0 +1,79 @@
/* $Id$ */
#define TITLE "cube"
#include "glutlayer.c"
#include <GL/glu.h>
static double deg = 0;
static void draw(void) {
int i;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(deg, 1, 0, 0);
glRotatef(deg, 0, 1, 0);
glRotatef(deg, 0, 0, 1);
for(i = 0; i < 6; i++) {
if(i == 0) glColor3f(1, 0, 0);
if(i == 1) glColor3f(0, 1, 0);
if(i == 2) glColor3f(1, 1, 0);
if(i == 3) glColor3f(0, 0, 1);
if(i == 4) glColor3f(1, 0, 1);
if(i == 5) glColor3f(0, 1, 1);
glBegin(GL_QUADS);
glNormal3f(0, 1, 0);
glVertex3f(-1, 1, -1);
glVertex3f(-1, 1, 1);
glVertex3f(1, 1, 1);
glVertex3f(1, 1, -1);
glEnd();
if(i < 3) glRotatef(90, 0, 0, 1);
if(i == 3) glRotatef(90, 1, 0, 0);
if(i == 4) glRotatef(180, 0, 0, 1);
}
glPopMatrix();
}
static void idle(void) {
deg += 1.0;
}
static void reshape(int width, int height) {
GLfloat lpos[4];
lpos[0] = 1;
lpos[1] = 1;
lpos[2] = 1;
lpos[3] = 0;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (double)width / height, 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2, 2, 2, 0, 0, 0, 0, 1, 0);
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
}
static void init(void) {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_CULL_FACE);
glEnable(GL_NORMALIZE);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
}
static void key(int k) {
(void)k;
}

244
examples/gldemos/gears.c Normal file
View File

@@ -0,0 +1,244 @@
/* $Id$ */
#define TITLE "gears"
#include "glutlayer.c"
/*
* 3-D gear wheels. This program is in the public domain.
*
* Brian Paul
*/
/* Conversion to GLUT by Mark J. Kilgard */
#include <math.h>
#include <stdlib.h>
#ifndef M_PI
#define M_PI 3.14159265
#endif
/**
Draw a gear wheel. You'll probably want to call this function when
building a display list since we do a lot of trig here.
Input: inner_radius - radius of hole at center
outer_radius - radius at center of teeth
width - width of gear
teeth - number of teeth
tooth_depth - depth of tooth
**/
static void gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, GLint teeth, GLfloat tooth_depth) {
GLint i;
GLfloat r0, r1, r2;
GLfloat angle, da;
GLfloat u, v, len;
r0 = inner_radius;
r1 = outer_radius - tooth_depth / 2.0;
r2 = outer_radius + tooth_depth / 2.0;
da = 2.0 * M_PI / teeth / 4.0;
glShadeModel(GL_FLAT);
glNormal3f(0.0, 0.0, 1.0);
/* draw front face */
glBegin(GL_QUAD_STRIP);
for(i = 0; i <= teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
}
glEnd();
/* draw front sides of teeth */
glBegin(GL_QUADS);
da = 2.0 * M_PI / teeth / 4.0;
for(i = 0; i < teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
}
glEnd();
glNormal3f(0.0, 0.0, -1.0);
/* draw back face */
glBegin(GL_QUAD_STRIP);
for(i = 0; i <= teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
}
glEnd();
/* draw back sides of teeth */
glBegin(GL_QUADS);
da = 2.0 * M_PI / teeth / 4.0;
for(i = 0; i < teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
}
glEnd();
/* draw outward faces of teeth */
glBegin(GL_QUAD_STRIP);
for(i = 0; i < teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5);
glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5);
u = r2 * cos(angle + da) - r1 * cos(angle);
v = r2 * sin(angle + da) - r1 * sin(angle);
len = sqrt(u * u + v * v);
u /= len;
v /= len;
glNormal3f(v, -u, 0.0);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5);
glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5);
glNormal3f(cos(angle), sin(angle), 0.0);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), width * 0.5);
glVertex3f(r2 * cos(angle + 2 * da), r2 * sin(angle + 2 * da), -width * 0.5);
u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da);
v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da);
glNormal3f(v, -u, 0.0);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), width * 0.5);
glVertex3f(r1 * cos(angle + 3 * da), r1 * sin(angle + 3 * da), -width * 0.5);
glNormal3f(cos(angle), sin(angle), 0.0);
}
glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5);
glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5);
glEnd();
glShadeModel(GL_SMOOTH);
/* draw inside radius cylinder */
glBegin(GL_QUAD_STRIP);
for(i = 0; i <= teeth; i++) {
angle = i * 2.0 * M_PI / teeth;
glNormal3f(-cos(angle), -sin(angle), 0.0);
glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5);
glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5);
}
glEnd();
}
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
static GLint gear1, gear2, gear3;
static GLfloat angle = 0.0;
static void draw(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(view_rotx, 1.0, 0.0, 0.0);
glRotatef(view_roty, 0.0, 1.0, 0.0);
glRotatef(view_rotz, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(-3.0, -2.0, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glCallList(gear1);
glPopMatrix();
glPushMatrix();
glTranslatef(3.1, -2.0, 0.0);
glRotatef(-2.0 * angle - 9.0, 0.0, 0.0, 1.0);
glCallList(gear2);
glPopMatrix();
glPushMatrix();
glTranslatef(-3.1, 4.2, 0.0);
glRotatef(-2.0 * angle - 25.0, 0.0, 0.0, 1.0);
glCallList(gear3);
glPopMatrix();
glPopMatrix();
}
static void idle(void) {
angle += 2.0;
}
/* new window size or exposure */
static void reshape(int width, int height) {
GLfloat h = (GLfloat)height / (GLfloat)width;
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -40.0);
}
static void init(void) {
static GLfloat pos[4] =
{5.0, 5.0, 10.0, 0.0};
static GLfloat red[4] =
{0.8, 0.1, 0.0, 1.0};
static GLfloat green[4] =
{0.0, 0.8, 0.2, 1.0};
static GLfloat blue[4] =
{0.2, 0.2, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
/* make the gears */
gear1 = glGenLists(1);
glNewList(gear1, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
gear(1.0, 4.0, 1.0, 20, 0.7);
glEndList();
gear2 = glGenLists(1);
glNewList(gear2, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
gear(0.5, 2.0, 2.0, 10, 0.7);
glEndList();
gear3 = glGenLists(1);
glNewList(gear3, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
gear(1.3, 2.0, 0.5, 10, 0.7);
glEndList();
glEnable(GL_NORMALIZE);
}
static void key(int k) {
(void)k;
if(k == MwLLKeyLeft) {
view_roty += 5.0;
} else if(k == MwLLKeyRight) {
view_roty -= 5.0;
} else if(k == MwLLKeyUp) {
view_rotx += 5.0;
} else if(k == MwLLKeyDown) {
view_rotx -= 5.0;
}
}

View File

@@ -0,0 +1,64 @@
#include <Mw/Milsko.h>
#include <Mw/Widget/OpenGL.h>
MwWidget opengl;
static void draw(void);
static void idle(void);
static void reshape(int width, int height);
static void init(void);
static void key(int k);
static void tick(MwWidget handle, void* user, void* client) {
(void)handle;
(void)user;
(void)client;
draw();
idle();
MwOpenGLSwapBuffer(opengl);
}
static void resize(MwWidget handle, void* user, void* client) {
int ww, wh;
(void)handle;
(void)user;
(void)client;
ww = MwGetInteger(handle, MwNwidth) - 100;
wh = MwGetInteger(handle, MwNheight) - 100;
MwVaApply(opengl,
MwNwidth, ww,
MwNheight, wh,
NULL);
reshape(ww, wh);
}
static void key_pressed(MwWidget handle, void* user, void* client) {
key(*(int*)client);
}
int main() {
MwWidget window;
window = MwVaCreateWidget(MwWindowClass, "main", NULL, MwDEFAULT, MwDEFAULT, 500, 500,
MwNtitle, TITLE,
NULL);
opengl = MwCreateWidget(MwOpenGLClass, "opengl", window, 50, 50, 400, 400);
MwOpenGLMakeCurrent(opengl);
init();
reshape(400, 400);
MwAddUserHandler(window, MwNresizeHandler, resize, NULL);
MwAddUserHandler(window, MwNtickHandler, tick, NULL);
MwAddUserHandler(opengl, MwNkeyHandler, key_pressed, NULL);
MwLoop(window);
}

View File

@@ -0,0 +1,87 @@
/* $Id$ */
#include <Mw/Milsko.h>
#include <Mw/Widget/OpenGL.h>
MwWidget window, opengl, button;
int ow, oh;
double deg = 0;
double dir = 1;
void resize(MwWidget handle, void* user_data, void* call_data) {
unsigned int w, h;
(void)user_data;
(void)call_data;
w = MwGetInteger(handle, MwNwidth);
h = MwGetInteger(handle, MwNheight);
MwVaApply(opengl,
MwNwidth, (ow = w - 100),
MwNheight, (oh = h - 150),
NULL);
MwVaApply(button,
MwNy, h - 50 - 50,
MwNwidth, ow,
MwNheight, 50,
NULL);
}
void tick(MwWidget handle, void* user_data, void* call_data) {
(void)handle;
(void)user_data;
(void)call_data;
MwOpenGLMakeCurrent(opengl);
glViewport(0, 0, ow, oh);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(deg, 0, 0, 1);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2f(0, 0.8);
glColor3f(0, 1, 0);
glVertex2f(-0.8, -0.8);
glColor3f(0, 0, 1);
glVertex2f(0.8, -0.8);
glEnd();
glPopMatrix();
MwOpenGLSwapBuffer(opengl);
deg += 120.0 / (1000.0 / MwWaitMS) * dir;
}
void activate(MwWidget handle, void* user_data, void* call_data) {
(void)handle;
(void)user_data;
(void)call_data;
dir = dir == 1 ? -1 : 1;
}
int main() {
window = MwVaCreateWidget(MwWindowClass, "main", NULL, 0, 0, 400, 450,
MwNtitle, "hello world",
NULL);
opengl = MwCreateWidget(MwOpenGLClass, "opengl", window, 50, 50, (ow = 300), (oh = 300));
button = MwVaCreateWidget(MwButtonClass, "button", window, 50, 350, 300, 50,
MwNtext, "Reverse",
NULL);
MwAddUserHandler(window, MwNresizeHandler, resize, NULL);
MwAddUserHandler(window, MwNtickHandler, tick, NULL);
MwAddUserHandler(button, MwNactivateHandler, activate, NULL);
MwLoop(window);
}

123
examples/gldemos/tripaint.c Normal file
View File

@@ -0,0 +1,123 @@
/* $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);
hints.min_width = hints.max_width = 640;
hints.min_height = hints.max_height = 480;
MwVaApply(window, MwNsizeHints, &hints, NULL);
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);
MwAddUserHandler(window, MwNtickHandler, tick, NULL);
MwAddUserHandler(opengl, MwNmouseDownHandler, mouse, NULL);
MwAddUserHandler(opengl, MwNmouseMoveHandler, mouse_move, NULL);
MwLoop(window);
}