MaximCherchuk

checkers

Feb 28th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <GL/gl.h>
  3. #include <cmath>
  4.  
  5. class checker {
  6. public:
  7.     enum color {white, black};
  8.    
  9.     checker(color checker_color, const GLint x, const GLint y, const GLint radius, const GLint segments = 30)
  10.         : checker_color(checker_color), x(x), y(y), radius(radius), segments(segments) {}
  11.    
  12.     inline bool is_white() {
  13.         return checker_color == white;
  14.     }
  15.    
  16.     inline bool is_black() {
  17.         return checker_color == black;
  18.     }
  19.    
  20.     void render() {
  21.         if(is_black()) {
  22.             glColor3f(0.0, 0.0, 0.0);
  23.         } else {
  24.             glColor3f(1.0, 1.0, 1.0);    
  25.         }
  26.         glBegin(GL_TRIANGLE_FAN);
  27.             glVertex2f(x, y);
  28.             for(int n = 0; n <= segments; ++n) {
  29.                 float const t = 2 * M_PI * float(n) / float(segments);
  30.                 glVertex2f(x + sin(t) * radius, y + cos(t) * radius);
  31.             }
  32.         glEnd();
  33.        
  34.         if(is_black()) {    
  35.             glColor3f(1.0, 1.0, 1.0);    
  36.         } else {
  37.             glColor3f(0.0, 0.0, 0.0);
  38.         }
  39.         glBegin(GL_LINE_LOOP);
  40.             for(int n = 0; n <= segments; ++n) {
  41.                 float const t = 2 * M_PI * float(n) / float(segments);
  42.                 glVertex2f(x + sin(t) * radius, y + cos(t) * radius);
  43.             }
  44.         glEnd();
  45.         glFlush();
  46.     }
  47.  
  48. private:
  49.     const color checker_color;
  50.     const GLint x;
  51.     const GLint y;
  52.     const GLint radius;
  53.     const GLint segments;
  54. };
  55.  
  56. class cell {
  57. public:
  58.     cell(const checker _checker, const GLint x, const GLint y)
  59.         : _checker(_checker), x(x), y(y) {}
  60.     ~cell() {}
  61.    
  62.     GLint get_x() {
  63.         return x;
  64.     }
  65.    
  66.     GLint get_y() {
  67.         return y;
  68.     }
  69.    
  70. private:
  71.     const checker _checker;
  72.     const GLint x;
  73.     const GLint y;
  74. };
  75.  
  76. class chessboard {
  77. public:
  78.    
  79.     chessboard(GLint left_corner_x=50, GLint left_corner_y=50, GLint polygon_size=50)
  80.     : left_corner_x(left_corner_x), left_corner_y(left_corner_y), polygon_size(polygon_size)
  81.     {
  82.        
  83.     }
  84.    
  85.     ~chessboard(){}
  86.  
  87.     void redner() {
  88.         glColor3f(0.0, 0.0, 0.0);
  89.    
  90.         for(int i = 0; i < 8; ++i) {
  91.             for(int j = 0; j < 8; ++j) {
  92.                 const GLint pos_x = left_corner_x + i * 50;
  93.                 const GLint pos_y = left_corner_y + j * 50;
  94.                 if((i + j) % 2 == 0) {
  95.                     black_box(pos_x, pos_y);
  96.                 } else {
  97.                     white_box(pos_x, pos_y);
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103. private:
  104.     void white_box(int x, int y) {
  105.         glBegin(GL_LINE_LOOP);
  106.             glVertex2i(x, y);
  107.             glVertex2i(x, y + polygon_size);
  108.             glVertex2i(x + polygon_size, y + polygon_size);
  109.             glVertex2i(x + polygon_size, y);
  110.         glEnd();
  111.     }
  112.  
  113.     void black_box(int x, int y) {
  114.         glBegin(GL_POLYGON);
  115.             glVertex2i(x, y);
  116.             glVertex2i(x, y + polygon_size);
  117.             glVertex2i(x + polygon_size, y + polygon_size);
  118.             glVertex2i(x + polygon_size, y);
  119.         glEnd();
  120.        
  121.         glFlush();
  122.     }
  123.    
  124.     GLint left_corner_x;
  125.     GLint left_corner_y;
  126.    
  127.     const GLint polygon_size;
  128. };
  129.  
  130.  
  131.  
  132. void display_function() {
  133.     glClear(GL_COLOR_BUFFER_BIT);
  134.     glColor3f(0.0, 0.0, 0.0);
  135.    
  136.     const GLint start_x = 100;
  137.     const GLint start_y = 100;
  138. //    chessboard cb(start_x, start_y);
  139. //    cb.redner();
  140.    
  141.     checker(checker::color::black, 100, 100, 50).render();
  142.    
  143.     glFlush();
  144. }
  145.  
  146.  
  147. void init() {
  148.     glClearColor(1.0, 1.0, 1.0, 0.0);
  149.     glColor3f(0.0f, 0.0f, 0.0f);
  150.     glPointSize(1.0);
  151.     glMatrixMode(GL_PROJECTION);
  152.     glLoadIdentity();
  153.     gluOrtho2D(0.0, 600.0, 0.0, 600.0);
  154. }
  155.  
  156. int main(int argc, char** argv) {
  157.     glutInit(&argc, argv);
  158.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  159.     glutInitWindowSize(600, 600);
  160.     glutInitWindowPosition(100, 150);
  161.     glutCreateWindow("Draw an 8X8 chess board using loop");
  162.     glutDisplayFunc(display_function);
  163.     init();
  164.     glutMainLoop();
  165. }
Advertisement
Add Comment
Please, Sign In to add comment