开发者

C# Form doesn't load until OpenGL animation has stopped working

开发者 https://www.devze.com 2023-03-11 01:40 出处:网络
I\'m currently working on a project which allows me to compare some game-dedicated artificial intelligences (which are C# classes) by making them play ones 开发者_如何学Pythonagainst the others. The p

I'm currently working on a project which allows me to compare some game-dedicated artificial intelligences (which are C# classes) by making them play ones 开发者_如何学Pythonagainst the others. The project includes an OpenGL animation to see the matchs between these classes.

So basically I have a Form containing the OpenGL animation, and another Form containing options and info about the comparison. But it seems that my animation Form is making the rest of the project freeze during the calculation time.

Here is the code when I start a match:

        FormGL formGL = new FormGL();
        formGL.Show();


        // Variables declaration
        //
        // Variable declaration

        while (verifVictoire(plateau, _playerTurn) == false && nombreTours < 64)
        {
            if (_playerTurn == 1) _playerTurn = 2;
            else _playerTurn = 1;
            try
            {
                // Calculation part
                if (_playerTurn == 1) resultat = _AIPlayer1.Joue(plateau, _playerTurn);
                else resultat = _AIPlayer2.Joue(plateau, _playerTurn);
                pion = resultat[2] * 16 + resultat[1] * 4 + resultat[0] + 1;
                plateau[resultat[0], resultat[1], resultat[2]] = _playerTurn;
                nombreTours++;

                formGL.affichPion(_playerTurn, pion); // Send the move to display in the animation
                Thread.Sleep(100); // Pause before next move
            }
            catch (IndexOutOfRangeException)
            {
                // Exception management
            }
        }

When I use that code, both of the Forms open, but none of them finish loading before the animation has stopped working. So I can only see the two forms (empty), the animation updates correctly, but any other content (Labels, menus, buttons, ...) are invisibles until the animation stops working. I know that a Thread.Sleep is affecting the current thread and could be the cause, but even if I remove it, the problem still occurs.

The FormGL contains these methods (among others, but I don't think they could be responsible of this)

public FormGL()
    {
        this.CenterToScreen();
        InitializeComponent();
    }

private void FormGL_Load(object sender, EventArgs e)
    {
        GLPanel.InitializeContexts();
        Gl.glShadeModel(Gl.GL_SMOOTH);
        Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        Gl.glClearDepth(1.0f);
        Gl.glEnable(Gl.GL_DEPTH_TEST);
        Gl.glDepthFunc(Gl.GL_LEQUAL);
        GLWindow_Resize(this, null);
        Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
    }

public void affichPion(int Player, int Pion)
    {
        if (Pion != 0)
        {
            joueur[Pion - 1] = Player;
            positionJeton[Pion - 1] = Pion;
            GLWindow_Resize(this, null);
            GLPanel.Refresh();
        }
    }

public void GLWindow_Resize(object sender, EventArgs e)
    {
        float aspect = (float)GLPanel.Width / (float)GLPanel.Height; 
        Gl.glMatrixMode(Gl.GL_PROJECTION);
        Gl.glLoadIdentity();
        Gl.glViewport(0, 0, GLPanel.Width, GLPanel.Height);
        Glu.gluPerspective(60, aspect, 1.0, 300);
        Gl.glMatrixMode(Gl.GL_MODELVIEW);
        Gl.glLoadIdentity();

        Glu.gluLookAt(x, y, z, centerX, centerY, centerZ, upX, upY, upZ);
        Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
    }

private void FormGL_Resize(object sender, EventArgs e)
    {
        GLWindow_Resize(this, null);
        GLPanel.Refresh();
    }


You are not giving the form any time to draw itself. Try putting Application.DoEvents() right before sleep.

However, I would recommend running the while loop on a different thread to avoid this problem in the first place.

0

精彩评论

暂无评论...
验证码 换一张
取 消