Need to learn SDL

clapton541
Unregistered
 
Post: #1
Im a beginner in a game programming but I know C, C++, and obj-c. I wanted to know if anyone knew of a really good SDL tutorial for C or C++ that really explained everything.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Your friend Google.com knows more than a few.
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #3
http://lazyfooproductions.com/SDL_tutorials/index.php
http://jnrdev.72dpiarmy.com/
http://osdl.sourceforge.net/OSDL/OSDL-0....penGL.html <-- Using SDL with OpenGL
http://www.meandmark.com/sdlopenglpart1.html <-- SDL and OpenGL specific for Mac OS X

Hopes these help! The first one is your best bet for learning SDL Smile
Quote this message in a reply
Member
Posts: 86
Joined: 2005.01
Post: #4
The libsdl site links to http://cone3d.gamedev.net, which is the tutorial I used.

Newest game: Glow, a sci-fi RPG with lots of zombie bashing. Get it: OS X
Quote this message in a reply
clapton541
Unregistered
 
Post: #5
Thanks a lot the cone3d tutorials really are helpful. I just have trouble with the second tutorial on loading and displaying images. Where should I put the image in the project folder to make it work? And also if its in the documents folder what should I type as the filepath?
Quote this message in a reply
clapton541
Unregistered
 
Post: #6
Actually a better way to put this question is that when I run it the screen is black. I added the images absolute paths and i even tried it with just the default and its always the same. What should I do?
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #7
I found filepaths in xcode are a bit screwy. I use this function:
Code:
char * resPath(char** argv, char* file )
{
    // returns the path to the "Resources" directory
    char    tempPath[1024];
    strncpy( tempPath, argv[0], 1023 );
    int    x = strlen(tempPath) -1;
    while( tempPath[x] != '/' && x > 0 )
        tempPath[x--] = 0;
    
    strncat( tempPath, "../Resources/", 1023 );
    return strncat( tempPath, file, 1023 );
}
It seems to work Smile
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
It doesn't.

If you search the boards, you'll find the real answer to how relative file paths work (it's very simple), and the real answer as to how to find your Resources folder (it's not as simple, but it always works).
Quote this message in a reply
clapton541
Unregistered
 
Post: #9
Ya thanks i figured it out its "Example.app/Contents/Resources/eximg.bmp"
Quote this message in a reply
clapton541
Unregistered
 
Post: #10
Also do you know whats wrong with this code? I added the bitmaps as an absolute path and copied the pictures into the folder. It works but the screen is just black. It's mostly based on the Cone3d tutorial and I got that to work.

Code:
#include <stdio.h>
#include <stdlib.h>

#include <SDL/SDL.h>

SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *screen;
int rspeed=1;
int rwidth = 150;
int rheight = 24;
int xpos=0, ypos=0;

int InitImages()
{
    back = SDL_LoadBMP("Pong!!.app/Contents/Resources/background.bmp");
    image = SDL_LoadBMP("Pong!!.app/Contents/Resources/rectangle.bmp");
    return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface(img, NULL, screen, &dest);

}

void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
    SDL_Rect dest;
    dest.x = x;
    dest.y = y;
    SDL_Rect dest2;
    dest2.x = x2;
    dest2.y = y2;
    dest2.w = w;
    dest2.h = h;
    SDL_BlitSurface(img, &dest2, screen, &dest);
}

void DrawBG()
{
    DrawIMG(back, 0, 0);
    
}

void DrawScene()
{
    DrawIMG(back, xpos-rspeed, ypos-rspeed, rwidth+rspeed*2, rheight+rspeed*2, xpos-rspeed, ypos-rspeed);
    DrawIMG(image, xpos, ypos);
    SDL_Flip(screen);
}
    

int main(int argc, char *argv[])
{
    
    
  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
  {
    printf("Unable to init SDL: %s\n", SDL_GetError());
    exit(1);
  }
  atexit(SDL_Quit);

  screen=SDL_SetVideoMode(640, 480, 32,SDL_HWSURFACE|SDL_DOUBLEBUF);
  if ( screen == NULL )
  {
    printf("Unable to set 640x480 video: %s\n", SDL_GetError());
    exit(1);
  }
  InitImages();
  DrawBG();
    
  int done=0;

  while(done == 0)
  {
    DrawScene();

    
    SDL_Event event;

    while ( SDL_PollEvent(&event) )
    {
      if ( event.type == SDL_QUIT )  {  done = 1;  }

      if ( event.type == SDL_KEYDOWN )
      {
        if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
      }
    }
   }
   return 0;
}


Any suggestions?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #11
Check the return value of SDL_LoadBMP, if it returns NULL it could not load the images otherwise it could.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
clapton541
Unregistered
 
Post: #12
Ok but if its NULL then how do I show the images?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #13
You can't, but knowing whether or not it's returning NULL will help you diagnose the problem by telling you whether or not that's the point of failure in your code.
Quote this message in a reply
clapton541
Unregistered
 
Post: #14
I figured out what was wrong. What I did was take pictures with a webcam which were jpgs and then I turned them into bitmaps so they didnt work.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  "Learn C" ? Marjock 15 6,698 Jul 5, 2004 01:23 PM
Last Post: Marjock