universe Temat założony przez niniejszego użytkownika  | 
OpenGL i problem z wczytywaniem z klawiatury. » 2014-09-13 17:58:52 Witam, ostatnio pisałem grę w rodzaju "Snake" i napotkałem problem. Chodzi o to ,że chciałem użyć getch() do wczytywania z klawiatury kilku rzeczy ,ale  najpierw chciałem przetestować czy to działa.Napisałem coś co miało się wyłączać jeśli kliknę 'y' , ale wywala mnie od razu. {     {         int n = 1;         float w = 0.0;          float s = 0.0;         float sm = 0.2;         float wm = 0.4;          int ch;         while( 1 != 0 )         {             ch == _getch();             ch == toupper( ch );             if( ch == 'Y' )             {                 break;             }             if( n == 1 )             {                                  while( w != wm && s != sm )                  {                                          kwadrat( 0.1, w, s );                     w += 0.2;                     s += 0.0;                                      }             }         }              }
   | 
 | 
Glazus  | 
» 2014-09-13 18:37:08 ? while( 1 != 0 ) ? ch == _getch(); ch == toupper( ch ); ?  | 
 | 
universe Temat założony przez niniejszego użytkownika  | 
» 2014-09-13 20:50:07 While(1!=0) je po to żeby cały czas mi powtarzało.  | 
 | 
Jacob99  | 
» 2014-09-14 14:23:35 Od tego jest while(true); A poza tym pokaż cały kod.  | 
 | 
universe Temat założony przez niniejszego użytkownika  | 
» 2014-09-16 19:30:23 Nie wiem czy to jest mądre kopiować cały kod ale ok. Chciałem do tego wstawić getcha ale nie działa  btw. mam pytanie dlaczego ten sleep(jest teraz zakomentowany) działa tylko raz , czyli czekam chwilkę i pojawiają się wszystkie klocki a chciałem by szło po kolei a nie na raz dlatego jest w while'u.  #include <windows.h> #include <windows.h> #include <gl/gl.h> #include <conio.h> #include <ctype.h> #include <iostream>
 
  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 ); void kwadrat( float a, float w, float s ) {     float red = 0.5;     float green = 1.0;     float blue = 0.5;     glBegin( GL_TRIANGLES );     glColor3f( red, green, blue ); glVertex2f( a + s, a + w );     glColor3f( red, green, blue ); glVertex2f( a + s, - a + w );     glColor3f( red, green, blue ); glVertex2f( - a + s, - a + w );     glBegin( GL_TRIANGLES );     glColor3f( red, green, blue ); glVertex2f( - a + s, a + w );     glColor3f( red, green, blue ); glVertex2f( - a + s, - a + w );     glColor3f( red, green, blue ); glVertex2f( a + s, a + w ); }
  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;          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 = "Snake";     RegisterClass( & wc );          hWnd = CreateWindow(     "Snake", "Snake",     WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,     0, 0, 512, 512,     NULL, NULL, hInstance, NULL );          EnableOpenGL( hWnd, & hDC, & hRC );          while( !bQuit )     {         if( PeekMessage( & msg, NULL, 0, 0, PM_REMOVE ) )         {             if( msg.message == WM_QUIT )             {                 bQuit = TRUE;             }             else             {                 TranslateMessage( & msg );                 DispatchMessage( & msg );             }         }         else         {             glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );             glClear( GL_COLOR_BUFFER_BIT );                          glPushMatrix();             glRotatef( theta, 0.0f, 0.0f, 1.0f );             {                 {                     int n = 1;                     float w = 0.0;                      float s = 0.0;                     float sm = 0.2;                     float wm = 0.8;                      while( w != wm && s != sm )                      {                                                  kwadrat( 0.1, w, s );                         w += 0.2;                         s += 0.0;                     }                 }                 glEnd();                 glPopMatrix();                                  SwapBuffers( hDC );                                  theta += 0.0f;                 Sleep( 5 );             }         }     }     DisableOpenGL( hWnd, hDC, hRC );     DestroyWindow( hWnd );          return msg.wParam; } 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 );     } } void EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC ) {     PIXELFORMATDESCRIPTOR pfd;     int iFormat;          * hDC = GetDC( hWnd );          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 );          * hRC = wglCreateContext( * hDC );     wglMakeCurrent( * hDC, * hRC );      } void DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC ) {     wglMakeCurrent( NULL, NULL );     wglDeleteContext( hRC );     ReleaseDC( hWnd, hDC ); }
   | 
 | 
Monika90  | 
» 2014-09-16 19:52:49  | 
 | 
