1
\$\begingroup\$

I've been adapting the NeHe OpenGL/Win32 code to be more object-oriented and I don't like the way some of the calls are structured.

The example has the following pseudo-structure:

Register window class
Change display settings with a DEVMODE
Adjust window rect
Create window
Get DC
Find closest matching pixel format
Set the pixel format to closest match
Create rendering context
Make that context current
Show the window
Set it to foreground
Set it to having focus
Resize the GL scene
Init GL

The points in bold are what I want to move into a rendering class (the rest are what I see being pure Win32 calls) but I'm not sure if I can call them after the Win32 calls.
Essentially what I'm aiming for is to encapsulate the Win32 calls into a Platform::Initiate() type method and the rest into a sort of Renderer::Initiate() method.

So my question essentially boils down to: "Would OpenGL allow these methods to be called in this order?"

Register window class
Adjust window rect
Create window
Get DC
Show the window
Set it to foreground
Set it to having focus

Change display settings with a DEVMODE
Find closest matching pixel format
Set the pixel format to closest match
Create rendering context
Make that context current
Resize the GL scene
Init GL

(obviously passing through the appropriate window handles and device contexts)

Thanks in advance.

\$\endgroup\$
2
  • \$\begingroup\$ Have you tried it, to see if it works? The only thing that I could see happening is the DC being affected by the DEVMODE. You also may want to accept some previous answers. \$\endgroup\$ Feb 25, 2011 at 10:52
  • \$\begingroup\$ I haven't built it yet I was exploring for advice before committing the time. Also 'working' isn't the result I need, for example it may work now then when I add complexity a serious error may occur due to not initialising the GL window in the appropriate manner. \$\endgroup\$ Feb 25, 2011 at 14:39

1 Answer 1

2
\$\begingroup\$

Off I don't see any reason why you couldn't, unless some of the operations depend on values produced by earlier operations. An easy way to "try it and see" is to simply re-order the calls where there are now, without actually implementing any of the extra abstraction layers you want. That should let you know if anything is trivially wrong that I'm forgetting.

If there is a more subtle problem, it is likely to be in an interaction between your new proposed setup and your other code that you have not shown, so we couldn't really diagnose it reliably now anyhow. You can always debug later -- don't be afraid of making the wrong decision now. That's why we refactor.

I wouldn't necessarily show the window before it is prepared for rendering, though.

You also might consider not separating the Win32 and GL calls, but just encapsulating them together. They are quite tightly tied together on a practical level -- especially if you are not currently developing your code against multiple OSs, it can neatly tie up your higher-level graphics API.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the pointers, the last point is certainly something im considering. \$\endgroup\$ Feb 26, 2011 at 8:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .