Page 1 of 4 123 ... LastLast
Results 1 to 10 of 39

Thread: HOWTO: Setting Up SDL for Beginners in Games Development

  1. #1
    Join Date
    Apr 2006
    Beans
    1,030
    Distro
    Ubuntu 9.10 Karmic Koala

    Arrow HOWTO: Setting Up SDL for Beginners in Games Development

    NOTE: This how to assumes you have already installed build-essential and g++ etc, and that you know how to program in C++. This is not C/C++ tutorial BTW.



    Welcome to the first in hopfully many Ubuntu games development HOWTO's. This first one will cover how to setup Simple DirectMedia Layer (SDL), which is a excellent starting point for anyone wanting to get into Games development on the Ubuntu/Linux.

    The first thing you need is to install the SDL libraries as well as the development libraries.

    So open up your favourite package manager and search for SDL; this should list all the SDL support/development libraries. Now all you need to do is select the following:

    libsdl-1.2debian
    libsdl-1.2debian-alsa
    libsdl-1.2debian-oss
    libsdl-1.2dev
    libsdl-image1.2
    libsdl-image1.2-dev
    libsdl-mixer1.2
    libsdl-mixer1.2-dev
    libsdl-net1.2
    libsdl-net1.2-dev

    If your doing OpenGL stuff you will also need:
    mesa-common-dev
    libglu1-mesa-dev

    Now you can click 'Apply' and the package manager will download and install your lovely new libraries!

    Next we need to test if it works, so we need a simple program:

    Code:
    #include "SDL.h"
    
    int main( int argc, char* argv[])
    {
    	// Fire up SDL, this starts all subsystems; audio video etc.
    	
            if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
                fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
                exit(1); 
            }
    	// Now Shut it down
    	atexit(SDL_Quit);
    
    	return 0;
    }
    save this into a file called test-sdl.cpp then open a terminal and go to the folder you saved test.cpp in and execute the following commands:

    Code:
    g++ `sdl-config --cflags --libs` -o test-sdl test-sdl.cpp
    ./test-sdl
    If the program executes and gives no errors! the test was successful.

    If NOT then you need to check/reinstall all the libraries and your compiler.

    To test the OpenGL stuff you can download a SDL version of glxgears from the SDL website here:
    http://www.libsdl.org/opengl/SDLgears-1.0.2.tar.gz

    download and unpack then go to the folder in your terminal and type the following:
    Code:
    ./configure
    make
    ./SDLgears
    You should see an SDL version of the gxlgears program whizzing round on the screen

    Well, thats it for the first HOWTO, I hope this was a good start and I hope that you might like to contribute if you have any comments or tutorials to add.

    Mike
    Last edited by Mickeysofine1972; November 29th, 2006 at 09:12 PM.

  2. #2
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: HOWTO: Setting Up SDL for Beginners in Games Development

    There is one big mistake in your tutorial, when using SDL you should be including it as:
    Code:
    #include "SDL.h"
    not
    Code:
    #include "SDL/SDL.h"
    and then when building, you should be typing
    Code:
    g++ `sdl-config --cflags --libs` -o test-sdl test-sdl.cpp
    ./test-sdl
    Another mistake, is that you should be using
    Code:
    atexit(SDL_Quit)
    to guarantee that the library shutdown procedure is called, even when your application fails unexpectedly.

    Less important, you shouldn't be initializing SDL using everything when you are not using it, that only slows things down. You should also be checking the return calls of the SDL functions to see if they actually succeeded.

    Also, this tutorial doesn't really do anything, so I have added slightly to it (including the fixes I mentioned above):
    Code:
    #include <cstdlib>
    #include "SDL.h"
    
    int main( int argc, char* argv[])
    {
        // Initialize SDL, using only the video library.
        if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
            fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
                exit(1); 
        }
        atexit(SDL_Quit); //clean up the library when you are done with it.
        
        // Create a screen surface, and set the video mode 
        SDL_Surface *screen;
        screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
        if ( screen == NULL ) {
           fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
           exit(1);
        }
    
        SDL_Event event;
        bool running = true;
        // Loop forever, waiting for the user to quit
        while(running){
           while ( SDL_PollEvent(&event) ) {
              switch (event.type) {
                 case SDL_KEYDOWN:
                 case SDL_KEYUP:
                    if (event.key.type == SDL_KEYUP)
                       printf("RELEASED: ");
                    else
                       printf("PRESSED: ");
                     printf( "%s\n", SDL_GetKeyName(event.key.keysym.sym));
                 break;
                 case SDL_MOUSEMOTION:
                    printf("Mouse moved by %d,%d to (%d,%d)\n", 
                        event.motion.xrel, event.motion.yrel, event.motion.x, event.motion.y);
                  break;
                  case SDL_MOUSEBUTTONDOWN:
                       printf("Mouse button %d pressed at (%d,%d)\n",
                           event.button.button, event.button.x, event.button.y);
                   break;
                   case SDL_QUIT:
                      running = false;
                   }
               }
            }
    
        return 0;
    }
    Again, to compile and run type:
    Code:
    g++ `sdl-config --cflags --libs` -o test-sdl test-sdl.cpp
    ./test-sdl
    Last edited by hod139; November 22nd, 2006 at 02:37 PM.
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  3. #3
    Join Date
    Apr 2006
    Beans
    1,030
    Distro
    Ubuntu 9.10 Karmic Koala

    Talking Re: HOWTO: Setting Up SDL for Beginners in Games Development

    Good stuff!

    It nice to see people getting involved.

    BTW please read the title of the thread again as you might have missed the point of the HOWTO.

    Thanks for the example program and I hope you keep contributing.

    Mike

    P.S. I have seen programs using SDL.h and SDL/SDL.h in both and haven't seen any major differences in their performance when compiled
    Last edited by Mickeysofine1972; November 22nd, 2006 at 03:38 PM.

  4. #4
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: HOWTO: Setting Up SDL for Beginners in Games Development

    Quote Originally Posted by Mickeysofine1972 View Post
    BTW please read the title of the thread again as you might have missed the point of the HOWTO.
    Read it again, what did I miss?

    Thanks for the example program and I hope you keep contributing.
    Time allowing I'll contribute as much as I can.

    P.S. I have seen programs using SDL.h and SDL/SDL.h in both and haven't seen any major differences in their performance when compiled
    The issue is not performance, but portability.
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  5. #5
    Join Date
    Apr 2006
    Beans
    1,030
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: HOWTO: Setting Up SDL for Beginners in Games Development

    Ok I Concede to your high godlike knowledge of SDL but lets keep it simple for the sake of the beginners reading this 'Setting Up SDL for Beginners in Games Development'

    I really don't want this getting too complex on a HOWTO number one. I did make this deliberately excluding a lot of stuff for the sake of ease of learning the basics.

    BTW if you have a HOWTO of your own, I and all the other guys trying to learn would love to read it.

    Mike

    NOTE OTHER READERS: I plan to cover the input processing contained in the extra program provided by our friend in a later HOWTO or maybe you could do that one hod139?
    Last edited by Mickeysofine1972; November 22nd, 2006 at 04:07 PM.

  6. #6
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: HOWTO: Setting Up SDL for Beginners in Games Development

    Quote Originally Posted by Mickeysofine1972 View Post
    Ok I Concede to your high godlike knowledge of SDL but lets keep it simple for the sake of the beginners reading this 'Setting Up SDL for Beginners in Games Development'

    I really don't want this getting too complex on a HOWTO number one. I did make this deliberately excluding a lot of stuff for the sake of ease of learning the basics.

    BTW if you have a HOWTO of your own, I and all the other guys trying to learn would love to read it.

    Mike

    NOTE OTHER READERS: I plan to cover the input processing contained in the extra program provided by our friend in a later HOWTO or maybe you could do that one hod139?
    I wish I could claim godlike knowledge . Now I at least understand what you are doing; if you wish to discuss the event loop at a later time this is fine. I just find that tutorials that don't do anything tend to get ignored by users. However, I still stand by my other comments (using pkg-config, atexit, and checking SDL error messages), if you are going to write tutorials, make sure you teach good habits early.
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  7. #7
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: HOWTO: Setting Up SDL for Beginners in Games Development

    Also, check out: http://www.libsdl.org/intro.en/toc.html
    which contains a very decent introduction.

    Here is there introduction code
    Code:
    #include <stdlib.h>
    #include "SDL.h"
    
    main(int argc, char *argv[])
    {
        if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
            fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
            exit(1);
        }
        atexit(SDL_Quit);
    
        ...
    }
    As you can see, they use:
    Code:
    #include "SDL.h"
    , they check the return value of SDL_Init, and they use atexit. These habits need to be taught at the earliest stage of the tutorials if you want to keep writing them.
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  8. #8
    Join Date
    Apr 2006
    Beans
    1,030
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: HOWTO: Setting Up SDL for Beginners in Games Development

    Adjusted to suite

    Mike

  9. #9
    Join Date
    Apr 2006
    Beans
    1,030
    Distro
    Ubuntu 9.10 Karmic Koala

    Arrow Re: HOWTO: Setting Up SDL for Beginners in Games Development

    I'd just like to say that I am really pleased with response from you guys

    Keep up the good work! and keep contributing!

    We could have a game in the offing!

    Mike

  10. #10
    Join Date
    Nov 2005
    Location
    Taunton, England
    Beans
    931
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: HOWTO: Setting Up SDL for Beginners in Games Development

    nice work

Page 1 of 4 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •