simonsthreebody 2023-07-10 15:54 采纳率: 0%
浏览 18

C++ opengl中getch不阻塞的问题

C++ getch()函数不阻塞的原因,求各位大 佬解决方案。
代码如下:

/**************************
 * Includes
 *
 **************************/

#include <windows.h>
#include <gl/gl.h>
#include <stdio.h> 
#include <conio.h>
#include <iostream>
#include <fstream>

/**************************
 * Function Declarations
 *
 **************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
double xfl=0.0f,yfl=0.0f;
double playerx=0.0f,playery=0.0f;
int keyb=0;

/**************************
 * WinMain
 *
 **************************/

int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;

    /* register window class */
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

    /* create main window */
    hWnd = CreateWindow (
        "GLSample", "OpenGL Sample", 
        WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
        0, 0, 512, 512,
        NULL, NULL, hInstance, NULL);

    /* enable OpenGL for the window */
    EnableOpenGL (hWnd, &hDC, &hRC);

    /* program main loop */
    while (!bQuit){
        /* check for messages */
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)){
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT){
                bQuit = TRUE;
            }
            else{
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else{
            /* OpenGL animation code goes here */

            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            glClear (GL_COLOR_BUFFER_BIT);

            glPushMatrix ();
            glBegin (GL_LINES);
            for(double xl=-248;xl<256;xl+=16){
                xfl=xl/256;
                glColor3f(0.5f,0.5f,0.5f);    glVertex2f(xfl,-0.96875f);
                glColor3f(0.5f,0.5f,0.5f);    glVertex2f(xfl,0.96875f);
            }
            for(double yl=-248;yl<256;yl+=16){
                yfl=yl/256;
                glColor3f(0.5f,0.5f,0.5f);    glVertex2f(-0.96875f,yfl);
                glColor3f(0.5f,0.5f,0.5f);    glVertex2f(0.96875f,yfl);
            }
            glEnd ();
            glBegin (GL_POINTS);
            glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx-0.0234375f,playery-0.0234375f);
            glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx+0.0234375f,playery-0.0234375f);
            glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx-0.0234375f,playery+0.0234375f);
            glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx+0.0234375f,playery+0.0234375f);
            glEnd ();
            glPopMatrix ();

            SwapBuffers (hDC);
            
            
            kbhit();
            keyb=getch();
            
            if(keyb==87||keyb==119){
                playery+=0.0625f;
            }
            if(keyb==65||keyb==97){
                playerx-=0.0625f;
            }
            if(keyb==83||keyb==115){
                playery-=0.0625f;
            }
            if(keyb==68||keyb==100){
                playerx+=0.0625f;
            }
            //playerx+=0.000625f;
            Sleep (1);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL (hWnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow (hWnd);

    return msg.wParam;
}


/********************
 * Window Procedure
 *
 ********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam){
    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}


/*******************
 * Enable OpenGL
 *
 *******************/

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC){
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC (hWnd);

    /* set the pixel format for the DC */
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}


/******************
 * Disable OpenGL
 *
 ******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC){
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}

编译正常通过,但运行时在107行时没有阻塞,似乎也没有返回值。
我是在取消第121行的注释时发现了问题,即:

/**************************
 * Includes
 *
 **************************/

#include <windows.h>
#include <gl/gl.h>
#include <stdio.h> 
#include <conio.h>
#include <iostream>
#include <fstream>

/**************************
 * Function Declarations
 *
 **************************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
double xfl=0.0f,yfl=0.0f;
double playerx=0.0f,playery=0.0f;
int keyb=0;

/**************************
 * WinMain
 *
 **************************/

