Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
206 lines (133 sloc) 4.97 KB

SDL

SDL: Simple DirectMedia Layer is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D.

SDL library is available in biicode at miguel/sdl2.

Graphical window interface

In this example you will open a new graphical window and load a background texture.

Creating a new project

Create a new project and a main_sdl.cpp file:

$ bii init sdl_example -L
$ cd sdl_example
$ # Create main_sdl.cpp

Now place the following code inside main_sdl.cpp:

main_sdl.cpp

#include "SDL.h"
#include <iostream>

int main(int argc, char *argv[])
{
  if (SDL_Init(SDL_INIT_VIDEO) != 0)
  {
    std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
    return 1;
  }

  SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);

  SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

  SDL_Surface *bmp = SDL_LoadBMP("examples/sdl_example/media/sdl.bmp");

  SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
  SDL_FreeSurface(bmp);

  if (tex == nullptr)
  {
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
    SDL_Quit();
    return 1;
  }

  //Wait for 3 seconds and render and present the screen each time
  for (int i = 0; i < 3; ++i)
  {
    //First clear the renderer
    SDL_RenderClear(ren);
    //Draw the texture
    SDL_RenderCopy(ren, tex, NULL, NULL);
    //Update the screen
    SDL_RenderPresent(ren);
    //Take a quick break
    SDL_Delay(3000);
  }

SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();

}

Now, add a cool background .bmp like this one to a new folder "media" and link it with your main_sdl.cpp file in biicode.conf [data] section like this:

[data]
    main_sdl.cpp + media/sdl.bmp

Now, change your path to the file in your code:

SDL_Surface *bmp = SDL_LoadBMP("your_user/sdl_example/media/sdl.bmp"); //Replace "your_user"!

Manage your dependencies

Check the dependencies of the project with bii deps:

$ bii deps
your_user/sdl_example depends on:
      system:
         iostream
      unresolved:
         SDL.h

Now, edit the biicode.conf file generated in the project folder. Add your [requirements] depending on the version you want and map your [includes] to point to miguel/sdl2/include/SDL.h:

[requirements]
    miguel/sdl2: 3

[includes]
    SDL.h: miguel/sdl2/include

Check again with bii deps and now all dependencies are solved.

Activating C++11

Building your project like this throws the next error:

$ bii build
 ...
 C:\...\sdl_example\main_sdl.cpp:21:13: error: 'nullptr' was not declared in this scope
 if (tex == nullptr)
            ^

So we have to activate C++11 support. With biicode you can reuse cmake macros, so we'll use this one: biicode/cmake/tools.cmake. It is very easy.

  • Edit CMakeLists.txt and write:
# Including tools.cmake from biicode/cmake user block
INCLUDE(biicode/cmake/tools)

ADD_BII_TARGETS()

# Calling specific macro to activate c++11 flags
ACTIVATE_CPP11(INTERFACE ${BII_BLOCK_TARGET})
  • Type bii find and all is done!
$ bii find
INFO: Processing changes...
INFO: Finding missing dependencies in server
INFO: Looking for biicode/cmake...
...
INFO: Saving files from: biicode/cmake

Build the project

The only thing left is building the project:

$ bii build

Execute the binary placed in bin directory and this is how output looks like:

$ cd bin
$ ./your_user_sdl_example_main_sdl

Here is the result:

/_static/img/c++/examples/sdl_example.png

That's it!

Open and build

This example is already in biicode: examples/sdl_example.

To give it a try, create a new project and open the block:

$ bii init sdl_project
$ cd sdl_project
$ bii open examples/sdl_example

Build the example and execute it:

$ bii build
$ cd bin
$ # Execute it

You will see your the above graphical window with SDL background texture.

Got any doubts? Do not hesitate to contact us, visit our forum and feel free to ask any questions.