whats the prob with this

Started by
15 comments, last by y2jsave 16 years, 1 month ago
iam trying to make a opengl window . whenever the user lefts clicks the mouse on this window , a second window should appear. so i have used two cpp files here i was using dev c++ here goes my code .......... the problem is the second window comes and goes away......... also drawing on the main window are effected can anyone tell the problem with my code ******************* file 1 ******************



#include<vector>
#include <windows.h>

#include<commctrl.h>
#include <gl\gl.h>			
#include <gl\glu.h>	
#include<math.h>
#include<iostream>
using namespace std;

#define HEIGHT 600
#define WIDTH 1000

extern int show_second_window();  
HDC hDC;

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
/* Make the classname into a global variable */
char szClassName[] = "Windows Example";
HINSTANCE hThisInstance;

VOID EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);
VOID DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);

POINT pt ;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)

{
 WNDCLASS wc;
  HWND hWnd;
  //HDC hDC;
  HGLRC hRC;    
  MSG msg;
  BOOL bQuit = FALSE;
  
   // register window class
  wc.style = CS_OWNDC;
  wc.lpfnWndProc = WindowProcedure;
  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 );
  
  
  
hWnd = CreateWindow( 
  "GLSample", "opengl", 
  WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE|WS_MAXIMIZEBOX |WS_MINIMIZEBOX,
  0, 0,WIDTH , HEIGHT,
  NULL, NULL, hInstance, NULL);

  
  EnableOpenGL( hWnd, &hDC, &hRC );

   ShowWindow(hWnd, nFunsterStil);
   
   
   while(GetMessage(&msg, NULL, 0, 0))
    {   
      /* Send message to WindowProcedure */
           DispatchMessage(&msg);
      glBegin(GL_LINES);						
			glVertex2d(0,0);					
			glVertex2d(1,1);		
           glEnd();
        
       SwapBuffers (hDC);
    }

    /* The program returvalue is 0 - The value that PostQuitMessage( ) gave */
    return msg.wParam;
}

   
   LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{ 
           switch (message)                  /* handle the messages */
    {
   
       case WM_LBUTTONDOWN: 

             show_second_window(); 
             return 0;
   
 default:                   /* for messages that we don't deal with */
      return DefWindowProc(hwnd, message, wParam, lParam);
   }
  return 0;
}  




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 );
} 




*************file 2******************************]

#include<vector>
#include <windows.h>

#include<commctrl.h>
#include <gl\gl.h>			
#include <gl\glu.h>	
#include<math.h>
#include<iostream>
using namespace std;

int const WIDTH_for_second_window =600;
int const HEIGHT_for_second_window =1600;
HDC hDC_for_second_window;

LRESULT CALLBACK WindowProcedure_for_second_window(HWND, UINT, WPARAM, LPARAM);

HINSTANCE hThisInstance_for_second_window;

 VOID EnableOpenGL_for_second_window(HWND hWnd_for_second_window, HDC * hDC_for_second_window, HGLRC * hRC_for_second_window);
 VOID DisableOpenGL_for_second_window(HWND hWnd_for_second_window, HDC hDC_for_second_window, HGLRC hRC_for_second_window);
 POINT pos_for_second_window;
 
