Using FreeGlut with Code::Blocks

From Code::Blocks
Revision as of 12:33, 8 July 2011 by JackDawson (talk | contribs)

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.


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 : freeglut-MinGW-2.6.0-3.mp.zip ]

Think of freeglut as if it was the Glut32 Binaries, which the source code to Glut32 is never available since its proprietary. But once you get the GLUT demo working, you can play with the freeglut source code from their website.


Installation

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


Freeglut 1.jpg


Copy all those files 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 you have copied the correct files and are ready to setup Code::Blocks.

So now, if you installed this on a 64Bit Windows, you should have a folder that has Program Files as a name but with the x86 on it too. Well that is where code::blocks installed too.

So now go to this folder : C:\Program Files (x86)\CodeBlocks\share\CodeBlocks\templates\wizard\glut

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 / NOTEPADD ++

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.AddProjectPathPage();
    if (PLATFORM == PLATFORM_MAC)
    {
        GlutPathDefault="/System/Library/Frameworks/GLUT.framework";
    }
    else
        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 (PLATFORM == PLATFORM_MSW)
        {
            if (!VerifyLibFile(dir_nomacro_lib, _T("freeglut"), _T("GLUT's"))) return false;
        }
        else
        {
            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
    if (PLATFORM != PLATFORM_MAC)
    {
        project.AddIncludeDir(GlutPathDefaultInc);
        project.AddLibDir(GlutPathDefaultLib);
    }

    // add link libraries
    if (PLATFORM == PLATFORM_MSW)
    {
        project.AddLinkLib(_T("freeglut"));
        project.AddLinkLib(_T("opengl32"));
        project.AddLinkLib(_T("glu32"));
        project.AddLinkLib(_T("winmm"));
        project.AddLinkLib(_T("gdi32"));
    }
    else if (PLATFORM == PLATFORM_MAC)
    {
        project.AddLinkerOption(_T("-framework GLUT"));
        project.AddLinkerOption(_T("-framework OpenGL"));

        project.AddLinkerOption(_T("-framework Cocoa")); // GLUT dependency
    }
    else
    {
        project.AddLinkLib(_T("glut"));
        project.AddLinkLib(_T("GL"));
        project.AddLinkLib(_T("GLU"));
        project.AddLinkLib(_T("Xxf86vm"));
    }

    // 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"));"

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" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="4" />
	<Project>
		<Option title="glut" />
		<Option pch_mode="0" />
		<Option compiler="gcc" />
		<Build>
			<Target title="default">
				<Option output="glut.exe" />
				<Option type="0" />
				<Option compiler="gcc" />
				<Option includeInTargetAll="1" />
			</Target>
		</Build>
		<Compiler>
			<Add directory="$(#glut.include)" />
		</Compiler>
		<Linker>
			<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)" />
		</Linker>
		<Unit filename="main.cpp">
			<Option compilerVar="CPP" />
			<Option target="default" />
		</Unit>
	</Project>
</CodeBlocks_project_file>


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 ).

So save those files and run code::blocks. Choose Glut. 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. ) So yea.. run the demo right out of the original Code::Blocks code and you will see some red spinning objects. :)


Freeglut 2.jpg


JackDawson 14:33, 8 July 2011 (CEST)