Thread: Setting Up SDL

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    40

    Setting Up SDL

    I just started out learning SDL. Im kind of just trolling through it and have no real idea what I'm doing here.

    Im following this tutorial: Learn Game Programming in C Episode 6 - Learning APIs - YouTube

    I finally got GCC to work after messing around trying to get the right flags but now I'm getting a error message:

    game.c:10:3: error: unknown type name ‘SDL_Window’
    SDL_Window *window;
    ^
    game.c:11:3: error: unknown type name ‘SDL_Renderer’
    SDL_Renderer *renderer;

    Here is the code...

    Code:
    #include <stdio.h>
    #include <SDL/SDL.h>
    //#include "SDl.h"
    
    
    typedef struct {
      int x;
      int y;
    }Bat;
    
    
    int main(){
      SDL_Window *window;
      SDL_Renderer *renderer;
    
    
      SDL_Init(SDL_INIT_VIDEO);
    
    
    }
    Is the tutorial out of date? Or is there something wrong with the way I'm trying to compile it?
    I'm using the header <SDL/SDL.h> and not "SDl.h" because I couldn't get the flags to work with "SDl.h".

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I think you're trying to compile SDL2 code with the SDL1.2 header/library. You need SDL2 installed and your compile line should look something like this:
    gcc -o prog prog.c `sdl2-config --cflags --libs`

    In addition, try <SDL.h> instead of <SDL/SDL.h>.
    Last edited by algorism; 11-19-2016 at 06:00 PM.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    I managed to get the flags right for GCC and it dosnt matter is I use <SDL.h> or <SDL/SDL.h>. I'm still getting the same errors.


  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Are you sure you have SDL2 installed (not just SDL1.2)? What OS are you using?

    Have you tried the compile line I showed above?
    It works for me.
    Code:
    // gcc -o sdl01 sdl01.c `sdl2-config --cflags --libs`
    #include <SDL.h>
    int main(){
      SDL_Window *window;
      SDL_Renderer *renderer; 
      SDL_Init(SDL_INIT_VIDEO);
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    Just checked and got this...
    libsdl2-dev is already the newest version (2.0.4+dfsg1-2ubuntu2).

    That is the correct package right? Your code is not working for me I have no idea why. The SDL example program Uni gave me works just fine. I'm using a make file to compile it... could it be the flags I'm using? I used this to compile....

    gcc -std=c99 -Wall game.c -I/usr/include/SDL -lSDL2 -o game

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The include directory on your compile line needs to be /usr/include/SDL2.

    Remember to try both <SDL/SDL.h> and <SDL.h> in your #includes.

    EDIT: Of course, the proper way is as I showed originally, which generates the correct parameters automatically for the given system:
    gcc -std=c99 -Wall -o game game.c `sdl2-config --cflags --libs`
    Last edited by algorism; 11-19-2016 at 08:26 PM.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Xecutive View Post
    libsdl2-dev is already the newest version (2.0.4+dfsg1-2ubuntu2).

    ...
    gcc -std=c99 -Wall game.c -I/usr/include/SDL -lSDL2 -o game
    According to Ubuntu – Details of package libsdl2-dev in xenial , that Ubuntu package installs its headers under /usr/include/SDL2. You have the wrong include path in your above build command.

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    Thanks guys its working now

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    Im having a new problem now... I thought I might as-well post in here than make a new thread.

    So I'm still following the same youtube tutorial. My program complies but when I run it nothing happens. It should create a new window with a white square on a blue background. Code bellow:

    Code:
    #include <stdio.h>
    #include <SDL.h>
    
    
    int main(){
    
    
      SDL_Window *window;
      SDL_Renderer *renderer;
    
    
      SDL_Init(SDL_INIT_VIDEO);
      return 0;
    
    
      window = SDL_CreateWindow( "Game window",
                                  SDL_WINDOWPOS_UNDEFINED,
                                  SDL_WINDOWPOS_UNDEFINED,
                                  640,
                                  480,
                                  0
                                  );
    
    
      renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    
    
    
    
      SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
    
    
      SDL_RenderClear(renderer);
    
    
      SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    
    
      SDL_Rect rect = {220, 140, 200, 200};
      SDL_RenderFillRect(renderer, &rect);
    
    
      SDL_Delay(2000);
    
    
      SDL_DestroyWindow(window);
      SDL_DestroyRenderer(renderer);
    
    
      SDL_Quit();
      return 0;
    
    
    
    
    }

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Could it possibly have something to do with the return statement on line 13?

    EDIT: You also left out the call to present the scene (just before the delay) :
    Code:
      SDL_RenderPresent(renderer);
    Last edited by algorism; 11-20-2016 at 01:02 PM.

  11. #11
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    Haha yeah... stupid mistakes... thanks!

  12. #12
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    I'm having a problem again... The program bellow, after about 7 seconds, stops responding every-time I run it. I have no idea why...

    Code:
    #include <stdio.h>
    #include <SDL.h>
    
    
    int main(){
    
    
      SDL_Window *window;
      SDL_Renderer *renderer;
    
    
      SDL_Init(SDL_INIT_VIDEO);
    
    
    
    
      window = SDL_CreateWindow( "Keep It UP!",
                                  SDL_WINDOWPOS_UNDEFINED,
                                  SDL_WINDOWPOS_UNDEFINED,
                                  640,
                                  480,
                                  0
                                  );
    
    
      renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    
    
    
    
      int x = 0;
      int y = 10;
    
    
      while (1) {
    
    
    
    
        if (x > 439 || x < 0) {
          y = y * -1;
        }
        x = x + y;
    
    
        SDL_SetRenderDrawColor(renderer, 0, 0,255, 255);
    
    
        SDL_RenderClear(renderer);
    
    
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    
    
        SDL_Rect rect = {x, 140, 200, 200};
        SDL_RenderFillRect(renderer, &rect);
    
    
        SDL_RenderPresent(renderer);
    
    
        SDL_Delay(100);
      }
    
    
    
    
      SDL_DestroyWindow(window);
      SDL_DestroyRenderer(renderer);
    
    
      SDL_Quit();
      return 0;
    
    
    
    
    }

  13. #13
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    Don't worry about the above post... I worked it out

  14. #14
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    I could use some help with this program again...

    I finally have TTF working. I wrote a simple bit of code to print some test text to the window. It compiles but when I run the program I get a segmentation fault. I have never used TTF before so it might be a stupid mistake.

    Here is the code:

    Code:
    int main(){
    
    
    
    
      int done = 0;
    
    
      Game game;
      game.score = 0;
      game.bbhold = 2;
      game.f1hold = 0;
    
    
      Bat bat;
      bat.x = 16;
      bat.y = 16;
    
    
      Ball balla;
      balla.x = 100;
      balla.y = 300;
      balla.conx = 7;
      balla.cony = 7;
    
    
      Ball ballb;
      ballb.x = 100;
      ballb.y = 10;
      ballb.conx = 2;
      ballb.cony = 3;
    
    
      Ball f1;
      f1.x = 0;
      f1.y = 100;
      f1.conx = 4;
      f1.cony = 4;
    
    
      Ball f2;
      f2.x = 800;
      f2.y = 100;
      f2.conx = 4;
      f2.cony = 4;
    
    
    
    
      //setup
      SDL_Window *window;
      SDL_Renderer *renderer;
    
    
      SDL_Init(SDL_INIT_VIDEO);
    
    
      TTF_Init();
    
    
      window = SDL_CreateWindow( "Keep It UP!",
                                  SDL_WINDOWPOS_UNDEFINED,
                                  SDL_WINDOWPOS_UNDEFINED,
                                  800,
                                  600,
                                  0
                                  );
    
    
      renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    
    
      //TTF setup
      TTF_Font *times;
      times = TTF_OpenFont("font.ttf", 20);
      SDL_Color white = {255, 255, 255};
    
    
      //end of setup
    
    
    
    
      //main game loop
      while (!done) {
    
    
    
    
    
    
        done = events(window, &bat);
    
    
    
    
    
    
        //screen setup
        SDL_SetRenderDrawColor(renderer, 0, 0,255, 255);
    
    
        SDL_RenderClear(renderer);
    
    
        // ball b drop zone
        SDL_SetRenderDrawColor(renderer, 255, 0, 50, 255);
    
    
        SDL_Rect rect = {350, 0, 100, 7};
        SDL_RenderFillRect(renderer, &rect);
    
    
        // fire ball drop zones
        SDL_SetRenderDrawColor(renderer, 255, 217, 0, 255);
    
    
        SDL_Rect rectf1 = {0, 100, 7, 80};
        SDL_RenderFillRect(renderer, &rectf1);
    
    
        SDL_Rect rectf2 = {793, 100, 7, 80};
        SDL_RenderFillRect(renderer, &rectf2);
    
    
        //set new life zones
        SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
    
    
        SDL_Rect rectg1 = {0, 250, 7, 50};
        SDL_RenderFillRect(renderer, &rectg1);
    
    
        SDL_Rect rectg2 = {793, 250, 7, 50};
        SDL_RenderFillRect(renderer, &rectg2);
    
    
    
    
    
    
        //main function calls
        bat_ren(renderer, &bat);
    
    
        mball(renderer, &balla, &bat, &game, &ballb, &f1, &f2);
    
    
        // ball b logic
        if (game.bbhold < 2) {
          bball(renderer, &ballb, &bat, &game);
        }
    
    
        //f ball logic
        if (game.f1hold > 0) {
          ballf1(renderer, &f1, &bat, &game);
          ballf2(renderer, &f2, &bat, &game);
        }
    
    
        //ttf text
        SDL_Surface *message = TTF_RenderText_Solid(times, "test text", white);
        
    
    
    
    
        //render objects
        SDL_RenderPresent(renderer);
    
    
        SDL_Delay(10);
    
    
        SDL_FreeSurface(message);
      }
    
    
    
    
      SDL_DestroyWindow(window);
      SDL_DestroyRenderer(renderer);
    
    
      TTF_Quit();
      SDL_Quit();
      return 0;
    The TTF code is all written in main and is not interacting with any of my other functions so I left them out.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Perhaps you should check for errors rather than blindly stumbling through hoping for success.

    TTF_OpenFont and TTF_RenderText_Solid both return NULL on error, and subsequently trying to dereference NULL in a followon function is guaranteed to get you a segfault.

    It's a good opportunity to learn about gdb at this point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting up SDL for C
    By 7h3_0r4c1_3 in forum C Programming
    Replies: 1
    Last Post: 08-25-2010, 10:30 AM
  2. Bit setting
    By c_lady in forum C Programming
    Replies: 2
    Last Post: 03-09-2010, 01:11 PM
  3. Setting up Dev C++
    By Suchy in forum Tech Board
    Replies: 11
    Last Post: 11-11-2006, 02:38 PM
  4. Bit setting
    By fizz_uk83 in forum C Programming
    Replies: 2
    Last Post: 09-22-2003, 02:14 AM
  5. Setting up for md2
    By Ruski in forum Game Programming
    Replies: 10
    Last Post: 08-13-2002, 01:54 AM

Tags for this Thread