#include <stdlib.h>
#include <glut.h>
#define WIDTH 400 /* должна быть кратна 4 */
#define HEIGHT 400
unsigned char image[HEIGHT][WIDTH][3];
double Scale = 1;
int WinWidth, WinHeight;
void Put( int X, int Y, int R, int G, int B )
{
Y = (WinHeight - 1 - Y) / Scale;
X /= Scale;
if (X >= 0 && Y >= 0 && X < WIDTH && Y < HEIGHT)
{
image[Y][X][0] = B;
image[Y][X][1] = G;
image[Y][X][2] = R;
}
}
void Resize( int W, int H )
{
WinWidth = W;
WinHeight = H;
}
void Draw( void )
{
glClearColor(0.3, 0.5, 0.7, 0);
glClear(GL_COLOR_BUFFER_BIT);
glPixelZoom(Scale, Scale);
glDrawPixels(WIDTH, HEIGHT, GL_BGR_EXT, GL_UNSIGNED_BYTE, image);
glFinish();
glutSwapBuffers();
glutPostRedisplay();
}
void Kbd( unsigned char Key, int X, int Y )
{
if (Key == 27)
exit(0);
else if (Key == 'a')
Put(X, Y, 255, 0, 0);
else if (Key == '+')
Scale += 0.1;
else if (Key == '-')
Scale -= 0.1;
}
void MouseMove( int X, int Y )
{
Put(X, Y, 255, 255, 255);
}
void MouseButton( int Button, int State, int X, int Y )
{
int i, j;
if (Button == GLUT_LEFT_BUTTON && State == GLUT_DOWN)
Put(X, Y, 55, 155, 255);
else if (Button == GLUT_RIGHT_BUTTON && State == GLUT_DOWN)
for (i = -8; i <= 8; i++)
for (j = -8; j <= 8; j++)
Put(X + j, Y + i, 255, 255, 255);
}
void main( int argc, char *argv[] )
{
int x, y, c;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(300, 300);
glutCreateWindow("11-6 forever...");
glutDisplayFunc(Draw);
glutKeyboardFunc(Kbd);
glutReshapeFunc(Resize);
glutMotionFunc(MouseMove);
glutMouseFunc(MouseButton);
for (y = 0; y < HEIGHT; y++)
for (x = 0; x < WIDTH; x++)
for (c = 0; c < 3; c++)
image[y][x][c] = 0;
glutMainLoop();
}