// JP opened flex table

Click to See Complete Forum and Search --> : Class Problem [MORE]


Drezard
February 20th, 2007, 09:20 PM
Hello, I'm trying to write an engine. I've almost got the win32 code working.

Okay, Heres my code:

SystemCode.h


// SystemCore.h
// Last updated: 20 Feb 2007, Drezard

#include <windows.h> // Standard windows include file
#include <windowsx.h> // Standard windows include file

#ifndef SYSTEMCORE_H // Checks if the header has already been called
#define SYSTEMCORE_H // If it hasnt then it will call it with this code

class DrezSystem; // Declare the class

static LRESULT CALLBACK AppWindowProc(HWND hWnd, // Declare a WNDPROC to handle Wm_Quit
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
static DrezSystem* g_pApp = 0; // Pointer to the class

class DrezSystem { // Class prototype

private: // Declare all the private members

WNDCLASSEX m_Wcex; // Declare our windows struct
HINSTANCE m_hInst; // Declare a variable for our programs Instance
HWND m_hWnd; // Declare a variable for our programs Handle

public: // Declare all the public members

DrezSystem(); // Constructor
~DrezSystem(); // Destructor

bool run(); // Registers the class

virtual LRESULT CALLBACK MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

return DefWindowProc(hWnd, uMsg, wParam, lParam);

}

};

#endif


SystemCore.cpp

// SystemCore.cpp
// Last updated: 20 Feb 2007, Drezard

#include "SystemCore.h"

DrezSystem::DrezSystem(){ // Constructor

g_pApp = this; // Set g_aApp pointer to the class
m_hInst = GetModuleHandle(0); // Set the programs instance
m_hWnd = NULL; // Set the programs handle


m_Wcex.cbSize = sizeof(WNDCLASSEX); // Size of the windows class
m_Wcex.style = CS_CLASSDC; // Style
m_Wcex.lpfnWndProc = AppWindowProc; // No Win Proc yet
m_Wcex.cbClsExtra = 0;
m_Wcex.cbWndExtra = 0;
m_Wcex.hInstance = m_hInst; // Hinstance
m_Wcex.hIcon = LoadIcon(0, IDI_APPLICATION); // Load the icon
m_Wcex.hCursor = LoadCursor(0, IDC_ARROW); // Load the cursor
m_Wcex.hbrBackground = 0;
m_Wcex.lpszMenuName = 0;
m_Wcex.lpszClassName = L"TEST";
m_Wcex.hIconSm = LoadIcon(0, IDI_APPLICATION);


}

DrezSystem::~DrezSystem(){ // Destructor

m_hInst = NULL; // Set the programs instance (Memory Release)
m_hWnd = NULL; // Set the programs handle (Memory Release)

}


LRESULT CALLBACK AppWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(true);
break;
default:
return g_pApp->MsgProc(hWnd, uMsg, wParam, lParam);
break;
}

return 0;
}

bool DrezSystem::run() { // Registers the class

if(!(RegisterClassEx(&m_Wcex))) { // If Registering the Class fails

return false; // Return false

}

UnregisterClass(L"TEST", m_hInst); // Unregister the class (Memory Release)

return true; // Return true

m_hWnd = CreateWindowEx(NULL, // Set the handle
L"TEST", // Our class
L"Our First Windowed Program", // Window title
WS_OVERLAPPEDWINDOW, // Window style
300, // X-position of the window
300, // Y-position of the window
500, // Width of the window
400, // Height of the window
NULL, // Parent Window
NULL, // Menus
m_hInst, // Application Handle
NULL); // Mutliple Windows

ShowWindow(m_hWnd, nCmdShow); // Show the Window

MSG msg;

while(GetMessage(&msg, NULL, 0, 0))
{

TranslateMessage(&msg);

DispatchMessage(&msg);

}
};




WinMain.cpp

#include "SystemCore.h"
#include <windows.h>
#include <windowsx.h>

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