universe Temat założony przez niniejszego użytkownika  | 
» 2014-09-16 20:15:33 Usunąłem jeden begin i jest ok. Postanowiłem zrobić przesuwający się klocek i ogólnie usprawniłem kod , tylko jak działa clear buffer albo jak zrobić by klocki nie migały. #include <windows.h> #include <windows.h> #include <gl/gl.h> #include <conio.h> #include <ctype.h> #include <iostream>
 
  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 ); void kwadrat( float a, float w, float s ) {     float red = 0.5;     float green = 1.0;     float blue = 0.5;     glBegin( GL_TRIANGLES );     glColor3f( red, green, blue ); glVertex2f( a + s, a + w );     glColor3f( red, green, blue ); glVertex2f( a + s, - a + w );     glColor3f( red, green, blue ); glVertex2f( - a + s, - a + w );     ( GL_TRIANGLES );     glColor3f( red, green, blue ); glVertex2f( - a + s, a + w );     glColor3f( red, green, blue ); glVertex2f( - a + s, - a + w );     glColor3f( red, green, blue ); glVertex2f( a + s, a + w ); }
  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;          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 = "Snake";     RegisterClass( & wc );          hWnd = CreateWindow(     "Snake", "Snake",     WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,     0, 0, 512, 512,     NULL, NULL, hInstance, NULL );          EnableOpenGL( hWnd, & hDC, & hRC );          while( !bQuit )     {         if( PeekMessage( & msg, NULL, 0, 0, PM_REMOVE ) )         {             if( msg.message == WM_QUIT )             {                 bQuit = TRUE;             }             else             {                 TranslateMessage( & msg );                 DispatchMessage( & msg );             }         }         else         {             glClear( GL_COLOR_BUFFER_BIT );             glPushMatrix();             {                 {                     int n = 1;                     float w = 0.0;                      float s = 0.0;                     float sm = 0.2;                     float wm = 0.8;                      for( float i = w; w < wm; w += 0.2 )                     {                         kwadrat( 0.1, w, s );                         SwapBuffers( hDC );                         Sleep( 1000 );                     }                 }                 glEnd();                 glPopMatrix();                                  SwapBuffers( hDC );                 Sleep( 5 );             }         }     }     DisableOpenGL( hWnd, hDC, hRC );     DestroyWindow( hWnd );          return msg.wParam; } 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 );     } } void EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC ) {     PIXELFORMATDESCRIPTOR pfd;     int iFormat;          * hDC = GetDC( hWnd );          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 );          * hRC = wglCreateContext( * hDC );     wglMakeCurrent( * hDC, * hRC );      } void DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC ) {     wglMakeCurrent( NULL, NULL );     wglDeleteContext( hRC );     ReleaseDC( hWnd, hDC ); }
  *Edit Udało mi się to naprawić ale mam jeszcze jedno pytanie. Dlaczego po użyciu clear buffer i po ponownym stworzeniu kwadratu poprzedni nadal istnieje. Problem na obrazku:   I kod :) #include <windows.h> #include <gl/gl.h> #include <conio.h> #include <ctype.h> #include <iostream> #include <cstdlib> #include <ctime> #include <String> 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 ); void kwadrat( float a, float w, float s ) {     float red = 0.0;     float green = 0.0;     float blue = 0.0;     glBegin( GL_TRIANGLES );     glColor3f( red, green, blue ); glVertex2f( a + s, a + w );     glColor3f( red, green, blue ); glVertex2f( a + s, - a + w );     glColor3f( red, green, blue ); glVertex2f( - a + s, - a + w );     ( GL_TRIANGLES );     glColor3f( red, green, blue ); glVertex2f( - a + s, a + w );     glColor3f( red, green, blue ); glVertex2f( - a + s, - a + w );     glColor3f( red, green, blue ); glVertex2f( a + s, a + w );     Sleep( 1000 );     glClear( GL_COLOR_BUFFER_BIT ); }
  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;          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 = "Snake";     RegisterClass( & wc );          hWnd = CreateWindow(     "Snake", "Snake",     WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,     0, 0, 512, 512,     NULL, NULL, hInstance, NULL );          EnableOpenGL( hWnd, & hDC, & hRC );          while( !bQuit )     {         if( PeekMessage( & msg, NULL, 0, 0, PM_REMOVE ) )         {             if( msg.message == WM_QUIT )             {                 bQuit = TRUE;             }             else             {                 TranslateMessage( & msg );                 DispatchMessage( & msg );             }         }         else         {             glClearColor( 0.5f, 1.0f, 0.5f, 0.0f );             glClear( GL_COLOR_BUFFER_BIT );             SwapBuffers( hDC );             int n = 1;             float w = 0.0;              float s = 0.0;             float sm = 1.0;             float wm = 1.0;              float krok = 0.2;             for( float i = w; w < wm; w += krok )             {                 glClear( GL_COLOR_BUFFER_BIT );                 kwadrat( 0.1, w, s );                 SwapBuffers( hDC );                 Sleep( 1000 );                 SwapBuffers( hDC );             }             DestroyWindow( hWnd );             glEnd();         }     }     DisableOpenGL( hWnd, hDC, hRC );     DestroyWindow( hWnd );          return msg.wParam; } 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 );     } } void EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC ) {     PIXELFORMATDESCRIPTOR pfd;     int iFormat;          * hDC = GetDC( hWnd );          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 );          * hRC = wglCreateContext( * hDC );     wglMakeCurrent( * hDC, * hRC );      } void DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC ) {     wglMakeCurrent( NULL, NULL );     wglDeleteContext( hRC );     ReleaseDC( hWnd, hDC ); }
   | 
 | 
|  « 1 »  |