Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Elfs1der

All programs I make crash??!?!

This topic is 5230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Everytime I make a win32 application even if its the simplest one it crashes, i get no errors or warnings and when it runs I get those stupid "Do you want to send report" things poping up! Heres my code:
/*
 *	NewGame
 */

#include <windows.h>
#include <stdlib.h>

#define WINDOWTITLE "Newgame"
#define WINDOWCLASS "wndClass"

HWND hwndMain;
HINSTANCE hInstMain;

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	// Check the messages being sent

	switch(uMsg)
	{
		case WM_DESTROY:
		{
			// Tell the application we are quitting

			PostQuitMessage(0);
			return(0);
		} break;
		case WM_PAINT:
		{
			PAINTSTRUCT ps;

			hdc = BeginPaint(hwnd, &ps);
			EndPaint(hwnd, &ps);

			return(0);
		} break;
	}
	// Pass away any other messages to default message handler

	return(DefWindowProc(hwnd, uMsg, wParam, lParam));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	// Assign instance to global variable

	hInstMain = hInstance;
	// Message structure

	MSG msg;
	// Create window class

	WNDCLASSEX wcx;
	
	wcx.cbSize = sizeof(WNDCLASSEX);
	wcx.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
	wcx.lpfnWndProc = WndProc;
	wcx.cbClsExtra = 0;
	wcx.cbWndExtra = 0;
	wcx.hInstance = hInstMain;
	wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wcx.lpszClassName = WINDOWCLASS;
	wcx.hIconSm = NULL;
	
	// register the window class

	if(!RegisterClassEx(&wcx))
	{
		return(0);
	}

	hwndMain = CreateWindowEx(0, WINDOWCLASS, WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_CAPTION | WS_VISIBLE, 
							  0, 0, 320, 240, NULL, NULL, hInstMain, NULL);

	if(!hwndMain)
	{
		return(0);
	}
	
	for(;;)
	{
		// Look for a message

		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			// There is a message

			// Check that we aren''t quitting

			if(msg.message == WM_QUIT)
			{
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	//	gameLoop();

	}
	//gameDone();

	return(msg.wParam);
}

Share this post


Link to post
Share on other sites
Advertisement
You''ve done atleast 2 mistakes here.
First of all, what makes your code crash is that you haven''t set the menuname to NULL. Secondly, the classname is supposed to be a string, so use "WINDOWCLASS" instead. I recommend using another name for it though. Use the string in the createwindow aswell.
Another small thing; instead of the for(;, use a while(PeekMessage(&msg,NULL,0,0,false)!=WM_QUIT). This ensures the loop isn''t run at all if the first message in the queue is a WM_QUIT. Otherwise, small bugs can creep in, that you won''t notice.

Share this post


Link to post
Share on other sites
quote:
Original post by tok_junior
Secondly, the classname is supposed to be a string, so use "WINDOWCLASS" instead. I recommend using another name for it though.

Read the top of the window...
#define WINDOWCLASS "wndClass" 



If a plant cannot live according to its nature, it dies; so a man.

(princ(substitute #\Space #\0(format()"~36R"5688852237040631986030796883)))

Share this post


Link to post
Share on other sites
quote:
Original post by ZealousElixir
Thou shalt set all properties of thy window class, heathen!


I dont know why but I found this quote to be amusing. Its like the 10 commandments of Windows programming.

"Give a man a fish and he will eat for a day, drown a man in the water and the fish will eat for a week!

Share this post


Link to post
Share on other sites
Argh, beaten by half a minute. Oh well, ''least I got style.

@tok: WM_QUIT can never be the first message in the queue (get min/max info, then nccreate come first I think), and even if it were, the loop would break before the game logic was executed, as written. You should also be aware that the return value of PeekMessage is not Msg.message, but a Boolean indicator of whether messages were processed.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!