Monday, February 21, 2011

Setup SDL 2 in code::block

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 download another file for Visual C++. 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\System32" 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[] ){
 SDL_Window* window = NULL;  //The window we'll be rendering to
 SDL_Surface* screenSurface = NULL;  //The surface contained by the window
 //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;
}


You can follow the video below to know the steps in detail:


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, built and run the default program.

You can follow the video below to know the steps in detail:

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