Sign in to follow this  
phil05

hehe annoying invalidate rect question for win32 here

Recommended Posts

I execute my program, and the function loads the landscape fine, but its off the window and is located at 0, 0 on the screen (outside from the window). wtf.. is it a invalidaterect() function I need to call, and how would I do that? Pic: http://phil.webula.net/pic.JPG

// PREPROCESSOR DIRECTIVES
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include "resource.h"

// GLOBALS
#define WIN32_LEAN_AND_MEAN
const char g_szClassName[] = "myClassWindow";
bool keys[256];
bool Quit = false;		// False by default
HINSTANCE hInst;
HWND hWnd;

// PROTOTYPES
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
void LoadMap(int Map);

// WINMAIN
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hWnd;
	MSG	Msg;
	hInst = hInstance;

	// Background Color on Parent Window
	LOGBRUSH Brush;
	Brush.lbColor	= RGB(0, 90, 0);
	Brush.lbHatch	= 0;
	Brush.lbStyle	= BS_SOLID;
	HBRUSH hBrush	= CreateBrushIndirect(&Brush);

	// Registering Class Window
	wc.cbClsExtra		= 0;
	wc.cbSize			= sizeof(WNDCLASSEX);
	wc.cbWndExtra		= 0;
	wc.hbrBackground	= hBrush;
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hIcon			= LoadIcon(hInstance, (LPTSTR)IDI_LOKICON);
	wc.hIconSm			= LoadIcon(hInstance, (LPTSTR)IDI_LOKICON);
	wc.hInstance		= hInstance;
	wc.lpfnWndProc		= WndProc;
	wc.lpszClassName	= g_szClassName;
	wc.lpszMenuName		= (LPTSTR)IDR_MENU1;
	wc.style			= CS_HREDRAW | CS_VREDRAW;

	// Did registering the class window fail? If so, alert user and close application.
	if (!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Registering Class Window Failed!", "Error...", MB_OK | MB_ICONERROR);
		return 0;
	}

	// Creating Parent Window
	if (! (hWnd = CreateWindowEx(NULL, g_szClassName, "The Legends of Kylen", WS_MINIMIZEBOX | WS_BORDER | 
		WS_CAPTION | WS_VISIBLE | WS_SYSMENU, 0, 0, 1024, 768, NULL, NULL, hInstance, NULL)))
		{
			MessageBox(NULL, "Creating Parent Window Failed!", "Error...", MB_OK | MB_ICONERROR);
			return 0;
		} 

	// Show & Update Window
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Message & Game Loop
	while (!Quit)
	{
		if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			if (Msg.message == WM_QUIT)
			{
				Quit = true;
			}
			else
			{
				TranslateMessage(&Msg);
				DispatchMessage(&Msg);
			}
		}
		else
		{
			/* IF USER HITS ESCAPE, SET QUIT TO TRUE. */
			if (keys[VK_ESCAPE])										
			{
				if (MessageBox(NULL, "Are you sure you want to exit?", "Exiting Game...", MB_YESNO | 
						MB_ICONQUESTION) == IDYES)
					Quit = true;
				else
					keys[VK_ESCAPE] = false;
			}
		
		
			/* LOAD CURRENT MAP */
			LoadMap(1);
		
		
		
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////

	return Msg.wParam;
}


// WINDOW PROCEDURE
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
		case WM_CREATE:	
			{
				// Centering Parent Window
				RECT cRect;
				GetWindowRect(hWnd, &cRect);
				int xSize = cRect.right - cRect.left;
				int ySize = cRect.bottom - cRect.top;
				int xx	  = (GetSystemMetrics(SM_CXSCREEN) - xSize) / 2;
				int yy	  = (GetSystemMetrics(SM_CYSCREEN) - ySize) / 2;
				SetWindowPos(hWnd, 0, xx, yy, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
			}
			break;

		case WM_KEYDOWN:
			{
				// Pressing Key Down
				keys[wParam] = true;
			}
			break;

		case WM_KEYUP:
			{
				// Pressing Key Up
				keys[wParam] = false;
			}
			break;

		case WM_COMMAND:
			{
				switch (LOWORD(wParam))
				{
					case IDR_FILEEXIT: 
						{
							/* IF USER CLICKS 'EXIT' ON MENU, EXIT PROGRAM. */
							if (MessageBox(NULL, "Are you sure you want to exit?", "Exiting Game...", 
								MB_YESNO | MB_ICONQUESTION) == IDYES)
									PostQuitMessage(WM_QUIT);
							else
								return TRUE;
						}
						break;
				}
			}
			break;

		case WM_CLOSE:   DestroyWindow(hWnd);
						 break;
		case WM_DESTROY: PostQuitMessage(WM_QUIT);
						 break;
		default:		 return DefWindowProc(hWnd, Msg, wParam, lParam);
	}

	return 0;
}



		

void LoadMap(int Map)
{
	HDC hDC, hDCGrass;
	HBITMAP bmpGrass;

	// Declaring Landscape Values
	enum LANDSCAPE { GRASS };

	// Bitmap: Grass
	hDC = GetDC(hWnd);

	bmpGrass = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_GRASS));
	hDCGrass = CreateCompatibleDC(hDC);
	SelectObject(hDCGrass, bmpGrass);

	switch (Map)
	{
		case 1:
			{
				/* HILLCREST VILLAGE */
				int HillcrestVillage[19][25] =
				{
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
					{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
				};

				for (int y = 0; y < 19; y++)
				{
					for (int x = 0; x < 25; x++)
					{
						if (HillcrestVillage[y][x] == GRASS)
						{
							BitBlt(hDC, x*32, y*32, 32, 32, hDCGrass, 0, 0, SRCCOPY);
						}
					}
				}

				DeleteDC(hDCGrass);
				DeleteObject(bmpGrass);

			}
			break;


	}

	ReleaseDC(hWnd, hDC);
}

Share this post


Link to post
Share on other sites
Make sure your hWnd isn't NULL. It's undocumented by MS, but you get a device context to the fullscreen by calling GetDC with a NULL argument.


edit: I noticed you declared your hWnd twice, both globaly and inside of winmain. Maybe this is your problem.

Share this post


Link to post
Share on other sites
Okay. I take out the global hWnd, but I get this message...

(191) : error C2065: 'hWnd' : undeclared identifier
Error executing cl.exe.

How can I get hWnd to transfer over to the function?

Share this post


Link to post
Share on other sites
I'm figuring each time it calls this function, its constantly reloading the bitmaps. Is this how games are done? It seems to be slow sometimes when exiting because of this.

Share this post


Link to post
Share on other sites
Simple games will generally load all resources for the game once. More complex games that need more resources than can fit in available memory need to do more sophisticated resource management.

Share this post


Link to post
Share on other sites
Quote:
Original post by philvaira
I'm figuring each time it calls this function, its constantly reloading the bitmaps. Is this how games are done? It seems to be slow sometimes when exiting because of this.


this is exactly what you don't want to do. load on WM_CREATE, or before the message loop, draw in WM_PAINT and call InvalidateRect() on a timer (google SetTimer and WM_TIMER). global bitmaps are often used. hope this helps.

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