Archived

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

Daaark

Problem bringing up a window in Win32

Recommended Posts

Daaark    3553
I keep copying out the first program in the WINDOWS GAME PROGRAMMING FOR DUMMIES BOOK, and whenever I compile, the program ends right away instead of showing a window!!! Can someone tell me what's wrong?
#define WIN32_LEAN_AND_MEAN
#include 
#include 
#include 
#include 

#define WINDOW_CLASS_NAME "WNDCLASS1"

HWND main_window_handle = NULL;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	PAINTSTRUCT ps;
	HDC hdc;

	switch(msg)
	{
		case WM_CREATE:
		{
			return(0);
		} break;
		case WM_PAINT:
		{
			hdc = BeginPaint(hwnd,&ps);
			EndPaint(hwnd,&ps);
			return (0);
		} break;
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return(0);
		} break;
		default :break;
	}

	return(DefWindowProc(hwnd,msg,wparam,lparam));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASS winclass;
	HWND hwnd;
	MSG msg;
	
	winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	winclass.lpfnWndProc = WindowProc;
	winclass.cbClsExtra = 0;
	winclass.cbWndExtra = 0;
	winclass.hInstance = hInstance;
	winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	winclass.lpszMenuName = NULL;
	winclass.lpszClassName = WINDOW_CLASS_NAME;

	if (!RegisterClass(&winclass)) return 0;
	if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME,"TEST",WS_OVERLAPPED |WS_VISIBLE,
		0,0,320,200,NULL,NULL,hInstance,NULL))) return(0);
	
	main_window_handle = hwnd;

	while(1)
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message == WM_QUIT) break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

        //main game processing goes here
	}

	return(msg.wParam);

}
  
I hope that formats right --------- |GOT MALK?| --------- Edited by - felisandria on August 2, 2001 11:07:34 AM

Share this post


Link to post
Share on other sites
ratt1233    148
see if this helps...
your #includes should be...
#include
#include
#include
first use WNDCLASSEX instead of WNDCLASS ... thats what I learned
second you''ll need to add a few member flags to your windows class...
winclass.cbSize = sizeof(WNDCLASSEX); // before style
winclass.hCursor = LoadCursor(NULL,IDC_CURSOR); // after hIcon
winclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION); // last member

then change these...
when you register your window make it this
if(!RegisterClassEx(&winclass))
return(0);
when you create your window make it this
if (!(hwnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
"TEST",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, //you had WS_OVERLAPPED.. I''m not sure but use this instead//
0,0,
320,200,
NULL,
NULL,
hInstance,
NULL)))
return(0);

if this doesn''t help or is useless crap... e-mail me @ ratt1233@mindspring.com

Share this post


Link to post
Share on other sites
Daaark    3553


I commented out the default statement.. same thing happens,

and ratt1233, The includes you wrote (and I wrote) aren''t showing up on the screen... I had windows.h, windowsx.h, math.h, and stdio.h






---------
|GOT MALK?|
---------

Share this post


Link to post
Share on other sites
baumep    375
This is a working version with minimal changes.

#define WIN32_LEAN_AND_MEAN
#include "windows.h"
//#include "windowsx.h" //You don''t need those right now
//#include "stdio.h"
//#include "math.h"
// replace the " with the brackets (the browser interprets them as html-tags)

#define WINDOW_CLASS_NAME "WNDCLASS1"
HWND main_window_handle = NULL;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{ PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_CREATE:
{
return(0);
} break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return (0);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;
default:
{
return(DefWindowProc(hwnd,msg,wparam,lparam)); // NEW: I think it''s better to let the DefWindowProc handle all messages you don''t want to mess with
} break; // In your version default messages were unhandled producing errors
}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX winclass; // NEW: WNDCLASSEX
HWND hwnd;
MSG msg;

//NEW
ZeroMemory(&winclass, sizeof(winclass)); // This inits all values of the structure with zeros
winclass.cbSize = sizeof(winclass); // Important for version checking for windows
//NEW

winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;


if (!RegisterClassEx(&winclass)) // NEW: RegisterClassEx
return 0; // Your version failed here, because you forgot the two lines above
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME,"TEST",WS_OVERLAPPED |WS_VISIBLE,
0,0,320,200,NULL,NULL,hInstance,NULL)))
return(0);
main_window_handle = hwnd;
while(1)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//main game processing goes here
}
return(msg.wParam);
}

(To quit your app press "alt+F4" because it lacks the x-button

baumep

Share this post


Link to post
Share on other sites
Daaark    3553
Thanks baumep!

I''ll go try and get that to work

I guess I should toss WINDOWS GAME PROGRAMMING FOR DUMMIES in my trash I thought I would pick this stuff up quicker. I''ve programmed in Qbasic, and C with DJGPP/ALLEGRO before. That was easy This stuff is imposibble

Share this post


Link to post
Share on other sites
Daishim    244
Windows Game Programming For Dummies was written by Andre LaMothe also, but I would reccommend the Tricks Of The Windows Game Programming Gurus anyway.

Share this post


Link to post
Share on other sites
baumep    375
I don''t know which "DX stuff" you mean. It might even work if it''s based on the same basic app.
My suggestion would be trying to blit some bitmaps and put some pixels to the window (lookup BitBlt and SetPixelV) using standard GDI functions. Then create some sort of demo so yo can get experience with graphics/game programming in general before moving to something complex and confusing (especially for a beginner, like me like DX

baumep

Share this post


Link to post
Share on other sites
Daaark    3553
I''m that much of a newbie!

I''ve made games before!

I''ve made some text based RPGs, a LORD2(old bbs door game) clone and a 320x200 tile based rpg game in Qbasic.

And I had a working 320x200 tile based rpg game engine in C (djgpp/allegro)

I wanted to try and get one of my screen savers I made. How hard is it to set up a screen, and blit bitmaps with DX?

Share this post


Link to post
Share on other sites
baumep    375
Sorry, but I haven''t done much DX myself. Take a look at the "Game Programming Genesis"-Tutorials (if you haven''t already)in the resources area. There''s a nice step by step guide to Direct Draw. Shouldn''t be that hard.

baumep

Share this post


Link to post
Share on other sites