PDA

View Full Version : Beginners Problem



powerpad
09-10-2002, 09:46 AM
Can someone please help me, i don't know why my Code doesn't work i am not getting any rendering context's, choosepixelformat alway's fails.

I am using MS VC++ 6.0 on a PIII 450, Geforce1 System but that should not be the reason since my code doesn't get any valid
either software emulated or hardware pixelformat.
Please explain me my faults...
tnx in advance

#include "windows.h"
#include "gl/gl.h"

#define WNDCLASSNAME "WndClassName"
#define WNDNAME "WndName"
#define WNDX 0
#define WNDY 0
#define WNDWIDTH 640
#define WNDHEIGHT 480
#define WNDCOLORDEPTH 16
#define DEPTHBUFFER 16
#define ALPHABUFFER 0
#define STENCILBUFFER 8
#define ACCUMBUFFER 32
#define AUXILIARYBUFFER 0

HWND hMainWindow = NULL;
DEVMODE DevMode, OldScreen;
PIXELFORMATDESCRIPTOR pfd;
int nNumberOfPixelFormats = 0;
HDC hDC = NULL;
int pixelFormat;

LRESULT CALLBACK WindowProc(HWND hWnd,
UINT uMsg, WPARAM wParam, LPARAM lParam)
{
//local variables and identifiers
//main stuff
//process messages
switch(uMsg)
{
case WM_CREATE:
hDC = GetDC(hWnd);
if(hDC == NULL)
{
MessageBox(NULL, "Error", "Error", MB_OK);
return (0);
}
memset(&pfd, 0,
sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_SUPPORT_OPENGL |
PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
//here i get always 0 for pixelformat but why
pixelFormat = ChoosePixelFormat(hDC, &pfd);
if (pixelFormat == 0)
{
// Handle error here
MessageBox(NULL, "Failed to ChoosePixelFormat", "Error", MB_OK);
return (0);
}
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
//end
return (DefWindowProc(hWnd, uMsg, wParam,
lParam));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
//local variables and identifiers
WNDCLASSEX WndClass;
HWND hWnd;
MSG Msg;
//main stuff
//Fill WNDCLASS Structure
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.hbrBackground =
GetStockObject(BLACK_BRUSH);
WndClass.hCursor =
LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon =
LoadIcon(NULL, IDI_APPLICATION);
WndClass.hIconSm = NULL;
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WindowProc;
WndClass.lpszClassName = WNDCLASSNAME;
WndClass.lpszMenuName = NULL;
WndClass.style =
CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
//Register Window
if(!RegisterClassEx(&WndClass))
{
return (0);
}
//Create Window
hWnd = CreateWindowEx(0, WNDCLASSNAME, WNDNAME, WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
WNDX, WNDY, WNDWIDTH, WNDHEIGHT, NULL, NULL, hInstance, NULL);
if(hWnd == NULL)
{
return (0);
}
//Save Window into a Global
hMainWindow = hWnd;
//Main MessageLoop
while(1)
{
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)
!= 0)
{
//Message available
if(Msg.message == WM_QUIT)
{
break;
}
//Translate Message
TranslateMessage(&Msg);
//Dispatch Message
DispatchMessage(&Msg);
}
}
return (Msg.wParam);
}

[This message has been edited by powerpad (edited 09-10-2002).]

powerpad
09-11-2002, 04:12 AM
is there noone that knows why my piec'a code doesn't work

09-11-2002, 04:28 AM
Try a call to glGetError() before the ChoosePixelFormat() instruction.

Don't know why, but it worked in my case(!) ...

Mazy
09-11-2002, 04:47 AM
pfd.cDepthBits = 32;

Good luck finding a card with 32bit Z-buffer

set Color to 32 and depth to 24 then the coose might find a propery pixelformat

powerpad
09-11-2002, 08:55 AM
tnx to both of you, i'll try but should choosepixelformat not normally select the best-suiting pixelformat available, so it could somehow select a format with a lower depth value.
But anyway tnx for your solutions i'll try

powerpad
09-11-2002, 10:43 AM
okay i tried again, my programm stayed nearly the same i just altered
WM_CREATE:
//Pixelformatdescriptor with
16bit Depthbits
32bit Colorbits
//the rest remained the same

calling glGetError() returned me
GL_INVALID_OPERATION
but why i didn't make any opengl calls i just filled the PIXELFORMATDESCRIPTOR structure and that's it nothing else

powerpad
09-11-2002, 02:00 PM
now everything seems to work, i don't know why because i didn't change much maybe it was the 32bit depth buffer but why ask if erverything is working

Allan Walton
09-12-2002, 12:10 AM
Morning,

This is a warning to everyone as i had a nightmare of a problem which sounds disturbingly similar to this one.

if creating a Win32 shell program / testbed you must use a gl call somewhere in the program (even if it's just glGetString())

This is because ChoosePixelFormat uses gl internally but if your program doesn't have a gl call in it the "helpful" compiler doesnt link to the library so ChoosePixelFormat always fails.

adding a single gl call solves this.

Cheers

Allan

powerpad
09-12-2002, 01:55 AM
what do you mean by adding a call somewhere
something like this

//make a glCall before this happens
glGetError();
//init pixelformatdescriptor
int nPixel = ChoosePixelFormat(..);

or do you mean anywhere not necessarily direct before choosepixelformat(..)

tnx

Deiussum
09-12-2002, 04:17 AM
You got an error using glGetError before ChoosePixelFormat because every gl* call will fail until you actually set your GL context up.

Allan seems to be describing something different, where he just used the Chooose/Describe/SetPixelFormat stuff and no gl calls and the linker didn't include the opengl library, because those are actually in gdi32.lib and he had nothing used from opengl32.lib.

powerpad
09-12-2002, 06:33 AM
the glGetError() thing i already found out it was said in the help files
but thanx anyway