DrezSystem SystemClass;

SystemClass.DrezSystem();

bool SystemClass.run();

SystemClass.~DrezSystem();

}


Now, my problem with my code is... In Win32 Code u declare the variable nCmdShow in the WinMain function. Now I need to
use that variable again in my run function. How would I use that variable in my class?

- Cheers, Daniel

TheCPUWizard
February 20th, 2007, 09:20 PM
Daniel,

How many times have people asked you to use code tags when posting???? :mad:

Drezard
February 20th, 2007, 09:45 PM
I did now...

TheCPUWizard
February 20th, 2007, 09:53 PM
Thanks, now your code is readable :)

I am looking at it right now....

TheCPUWizard
February 20th, 2007, 09:59 PM
1) Pass it as a parameter to your run method
2) Make it a property of your DrezSystem.

also your code is seriously flawed. One never calls constructors/destructors directly. They are called implicitly when the object is created/destroyed:

The following is sufficient:

{
DrezSystem SystemClass;
bool someValue = SystemClass.run();
}

Drezard
February 20th, 2007, 10:11 PM
Hello, I've debugged the code now. There are no errors now. But, it doesn't give me the exact output I wanted. I wanted it to output a blank Win32 Window. Now, heres my updated code:

SystemCore.h:
// SystemCore.h
// Last updated: 20 Feb 2007, Drezard

#include <windows.h> // Standard windows include file
#include <windowsx.h> // Standard windows include file

#ifndef SYSTEMCORE_H // Checks if the header has already been called
#define SYSTEMCORE_H // If it hasnt then it will call it with this code

class DrezSystem; // Declare the class

static LRESULT CALLBACK AppWindowProc(HWND hWnd, // Declare a WNDPROC to handle Wm_Quit
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
static DrezSystem* g_pApp = 0; // Pointer to the class

class DrezSystem { // Class prototype

private: // Declare all the private members

WNDCLASSEX m_Wcex; // Declare our windows struct
HINSTANCE m_hInst; // Declare a variable for our programs Instance
HWND m_hWnd; // Declare a variable for our programs Handle

public: // Declare all the public members

DrezSystem(); // Constructor
~DrezSystem(); // Destructor

bool run(int nCmdShow); // Registers the class

virtual LRESULT CALLBACK MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

return DefWindowProc(hWnd, uMsg, wParam, lParam);

}

};

#endif


SystemCore.cpp
// SystemCore.cpp
// Last updated: 20 Feb 2007, Drezard

#include "SystemCore.h"

DrezSystem::DrezSystem(){ // Constructor

g_pApp = this; // Set g_aApp pointer to the class
m_hInst = GetModuleHandle(0); // Set the programs instance
m_hWnd = NULL; // Set the programs handle


m_Wcex.cbSize = sizeof(WNDCLASSEX); // Size of the windows class
m_Wcex.style = CS_CLASSDC; // Style
m_Wcex.lpfnWndProc = AppWindowProc; // No Win Proc yet
m_Wcex.cbClsExtra = 0;
m_Wcex.cbWndExtra = 0;
m_Wcex.hInstance = m_hInst; // Hinstance
m_Wcex.hIcon = LoadIcon(0, IDI_APPLICATION); // Load the icon
m_Wcex.hCursor = LoadCursor(0, IDC_ARROW); // Load the cursor
m_Wcex.hbrBackground = 0;
m_Wcex.lpszMenuName = 0;
m_Wcex.lpszClassName = L"TEST";
m_Wcex.hIconSm = LoadIcon(0, IDI_APPLICATION);


}

DrezSystem::~DrezSystem(){ // Destructor

m_hInst = NULL; // Set the programs instance (Memory Release)
m_hWnd = NULL; // Set the programs handle (Memory Release)

}


LRESULT CALLBACK AppWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(true);
break;
default:
return g_pApp->MsgProc(hWnd, uMsg, wParam, lParam);
break;
}

return 0;
}

bool DrezSystem::run(int nCmdShow) { // Registers the class

if(!(RegisterClassEx(&m_Wcex))) { // If Registering the Class fails

return false; // Return false

}

UnregisterClass(L"TEST", m_hInst); // Unregister the class (Memory Release)

return true; // Return true

m_hWnd = CreateWindowEx(NULL, // Set the handle
L"TEST", // Our class
L"Our First Windowed Program", // Window title
WS_OVERLAPPEDWINDOW, // Window style
300, // X-position of the window
300, // Y-position of the window
500, // Width of the window
400, // Height of the window
NULL, // Parent Window
NULL, // Menus
m_hInst, // Application Handle
NULL); // Mutliple Windows

ShowWindow(m_hWnd, nCmdShow); // Show the Window

MSG msg;

while(GetMessage(&msg, NULL, 0, 0))
{

TranslateMessage(&msg);

DispatchMessage(&msg);

}
};




WinMain.cpp

#include "SystemCore.h"
#include <windows.h>
#include <windowsx.h>

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

DrezSystem mySystemObj;

mySystemObj.run(nCmdShow);

}


Whats wrong? Why won't it output a window?

- Cheers, Daniel

Notsosuperhero
February 21st, 2007, 04:26 AM
In the run() function, you return true before CreateWindowEx() is called so it will never get called.

So move the return true to the bottom of the function and also put UnregisterClass right before the return at the bottom.

On another note, if you want a white window(when I run it the window gets messed up with the window behind it) in you WNDCLASSEX structure, set hbrBackground to

m_Wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);


Regards
-Joe

Drezard
February 22nd, 2007, 03:21 AM
Okay, I tried to edit it abit now its fully gone from me. Its giving me a million errors that don't make sense...


c:\documents and settings\daniel\my documents\engine\systemcore\systemcore\systemcore\systemcore.h(10) : error C2236: unexpected 'class' 'SystemCore'. Did you forget a ';'?
c:\documents and settings\daniel\my documents\engine\systemcore\systemcore\systemcore\systemcore.h(16) : error C2143: syntax error : missing ';' before '*'
c:\documents and settings\daniel\my documents\engine\systemcore\systemcore\systemcore\systemcore.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\daniel\my documents\engine\systemcore\systemcore\systemcore\systemcore.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\documents and settings\daniel\my documents\engine\systemcore\systemcore\systemcore\winmain.cpp(16) : error C2146: syntax error : missing ';' before identifier 'SysClass'
c:\documents and settings\daniel\my documents\engine\systemcore\systemcore\systemcore\winmain.cpp(16) : error C2065: 'SysClass' : undeclared identifier
c:\documents and settings\daniel\my documents\engine\systemcore\systemcore\systemcore\winmain.cpp(19) : error C2228: left of '.Run' must have class/struct/union


And thats just the first few...

Here's my code:

SystemCore.h

// SystemCore.h
// Last updated: 21 Feb 2007, Drezard

#include <windows.h> // Standard windows include file
#include <windowsx.h> // Standard windows include file

#ifndef SYSTEMCORE_H // Checks if the header has already been called
#define SYSTEMCORE_H // If it hasnt then it will call it with this code

class SystemCore; // Declare the class

static LRESULT CALLBACK AppWindowProc(HWND hWnd, // Declare a WNDPROC to handle Wm_Quit
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
static SystemCore* g_pApp = 0; // Pointer to the class

class SystemCore { // Class prototype

public: // Declare all the public members

WNDCLASSEX m_Wcex; // Declare our windows struct
HINSTANCE m_hInst; // Declare a variable for our programs Instance
HWND m_hWnd; // Declare a variable for our programs Handle

SystemCore(); // Constructor
~SystemCore(); // Destructor

bool Run(int nCmdShow, int scrn_w, int scrn_l); // Registers the class

virtual LRESULT CALLBACK MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

return DefWindowProc(hWnd, uMsg, wParam, lParam);

}

};

