#include "basis.h"

static void Init(void );
static void CALLBACK Reshape(int width,int height);
static void CALLBACK Draw(void );

GLint windW, windH;

static void Init(void)
{
  glClearColor(0.75f, 0.75f, 0.75f, 1.0f);
}

static void CALLBACK Reshape(int width, int height)
{
  windW = (GLint)width;
  windH = (GLint)height;
}

static void CALLBACK DrawAxies(int left)
{
  GLint x;
  GLushort pattern;
  GLfloat width;

  glColor3f(0.0f, 0.0f, 0.0f);
  if(!left){
    pattern = 0x1C47;
    x = windW/4;
  }
  else{
    pattern = 0x471C;
    x = -windW/4;
  }
  glPushMatrix();
    glGetFloatv(GL_LINE_WIDTH, &width);
    glLineWidth(2.0f);
    glLineStipple(2, pattern);
    glEnable(GL_LINE_STIPPLE);
    glBegin(GL_LINES);
      glVertex2i(x, -windH/2);
      glVertex2i(x,  windH/2);
    glEnd();
    glDisable(GL_LINE_STIPPLE);
    glLineWidth(width);

    glBegin(GL_LINES);
      glVertex2i(-windW/4 + 10, 0);
      glVertex2i( windW/4 - 10, 0);
      glVertex2i(0, -windH/2 + 10);
      glVertex2i(0,  windH/2 - 10);
    glEnd();
  glPopMatrix();
}

static void CALLBACK InitViewport(int x, int y, int width, int height)
{
  glViewport(x, y, width, height);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-windW/4, windW/4, -windH/2, windH/2);
  glMatrixMode(GL_MODELVIEW);
}

static void CALLBACK DrawScene(GLenum mode)
{
  // Здесь вызываются команды рисования
  // ...

  glBegin(mode);
    glVertex2f(-windW/4 + windW/20, -windH/2 + windH/10);
    glVertex2f( windW/4 - windW/20, -windH/2 + windH/10);
    glVertex2f( windW/4 - windW/20,  windH/2 - windH/10);
  glEnd();
}

static void CALLBACK DrawLine(void)
{
  glLineWidth(20.0f);
  glBegin(GL_LINES);
    glVertex2f(-windW/4 + windW/20, -windH/2 + windH/5);
    glVertex2f( 0, windH/2 - windH/10);
    glVertex2f(-windW/4 + windW/20 + 30, -windH/2 + windH/5);
    glVertex2f( 30, windH/2 - windH/10);
  glEnd();
  glLineWidth(1.0f);
}

static void CALLBACK Draw(void)
{
  glClear(GL_COLOR_BUFFER_BIT);

  InitViewport(0,0, windW/2, windH);
  DrawAxies(0);

  glColor3f(0.0, 0.0, 1.0);
  DrawScene(GL_LINE_STRIP);
  glColor3f(1.0, 0.0, 1.0);
  DrawLine();

  InitViewport(windW/2, 0, windW/2, windH);
  DrawAxies(1);
  
  glColor3f(1.0, 0.0, 0.0);
  DrawScene(GL_LINE_LOOP);

  glEnable(GL_LINE_SMOOTH);
  glColor3f(1.0, 0.0, 1.0);
  DrawLine();
  glDisable(GL_LINE_SMOOTH);

  glFinish();
  auxSwapBuffers();
}

void main(int argc, char **argv)
{
    windW = 400;
    windH = 300;
    auxInitPosition(200, 100, windW, windH);

    auxInitDisplayMode(AUX_RGB | AUX_DOUBLE);

    if (auxInitWindow("Линии, линии, линии ...") == GL_FALSE) {
      auxQuit();
    }

    Init();

    auxExposeFunc((AUXEXPOSEPROC)Reshape);
    auxReshapeFunc((AUXRESHAPEPROC)Reshape);
    auxMainLoop(Draw);
}
