简体   繁体   English

国际象棋棋盘

[英]Chess board using Recti

i was making a program to draw a chess board the code is 我正在编写一个程序来画一个棋盘,代码是

#include "glut.h"

void myDisplay(void);
void myInit(void);


int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(100, 150);
    glutCreateWindow("hello");
    glutDisplayFunc(myDisplay);
    myInit();
    glutMainLoop();

    return 0;
}

void myInit(void)
{
    glClearColor(1, 1, 1, 0);
    glColor3f(0, 0, 0);
    glPointSize(10);
    glutInitDisplayMode(GLUT_SINGLE | GL_RGB);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(640, 0, 480, 0);
}


void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    int counter = 0;
    for (GLint x = 0; x < 120; x += 15)
    {
        for (GLint y = 0; y < 120; y += 15)
        {
                counter++;
            if (counter & 1 ){ 
                glColor3f(.6, .6, .6);
            }
            else{
                glColor3f(.2, .2, .2);
            }
            glRecti(x, y, (x + 15), (y + 15));
        }//end of y     
    }//end of x 
    glFlush();
}

and the output was 和输出是

在此处输入图片说明

now if i just add the equal (=) sign to the condition in my (loops) in the myDisplay function it works fine but it also add one more row and one more colu. 现在,如果我只是在myDisplay函数的(循环)中的条件上添加等号 (=),它可以正常工作,但它还会增加一行和另一行。 so it's a new chess bored 9X9 所以这是一种新的无聊的国际象棋9X9

note: i already debug the program and the funny thing it works fine on the debug mode since it did switch between the conditions ... 注意:我已经调试了程序,有趣的是它在调试模式下可以正常工作,因为它确实在条件之间进行了切换...

It "works" when you use <= of course, since then, you are drawing an odd number of rectangles per column. 当然,当您使用<=时,它将“起作用”,因为从那时起,每列将绘制奇数个矩形。 But if you draw the correct number of 8, you get an even number of invocations of your inner loop,hence counter will always be even when you start a new column. 但是,如果您绘制正确的数字8,则您对内部循环的调用次数将是偶数,因此即使您开始新的列,计数器也将始终为偶数。

That is due to the fact that counter & 1 is just not the right condition to make a checkerboard pattern. 这是由于counter & 1并不是制作棋盘格图案的正确条件。 The simplest approach in your case would be to drop counter alltogether and just use something like ((x+y)/15) & 1 to check which type of field you are at. 在您的情况下,最简单的方法是将所有counter放在一起,并仅使用((x+y)/15) & 1来检查您所处的字段类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM