Sunday, February 27, 2011

Setup GLFW 3 library in Codeblocks IDE

GLFW is a free, open-source, multi-platform library for OpenGL and OpenGL ES especially design for game development. OpenGL itself does not provide features to handle window creation and other input, so GLFW is very useful to create the necessary context, manage windows, timing, read and control user input via keyboard, mouse, joysticks and other purposes. There are many other library alternatives to GLFW such as SDL and freeglut but if you are looking best library for 3D game development then GLFW is best. GLFW community is large and very active, at the time of updating this article latest stable version of GLFW is 3.3.2 which is released on January 20, 2020. It comes with documentation, high standard tutorials and guides and lots of test programs.


Here, I will guide you through a step by step procedure to setup GLFW 3 in Codeblocks IDE on Windows 10.

Visit GLFW download page https://www.glfw.org/download.html and download 32 bit/64 bit windows binary files from the Windows pre-compiled binaries section. You have to make sure that bit of your C++ compiler and downloaded GLFW library files should match. Currently, GLFW releases are tested with MinGW, MinGW-w64 and Visual C++ 2010 and above version.

Steps to setup GLFW library and create a project in Code::blocks:
  1. Extract downloaded zip file.
  2. Create a folder in any directory and give a name "GLFW" (eg. c:\GLFW).
  3. Browse extracted files in step 1. Copy "include" folder and paste it inside the directory that you have created in step 2. Copy lib folder of corresponding to your compiler for e.g if you are using minGW then copy lib-mingw folder and paste it inside the directory that you have created in step 2 and rename it to lib. Also copy glfw3.dll file from bin folder to "C:\Windows\System32" if you are using 32-bit library files or copy to "C:\Windows\System" if you are using 64-bit library files.
  4. Now, open the code::blocks and go to File -> New -> Project or click create a new project link from the start page.
  5. From the project wizard select "empty project" and click "Go" button and provide all necessary information. Also, do not forget to add main.cpp file.
  6. Go to the menu bar and select project-> Project properties. A dialogue box will appear and click "Project's build options". Another dialogue box will appear and click the "Search directories" tab. Under the "Compiler" tab add the path of GLFW include folder (e.g. c:\GLFW\include) and under "Linker" tab add the path of GLFW lib folder (e.g. c:\GLFW\lib).
  7. Now go to "Linker Setting" tab and add three linker libglfw3.a, libglfw3dll.a and libopengl32.a. Linker libglfw3.a and libglfw3dll.a are available inside GLFW lib folder and libopengl32.a is available inside minGW compiler lib folder.
Copy and paste below code in main.cpp file and compile it to verify GLFW library setup.

#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <iostream>
#include <stdexcept>

GLFWwindow* window = NULL;
GLFWmonitor* monitor = NULL;

int main(int argc, char *argv[]) {

    if(!glfwInit())
    throw std::runtime_error("glfwInit failed");

    window = glfwCreateWindow(800, 600, "test window",NULL, NULL);
    if(!window)
        throw std::runtime_error("glfwOpenWindow failed.");

   // GLFW settings
    glfwMakeContextCurrent(window);

    while(!glfwWindowShouldClose(window)){
    // process pending events
       glfwPollEvents();
       glClearColor(1, 0, 0, 1); // black
       glClear(GL_COLOR_BUFFER_BIT);
       glfwSwapBuffers(window);
    }
   // clean up and exit
   glfwTerminate();
    return 0;
}

You have to make sure to define GLFW_DLL  and include glfw3.h header always on top of your source code as shown in the above sample code otherwise the compiler will throw undefine reference error.

Please watch the video below for a detailed explanation.


Friday, February 25, 2011

Setup Code::blocks in Windows 10 and It's Features


Features

Highlights:

  • Open Source! GPLv3, no hidden costs.
  • Cross-platform. Runs on Linux, Mac, Windows (uses wxWidgets).
  • Written in C++. No interpreted languages or proprietary libs needed.
  • Extensible through plugins

