SDL problem, why does the exe crash straight away

the problem is something to do with my attempt to colour key the surface "object" though i dont know what...heres the code the the color key line is highlited

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <sdl/sdl.h>
#include <sdl/sdl_image.h>
#include <string>

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

SDL_Rect wall;

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_Init(SDL_INIT_EVERYTHING);
bool quit = false;
int x = 0;
int y = 0;
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
wall.h=200;
wall.w=200;
wall.x=300;
wall.y=300;

 SDL_FillRect (screen,NULL, SDL_MapRGB( screen->format, 255, 255, 255));
 SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 77, 77, 77 ) );

 Uint32 colorkey = SDL_MapRGB(object->format, 255,255,255);
SDL_SetColorKey(object,SDL_SRCCOLORKEY,colorkey);////here is the line/////////////
 SDL_WM_SetCaption( "Can I Move the Dot though?", NULL );
object = load_image("object.png");

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;
               case SDLK_ESCAPE: quit=true;break;
            }
       }
     else if (event.type == SDL_QUIT)
         {
             quit = true;
         }
       }

if (velx<0)
{
    velx+=5;
}
else if (velx>SCREEN_WIDTH-200)
{
    velx-=5;
}
else if (velx==wall.x+wall.w)
{
    velx-=5;
}
SDL_Delay (1);

apply_surface (velx +=x,vely +=y,object,screen);
SDL_Flip (screen);
}
SDL_FreeSurface (object);
return 0;
}
Last edited on
The problem is on line 59. You have to initialize "object" before you can start to implement it...it should work if you put it on line 55
Topic archived. No new replies allowed.