#endif


And heres SystemCore.cpp

// SystemCore.cpp
// Last updated: 21 Feb 2007, Drezard

#include "SystemCore.h"

SystemCore::SystemCore(){ // Constructor

g_pApp = this; // Set g_aApp pointer to the class
m_hInst = GetModuleHandle(0); // Set the programs instance
m_hWnd = NULL; // Set the programs handle


m_Wcex.cbSize = sizeof(WNDCLASSEX); // Size of the windows class
m_Wcex.style = CS_CLASSDC; // Style
m_Wcex.lpfnWndProc = AppWindowProc; // No Win Proc yet
m_Wcex.cbClsExtra = 0;
m_Wcex.cbWndExtra = 0;
m_Wcex.hInstance = m_hInst; // Hinstance
m_Wcex.hIcon = LoadIcon(0, IDI_APPLICATION); // Load the icon
m_Wcex.hCursor = LoadCursor(0, IDC_ARROW); // Load the cursor
m_Wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
m_Wcex.lpszMenuName = 0;
m_Wcex.lpszClassName = L"TEST";
m_Wcex.hIconSm = LoadIcon(0, IDI_APPLICATION);


}

SystemCore::~SystemCore(){ // Destructor

m_hInst = NULL; // Set the programs instance (Memory Release)
m_hWnd = NULL; // Set the programs handle (Memory Release)

}


LRESULT CALLBACK AppWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(true);
break;
default:
return g_pApp->MsgProc(hWnd, uMsg, wParam, lParam);
break;
}

return 0;
}

bool SystemCore::Run(int nCmdShow, int scrn_w, int scrn_l) { // Registers the class

if(!(RegisterClassEx(&m_Wcex))) { // If Registering the Class fails

return false; // Return false

}

UnregisterClass(L"TEST", m_hInst); // Unregister the class (Memory Release)

m_hWnd = CreateWindowEx(NULL, // Set the handle
L"TEST", // Our class
L"Our First Windowed Program", // Window title
WS_OVERLAPPEDWINDOW, // Window style
300, // X-position of the window
300, // Y-position of the window
scrn_w, // Width of the window
scrn_l, // Height of the window
NULL, // Parent Window
NULL, // Menus
m_hInst, // Application Handle
NULL); // Mutliple Windows

ShowWindow(m_hWnd, nCmdShow); // Show the Window

return true; // Return true

};





And heres WinMain (My tester):


#include "RenderDevice.h"
#include "SystemCore.h"
#include <windows.h>
#include <windowsx.h>

int Scrn_W = 640;
int Scrn_L = 480;

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

DrezRenderDevice RenDevClass;
SystemCore SysClass;


SysClass.Run(nCmdShow, Scrn_W, Scrn_L);

RenDevClass->InitD3D(HWND SysClass.m_hWnd);

while(true) {

MSG msg;

DWORD starting_point = GetTickCount();

while(GetMessage(&msg, NULL, 0, 0))
{

TranslateMessage(&msg);

DispatchMessage(&msg);

}


while ((GetTickCount() - starting_point) < 25);

}

RenDevClass->CleanD3D(void);

}


- Cheers, Daniel

Whats wrong?

Drezard
February 22nd, 2007, 11:59 PM
Please help!

TheCPUWizard
February 23rd, 2007, 12:25 AM
Do people like you prefer to make it hard on themselves??

You did good. All of your code is in code tags.

Not we are looking at a bunch of error messages, and NO WAY TO CORRELATE them to the code.....

MrViggy
February 23rd, 2007, 01:58 PM
C2236 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/C2236.asp)
C2143 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/C2143.asp)
C4430 (http://msdn2.microsoft.com/en-us/library/ms173696.aspx)
C2146 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/C2146.asp)
C2065 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/C2065.asp)
C2228 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/C2228.asp)

Viggy

//JP added flex table