SDL question

Started by
7 comments, last by jmg5 20 years, 8 months ago
I am completely lost here. Basically what I am trying to do is get the ball to bounce around the screen. I can load the ball and see it, but when I use this function that I created the screen goes black. Can anyone point me in the right direction? What am I doing wrong?

void load_ball(SDL_Surface *ball, int x, int y)
{
     SDL_Rect dest;

     image = SDL_LoadBMP("ball.bmp");
     if (ball == NULL) {
          printf("Couldn't load BMP: %s\n", SDL_GetError());
          return;
     }

     dest.x = x;
     dest.y = y;
     dest.w = image->w;
     dest.h = image->h;
     SDL_BlitSurface(image, NULL, screen, &dest);
     
     int z = 1;
     while (z){
     x++;
     y++;
     if (x != 640 || y != 480)
     {
          x++;
          y++;
     }     
     SDL_UpdateRects(screen, 1, &dest);}
     SDL_FreeSurface(image);
}
All help is appreciated. [edited by - jmg5 on August 16, 2003 10:28:24 PM]
Advertisement
quote:Original post by jmg5
         int z = 1;     while (z){     x++;     y++;     if (x != 640 || y != 480)     {          x++;          y++;     }          SDL_UpdateRects(screen, 1, &dest);}     SDL_FreeSurface(image);}



How are you breaking out of the while loop here? I don''t see z being changed here nor do I see any escape condition for a break here. And I''m not sure I see a closing brace for this while loop. I''m guessing there''s more stuff in this function that you omitted?




--{You fight like a dairy farmer!}

--{You fight like a dairy farmer!}

I thinkg you net a Blit call in that loop somewhere. you also need some kind of exit from the loop.
Well it breaks out of the loop when you exit the program. I tried something like
void load_ball(SDL_Surface *ball, int x, int y){     SDL_Rect dest;     image = SDL_LoadBMP("ball.bmp");     if (ball == NULL) {          printf("Couldn''t load BMP: %s\n", SDL_GetError());          return;     }     dest.x = x;     dest.y = y;     dest.w = image->w;     dest.h = image->h;          int z = 1;     while (z){     x++;     y++;     if (x == 640)     {          x--;     }     if (y == 480)     {          y--;     }     SDL_BlitSurface(image, NULL, screen, &dest);     SDL_UpdateRects(screen, 1, &dest);     SDL_FreeSurface(image);      }}

Which I thought would work (but didn''t).

I am calling the function like this:
load_ball(ball, x, y);
in the main game loop.

I''ll keep trying ...

Well, the loop exits when you exit the program... but while it''s stuck in that loop, you aren''t handling any events, and therefore the only way to close the program is to open up Task Manager and close it manually. I''m not exactly sure what this is supposed to do either. You aren''t actually changing anything each time that loop iterates, besides the variables x and y, which are released after that function exits... you only need to update the screen ONCE, right after you blit the image to the screen.
ummmm, kogase dude
EDITED: ok new code that works now, i hope it helps you a bit to get a few hints.

of course its not the only way to do it, but in your function you should:
- clear old ball position,
- if the ball collides with a border -> invert velocity
- update ball position,
- draw ball

and SDL_Flip once in the loop or update your rect manually with SDL_UpdateRect, but i am not very familiar with that


#include "SDL.h"#include <iostream>using namespace std;/* Global Variables  :) */SDL_Surface		*screen = NULL;/* you could use double variables for speed and positions, easier with ints tho :) */int xSpeed = 3;int ySpeed = 2;/* - clear old ball position,   - if the ball collides with a border -> invert velocity   - update ball position,   - draw ball*/void move_ball( SDL_Surface *ball, SDL_Rect *dest ){   SDL_FillRect( screen, dest, 0 );   if ( (dest->x + dest->w) == 640) {        xSpeed *= -1;       }   if ( (dest->y + dest->h) == 480) {        ySpeed *= -1;    }    if (dest->x == 0) {        xSpeed *= -1;       }    if (dest->y <= 0) {        ySpeed *= -1;    }    dest->y += ySpeed;    dest->x += xSpeed;    SDL_BlitSurface( ball, NULL, screen, dest );}int main(int argc, char* argv[]){    SDL_Event		event;		    if(SDL_Init(SDL_INIT_VIDEO) < 0)    {       cout << "SDL Video initialisation failed :(\n             << SDL_GetError() << endl";       system("Pause");       return -1;    }    atexit(SDL_Quit);							    /* set wideo mode */    screen = SDL_SetVideoMode( 640,480,32, SDL_SWSURFACE );    if ( screen == NULL )     {        printf("Unable to set Video: %s\n", SDL_GetError());	return -1;    }    SDL_Surface* ball = NULL;    SDL_Rect dest;    /* Load ball bmp */    ball = SDL_LoadBMP("ball.bmp");    if (ball == NULL) {        printf("Couldn't load BMP: %s\n", SDL_GetError());        return -1;    }    /* starting point of the ball */    dest.x = 50;    dest.y = 50;    /* width and height of the bmp neede to check        collision with the window borders */    dest.w = ball->w;    dest.h = ball->h;       bool exit = false;    /* Main Loop */    while( exit != true )								    {        SDL_PollEvent(&event);        /* Stop the program if the Window is being closed */        if ( event.type == SDL_QUIT)             { exit = true;}		if ( event.type == SDL_KEYDOWN )				{            /* Stop the program when ESC is pressed */	    if ( event.key.keysym.sym == SDLK_ESCAPE )                { exit = true; }	    }                /* do your drawings/ updates here */        move_ball( ball, &dest );        /* you could only update the SDL_Rect who changed,            but its too fast anyway :)           SDL_Flip slows down the drawings nicely,           without doouble buffering this is like SDL_FillRect screen, NULL,0) which is pretty slow             (and OK for this demo) */        SDL_Flip( screen );    }        /* free allocated memory */    SDL_FreeSurface(ball);    return 0;}


where is the preview button anyway ??

[edited by - Lazzar on August 18, 2003 7:25:37 AM]

[edited by - Lazzar on August 18, 2003 12:34:21 PM]

[edited by - Lazzar on August 18, 2003 12:39:27 PM]
---------------------------------------------------------if god gave us the source code, we could change the world!
Thanks a lot, Lazzar. It works great.

Just two more questions:
1. How can I change the background color? (It defaults to black)

2. How can I change the title bar caption? (It defaults to SDL_App)

Thanks to everyone for the help.
Short answers:
1. SDL_FillRect
2. SDL_WM_SetCaption
Check the SDL documentation for more information about those functions.
default background color is black RGB = 0,0,0

if you want it another color, try before the main loop starts:
SDL_FillRect( screen, NULL, SDL_MapRGB( screen->format, 0,255,0) );

to set the background to Yellow.

then when you draw the ball, you could paint the last psition of the ball blue

void move_ball( SDL_Surface *ball, SDL_Rect *dest ){    SDL_FillRect( screen, dest, SDL_MapRGB( screen->format, 0,0,255) );     .    .    .



next functions you have to play with is
SDL_SetAlpha — Adjust the alpha properties of a surface

have fun playing with SDL

Lazzar

if god gave us the source code, we could change the world!

[edited by - Lazzar on August 19, 2003 3:04:14 AM]

[edited by - Lazzar on August 19, 2003 3:05:06 AM]
---------------------------------------------------------if god gave us the source code, we could change the world!

This topic is closed to new replies.

Advertisement