Using FreeGlut with Code::Blocks

From Code::Blocks
Revision as of 05:16, 20 August 2015 by JackDawson (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Written By John Mieske ( Also known by his game name as JackDawson 23:43, 10 July 2011 (CEST) )

A little Info on Glut32 and FreeGlut

The original Glut32 written by Mark Kilgard was ported over by Nate Robins. And you can get the ported Glut32 Binaries if you want to see it.

NOTE : The latest version of the Glut32 library is 3.7.6 ( Dated Nov 8, 2001 )

This is the freeglut download links.

in that link you will see the title : Prepackaged Releases and the first link takes you to the binaries to be downloaded on this link

Its in there where you will see the MinGW download package that Is described in this tutorial. ( NOTE : Latest Binary release is dated May 11th 2013 known as Martin Payne's Windows binaries. )

Download freeglut 2.8.1‑1 for MinGW -------->

This was in reference to Windows and Linux packages according to their website.

When Glut32 was ported over it stopped being updated in 2001. So some folks decided to recreate it and named it FreeGlut. The interesting part is freeGlut and Glut32 can be installed just about the same way for Code::Blocks, with the exception that you would not have to edit the two scripts if your installing Glut32. FreeGlut and Glut32 are a set of functions for use with OpenGL. It has made graphic software a little easier to code when it comes to programming OpenGL with 3D in mind.

Operating System Info

This tutorial was demonstrated to work under as well as assumes that your using Microsoft Windows 7 64-Bit. However, it should work on any windows system ( as long as Code::Blocks and OpenGL are supported on that Operating System ) as well as Linux. In this tutorial you will see the path reference as the 64-Bit style of the folder ( x86 ).

An example of what I mean is : C:\Program Files (x86)\CodeBlocks\

On a 32-Bit Windows system you will see it as : C:\Program Files\CodeBlocks\

Notice the ( x86 ) is missing.

EDIT ( December 4th 2011 ) : Because I am constantly being asked why they cannot see the files to edit that I listed below, well keep in mind its about file extensions that are hidden. You can change your extensions to show up in your Windows Explorer ORGANIZE. So its ORGANIZE --> FOLDER AND SEARCH OPTIONS --> VIEW ( TAb ) and then you should see where its saying to hide the file extensions. You do NOT want to hide your extensions except your protected system files. Leave those hidden.



What You Need To Download

1.) Code::Blocks You can install the default settings or check mark ALL boxes and install everything. Either way, it's up to you. I prefer to install everything just in case.

2.) MinGW (NOTE : This is optional )

3.) FreeGlut Do NOT get the source code. GET THE BINARIES. [ Example : ] ( Latest stable release dated 2013 )

Think of freeglut as if it was the Glut32 Binaries, which the original source code to Glut32 was never available since its proprietary. But once you get the GLUT demo working, you can play with the freeglut source code from their website. You can however still play with the Ported Glut32 source code. It's latest stable release was back in 2001 though.


Once you install Code::Blocks, then you can download and unzip the freeglut ZIP file, which will contain this..

Freeglut 1.jpg

Move all of those files and folders into the C:\Program Files (x86)\CodeBlocks\mingw folder. ( This is the folder where you see Include and Lib folders. This is important. ) If it asks you to overwrite folders, that's OK, say yes. It will not overwrite anything important.

Now copy, do not move, just copy the freeglut.dll file that should currently be in your C:\Program Files (x86)\CodeBlocks\minGW folder into your C:\Windows folder. The reason to copy this into that folder is because C:\Windows is in your path, and to keep the confusion down, It is easier to just copy it to there. You can always change it later. It would be highly advised that you add the C:\Program Files (x86)\CodeBlocks\MinGW\bin folder to your path as well.

So now that you have copied the correct files and are ready to setup Code::Blocks, go to this folder : C:\Program Files (x86)\CodeBlocks\share\CodeBlocks\templates\wizard\glut and in there you will see a file named wizard.script. Its this file where it is looking for Glut32. You just simply replace any reference to Glut32 with freeglut. And it really is that simple.

NOTE : USE NOTEPADD or NOTEPADD ++, do Not use Microsoft word or WordPad.

Here is what it should look like.

The wizard.script File

// GLUT project wizard

// globals
GlutPathDefault    <- _T("$(#glut)");
GlutPathDefaultInc <- _T("$(#glut.include)");
GlutPathDefaultLib <- _T("$(#glut.lib)");
GlutPath <- _T("");

function BeginWizard()
    local intro_msg = _T("Welcome to the new GLUT project wizard!\n\n" +
                         "This wizard will guide you to create a new project\n" +
                         "using the GLUT OpenGL extensions.\n\n" +
                         "When you 're ready to proceed, please click \"Next\"...");

    local glutpath_descr = _T("Please select the location of GLUT on your computer.\n" +
                              "This is the top-level folder where GLUT was installed (unpacked).\n" +
                              "To help you, this folder must contain the subfolders\n" +
                              "\"include\" and \"lib\".");

    Wizard.AddInfoPage(_T("GlutIntro"), intro_msg);
        Wizard.AddGenericSelectPathPage(_T("GlutPath"), glutpath_descr, _T("Please select GLUT's location:"), GlutPathDefault);
    Wizard.AddCompilerPage(_T(""), _T("*"), true, true);

// GLUT's path page

