Tag: sdl

  • 3 SDL Examples

    This post isn’t Chess related but it is still very nerdy. I have made 3 different SDL programs that all do the exact same thing. However they are written using different SDL versions over time as the API has changed. Therefore, SDL version 1,2, and 3 source code is provided as well as the commands to compile them!

    SDL Version 1

    #include <stdio.h>
    #include <SDL.h>
    int width=1280,height=720;
    int loop=1;
    SDL_Surface *surface;
    SDL_Event e;
    int main(int argc, char **argv)
    {
     if(SDL_Init(SDL_INIT_EVERYTHING)){return 1;}
    
     SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
     SDL_WM_SetCaption("SDL1 Program",NULL);
     surface=SDL_SetVideoMode(width,height,32,SDL_SWSURFACE);
     if(surface==NULL){return 1;}
    
     SDL_FillRect(surface,NULL,0xFF00FF);
    
     while(loop)
     {
      while(SDL_PollEvent(&e))
      {
       if(e.type==SDL_QUIT){loop=0;}
       if(e.type==SDL_KEYUP){if(e.key.keysym.sym==SDLK_ESCAPE){loop=0;}}
      }
      SDL_Flip(surface);
     }
    
     SDL_Quit();
     return 0;    
    }
    
    /*
    SDL1 program that creates a window but nothing else.
    Closes when user presses Esc or clicks the X of the window.
    This is to test whether SDL1 programs can be compiled.
    
     With the sdl-config script:
     gcc -Wall -ansi -pedantic sdl1-test.c -o sdl1-test `sdl-config --cflags --libs` && ./sdl1-test
    
     Without the sdl-config script:
     gcc -Wall -ansi -pedantic sdl1-test.c -o sdl1-test -I/usr/include/SDL -D_GNU_SOURCE=1 -L/usr/lib/x86_64-linux-gnu -lSDL && ./sdl1-test
    */
    

    SDL Version 2

    #include <stdio.h>
    #include <SDL.h>
    int width=1280,height=720;
    int loop=1;
    SDL_Window *window;
    SDL_Surface *surface;
    SDL_Event e;
    int main(int argc, char **argv)
    {
     if(SDL_Init(SDL_INIT_VIDEO))
     {
      printf( "SDL could not initialize! SDL_Error: %s\n",SDL_GetError());return -1;
     }
     window=SDL_CreateWindow("SDL2 Program",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN );
     if(window==NULL){printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );return -1;}
     surface = SDL_GetWindowSurface( window ); /*get surface for this window*/
     SDL_FillRect(surface,NULL,0xFF00FF);
     SDL_UpdateWindowSurface(window);
     printf("SDL Program Compiled Correctly\n");
     while(loop)
     {
      while(SDL_PollEvent(&e))
      {
       if(e.type == SDL_QUIT){loop=0;}
       if(e.type == SDL_KEYUP)
       {
        if(e.key.keysym.sym==SDLK_ESCAPE){loop=0;}
       }
      }
     }
     SDL_DestroyWindow(window);
     SDL_Quit();
     return 0;
    }
    
    /*
     This source file is an example to be included in Chastity's Code Cookbook. This example follows the SDL version 2 which works differently than the most up to date version (version 3 at this time). There is also an updated version in the same repository that works with version 3.
    
     Linux Compile Command:
    
     With the sdl2-config script:
     gcc -Wall -ansi -pedantic sdl2-test.c -o sdl2-test `sdl2-config --cflags --libs` && ./sdl2-test
    
     Without the sdl2-config script:
     gcc -Wall -ansi -pedantic sdl2-test.c -o sdl2-test -I/usr/include/SDL2 -lSDL2 && ./sdl2-test
    */
    

    SDL Version 3

    #include <stdio.h>
    #include <SDL.h>
    int width=1280,height=720;
    int loop=1;
    SDL_Window *window;
    SDL_Surface *surface;
    SDL_Event e;
    int main(int argc, char **argv)
    {
     if(!SDL_Init(SDL_INIT_VIDEO))
     {
      printf( "SDL could not initialize! SDL_Error: %s\n",SDL_GetError());return -1;
     }
     window=SDL_CreateWindow("SDL3 Program",width,height,0);
     if(window==NULL){printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );return -1;}
     surface = SDL_GetWindowSurface( window ); /*get surface for this window*/
     SDL_FillSurfaceRect(surface,NULL,0xFF00FF);
     SDL_UpdateWindowSurface(window);
     printf("SDL Program Compiled Correctly\n");
     while(loop)
     {
      while(SDL_PollEvent(&e))
      {
       if(e.type == SDL_EVENT_QUIT){loop=0;}
       if(e.type == SDL_EVENT_KEY_UP)
       {
        if(e.key.key==SDLK_ESCAPE){loop=0;}
       }
      }
     }
     SDL_DestroyWindow(window);
     SDL_Quit();
     return 0;
    }
    
    /*
     This source file is an example to be included in Chastity's Code Cookbook. By following the migration guide, I converted the SDL2-test program to the changes in SDL3.
    
     https://wiki.libsdl.org/SDL3/README-migration
    
     Windows Compile Command:
    
     gcc -Wall -ansi -pedantic sdl3-test.c -o sdl3-test -IC:/w64devkit/include/SDL3 -lSDL3 && sdl3-test
    
     Linux Compile Command:
    
     gcc -Wall -ansi -pedantic sdl3-test.c -o sdl3-test -I/usr/include/SDL3 -lSDL3 && ./sdl3-test
    
    */
    

    No matter which of those I compile and run, I get the same result:

    All the programs do is create a window and fill the whole thing with Magenta. It’s not that exciting, but it is pretty, and it shows that all 3 versions of SDL work on my Debian Linux machine.