Compiler:

  • Multiple compiler support:
    • GCC (MingW / GNU GCC)
    • MSVC++
    • Digital Mars
    • Borland C++ 5.5
    • Open Watcom
    • ...and more
  • Very fast custom build system (no makefiles needed)
  • Support for parallel builds (utilizing your CPU's extra cores)
  • Multi-target projects
  • Workspaces to combine multiple projects
  • Inter-project dependencies inside a workspace
  • Imports MSVC projects and workspaces (NOTE: assembly code not supported yet)
  • Imports Dev-C++ projects

Tuesday, February 22, 2011

Setup wxWidgets in Codeblocks

wxWidgets is free software, multi-platform GUI library written in C++ programing language. GUI applications created using the wxWidgets library run on multiple platforms such as Windows, Linux, Mac, and others that utilize the native platform's controls and utilities. Since it is built and compiled in C++, it has high performance and is nearly as fast as using the native toolkits themselves.

The Codeblocks IDE that we are using is also developed using wxWidgets. Here, I will guide you through a step-by-step procedure to set up wxWidgets in Codeblocks IDE on Windows.

Download Library Files:

Open the wxWidgets download page https://www.wxwidgets.org/downloads/ and download Windows Binaries. You can download source code and compile library using Codeblocks or visual C++ compiler but it is a very complex process and difficult for the beginner. So, I will help you to setup a library using pre-compiled library and header files. In the download pages under the Binaries section, there is "Download Windows Binaries" button. Click this button and select "MinGW-w64 8.1" option (Note: if you are using an older version of Codeblocks then you should choose as per your MinGW version. However, I suggest you to use the latest version of the Codeblocks and download the latest version of the MinGW library). Now download "Header Files" and "Development Files" 32-Bit or 64-Bit as per the Codeblocks that you are using. I am using 64 Codeblocks so I will download 64-Bit version "Development Files".  

Steps to setup wxWidgets library:
  1. Extract downloaded zip files both "Header" and "Development Files". (Note: you may need 7zip to extract files)
  2. Create a folder in any directory and give the name "wxWidgets" (eg. c:\wxWidgets).
  3. Browse extracted files in step 1. Copy "include" folder from the downloaded Header and paste it inside the directory that you have created in step 2. Copy "lib"  folder from the downloaded Development Files and paste it inside the directory that you have created in step2. If you open "lib" folder you will find dll files, you can copy all dll files to "C:\Windows\System32" folder if you are using 32-bit or "C:\Windows\SysWOW64" folder if you are using 64-bit. However, I do not suggest you to copy dll files to the system folder in the development phase of an application.
  4. Now, open the code::blocks and go to File -> New -> Project or click create a new project link from the start page.
  5. From the project wizard select "empty project" and click "Go" button and provide all necessary information. Also, do not forget to add main.cpp file.
  6. Go to the menu bar and select Project-> Properties. A dialogue box will appear and click "Project's build options". Another dialogue box will appear and click the "Search directories" tab. Make sure that in left side panel you must select "<project name>" and under the "Compiler" tab add the path of wxWidgets include folder (e.g. c:\wxWidgets\include) and under "Linker" tab add the path of wxWidgets lib folder (e.g. c:\wxWidgets\lib\gcc810_x64_dll) where you can find *.a and *.dll files.
  7. Select "Debug" option in left side panel and click the "Search directories" tab. Under the "Compiler" tab add the path of folder "mswud". This folder is located inside wxWidgets lib folder (e.g. c:\wxWidgets\lib\gcc810_x64_dll\mswud). Note: if you are using 32-bit then the path might be different (e.g. c:\wxWidgets\lib\gcc_dll\mswud). Now select "Linker Setting" tab and add two linkers wxmsw31ud_core and wxbase31ud. Please type exactly the same name don't write "lib" prefix and ".a" extension.
  8. Select "Release" option in left side panel and click the "Search directories" tab. Under the "Compiler" tab add the path of folder "mswu". This folder is located inside wxWidgets lib folder (e.g. c:\wxWidgets\lib\gcc810_x64_dll\mswu). Note: if you are using 32-bit then the path might be different (e.g. c:\wxWidgets\lib\gcc_dll\mswu). Now select "Linker Setting" tab and add two linkers wxmsw31u_core and wxbase31u. Please type exactly the same name don't write "lib" prefix and ".a" extension.  
Please watch the video below for a detailed explanation.



Now copy the sample code given below inside "main.cpp" file and build and run it. Sample code is taken from wxWidgets official website.

// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include &lt"wx/wxprec.h"&gt
#ifndef WX_PRECOMP
    #include &lt"wx/wx.h"&gt
#endif
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
    MyFrame();
private:
    void OnHello(wxCommandEvent& event);
    void OnExit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
};
enum
{
    ID_Hello = 1
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame();
    frame->Show(true);
    return true;
}
MyFrame::MyFrame()
    : wxFrame(NULL, wxID_ANY, "Hello World")
{
    wxMenu *menuFile = new wxMenu;
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
                  "Help string shown in status bar for this menu item");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
    wxMenu *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuHelp, "&Help");
    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");
    Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}