int show_second_window()
{
  HINSTANCE hInstance_for_second_window;
    HINSTANCE hPrevInstance_for_second_window;
    LPSTR lpszArgument_for_second_window;
    int nFunsterStil_for_second_window;
int n;    
  n=nFunsterStil_for_second_window;
  WNDCLASS wc_for_second_window;
  HWND hWnd_for_second_window;
  //HDC hDC;
  HGLRC hRC_for_second_window;    
  MSG msg_for_second_window;
  BOOL bQuit = FALSE;
  
  
  
   // register window class
  wc_for_second_window.style = CS_OWNDC;
  wc_for_second_window.lpfnWndProc = WindowProcedure_for_second_window;
  wc_for_second_window.cbClsExtra = 0;
  wc_for_second_window.cbWndExtra = 0;
  wc_for_second_window.hInstance = hInstance_for_second_window;
  wc_for_second_window.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  wc_for_second_window.hCursor = LoadCursor( NULL, IDC_ARROW );
  wc_for_second_window.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
  wc_for_second_window.lpszMenuName = NULL;
  wc_for_second_window.lpszClassName = "GLSample";
  RegisterClass( &wc_for_second_window );
  
  
  hWnd_for_second_window = CreateWindow( 
  "GLSample", "second window", 
  WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE|WS_MAXIMIZEBOX |WS_MINIMIZEBOX,
  0, 0,WIDTH_for_second_window , HEIGHT_for_second_window,
  NULL, NULL, hInstance_for_second_window, NULL);
  
  
   EnableOpenGL_for_second_window( hWnd_for_second_window, &hDC_for_second_window, &hRC_for_second_window );

   ShowWindow(hWnd_for_second_window, nFunsterStil_for_second_window);
    
  
   while(GetMessage(&msg_for_second_window, NULL, 0, 0))
    {  
                    DispatchMessage(&msg_for_second_window);
  
  glBegin(GL_LINES);						
    	                	glVertex2d(0,0);				
		                	glVertex2d(1,1);	
                        glEnd();  
        
       SwapBuffers (hDC_for_second_window);
    }

    /* The program returvalue is 0 - The value that PostQuitMessage( ) gave */
    return msg_for_second_window.wParam;
    

}// end of function 

 LRESULT CALLBACK WindowProcedure_for_second_window(HWND hwnd_for_second_window, UINT message_for_second_window, WPARAM wParam_for_second_window, LPARAM lParam_for_second_window)
{
          return DefWindowProc(hwnd_for_second_window, message_for_second_window, wParam_for_second_window, lParam_for_second_window);
          
             
}


VOID EnableOpenGL_for_second_window( HWND hWnd_for_second_window, HDC * hDC_for_second_window, HGLRC * hRC_for_second_window )
{
  PIXELFORMATDESCRIPTOR pfd_for_second_window;
  int iFormat_for_second_window;

  // get the device context (DC)
  *hDC_for_second_window = GetDC( hWnd_for_second_window );

  // set the pixel format for the DC
  ZeroMemory( &pfd_for_second_window, sizeof( pfd_for_second_window ) );
  pfd_for_second_window.nSize = sizeof( pfd_for_second_window );
  pfd_for_second_window.nVersion = 1;

  pfd_for_second_window.dwFlags = PFD_DRAW_TO_WINDOW | 
  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;


  pfd_for_second_window.iPixelType = PFD_TYPE_RGBA;
  pfd_for_second_window.cColorBits = 24;
  pfd_for_second_window.cDepthBits = 16;
  pfd_for_second_window.iLayerType = PFD_MAIN_PLANE;
  iFormat_for_second_window = ChoosePixelFormat( *hDC_for_second_window, &pfd_for_second_window );
  SetPixelFormat( *hDC_for_second_window, iFormat_for_second_window, &pfd_for_second_window );

  // create and enable the render context (RC)
  *hRC_for_second_window = wglCreateContext( *hDC_for_second_window );
  wglMakeCurrent( *hDC_for_second_window, *hRC_for_second_window );
}

// Disable OpenGL

VOID DisableOpenGL_for_second_window( HWND hWnd_for_second_window, HDC hDC_for_second_window, HGLRC hRC_for_second_window )
{
  wglMakeCurrent( NULL, NULL );
  wglDeleteContext( hRC_for_second_window );
  ReleaseDC( hWnd_for_second_window, hDC_for_second_window );
} 





Advertisement
any suggestions?
Quote:Original post by y2jsave
can anyone tell the problem with my code


There are 2 problems that are immediately obvious:

1. Your message pumping logic is completely borked
2. Your drawing updates are inside your message pump, which is using GetMessage() rather than PeekMessage().

In more depth:

1. You have a message pump for each window, which is neither necessary nor desirable. One of those message pumps is inside another window's message handler, which is just plain evil.
Remove the second window's message pump. It's completely unnecessary and the fact that it's inside your first window's message handler is probably the cause of half your problems straight away.

