This sdl code just blinks on then off again, cant think why, must be a flaw in a while loop

it is supposed to crudley move an object arround, can you see a flaw? it just flashes on then off real quick, would really appreciate the help.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <sdl/sdl.h>
#include <sdl/sdl_image.h>
#include <string>

//SDL_Surface *background=NULL;
SDL_Surface *screen=NULL;
SDL_Surface *object=NULL;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;


SDL_Event event;

SDL_Surface *load_image( std::string filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
return optimizedImage;
}

void apply_surface(int x,int y, SDL_Surface *source,SDL_Surface *Destination,SDL_Rect *clip=NULL)
{
    SDL_Rect offset;
    offset.x=x;
    offset.y=y;
    SDL_BlitSurface (source,clip,Destination,&offset);
}


int main(int argc,char *args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
bool quit = false;
int x = 40;
int y = 40;
 screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
 SDL_WM_SetCaption( "Can I Move the Dot though?", NULL );
object = load_image("object.xcf");
//background = load_image(  );
//apply_surface (SCREEN_WIDTH,SCREEN_HEIGHT,background,screen);
while (quit!=true)
{
    if (SDL_PollEvent (&event))
    {
        if (event.type ==SDL_KEYDOWN)
        {
            switch (event.key.keysym.sym)
            {
                case SDLK_UP: x=x-5;break;
                case SDLK_DOWN:x=x+5;break;
                case SDLK_LEFT:y=y-5;break;
                case SDLK_RIGHT:y=y+5;break;
            }
        }
       else if (event.type ==SDL_KEYUP)
       {
            switch (event.key.keysym.sym)
            {
                case SDLK_UP:x=x+5;break;
                case SDLK_DOWN:x=x-5;break;
                case SDLK_LEFT:y=y+5;break;
                case SDLK_RIGHT:y=y-5;break;
            }
       }
     else if (event.type == SDL_QUIT)
         {
             quit = true;
         }
       }


SDL_Delay (1000);
apply_surface (x,y,object,screen);
SDL_Flip (screen);
}
SDL_FreeSuface (object);
return 0;
}
Make sure the image is loading correctly. IMG_Load will return a null pointer if it fails to load the image. You shouldn't be passing a null pointer to SDL_DisplayFormat so that is likely to crash the program if you do.
yep fantastic, could you also tell me why the updated program crashes, its to do with my trying to fill the screen, its an issue with rect, im not sure how it applies to the screen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <sdl/sdl.h>
#include <sdl/sdl_image.h>
#include <string>

//SDL_Surface *background=NULL;
SDL_Surface *screen=NULL;
SDL_Surface *object=NULL;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;


SDL_Event event;

SDL_Surface *load_image( std::string filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
return optimizedImage;
}

void apply_surface(int x,int y, SDL_Surface *source,SDL_Surface *Destination,SDL_Rect *clip=NULL)
{
    SDL_Rect offset;
    offset.x=x;
    offset.y=y;
    SDL_BlitSurface (source,clip,Destination,&offset);
}



int main(int argc,char *args[])
{
int velx = 0;
int vely = 0;
SDL_FillRect (screen,NULL, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF));
SDL_Init(SDL_INIT_EVERYTHING);//above if applied makes it not workify//
bool quit = false;
int x = 0;
int y = 0;
 screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
 SDL_WM_SetCaption( "Can I Move the Dot though?", NULL );
object = load_image("object.png");
//background = load_image(  );
//apply_surface (SCREEN_WIDTH,SCREEN_HEIGHT,background,screen);
while (quit!=true)
{
    if (SDL_PollEvent (&event))
    {
        if (event.type ==SDL_KEYDOWN)
        {
            switch (event.key.keysym.sym)
            {
               case SDLK_UP: y -= 10 / 2; break;
               case SDLK_DOWN: y +=10  / 2; break;
               case SDLK_LEFT: x -=  10/ 2; break;
               case SDLK_RIGHT: x += 10 / 2; break;
            }
        }
       else if (event.type ==SDL_KEYUP)
       {
            switch (event.key.keysym.sym)
            {
                 case SDLK_UP: y += 10 / 2; break;
               case SDLK_DOWN: y -=10  / 2; break;
               case SDLK_LEFT: x +=  10/ 2; break;
               case SDLK_RIGHT: x -= 10 / 2; break;
            }
       }
     else if (event.type == SDL_QUIT)
         {
             quit = true;
         }
       }


SDL_Delay (1);
apply_surface (velx=velx+x,vely=vely+y,object,screen);
SDL_Flip (screen);
}
SDL_FreeSurface (object);
return 0;
}
Last edited on
It's crashing because you are trying to draw a rectangle on the screen before you have called SDL_Init and SDL_SetVideoMode.
SDL_FillRect (screen,&therect,SDL_MapRGB(screen->format,0,0,0));
beautiful peter, i should read up a little more on how it works rather than follow the tutorials, thanks for your help...how long it take you to getthe hang of too?
Topic archived. No new replies allowed.