function OnLeave_GlutPath(fwd)
    if (fwd)
        local dir         = Wizard.GetTextControlValue(_T("txtFolder")); // txtFolder is the text control in GenericSelectPathPage
        local dir_nomacro = VerifyDirectory(dir);

        if (dir_nomacro.IsEmpty())
            return false;

        // verify include dependencies
        local dir_nomacro_inc = GetCompilerIncludeDir(dir, GlutPathDefault, GlutPathDefaultInc);
        if (dir_nomacro_inc.IsEmpty())
            return false;
        if (!VerifyFile(dir_nomacro_inc + wxFILE_SEP_PATH + _T("GL"), _T("glut.h"), _T("GLUT's include"))) return false;

        // verify library dependencies
        local dir_nomacro_lib = GetCompilerLibDir(dir, GlutPathDefault, GlutPathDefaultLib);
        if (dir_nomacro_lib.IsEmpty())
            return false;

            if (!VerifyLibFile(dir_nomacro_lib, _T("freeglut"), _T("GLUT's"))) return false;
            if (!VerifyLibFile(dir_nomacro_lib, _T("glut"), _T("GLUT's"))) return false;

        GlutPath = dir; // Remember the original selection.

        local is_macro = _T("");

        // try to resolve the include directory as macro
        is_macro = GetCompilerIncludeMacro(dir, GlutPathDefault, GlutPathDefaultInc);
        if (is_macro.IsEmpty())
            // not possible -> use the real inc path we had computed instead
            GlutPathDefaultInc = dir_nomacro_inc;

        // try to resolve the library directory as macro
        is_macro = GetCompilerLibMacro(dir, GlutPathDefault, GlutPathDefaultLib);
        if (is_macro.IsEmpty())
            // not possible -> use the real lib path we had computed instead
            GlutPathDefaultLib = dir_nomacro_lib;
    return true;

// return the files this project contains
function GetFilesDir()
    return _T("glut/files");

// setup the already created project
function SetupProject(project)
    // set project options

    // add link libraries
    else if (PLATFORM == PLATFORM_MAC)
        project.AddLinkerOption(_T("-framework GLUT"));
        project.AddLinkerOption(_T("-framework OpenGL"));

        project.AddLinkerOption(_T("-framework Cocoa")); // GLUT dependency

    // enable compiler warnings (project-wide)
    WarningsOn(project, Wizard.GetCompilerID());

    // Debug
    local target = project.GetBuildTarget(Wizard.GetDebugName());
    if (!IsNull(target))
        target.SetTargetType(ttConsoleOnly); // ttConsoleOnly: console for debugging
        target.SetOutputFilename(Wizard.GetDebugOutputDir() + Wizard.GetProjectName() + DOT_EXT_EXECUTABLE);
        target.SetWorkingDir(GlutPath + _T("/bin"));
        // enable generation of debugging symbols for target
        DebugSymbolsOn(target, Wizard.GetCompilerID());

    // Release
    target = project.GetBuildTarget(Wizard.GetReleaseName());
    if (!IsNull(target))
        target.SetTargetType(ttExecutable); // ttExecutable: no console
        target.SetOutputFilename(Wizard.GetReleaseOutputDir() + Wizard.GetProjectName() + DOT_EXT_EXECUTABLE);
        target.SetWorkingDir(GlutPath + _T("/bin"));
        // enable optimizations for target
        OptimizationsOn(target, Wizard.GetCompilerID());

    return true;

This line "project.AddLinkLib(_T("Glut32"));" should now say "project.AddLinkLib(_T("freeglut"));"

Also, this line "if (!VerifyLibFile(dir_nomacro_lib, _T("glut32"), _T("GLUT's"))) return false;" should now say "if (!VerifyLibFile(dir_nomacro_lib, _T("freeglut"), _T("GLUT's"))) return false;"

NOTE : Because of windows Protections, it will force you to save it somewhere else. Just save it to your desktop and then MOVE the file into the same folder as the old one and overwrite. It will ask for permission, click continue. ( or yes on some machines ).

Now.. one more file to edit and your done..

go to this folder by back tracking a little bit : C:\Program Files (x86)\CodeBlocks\share\CodeBlocks\templates

in there you will find glut.cbp

This is the file that is for your project to work off of. You need to edit this too. Here is the code :

The glut.cbp File

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
	<FileVersion major="1" minor="4" />
		<Option title="glut" />
		<Option pch_mode="0" />
		<Option compiler="gcc" />
			<Target title="default">
				<Option output="glut.exe" />
				<Option type="0" />
				<Option compiler="gcc" />
				<Option includeInTargetAll="1" />
			<Add directory="$(#glut.include)" />
			<Add library="freeglut" />
			<Add library="glu32" />
			<Add library="opengl32" />
			<Add library="winmm" />
			<Add library="gdi32" />
			<Add library="user32" />
			<Add library="kernel32" />
			<Add directory="$(#glut.lib)" />
		<Unit filename="main.cpp">
			<Option compilerVar="CPP" />
			<Option target="default" />

See this line "<Add library="Glut32" />" should now say "<Add library="freeglut" />"

NOTE : Because of windows Protections, it will force you to save it somewhere else. Just save it to your desktop and then MOVE the file into the same folder as the old one and overwrite. It will ask for permission, click continue. ( or yes on some machines ).

Time to run the demo code from Code::Blocks itself

Start up code::blocks and choose Create New Project and then choose Glut / Glut Project. Click next and give it a name and point to where you want the project saved too. Click next again and where it asks where Glut is, point to : C:\Program Files (x86)\CodeBlocks\MinGW. It will find the glut.h file you need. It will ask you what compiler, I use GNU GCC Compiler. ( The name MinGW is not in the list for some reason. )

Freeglut 3.jpg

Freeglut 4.jpg

Freeglut 5.jpg

Freeglut 6.jpg

Freeglut 7.jpg

So yea.. run the demo right out of the original Code::Blocks code and you will see some red spinning objects. :)

Freeglut 2.jpg