Skip to main content
gamedev.net
🔒 Locked

Window Class

Started by Armadon 5 replies 1.4k views
Original Post
Armadon
Armadon
Hi Guys, Share your views and comments on this windows class. If it works for you great. The CWindow.h //---------------------CWindow.h------------------------------// #ifndef __Window_H_ #define __Window_H_ #include <windows.h> class CWindow { public: HWND hWnd; HINSTANCE hInstance; int Width, Height; void Create(HINSTANCE hinstance, char* title, char* classname, int width, int height); int CheckMessages(); static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); }; #endif CWindow.cpp //---------------------CWindow.h------------------------------// #include "CWindow.h" void CWindow::Create(HINSTANCE hinstance, char* title, char* classname, int width, int height) { hInstance = hinstance; Width = width; Height = height; WNDCLASSEX winclass; winclass.cbSize = sizeof(WNDCLASSEX); winclass.style = CS_OWNDC; winclass.lpfnWndProc = WndProc; winclass.cbClsExtra = NULL; winclass.cbWndExtra = NULL; winclass.hInstance = hInstance; winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); winclass.hCursor = LoadCursor(NULL, IDC_ARROW); winclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); winclass.lpszMenuName = NULL; winclass.lpszClassName = classname; winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&winclass); hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, classname, title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, Width, Height, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); } int CWindow::CheckMessages() { MSG msg; while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); if(msg.message == WM_QUIT) return -1; } return 0; } LRESULT CALLBACK CWindow::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CLOSE : DestroyWindow(hwnd); break; case WM_DESTROY : PostQuitMessage(0); break; default : break; } return DefWindowProc(hwnd, msg, wParam, lParam); } If there are any problems with the class please tell me what they are... so we can give the beginners a chance to concentrate more on DirectX than all the bulky window code. Basically it's for playing around with Direct3d while learning the API. it runs in window mode and take the width and height from the user. The game loop is then initialized as this. //-----------------------Game Loop----------------------------// CWindow Window; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR CmdLine, int CmdShow) { Window.Create(hInstance, "Window", "mywindow", 640, 480); while(1) { if(Window.CheckMessages() == -1) break; } //-----------------------Game Loop----------------------------// ---DirectXSA--- Lachdannan Corp. www.lachdannan.za.net [edited by - Armadon on April 21, 2004 12:39:31 AM] [edited by - Armadon on April 21, 2004 12:53:14 AM]
Programmer16
Programmer16
You need to change the way you call CheckMessages() if you're going to be doing render.

Like this maybe:
while(true)
{
if(g_cWnd.CheckMessages() != -1)
{
// Idle time, get to rendering!

}
else
{
// Nasty error, better close

}
}


Other than that and your variable naming (my opinion ), I say great job!


/*
I use DirectX 9 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/

[edited by - Programmer16 on April 22, 2004 1:18:09 AM]
ChaosEngine
ChaosEngine
Two things.
1: public member variables are the spawn of satan. First rule of encapsulation is make everything private, then see what needs to be public.

2: if you''re posting code use either [ source ] and [ /source ] or [ code ] and [ /code ] tags (remove the spaces). just makes it more readable
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Armadon
Armadon
ChaosEngine, there is a reason for using public variables.
I want to be able to use it in my Direct3d Initializing...
Since I need to point to the handle of the window and I need Width and the Height of the window for D3DXMatrixPerspectiveFovLH();

Though the other variables are un called for. I do understand what you are saying
Barguast
Barguast
Use an accessor function to read private variables - it''s better in the long run.
Using Visual C++ 7.0
Galapaegos
Galapaegos
Armadon,

Instead of making the callback function static, you should use a thunk. That way if you keep track of anything in your class, you have direct access to it. do a google search on thunk and you''ll lots of hits and src to make it work.

-brad
-brad
clayasaurus
clayasaurus
I agree with Barguast, and IMO i tend to design classes like this

class cClass
{
public:
process()
inline width(); // return width
inline width(int); // set width

private:
int m_width;
char *mp_name;
}


it might seem like a pain, but IMO it is easier in the long run and allows you to totally mess with variable names and types etc. without having to recompile source code. It is also easier to spot member variables like this, and pointers if you like.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.