2. GetMessage() is a blocking function: it will suspend the thread until a message comes along. That means that unless you're causing a constant stream of events (e.g calling something like InvalidateRect every time through the loop) your drawing functions will not get called regularly.
PeekMessage() does not block and wait for a message; it checks the message queue and returns whether there is a message or not. If you want to update your graphics regularly, and you're doing it it from inside your message pump, you'll probably want to use this. However, using PeekMessage will basically cause your app to update as fast as it possibly can, so it will consume 100% CPU even if it's not doing very much.

Alternatively, or perhaps even additionally, you will need to rethink where you call your rendering code from. It might make the most sense to move it inside the WM_PAINT or a WM_TIMER event for the appropriate window, and use InvalidateRect or a timer to force continuous redraws. Not the best solution for high performance rendering, but it will be relatively easy to implement and understand, and as you wouldn't need to use PeekMessage, it shouldn't consume 100% CPU all the time, and so works better for a windows app.

i changed my loops to these
 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         glBegin(GL_LINES);						    	                	glVertex2d(0,0);						                	glVertex2d(1,1);	                        glEnd();                 SwapBuffers (hDC);        }  }



still second window comes and goes away.........

also drawing on the main window are effected


also i cannot remove my message loop of second window since it too has to handle some events.


also depending on various positions of mouse clicks , various drawings are being done......... so i think i cant use wm_paint (i suppose ....... dont know abt wm_paint)

how to proceed now
thanks
Quote:Original post by y2jsave
also i cannot remove my message loop of second window since it too has to handle some events.


Sure, but you still don't need a separate pump for each window; the first one is perfectly capable of sending messages to any and all of the windows in your application.

Just delete it and most your problems will go away; you'll just need to figure out where to put the drawing code to update each separate window correctly. Which is where looking into WM_PAINT and/or WM_TIMER come in.
can u please calrify your point further .
if i remove my loop in my second file , then where to put the code for making line on my second window?


i changed my file 2 to this

and still same errors are encountered........

