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