Need to learn SDL
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.
Your friend Google.com knows more than a few.
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
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
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
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?
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?
I found filepaths in xcode are a bit screwy. I use this function:
It seems to work
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 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).
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).
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.
Any suggestions?
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?
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!
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| "Learn C" ? | Marjock | 15 | 5,893 |
Jul 5, 2004 01:23 PM Last Post: Marjock |
|