#include<vector>#include <windows.h>#include<commctrl.h>#include <gl\gl.h>			#include <gl\glu.h>	#include<math.h>#include<iostream>using namespace std;int const WIDTH_for_second_window =600;int const HEIGHT_for_second_window =1600;HDC hDC_for_second_window;LRESULT CALLBACK WindowProcedure_for_second_window(HWND, UINT, WPARAM, LPARAM);HINSTANCE hThisInstance_for_second_window; VOID EnableOpenGL_for_second_window(HWND hWnd_for_second_window, HDC * hDC_for_second_window, HGLRC * hRC_for_second_window); VOID DisableOpenGL_for_second_window(HWND hWnd_for_second_window, HDC hDC_for_second_window, HGLRC hRC_for_second_window); POINT pos_for_second_window; int show_second_window(){  HINSTANCE hInstance_for_second_window;    HINSTANCE hPrevInstance_for_second_window;    LPSTR lpszArgument_for_second_window;    int nFunsterStil_for_second_window;int n;      n=nFunsterStil_for_second_window;  WNDCLASS wc_for_second_window;  HWND hWnd_for_second_window;  //HDC hDC;  HGLRC hRC_for_second_window;      MSG msg_for_second_window;  BOOL bQuit = FALSE;         // register window class  wc_for_second_window.style = CS_OWNDC;  wc_for_second_window.lpfnWndProc = WindowProcedure_for_second_window;  wc_for_second_window.cbClsExtra = 0;  wc_for_second_window.cbWndExtra = 0;  wc_for_second_window.hInstance = hInstance_for_second_window;  wc_for_second_window.hIcon = LoadIcon( NULL, IDI_APPLICATION );  wc_for_second_window.hCursor = LoadCursor( NULL, IDC_ARROW );  wc_for_second_window.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );  wc_for_second_window.lpszMenuName = NULL;  wc_for_second_window.lpszClassName = "GLSample";  RegisterClass( &wc_for_second_window );      hWnd_for_second_window = CreateWindow(   "GLSample", "second window",   WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE|WS_MAXIMIZEBOX |WS_MINIMIZEBOX,  0, 0,WIDTH_for_second_window , HEIGHT_for_second_window,  NULL, NULL, hInstance_for_second_window, NULL);       EnableOpenGL_for_second_window( hWnd_for_second_window, &hDC_for_second_window, &hRC_for_second_window );   ShowWindow(hWnd_for_second_window, nFunsterStil_for_second_window);                          glBegin(GL_LINES);						    	                	glVertex2d(0,0);						                	glVertex2d(1,1);	                        glEnd();                 SwapBuffers (hDC_for_second_window);        /* The program returvalue is 0 - The value that PostQuitMessage( ) gave */    return msg_for_second_window.wParam;    }// end of function  LRESULT CALLBACK WindowProcedure_for_second_window(HWND hwnd_for_second_window, UINT message_for_second_window, WPARAM wParam_for_second_window, LPARAM lParam_for_second_window){          return DefWindowProc(hwnd_for_second_window, message_for_second_window, wParam_for_second_window, lParam_for_second_window);                       }VOID EnableOpenGL_for_second_window( HWND hWnd_for_second_window, HDC * hDC_for_second_window, HGLRC * hRC_for_second_window ){  PIXELFORMATDESCRIPTOR pfd_for_second_window;  int iFormat_for_second_window;  // get the device context (DC)  *hDC_for_second_window = GetDC( hWnd_for_second_window );  // set the pixel format for the DC  ZeroMemory( &pfd_for_second_window, sizeof( pfd_for_second_window ) );  pfd_for_second_window.nSize = sizeof( pfd_for_second_window );  pfd_for_second_window.nVersion = 1;  pfd_for_second_window.dwFlags = PFD_DRAW_TO_WINDOW |   PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;  pfd_for_second_window.iPixelType = PFD_TYPE_RGBA;  pfd_for_second_window.cColorBits = 24;  pfd_for_second_window.cDepthBits = 16;  pfd_for_second_window.iLayerType = PFD_MAIN_PLANE;  iFormat_for_second_window = ChoosePixelFormat( *hDC_for_second_window, &pfd_for_second_window );  SetPixelFormat( *hDC_for_second_window, iFormat_for_second_window, &pfd_for_second_window );  // create and enable the render context (RC)  *hRC_for_second_window = wglCreateContext( *hDC_for_second_window );  wglMakeCurrent( *hDC_for_second_window, *hRC_for_second_window );}// Disable OpenGLVOID DisableOpenGL_for_second_window( HWND hWnd_for_second_window, HDC hDC_for_second_window, HGLRC hRC_for_second_window ){  wglMakeCurrent( NULL, NULL );  wglDeleteContext( hRC_for_second_window );  ReleaseDC( hWnd_for_second_window, hDC_for_second_window );} 



i trie dto find using wm_paint with mouse clicks but cant find
Also, what the hell is going on with 'nFunsterStil_for_second_window'?

You're calling ShowWindow() with it, but you haven't actually initialized it with a value as far as I can see. This is probably why your second window disappears almost instantly; you might as well be passing it a random number.

Just forget the whole nFunsterStil malarkey and pass SW_SHOW to your ShowWindow calls.
Quote:Original post by y2jsave
i trie dto find using wm_paint with mouse clicks but cant find


WM_PAINT has nothing to do with the mouse. It's a window event that gets fired when a window needs drawing, either in response to the OS telling it needs to redraw, or because you forced it to fire by calling InvalidateRect() on it.

WM_PAINT
Quote:Original post by Sandman
Also, what the hell is going on with 'nFunsterStil_for_second_window'?

You're calling ShowWindow() with it, but you haven't actually initialized it with a value as far as I can see. This is probably why your second window disappears almost instantly; you might as well be passing it a random number.

to which value do we have to initialise it

This topic is closed to new replies.

Advertisement