예제 #1
0
// Process Window Message Callbacks
LRESULT CALLBACK WindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

	// Get The Window Context
	gl_window_t *win = (gl_window_t *)(GetWindowLong (hWnd, GWL_USERDATA));
	//printf("hwnd=%d uMsg=%d\n", hWnd, uMsg);
	switch (uMsg) {
		case WM_ERASEBKGND:												// Check To See If Windows Is Trying To Erase The Background
			return 0;													// Return 0 (Prevents Flickering While Resizing A Window)

		case WM_PAINT:													// Window Needs Updating
		{
			//PAINTSTRUCT ps;
			//BeginPaint(win->hWnd, &ps);

			// uncomment this to repaint window on size/move event, maybe add win->sizemoveevent = TRUE,
			// so it will not interfer with main loop

			if (win->win32_wndproconly) {
				WindowUpdate(win);
				WindowDraw(win);
			}

			//  I've read that opengl should be drawn in WM_PAINT, but that just caused problems (unfocused window e.g. just would not update at all)
			//EndPaint(win->hWnd, &ps);
			return 0;
		}

		case WM_SYSCOMMAND:												// Intercept System Commands
		{
			switch (wParam)												// Check System Calls
			{
				case SC_SCREENSAVE:										// Screensaver Trying To Start?
				case SC_MONITORPOWER:									// Monitor Trying To Enter Powersave?
				return 0;												// Prevent From Happening
			}
			break;														// Call DefWindowProc()
		}


		case WM_CREATE:													// Window Creation
		{
			CREATESTRUCT* creation = (CREATESTRUCT*)(lParam);			// Store Window Structure Pointer
			win = (gl_window_t *)(creation->lpCreateParams);
			SetWindowLong (hWnd, GWL_USERDATA, (LONG)(win));
			return 0;
		}


		case WM_CLOSE:
			WindowTerminate(win);
			return 0;

		case WM_SIZE:													// Size Action Has Taken Place
			//printf("WM_SIZE\n");
			switch (wParam) {
				case SIZE_MINIMIZED:									// Was Window Minimized?
					win->isVisible = FALSE;							// Set isVisible To False
					return 0;

				case SIZE_MAXIMIZED:									// Was Window Maximized?
					win->isVisible = TRUE;							// Set isVisible To True
					WindowMakeCurrent(win);
					ReshapeGL (win, LOWORD (lParam), HIWORD (lParam));		// Reshape Window - LoWord=Width, HiWord=Height
					return 0;

				case SIZE_RESTORED:										// Was Window Restored?
					win->isVisible = TRUE;							// Set isVisible To True
					WindowMakeCurrent(win);
					ReshapeGL (win, LOWORD (lParam), HIWORD (lParam));		// Reshape Window - LoWord=Width, HiWord=Height
					return 0;
			}
			break;

		case WM_KEYDOWN:												// Update Keyboard Buffers For Keys Pressed
			if ((wParam >= 0) && (wParam <= 255))						// Is Key (wParam) In A Valid Range?
			{
				win->keys.keyDown [wParam] = TRUE;					// Set The Selected Key (wParam) To True
				return 0;												// Return
			}
			break;

		case WM_KEYUP:													// Update Keyboard Buffers For Keys Released
			if ((wParam >= 0) && (wParam <= 255))						// Is Key (wParam) In A Valid Range?
			{
				win->keys.keyDown [wParam] = FALSE;					// Set The Selected Key (wParam) To False
				return 0;												// Return
			}
			break;

		case WM_TOGGLEFULLSCREEN:										// Toggle FullScreen Mode On/Off
			//win->toggleFullscreen = TRUE;
			PostMessage (hWnd, WM_QUIT, 0, 0);
			break;
	}

	return DefWindowProc (hWnd, uMsg, wParam, lParam);					// Pass Unhandled Messages To DefWindowProc
}
예제 #2
0
// Program Entry (WinMain)
//int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
int main()
{
	HINSTANCE hInstance = NULL;
	application_t	*app; // = ApplicationNew("OpenGL", hInstance);
	application_t	*app2; // = ApplicationNew("OpenGL 2", hInstance);
	gl_window_t	*win1, *win2;
	
	app = ApplicationNew("OpenGL", hInstance);
	app2 = ApplicationNew("OpenGL 2", hInstance);
start:
	
	// left, top, width, height
	char title[1024];
	sprintf(title, "system metrics: %dx%d", GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));

	win1 = WindowNew(app, title, 100, 100, 1024, 768, MyInitialize, MyDeinitialize, MyUpdate, MyDraw);
	if ( ! win1->isCreated) {
		MessageBox (HWND_DESKTOP, "Error Creating OpenGL Window", "Error", MB_OK | MB_ICONEXCLAMATION);
		return -1;
	}

	win2 = WindowNew(app2, title, 100, 100, 1024, 768, MyInitialize, MyDeinitialize, MyUpdate, MyDraw);
	if ( ! win2->isCreated) {
		MessageBox (HWND_DESKTOP, "Error Creating OpenGL Window", "Error", MB_OK | MB_ICONEXCLAMATION);
		return -1;
	}

	// Ask The User If They Want To Start In FullScreen Mode? (Remove These 4 Lines If You Want To Force Fullscreen)
	//if (MessageBox (HWND_DESKTOP, "Would You Like To Run In Fullscreen Mode?", "Start FullScreen?", MB_YESNO | MB_ICONQUESTION) == IDNO)
	//{
	//	window.init.isFullScreen = FALSE;								// If Not, Run In Windowed Mode
	//}
	
	
	WindowInit(win1);
	WindowInit(win2);
	int quitcounter = 0;
	while (1) {
		if (! win1->quit) {
			WindowUpdate(win1);
			WindowDraw(win1);
			WindowMessageLoop(win1);
		}
		
		if (! win2->quit) {
			WindowUpdate(win2);
			WindowDraw(win2);
			WindowMessageLoop(win2);
		}

		if (win1->quit && win2->quit)
			break;
	}
	
	goto start;
	
	ApplicationClose(app);
	ApplicationClose(app2);

	
	return 0;
}
예제 #3
0
// draws everything (output)
void cSteam::draw()
{
	if (game.state == GAME_MENU)
	{

		// Menu Menu window is always drawn...
		WindowDraw(bmp_screen, 16,360,120,100, "Main Menu");

		// Main Menu / main
		if (iMainState == STATE_MAIN_MENU)
		{
			// Draw menu items and react upon them
			DrawMenuItems(36, 390, 0, true);
		}

		if (iMainState == STATE_MAIN_OPTIONS)
		{
			// Draw main options buttons, but do not use them
			DrawMenuItems(36, 390, 0, false);

			// draw options window
			WindowDraw(bmp_screen, 150, 60, 400,400, "Options");

			DrawMenuItems(170, 90, 1, true);

			// devide bar (vertically)
			line(bmp_screen, 240, 90, 240, 420, makecol(40,46,34));
			line(bmp_screen, 241, 90, 241, 420, makecol(136,145,128));

		}

		// Start a new game
		if (iMainState == STATE_MAIN_NEWGAME)
		{
			iMainState = STATE_NONE;
			iSubState  = STATE_NONE;
			game.state = GAME_GAME;
		}

		// Start editor
		if (iMainState == STATE_MAIN_EDITOR)
		{
			iMainState = STATE_EDITOR;
			iSubState  = STATE_NONE;
			game.state = GAME_EDITOR;
		}

		// get out the game here
		if (iMainState == STATE_MAIN_EXIT)
			game.playing = false;
	}
	
	// In the game
	if (game.state == GAME_GAME)
	{
		if (iMainState == STATE_INGAME_OPTIONS)
		{
			WindowDraw(bmp_screen, 160,120,320,240, "In-game options");
			DrawMenuItems(180, 150, 2, true);
		}

		if (iMainState == STATE_MAIN_MENU)
			game.state = GAME_MENU;
	}

	// In the editor
	if (game.state == GAME_EDITOR)
	{
		if (iMainState == STATE_MAIN_MENU)
			game.state = GAME_MENU;

		// Editor with options
		WindowDraw(bmp_screen, 0, 300, 160, 180, "Editor");
		// Minimap window
		WindowDraw(bmp_screen, 0, 0, 160, 140, "Minimap");

		if (iMainState == STATE_EDITOR)
		{
			DrawMenuItems(20, 330, 3, true);			

			// depending on sub-state:
			if (iSubState == STATE_EDITOR_SUB_TEXTURES)
			{
				WindowDraw(bmp_screen, 0, 141, 160, 158, "Textures");
				bool bBlock;

				// draw a window of the current texture + 2 buttons (next/prev)
				bool bNext = ButtonDraw(bmp_screen, 126, 274, 19, 16, ">>");
				bool bPrev = ButtonDraw(bmp_screen, 16, 274, 19, 16, "<<");
				
				bool bUp = ButtonDraw(bmp_screen, 140, 187, 12, 20, "+");
				bool bDown = ButtonDraw(bmp_screen, 140, 227, 12, 40, "-");
				
				// Blocked or not?
				if (game.bBlockTextures)
					bBlock = ButtonDraw(bmp_screen, 140, 167, 12, 20, "U");
				else
					bBlock = ButtonDraw(bmp_screen, 140, 167, 12, 20, "B");

				if (bBlock)
				{
					if (game.bBlockTextures)
						game.bBlockTextures=false;
					else
						game.bBlockTextures=true;
				}

				// ---- 
				// BUTTON: Switch from GROUP to SINGLE and back
				bool bGroupPressed = false;
				if (game.bShowGroupTextures == false)
					bGroupPressed = ButtonDraw(bmp_screen, 46, 274, 68, 16, "< GROUP >");
				else
					bGroupPressed = ButtonDraw(bmp_screen, 46, 274, 68, 16, "< SINGLE >");

				if (bGroupPressed)
				{
					if (game.bShowGroupTextures)
						game.bShowGroupTextures = false;
					else
						game.bShowGroupTextures = true;
				}
				// ----

				// next
				if (bNext)
				{
					if (game.bShowGroupTextures)
						game.iGroupNumber++;
					else
						game.iSingleNumber++;
				}
				
				// go back
				if (bPrev)
				{
					if (game.bShowGroupTextures)
						game.iGroupNumber--;
					else
						game.iSingleNumber--;

					if (game.iGroupNumber < 0) game.iGroupNumber = 0;
					if (game.iSingleNumber < 0) game.iSingleNumber = 0;
				}

				// Upper and lower the brightness
				if (bUp)
				{
					game.iBrightness+=10;
					if (game.iBrightness > 255)
						game.iBrightness = 255;
				}

				if (bDown)
				{
					game.iBrightness -=10;
					if (game.iBrightness < 10)
						game.iBrightness = 10;
				}
				
				// draw something black in the window
				rectfill(bmp_screen, 10, 165, 130, 260, makecol(0,0,0));
				set_clip(bmp_screen, 10, 165, 130, 260);

				set_trans_blender(255, 255, 255, game.iBrightness);

				// clip the entire texture group / or single texture!
				if (game.bShowGroupTextures)
				{
					// draw the entire thingy
					int dx=0;
					int dy=0;				
					
					for (int i=0; i < 64; i++)
					{
						if (game.GROUP_Textures[game.iGroupNumber].iTextures[i] > -1)
						{
							// now create tile at the correct spot at the map:
							draw_trans_sprite(bmp_screen, game.DATA_Textures[game.GROUP_Textures[game.iGroupNumber].iTextures[i]].bmp, 11+(dx*32), 166+(dy*32));
					
							dx++;
							if (dx >= game.GROUP_Textures[game.iGroupNumber].iWidth)
							{
								dx=0;
								dy++;
							}			
						}
					}
				}
				else
				{
					// its a 32x32 square
					if (game.DATA_Textures[game.iSingleNumber].bmp != NULL)
						draw_trans_sprite(bmp_screen, game.DATA_Textures[game.iSingleNumber].bmp, 60, 180);
				}

				set_clip(bmp_screen, 0, 0, game.screen_x, game.screen_y);


				// When the mouse is on the editing field, and a mouse button is pressed
				// add the tiles (group or single)
				if (mouse_x > 160)
				{
					if (game.MOUSE_PRESSED_LEFT())
					{
						// calculate x and y
						int x=(mouse_x-160)/32;
						int y=mouse_y/32;
						x+=(map.lScrollX/32);
						y+=(map.lScrollY/32);

						if (game.bShowGroupTextures)						
							map.create_tile_group(x,y,game.iGroupNumber, game.iBrightness, game.bBlockTextures);
						else
							map.create_tile(x,y,game.iSingleNumber, game.iBrightness, game.bBlockTextures);
						
					}
					else
					{
						int ddx, ddy;
						ddx=mouse_x/32;
						ddy=mouse_y/32;
						
						ddx*=32;
						ddy*=32;

						// Increase (because editor = 160 (=5x32) + 5 pixels = 165)
						ddx+=5;

						// Make sure scrolling is taken into account to!
						ddx-= (map.lScrollX)-((int)(map.lScrollX/32)*32);
						ddy-= (map.lScrollY)-((int)(map.lScrollY/32)*32);



					/*	ddx+= (map.lScrollX-(map.lScrollX/32));
						ddy+= (map.lScrollY-(map.lScrollY/32));*/

						if (game.bShowGroupTextures)
						{
							int dx=0;
							int dy=0;				
							
							if (game.GROUP_Textures[game.iGroupNumber].iWidth > -1)
							{
								rectfill(bmp_screen, ddx+2, ddy+2, ddx+2+game.GROUP_Textures[game.iGroupNumber].iWidth*32, ddy+2+game.GROUP_Textures[game.iGroupNumber].iHeight*32, makecol(0,0,0));
							}

							for (int i=0; i < 64; i++)
							{
								if (game.GROUP_Textures[game.iGroupNumber].iTextures[i] > -1)
								{
									// now create tile at the correct spot at the map:
									draw_trans_sprite(bmp_screen, game.DATA_Textures[game.GROUP_Textures[game.iGroupNumber].iTextures[i]].bmp, ddx+(dx*32), ddy+(dy*32));
									
									dx++;
									if (dx >= game.GROUP_Textures[game.iGroupNumber].iWidth)
									{
										dx=0;
										dy++;
									}			
								}
							}							
						}
						else
						{
							if (game.DATA_Textures[game.iSingleNumber].bmp != NULL)
							{
								rectfill(bmp_screen, ddx+2, ddy+2, ddx+34, ddy+34, makecol(0,0,0));								
								draw_trans_sprite(bmp_screen, game.DATA_Textures[game.iSingleNumber].bmp, ddx, ddy);
							}
						}
					}
				}
			}
				// substate
				if (iSubState == STATE_EDITOR_SUB_LIGHT)
				{
					WindowDraw(bmp_screen, 0, 141, 160, 158, "Light/Shadows");
					// draw a window of the current texture + 2 buttons (next/prev)
					bool bNext = ButtonDraw(bmp_screen, 126, 274, 19, 16, ">>");
					bool bPrev = ButtonDraw(bmp_screen, 16, 274, 19, 16, "<<");
					
					bool bUp = ButtonDraw(bmp_screen, 140, 167, 12, 40, "+");
					bool bDown = ButtonDraw(bmp_screen, 140, 227, 12, 40, "-");
				
					// next
					if (bNext)
						if (shadows[game.iShadowNumber].type != DAT_END)
						game.iShadowNumber++;
					
					// go back
						if (bPrev || shadows[game.iShadowNumber].type != DAT_BITMAP)
							game.iShadowNumber--;
					
					if (game.iShadowNumber < 0) game.iShadowNumber = 0;
					

					// Upper and lower the brightness
					if (bUp)
					{
						game.iBrightness+=10;
						if (game.iBrightness > 255)
							game.iBrightness = 255;
					}

					if (bDown)
					{
						game.iBrightness -=10;
						if (game.iBrightness < 10)
							game.iBrightness = 10;
					}
					
					// draw something black in the window
					rectfill(bmp_screen, 10, 165, 130, 260, makecol(192,192,192));
					set_clip(bmp_screen, 10, 165, 130, 260);

					// fill in
					set_trans_blender(255, 255, 255, game.iBrightness);				
					draw_trans_sprite(bmp_screen, (BITMAP *)shadows[game.iShadowNumber].dat, 60, 180);

					// set clip back
					set_clip(bmp_screen, 0, 0 , game.screen_x, game.screen_y);

					if (mouse_x > 160)
					{
						if (game.MOUSE_PRESSED_LEFT())
						{
							// calculate x and y
							int x=(mouse_x-160)/32;
							int y=mouse_y/32;						
							x+=(map.lScrollX/32);
							y+=(map.lScrollY/32);

							map.shadow_map[x][y] = game.iShadowNumber;
						}
						else
						{
							int ddx, ddy;
							ddx=mouse_x/32;
							ddy=mouse_y/32;
							
							ddx*=32;
							ddy*=32;

							// Increase (because editor = 160 (=5x32) + 5 pixels = 165)
							ddx+=5;

							// Make sure scrolling is taken into account to!
							ddx-= (map.lScrollX)-((int)(map.lScrollX/32)*32);
							ddy-= (map.lScrollY)-((int)(map.lScrollY/32)*32);

							rectfill(bmp_screen, ddx+2, ddy+2, ddx+34, ddy+34, makecol(0,0,0));						
							draw_trans_sprite(bmp_screen, (BITMAP *)shadows[game.iShadowNumber].dat, ddx, ddy);
							
						}
					}				
				}
		}


	}
}