Welcome to the new Friends-of-FPC!

Here you can find all kinds of information about the FreePascal Compiler. We have many tutorials and howtos as well as a selection of tools to help you with your programming. We also have some example codes for you. And if you want to contribute some information/ sources/ tools yourself you can do so.
Also we have finally relaunched the FoFPC forum. It's your chance for some Q&A about everything FreePascal.

Friends-of-FPC

Tutorials: Learn how to code with FreePascal.

Source Codes: A collection of examples, miscellaneous source codes and open source stuff.

Tools and Help Files: Intro- duction of some tools that might help you with FPC.

Community

Forum: Ask or answer questions about the FreePascal Compiler, programming or just babble about coding.

Contribute! Contribute your own Tutorial, Source Codes or Tools and send them to us!

Website

About: Information about Friends-of-FPC.org.

OpenGL/ FPC - Chapter 4 - by Delax

I think this topic is something to get excited about. OpenGL stuff looks so much cooler if it is run in fullscreen mode. The catch is that fullscreen rendering implies more Windows API stuff. Let's get going.

The first thing we do is using the "fullscreen" variable. It contains the mode we want to run our program in. If true, then fullscreen, if not, then windowed.

This "fullscreen" mode is a simple window without title bar and edges with the size of the actual desktop. The hard part about this is changing the resolution of the desktop.

Let us assume a user is running his desktop with 1024x768 pixel but we want a 640x480 sized window. The window would open allright, but the rest of the desktop would still be visible. So we have to adjust the resolution of the desktop to the size of our window.

Windows has a list of all resolutions it's able to run on the users card. Don't forget: not all gfx cards manage the same resolutions. Stuff like 720x540 may only work on some cards. So the simpelest way to assure that the resolution is possible is to ask Windows if it is possible. If we find a suitable mode, then we change into it.

This list can be read with EnumDisplaySettings. Forget the first parameter. The second one is the row of the list. We want to look at every resolution possible, so we go through it one by one. The third parameter is a pointer to a structure that stores the information of the list.

We ask for a certain width/ height/ depth. If there is a mode with those attributes, we leave the REPEAT/ UNTIL loop. If we didn't find a suitable mode until we reached 255 list entries, we give up and print an error.

If we found our mode of choice we'll use ChangeDisplaySettings to change into it. We also hide the mouse cursor as we don't need it for our OGL programs.

   i := -1;
   repeat
     inc(i);
     EnumDisplaySettings(nil,i,@dmScreenSettings);
     if i > 255 then Throw('No suitable window mode found!');
   until (dmScreenSettings.dmPelsWidth = width) and (dmScreenSettings.dmPelsHeight = height)
	and (dmScreenSettings.dmBitsPerPel = bits);

	ChangeDisplaySettings(@dmScreenSettings,0);

The other method is just to tell Windows what you want and go for it.

  dmScreenSettings.dmSize := sizeof(dmScreenSettings);		// Size Of The Devmode Structure
  dmScreenSettings.dmPelsWidth := width;		// Selected Screen Width
  dmScreenSettings.dmPelsHeight := height;		// Selected Screen Height
  dmScreenSettings.dmBitsPerPel := bits;		// Selected Bits Per Pixel
  dmScreenSettings.dmFields := DM_BITSPERPEL OR DM_PELSWIDTH OR DM_PELSHEIGHT;

 if ChangeDisplaySettings(@dmScreenSettings,CDS_FULLSCREEN) <> DISP_CHANGE_SUCCESSFUL then begin
    ThrowError('Screen resolution is not supported by your gfx card!');
    WindowCreate := 0;
    Exit;
 end;

If our program is terminated we have to restore the desktop to it's former resolution. Don't forget that or the user will have your desktop resolution if he quits!

Lucky for us, there is a simple answer to this problem. If we call ChangeDisplaySettings with the parameter "0", we'll change into the normal mode that is stored in the registry. Not very gentleman, but it works. Oh, and we show the mouse cursor again.

procedure KillOGLWindow;
begin
  wglMakeCurrent( dcWindow, 0 );	// Kill Device Context
  wglDeleteContext( rcWindow );		// Kill Render Context
  ReleaseDC( hWindow, dcWindow );	// Release Window
  DestroyWindow( hWindow );		// Kill Window itself
end;

That leaves only the question of how to set the fullscreen mode in our program. Either you set it manually with fullscreen := true; or you leave the choice to the user. This can be managed through a simple "Yes/ No"-Messae Box.

 if MessageBox(0,'Fullscreen Mode?', 'Question!',MB_YESNO OR MB_ICONQUESTION) = IDNO 
  then fullscreen := false else fullscreen := true;

And that's it. Much Windows about nothing. Get the source code here.

Delax/ Sundancer Inc.
[delax@sundancerinc.de]

Back to previous page

Useful Links









Link to us