Don't get why this wont work. (SDL_surface loading to class)

Ok code looks roughly like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class animation(){
    private:
    SDL_surface* image;
    
    public:
    animation( SDL_Surface* im);
    void print();
};

animation::animation(SDL_Surface* im){
    image = im;
}

void animation::print(){
    apply_surface(image);
}

//Then when i try to load in a variable
SDL_Surface* Frame;
animation anim(Frame);

anim.print(); //does nothing 


However if i assign image(in the class) as a particular surface it will work fine.

Thanks for any help.

Chris
How doesn't it work? What exactly is your problem? Compiler errors? Runtime exceptions? ...
Last edited on
look below
Last edited on
Might just give you my code to make it easier:

The first snippet doesnt print out the image i want but the second does

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
class animation{
    private:
    bool status;
    int xpos, ypos, frame, length;
    SDL_Surface* image;
    SDL_Rect rect;

    public:
    animation(int x, int y, int w, int h, SDL_Surface* im, int l);
    int getframe();
    void printimage();
    void animate();
};
animation::animation(int x, int y, int w, int h, SDL_Surface* im, int l){
    status = true;
    frame = -1;
    xpos = x;
    ypos = y;
    rect.x = 0; rect.y = 0; rect.w = w; rect.h = h;
    image = im;
    length = l;
}
void animation::printimage(){
    apply_surface(xpos, ypos, image, screen, &rect);//here apply_surface wont work while        using image (defined in the class) as my source
}
void animation::animate(){
    if (status == false) return;
    if (frame > length) status = false;
    frame += 1;
    rect.x = rect.w*frame;
    printimage();
}


However this does work:

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
SDL_Surface* explosion;

class animation{
    private:
    bool status;
    int xpos, ypos, frame, length;
    SDL_Surface* image; commented out
    SDL_Rect rect;

    public:
    animation(int x, int y, int w, int h, SDL_Surface* im, int l);
    int getframe();
    void printimage();
    void animate();
};
animation::animation(int x, int y, int w, int h, SDL_Surface* im, int l){
    status = true;
    frame = -1;
    xpos = x;
    ypos = y;
    rect.x = 0; rect.y = 0; rect.w = w; rect.h = h;
    image = im;
    length = l;
}
void animation::printimage(){
    apply_surface(xpos, ypos, explosion, screen, &rect);//now apply_surface works fine using a surface defined outside the class.
}
void animation::animate(){
    if (status == false) return;
    if (frame > length) status = false;
    frame += 1;
    rect.x = rect.w*frame;
    printimage();
}



This is after calls to animate.

Oh and thanks for the reply
Last edited on
Topic archived. No new replies allowed.