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.

1 comment:

  1. i have problem with this step:

    Now give the path for SDL folder that you have made in c:\SDL
    Then click next.

    ReplyDelete