void MyFrame::OnExit(wxCommandEvent& event)
{
    Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
    wxMessageBox("This is a wxWidgets Hello World example",
                 "About Hello World", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello(wxCommandEvent& event)
{
    wxLogMessage("Hello world from wxWidgets!");
}

Setup GLUT (OpenGL) in Codeblocks


GLUT (OpenGL Utility Toolkit) is a toolkit to write OpenGL programs. It makes learning OpenGL programming very easy and simple. However, it is very old and unsupported for the last 20 years or more and it does not support modern requirements. I personally do not recommend this library and I request to choose one of the alternate libraries such as GLFW, freeglut, and SDL.

If you still need to setup this library to execute old code downloaded from the internet written in GLUT then follow the steps provided below:

Download the glut library from the website http://code.google.com/p/rawvideoplayer/downloads/detail?name=glut-3.7.6-bin.zip&can=2&q= for windows.

Steps to create GLUT project in Code::block.
Step 1: Extract the downloaded zip file.

Step 2: Browse extracted files. Copy the glut.h file and paste it into the directory "C:\ProgramFiles(x86)\CodeBlocks\MinGW\include\GL". Copy glut.lib file and paste it into the directory "C:\Program Files(x86)\CodeBlocks\MinGW\lib". Also, copy the glut.dll and paste it into
the directory "C:\Windows\System32".

Monday, February 21, 2011

Setup SDL 2 in Codeblocks

Setup SDL 2 in Code::Blocks (modern version)

The latest stable version of SDL (Simple DirectMedia Layer) is 2.0.10 at the time of updating this article. Unlike SDL 1.2, SDL 2 utilizes GPU memory and provides features to develop high-performance games and applications. Also, it includes easy access to graphics, sound and input handling.  
Here, I will guide you through a step by step procedure to setup SDL 2 in Codeblocks IDE on Windows 10.

Visit SDL download page https://www.libsdl.org/download-2.0.php and download the file "SDL2-devel-2.0.10-mingw.tar.gz (Mingw32)" from the Development Libraries section for your respective machine Win32/Linux. If you are using Visual C++ compiler in Code:blocks then Visuall C++ download link. Required links are highlighted in the screenshot below:




Steps to create SDL 2 project in Code::blocks:
  1. Extract downloaded zip file.
  2. Create a folder in any directory and give a name "SDL 2" (eg. c:\SDL 2).
  3. Browse extracted files in step 1. Copy "include" and "lib" folders and paste it inside the directory that you have created in step 2. Also, copy SDL.dll file from bin folder to "C:\Windows\System32" if you are using 32-bit library files or copy to "C:\Windows\System" if you are using 64-bit library files.
  4. Open the code::blocks and go to File -> New -> Project or click create a new project link from the start page.
  5. From the project wizard select "empty project" and click "Go" button and provide all necessary information. Also, do not forget to  add main.cpp file.
  6. Go to menu bar and select project-> Project properties. A dialogue box will appear and click "Project's build options". Another dialogue box will appear and click the "Search directories" tab. Under "Compiler" tab provide the path of SDL include folder (e.g. c:\SDL2\include) and under "Linker" tab provide the path of SDL lib folder (e.g. c:\SDL2\lib).
  7. Now go to "Linker Setting" tab and inside "Other linker options" text box add three linkers "-lmingw32", "-lSDL2main" and "-lSDL2"
  8. Copy and paste below code in main.cpp file and compile it to verify SDL setup.  

#include <SDL2\SDL.h>
#include <iostream>
//Screen dimension constants
const int SCREEN_WIDTH = 640,SCREEN_HEIGHT= 480;

int main( int argc, char* args[] ){
//The window we'll be rendering to
 SDL_Window* window = NULL;
 //The surface contained by the window
 SDL_Surface* screenSurface = NULL;  
 //Initialize SDL
 if( SDL_Init( SDL_INIT_VIDEO ) < 0 ){
  std::cout<<"SDL_Error: %s\n"<<SDL_GetError()<<std::endl;
 }else{
  //Create window
  window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED
  , SDL_WINDOWPOS_UNDEFINED
  , SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  if( window == NULL ){
   std::cout<<"SDL_Error: %s\n"<<SDL_GetError()<<std::endl;
  }else{
   screenSurface = SDL_GetWindowSurface( window ); //Get window surface
   SDL_FillRect( screenSurface, NULL
   , SDL_MapRGB( screenSurface->format, 0x00, 0xFF, 0xFF ) );
   //Fill the surface with color
   SDL_UpdateWindowSurface( window ); //Update the surface
   SDL_Delay( 2000 ); //Wait two seconds
  }
 }
 SDL_DestroyWindow( window ); //Destroy window
 SDL_Quit();  //Quit SDL subsystems
 return 0;
}


Please watch the video below for a detailed explanation.

Setup SDL 1.2 in Code::Blocks (old version)

Visit SDL download page https://www.libsdl.org/download-1.2.php and download the file "SDL-devel-1.2.15-mingw32.tar.gz (Mingw32)" from the Development Libraries section for your respective machine Win32/Linux. If you are using Visual C++ compiler in Code:blocks then download another file for Visual C++. Required links are highlighted in the screenshot below:




Steps to create SDL 1.2 project in Code::blocks:
  1. Extract downloaded zip file.
  2. Create a folder in any directory and give a name "SDL 1.2" (eg. c:\SDL 1.2).
  3. Browse extracted files in step 1. Copy "include" and "lib" folders and paste it inside the directory that you have created in step 2. Also, copy SDL.dll file from bin folder to "C:\Windows\System32" folder.
  4. Open the code::blocks and go to File -> New -> Project or click create a new project link from the start page.
  5. From the project wizard select "SDL project" and click "Go" button and provide all necessary information.


After creating a project, build and run the default program.

Please watch the video below for a detailed explanation.