Sign in to follow this  
Dancho

How to redraw opengl window after screensaver

Recommended Posts

Hi all, The problem is simple but solution is beyond my current knowledge so I'm asking for some help... This little snippet is just for example,it is classic win32+opengl beginner combo,compile/run it and small blue triangle wii be shown it,and then start your favorite screensaver and cancel it and small blue triangle is gone,i.e it isnt redraw again... Now Im not asking how to prevent screensaver to start,I know that,just how to force redraw of the window... As you can see I attempt something with wm_paint message but it isnt working,so Im little lost now what to do... thx all
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

LRESULT CALLBACK win_proc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
bool register_window(HINSTANCE hinstance);
HWND create_window(HINSTANCE hinstance);
void setup_pfd(HDC hdc);
void initgl();
void render_triangle();

const char app_class_name[]="OpenGL Class";
const char app_win_name[]="OpenGL Window";
const int WINDOW_WIDTH=800;
const int WINDOW_HEIGHT=600;

HDC g_hdc=NULL;
HGLRC g_hrc=NULL;

int WINAPI WinMain(HINSTANCE hIns,HINSTANCE hPins,LPSTR cmd,int show)
{
	HWND hwnd=NULL;
	MSG msg={0};
	
	if(!register_window(hIns))
		exit(EXIT_FAILURE);

	hwnd=create_window(hIns);
	if(hwnd==NULL)
		exit(EXIT_FAILURE);

	ShowWindow(hwnd,SW_SHOW);
	UpdateWindow(hwnd);

	bool running=true;

	initgl();

	while(running)
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message==WM_QUIT)
				running=false;
			TranslateMessage(&msg);
			DispatchMessage(&msg);	
		}

		render_triangle();
		SwapBuffers(g_hdc);
	
	}

	return (int)msg.wParam;
}

LRESULT CALLBACK win_proc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	static HDC hdc=NULL;
	PAINTSTRUCT ps={0};

	switch(msg)
	{
	case WM_CREATE:
		hdc=GetDC(hWnd);
		g_hdc=hdc;
		setup_pfd(g_hdc);
		g_hrc=wglCreateContext(g_hdc);
		wglMakeCurrent(g_hdc,g_hrc);
		break;
	case WM_CLOSE:
	case WM_DESTROY:
	case WM_QUIT:
		wglMakeCurrent(g_hdc,NULL);
		wglDeleteContext(g_hrc);
		PostQuitMessage(0);
		break;
	case WM_PAINT:
		BeginPaint(hWnd,&ps);
		render_triangle();
		EndPaint(hWnd,&ps);
		break;
	}

	return DefWindowProc(hWnd,msg,wParam,lParam);
}

void initgl()
{
	glClearColor(0.0,0.0,0.0,0.0);
	glViewport(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,(GLdouble)WINDOW_WIDTH,0.0,(GLdouble)WINDOW_HEIGHT);
    glMatrixMode(GL_MODELVIEW);
}

void render_triangle()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(400,300,0.0);
	glBegin(GL_TRIANGLES);
		glColor3f(0.0,0.0,1.0);
		glVertex2i(50,0);
		glVertex2i(25,25);
		glVertex2i(0,0);
	glEnd();
}

bool register_window(HINSTANCE hinstance)
{
	WNDCLASSEX wc;

	memset(&wc,0,sizeof(WNDCLASSEX));
	wc.cbSize=sizeof(WNDCLASSEX);
	wc.style=CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc=win_proc;
	wc.cbClsExtra=0;
	wc.cbWndExtra=0;
	wc.hInstance=hinstance;
	wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wc.hCursor=LoadCursor(NULL,IDC_ARROW);
	wc.hbrBackground=NULL;
	wc.lpszMenuName=NULL;
	wc.lpszClassName=app_class_name;
	wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(0,"Window Registration Failed","Error",MB_ICONERROR | MB_OK);
		return false;
	}

	return true;
}

HWND create_window(HINSTANCE hinstance)
{
	HWND hwnd=NULL;
	RECT window_rect={0};
	DWORD exstyle=0;
	DWORD style=0;

	exstyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; 
	style=WS_OVERLAPPEDWINDOW ^ WS_MAXIMIZEBOX;

	window_rect.left=0;
	window_rect.right=WINDOW_WIDTH;
	window_rect.top=0;
	window_rect.bottom=WINDOW_HEIGHT;

	AdjustWindowRectEx(&window_rect,style,FALSE,exstyle);

	hwnd=CreateWindowEx(exstyle,
						app_class_name,
					    app_win_name,
					    style,
						CW_USEDEFAULT,
						CW_USEDEFAULT,
						window_rect.right-window_rect.left,
						window_rect.bottom-window_rect.top,
						0,
						0,
						hinstance,
						NULL);
	return hwnd;
}

void setup_pfd(HDC hdc)
{
	PIXELFORMATDESCRIPTOR pfd;
	int pixel_format;

	memset(&pfd,0,sizeof(PIXELFORMATDESCRIPTOR));
	pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion=1;
	pfd.dwFlags=PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType=PFD_TYPE_RGBA;
	pfd.cColorBits=32;
	pfd.cDepthBits=24;
	pfd.iLayerType=PFD_MAIN_PLANE;

	pixel_format=ChoosePixelFormat(hdc,&pfd);
	SetPixelFormat(hdc,pixel_format,&pfd);
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this