Archived

This topic is now archived and is closed to further replies.

Pong in SDL

This topic is 5702 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was at cone3d.gamedev.net trying to get bitmaps to load for my pong game I''m currently making....but can''t seem to get it to work. I can use it''s source, compile, and it''ll work. If I do it myself, it won''t work. Does anyone know what to do?

Share this post


Link to post
Share on other sites

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

#include <SDL/SDL.h>

SDL_Surface *image;
SDL_Surface *screen;
SDL_Surface *lod;

void DrawPixel(SDL_Surface *lod, int x, int y,
Uint8 R, Uint8 G, Uint8 B);
void Slock(SDL_Surface *lod)
{
if ( SDL_MUSTLOCK(lod) )
{
if ( SDL_LockSurface(lod) < 0 )
{
return;
}
}
}

void Sulock(SDL_Surface *lod)
{
if ( SDL_MUSTLOCK(lod) )
{
SDL_UnlockSurface(lod);
}
}

void DrawPixel(SDL_Surface *lod, int x, int y,
Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(lod->format, R, G, B);
switch (lod->format->BytesPerPixel)
{
case 1: // Assuming 8-bpp

{
Uint8 *bufp;
bufp = (Uint8 *)lod->pixels + y*lod->pitch + x;
*bufp = color;
}
break;
case 2: // Probably 15-bpp or 16-bpp

{
Uint16 *bufp;
bufp = (Uint16 *)lod->pixels + y*lod->pitch/2 + x;
*bufp = color;
}
break;
case 3: // Slow 24-bpp mode, usually not used

{
Uint8 *bufp;
bufp = (Uint8 *)lod->pixels + y*lod->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: // Probably 32-bpp

{
Uint32 *bufp;
bufp = (Uint32 *)lod->pixels + y*lod->pitch/4 + x;
*bufp = color;
}
break;
}
}

void DrawScene(SDL_Surface *lod)
{
Slock(lod);
for(int x=0;x<640;x++)
{
for(int y=0;y<480;y++)
{
DrawPixel(lod, x,y,y/2,y/2,x/3);
}
}
Sulock(lod);
SDL_Flip(lod);
}

int xpos=0,ypos=0;

int InitImages()
{
image = SDL_LoadBMP("Image1.bmp");
return 0;
}
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);
void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = 0;
dest.y = 0;
SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawScene()
{
DrawIMG(image, xpos, ypos);

SDL_Flip(screen);
}

int main(int argc, char *argv[])
{
Uint8* keys;
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to load 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();
int done=0;

while(done == 0)
{
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; }
}
}

keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { ypos -= 1; }
if ( keys[SDLK_DOWN] ) { ypos += 1; }
if ( keys[SDLK_LEFT] ) { xpos -= 1; }
if ( keys[SDLK_RIGHT] ) { xpos += 1; }
DrawScene();
}
return 0;
}

That''s the source of my program that i''m having problems with

Share this post


Link to post
Share on other sites
Heres your problem I think, you are putting wrong values into tthe destination rect, you arnt giving it a width or height and are always makign it at the top left, which from the rest of your code I assume you dont want, try this


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


Ballistic Programs

Share this post


Link to post
Share on other sites
And let me guess, I got to declare the x and y values also....is there a better tutorial place for SDL with BloodShed Dev-C++ besides cone3d.gamedev.net and www.openrpgs.com?

Share this post


Link to post
Share on other sites
Just do what I do. Read the API documentation. SDL really isn''t all that hard, in fact it is much easier than DirectX.

--Baseball isn''''t right. A man can NOT walk with four balls.

Share this post


Link to post
Share on other sites