hehe annoying invalidate rect question for win32 here

Started by
6 comments, last by phil05 19 years, 7 months ago
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,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, 0, 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);
}

Advertisement
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.
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?
w00t! Nevermind, I put some basic c++ function passing stuff and it worked :) Thanks for pointing that out.
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.
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.
.
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.
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
ok thanks. I'm using an inline function for now since its about 30 lines. I'm figuring it will load once then.

This topic is closed to new replies.

Advertisement