home dev games gallery work
Lewpen.com»Research & Development»3D Graphics»OpenGL»Reflective Sphere

Reflective Sphere

Analytical reflections in OpenGL

/ Source / Winmain.cpp

// winmain.cpp

/*
	Windows OpenGL helper library

	Copyright (c) 1998 Gremlin Interactive Ltd.
	Written by Mark Lord
*/
#define WIN32_LEAN_AND_MEAN
#include "vars.h"
#include "qe_ogl.h"
#include <mmsystem.h> // multimedia shite
#include "winmain.h"

//================================================================

HINSTANCE			g_hInstance;
int					g_nShowCommand;

HWND				g_hWnd = NULL;

char				g_classname[] = "ClassName";
char				g_caption[] = "OpenGL Test Program";
BOOL				g_exitflag;
int					qeQuitFlag;


extern BOOL DrawMe;

extern double r1, r2;

LRESULT CALLBACK WndProc (HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam);
void InitWindowClasses ();
void CreateMainWindow ();
void ProcessMessages ();


void Initialise();
void GameMain();
void GameDraw();

BOOL lbutton = FALSE;



//================================================================

/*
====================
WinMain()
	
Entry point
====================
*/
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/,
	LPTSTR lpCmdLine, int nShowCommand)
{
	// set up globals 
	g_hInstance = hInstance;
	g_nShowCommand = nShowCommand;
	
	// prepare the window 
	InitWindowClasses ();
	CreateMainWindow ();

	// OpenGL is now running, we can do any special one time initialisation
	// here 
	Initialise();
// loop until exit 

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	g_exitflag = FALSE;
	while (! g_exitflag)
	{
		ProcessMessages ();
		GameMain ();
	}
	return 0;
}



/*
====================
InitWindowClasses()
	
Initialise the window classes we need
====================
*/
void InitWindowClasses ()
{
	WNDCLASS		wc;
	
	wc.style			= CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
	wc.lpfnWndProc		= (WNDPROC) WndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= g_hInstance;
	wc.hCursor			= LoadCursor (NULL, IDC_ARROW);
	wc.hIcon			= LoadIcon (NULL, IDI_APPLICATION);
	wc.hbrBackground	= (HBRUSH) GetStockObject (BLACK_BRUSH);
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= g_classname;

	
	if (! RegisterClass (&wc))
	{
		MessageBox (NULL, "Failed to register window class.", "Error", MB_OK|MB_ICONSTOP);
		exit (EXIT_FAILURE);
	}
}

/*
====================
CreateMainWindow ()
	
Create and display the main window
====================
*/
void CreateMainWindow ()
{
	HMENU g_hMenu=NULL;
//	g_hMenu=LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_MENU1));

//// windowed version 
//*	
	int width=1600;
	int height=1200;
	int xpos=(GetSystemMetrics(SM_CXSCREEN)-width) / 2;
	int ypos=(GetSystemMetrics(SM_CYSCREEN)-height) / 2;

	g_hWnd = CreateWindowEx (0, g_classname, g_caption, 
		WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
		xpos, ypos, width, height,
		NULL, g_hMenu, g_hInstance, NULL);
/*/
//// fullscreen version
	int width=GetSystemMetrics(SM_CXSCREEN);
	int height=GetSystemMetrics(SM_CYSCREEN);
	int xpos=0;
	int ypos=0;

	width=600;
	height=450;
	xpos=(GetSystemMetrics(SM_CXSCREEN)-width) / 2;
	ypos=(GetSystemMetrics(SM_CYSCREEN)-height) / 2;

	g_hWnd = CreateWindowEx (0, g_classname, g_caption, 
		WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_POPUP,
		xpos, ypos, width, height,
		NULL, NULL, g_hInstance, NULL);
//*/


	ShowWindow (g_hWnd, g_nShowCommand);
}



/*
====================
ProcessMessages ()
	
Process all messages that are pending in the event queue
====================
*/
void ProcessMessages ()
{
	MSG				msg;
	
	while (PeekMessage (&msg, 0, 0, 0, PM_REMOVE))
	{
		if (msg.message == WM_QUIT)
			g_exitflag = TRUE;
			
		TranslateMessage (&msg);
		DispatchMessage (&msg);
	}
}

//================================================================

/*
====================
WndProc()
	
Window procedure for the main window
====================
*/
LRESULT CALLBACK WndProc (HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
	HDC				hDC;
	PAINTSTRUCT		ps;
	static char		buf[1024];

  static int mx = 0, my = 0;

	switch (uMessage)
	{
		case WM_CREATE:
			hDC = GetDC (hWnd);
			if (! GL_InitGL (hWnd, hDC))
			{
				MessageBox (NULL, gl_error, "OpenGL Error", MB_OK | MB_ICONSTOP);
				exit (EXIT_FAILURE);
			}

			break;
			
		case WM_PAINT:
			BeginPaint (hWnd, &ps);
			EndPaint (hWnd, &ps);
			GameDraw();
			break;
			
		case WM_CLOSE:
			glFinish();
			DestroyWindow (hWnd);
			break;

    case WM_LBUTTONDOWN:
      lbutton = TRUE;
      SetCapture(hWnd);
      return 0;

    case WM_LBUTTONUP:
      lbutton = FALSE;
      SetCapture(NULL);
      return 0;

    case WM_MOUSEMOVE:
      POINT pt;
      GetCursorPos(&pt);

      if(lbutton) {
        r1 += (mx-pt.x);
        r2 += (my-pt.y);
      }

      mx = pt.x;
      my = pt.y;

      return 0;

		case WM_DESTROY:
			PostQuitMessage (0);
			CloseDownApplication();
			break;
			
		case WM_SIZE:
			GL_Initialise ((signed short)LOWORD (lParam), (signed short)HIWORD (lParam));
			break;
			
		// some very simple switches 
		case WM_CHAR:
			switch ((TCHAR) wParam)
			{
				case VK_ESCAPE:
					PostMessage (hWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
					PostMessage (hWnd, WM_CLOSE, 0, 0);
					break;

			}
			break;

		case WM_ACTIVATEAPP:
			DrawMe=(BOOL)wParam;
			glFinish();
			break;

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

void
CloseDownApplication()
{
	glFinish();
	GL_DoneGL ();
}
Related Articles

Procedural city block layout

Animated 4-dimensional objects mapped down to 3D

Simulating a cloth falling and slipping over objects

Windows C++ OpenGL example

Games

The Dodge Game
Flatspace

2-Player Games:

Quake 2D
Meteora

Puzzle Games:

Mini Tetris
Sudoku Solver

Development

3D Graphics:

3D Graphics Articles
WebGL Examples
OpenGL Examples
Flash 3D Engine
Java 3D Engine

Development:

Programming Articles
Game Development Examples

Work

Portfolio
Clients
Startups & Projects
Expertise

Links

CubeLogix Studios
PHP Charts & Graphs
Local Legends Football