int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){
    WNDCLASS wc;
    HWND hWnd;
    HDC hDC;
    HGLRC hRC;        
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;

    /* register window class */
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "GLSample";
    RegisterClass (&wc);

    /* create main window */
    hWnd = CreateWindow (
        "GLSample", "OpenGL Sample", 
        WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
        0, 0, 512, 512,
        NULL, NULL, hInstance, NULL);

    /* enable OpenGL for the window */
    EnableOpenGL (hWnd, &hDC, &hRC);

    /* program main loop */
    while (!bQuit){
        /* check for messages */
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)){
            /* handle or dispatch messages */
            if (msg.message == WM_QUIT){
                bQuit = TRUE;
            }
            else{
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }
        else{
            /* OpenGL animation code goes here */

            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            glClear (GL_COLOR_BUFFER_BIT);

            glPushMatrix ();
            glBegin (GL_LINES);
            for(double xl=-248;xl<256;xl+=16){
                xfl=xl/256;
                glColor3f(0.5f,0.5f,0.5f);    glVertex2f(xfl,-0.96875f);
                glColor3f(0.5f,0.5f,0.5f);    glVertex2f(xfl,0.96875f);
            }
            for(double yl=-248;yl<256;yl+=16){
                yfl=yl/256;
                glColor3f(0.5f,0.5f,0.5f);    glVertex2f(-0.96875f,yfl);
                glColor3f(0.5f,0.5f,0.5f);    glVertex2f(0.96875f,yfl);
            }
            glEnd ();
            glBegin (GL_POINTS);
            glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx-0.0234375f,playery-0.0234375f);
            glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx+0.0234375f,playery-0.0234375f);
            glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx-0.0234375f,playery+0.0234375f);
            glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx+0.0234375f,playery+0.0234375f);
            glEnd ();
            glPopMatrix ();

            SwapBuffers (hDC);
            
            
            kbhit();
            keyb=getch();
            
            if(keyb==87||keyb==119){
                playery+=0.0625f;
            }
            if(keyb==65||keyb==97){
                playerx-=0.0625f;
            }
            if(keyb==83||keyb==115){
                playery-=0.0625f;
            }
            if(keyb==68||keyb==100){
                playerx+=0.0625f;
            }
            playerx+=0.000625f;
            Sleep (1);
        }
    }

    /* shutdown OpenGL */
    DisableOpenGL (hWnd, hDC, hRC);

    /* destroy the window explicitly */
    DestroyWindow (hWnd);

    return msg.wParam;
}


/********************
 * Window Procedure
 *
 ********************/

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam){
    switch (message)
    {
    case WM_CREATE:
        return 0;
    case WM_CLOSE:
        PostQuitMessage (0);
        return 0;

    case WM_DESTROY:
        return 0;

    case WM_KEYDOWN:
        switch (wParam)
        {
        case VK_ESCAPE:
            PostQuitMessage(0);
            return 0;
        }
        return 0;

    default:
        return DefWindowProc (hWnd, message, wParam, lParam);
    }
}


/*******************
 * Enable OpenGL
 *
 *******************/

void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC){
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC (hWnd);

    /* set the pixel format for the DC */
    ZeroMemory (&pfd, sizeof (pfd));
    pfd.nSize = sizeof (pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat (*hDC, &pfd);
    SetPixelFormat (*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );

}


/******************
 * Disable OpenGL
 *
 ******************/

void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC){
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
}

在运行这一段取消121行注释的代码时,弹窗里的用四个点表示的玩家位置会不断向右,也就是说getch并没有阻塞。
附加信息:

#include<conio.h>
#include<iostream>
using namespace std;
int keyb=0;
int main(void){
  while(1){
    kbhit();
    keyb=getch();
    cout<<keyb<<endl;
  }
  return 0;
}

在这段代码中getch阻塞并正常返回输入字符的ascii值。
如果不是getch函数的问题,也希望在座及时指出错误,有任何有帮助的回答,感激不尽!
这里的gl.h文件已经下载,如果有表述不清晰之处,请及时来询问。再次感谢。

  • 写回答

1条回答 默认 最新

  • 赵4老师 2023-07-10 16:36
    关注
    
    /**************************
     * Includes
     *
     **************************/
    
    #include <windows.h>
    #include <gl/gl.h>
    #include <stdio.h>
    #include <conio.h>
    #include <iostream>
    #include <fstream>
    
    /**************************
     * Function Declarations
     *
     **************************/
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
    WPARAM wParam, LPARAM lParam);
    void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
    void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
    double xfl=0.0f,yfl=0.0f;
    double playerx=0.0f,playery=0.0f;
    int keyb=0;
    
    /**************************
     * WinMain
     *
     **************************/
    
    int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){
        WNDCLASS wc;
        HWND hWnd;
        HDC hDC;
        HGLRC hRC;
        MSG msg;
        BOOL bQuit = FALSE;
        float theta = 0.0f;
        /* register window class */
        wc.style = CS_OWNDC;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor (NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = "GLSample";
        RegisterClass (&wc);
        /* create main window */
        hWnd = CreateWindow (
            "GLSample", "OpenGL Sample",
            WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
            0, 0, 512, 512,
            NULL, NULL, hInstance, NULL);
        /* enable OpenGL for the window */
        EnableOpenGL (hWnd, &hDC, &hRC);
        /* program main loop */
        while (!bQuit){
            /* check for messages */
            if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)){
                /* handle or dispatch messages */
                if (msg.message == WM_QUIT){
                    bQuit = TRUE;
                }
                else{
                    TranslateMessage (&msg);
                    DispatchMessage (&msg);
                }
            }
            else{
                /* OpenGL animation code goes here */
                glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
                glClear (GL_COLOR_BUFFER_BIT);
                glPushMatrix ();
                glBegin (GL_LINES);
                for(double xl=-248;xl<256;xl+=16){
                    xfl=xl/256;
                    glColor3f(0.5f,0.5f,0.5f);    glVertex2f(xfl,-0.96875f);
                    glColor3f(0.5f,0.5f,0.5f);    glVertex2f(xfl,0.96875f);
                }
                for(double yl=-248;yl<256;yl+=16){
                    yfl=yl/256;
                    glColor3f(0.5f,0.5f,0.5f);    glVertex2f(-0.96875f,yfl);
                    glColor3f(0.5f,0.5f,0.5f);    glVertex2f(0.96875f,yfl);
                }
                glEnd ();
                glBegin (GL_POINTS);
                glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx-0.0234375f,playery-0.0234375f);
                glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx+0.0234375f,playery-0.0234375f);
                glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx-0.0234375f,playery+0.0234375f);
                glColor3f(1.0f,1.0f,1.0f);    glVertex2f(playerx+0.0234375f,playery+0.0234375f);
                glEnd ();
                glPopMatrix ();
                SwapBuffers (hDC);
    
    
                while (kbhit()) {
                    keyb=getch();
                    if (0==keyb || 0xE0==keyb) keyb=keyb<<8|getch();
                }
                keyb=getch();
                if (0==keyb || 0xE0==keyb) keyb=keyb<<8|getch();
    
                if(keyb==87||keyb==119){
                    playery+=0.0625f;
                }
                if(keyb==65||keyb==97){
                    playerx-=0.0625f;
                }
                if(keyb==83||keyb==115){
                    playery-=0.0625f;
                }
                if(keyb==68||keyb==100){
                    playerx+=0.0625f;
                }
                //playerx+=0.000625f;
                Sleep (1);
            }
        }
        /* shutdown OpenGL */
        DisableOpenGL (hWnd, hDC, hRC);
        /* destroy the window explicitly */
        DestroyWindow (hWnd);
        return msg.wParam;
    }
    
    /********************
     * Window Procedure
     *
     ********************/
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam){
        switch (message)
        {
        case WM_CREATE:
            return 0;
        case WM_CLOSE:
            PostQuitMessage (0);
            return 0;
        case WM_DESTROY:
            return 0;
        case WM_KEYDOWN:
            switch (wParam)
            {
            case VK_ESCAPE:
                PostQuitMessage(0);
                return 0;
            }
            return 0;
        default:
            return DefWindowProc (hWnd, message, wParam, lParam);
        }
    }
    
    /*******************
     * Enable OpenGL
     *
     *******************/
    
    void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC){
        PIXELFORMATDESCRIPTOR pfd;
        int iFormat;
    
        /* get the device context (DC) */
        *hDC = GetDC (hWnd);
    
        /* set the pixel format for the DC */
        ZeroMemory (&pfd, sizeof (pfd));
        pfd.nSize = sizeof (pfd);
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW |
          PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 24;
        pfd.cDepthBits = 16;
        pfd.iLayerType = PFD_MAIN_PLANE;
        iFormat = ChoosePixelFormat (*hDC, &pfd);
        SetPixelFormat (*hDC, iFormat, &pfd);
    
        /* create and enable the render context (RC) */
        *hRC = wglCreateContext( *hDC );
        wglMakeCurrent( *hDC, *hRC );
    
    }
    
    
    /******************
     * Disable OpenGL
     *
     ******************/
    
    void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC){
        wglMakeCurrent (NULL, NULL);
        wglDeleteContext (hRC);
        ReleaseDC (hWnd, hDC);
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 7月10日

悬赏问题

  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