SDL Help please

Ok so i instaled SDL into code blocks and im happy with how easy it is, now i was following a tutorial and he just lumped everything into main which i cannot stand so im trying to group everything into its own category but im having problems with putting my image pice of code in a function.

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
#include <SDL.h>
#include "EngineClass.h"

void KeyInput(SDL_Rect &offset)
{
     Uint8 *keyStates = SDL_GetKeyState(NULL);

        if(keyStates[SDLK_w])
        {
            offset.y -= 1;
        }
        if(keyStates[SDLK_a])
        {
            offset.x -= 1;
        }
        if(keyStates[SDLK_s])
        {
            offset.y += 1;
        }
        if(keyStates[SDLK_d])
        {
            offset.x += 1;
        }
}


void ImageHandling(SDL_Surface &image)
{
    image = SDL_LoadBMP("Test.bmp");
}

int main(int argc, char *argv[])
{
    bool running = true;
    SDL_Event occur;
    SDL_Surface *image;
    SDL_Rect offset;
    Uint32 colorkey = SDL_MapRGB(image->format, 255, 0, 255);
    SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
    SDL_SetAlpha(image, SDL_SRCALPHA, 255);
    offset.x = 10;
    offset.y = 10;

    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        running = false;
    }

    SDL_Surface *screen;

    screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);

    SDL_WM_SetCaption("This is a title, if you dont like it than fuck you", NULL);

    if(screen == NULL)
    {
        running = false;
    }

    while(running == true)
    {
        SDL_PollEvent(&occur);

        if(occur.type == SDL_QUIT)
        {
            running = false;
        }

        KeyInput(offset);

        SDL_FillRect(screen, NULL, 0);

        SDL_BlitSurface(image, NULL, screen, &offset);

        SDL_Flip(screen);
    }

    SDL_Quit();
    return 0;
}



error

C:\Users\Chay\Desktop\SDL Test\main.cpp||In function 'void ImageHandling(SDL_Surface&)':|
C:\Users\Chay\Desktop\SDL Test\main.cpp|29|error: no match for 'operator=' in 'image = SDL_LoadBMP_RW(SDL_RWFromFile(((const char*)"Test.bmp"), ((const char*)"rb")), 1)'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\SDL_video.h|96|note: candidates are: SDL_Surface& SDL_Surface::operator=(const SDL_Surface&)|
||=== Build finished: 1 errors, 0 warnings ===|
1
2
3
4
void ImageHandling(SDL_Surface &image)
{
    image = SDL_LoadBMP("Test.bmp");
}


Change function to take SDL_Surface pointer instead:

1
2
3
4
void ImageHandling(SDL_Surface * image)
{
    image = SDL_LoadBMP("Test.bmp");
}
I tried that but when i run the program it crashes.
If you want the function to modify the pointer passed to the function you should pass the pointer by reference.
1
2
3
4
void ImageHandling(SDL_Surface*& image)
{
	image = SDL_LoadBMP("Test.bmp");
}


You also need to actually call the function.
OK i managed to fix the errors but now my window just flashes on the screen and closes?

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.h>
#include "EngineClass.h"

void KeyInput(SDL_Rect &oSet)
{
     Uint8 *keyStates = SDL_GetKeyState(NULL);

        if(keyStates[SDLK_w])
        {
            oSet.y -= 1;
        }
        if(keyStates[SDLK_a])
        {
            oSet.x -= 1;
        }
        if(keyStates[SDLK_s])
        {
            oSet.y += 1;
        }
        if(keyStates[SDLK_d])
        {
            oSet.x += 1;
        }
}


void ImageHandling(SDL_Rect &P, SDL_Surface &IMG)
{
    SDL_Rect offset;
    SDL_Surface *image;
    image = SDL_LoadBMP("Test.bmp");
    Uint32 colorkey = SDL_MapRGB(image->format, 255, 0, 255);
    SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
    offset.x = 10;
    offset.y = 10;
}

void CreateWindow(SDL_Surface &S)
{
    SDL_WM_SetCaption("This is a title, if you dont like it than fuck you", NULL);
    SDL_Surface *Screen;
    Screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
}

int main(int argc, char *argv[])
{
    bool running = true;
    SDL_Event occur;
    SDL_Surface mainScreen;
    SDL_Rect Position;
    SDL_Surface image;

    CreateWindow(mainScreen);
    ImageHandling(Position, image);
    KeyInput(Position);

    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        running = false;
    }

    if(&mainScreen == NULL)
    {
        running = false;
    }

    while(running == true)
    {
        SDL_PollEvent(&occur);

        if(occur.type == SDL_QUIT)
        {
            running = false;
        }

        KeyInput(Position);

        SDL_FillRect(&mainScreen, NULL, 0);

        SDL_BlitSurface(&image, NULL, &mainScreen, &Position);

        SDL_Flip(&mainScreen);
    }

    SDL_Quit();
    return 0;
}
Last edited on
CreateWindow and ImageHandling doesn't do anything with the function parameters.
im lost so what do i do? should i just make a class and put the variables and all the creation data in there and then use it?
Last edited on
1
2
3
4
5
void CreateWindow(SDL_Surface* S)
{
    S = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
    SDL_WM_SetCaption("This is a title, if you dont like it than fuck you", NULL);    
}

1
2
3
4
5
6
7
8
void ImageHandling(SDL_Rect &P, SDL_Surface* IMG)
{    
    IMG = SDL_LoadBMP("Test.bmp");
    Uint32 colorkey = SDL_MapRGB(IMG->format, 255, 0, 255);
    SDL_SetColorKey(IMG, SDL_SRCCOLORKEY, colorkey);
    P.x = 10;
    P.y = 10;
}


Ok this is too much of a headach, more than its worth. I just tried putting everything back the way it was and now when i try to run the program it just crashes, i dont get any compile errors or anything. It just says SDL Test has stopped working.

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
#include <SDL.h>
#include "EngineClass.h"

void KeyInput(SDL_Rect &offset)
{

}


int main(int argc, char *argv[])
{
    bool running = true;
    SDL_Event occur;
    SDL_Surface *image;
    Uint8 *keyStates = SDL_GetKeyState(NULL);
    SDL_Rect offset;
    Uint32 colorkey = SDL_MapRGB(image->format, 255, 0, 255);
    SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
    SDL_SetAlpha(image, SDL_SRCALPHA, 255);
    image = SDL_LoadBMP("Test.bmp");
    offset.x = 10;
    offset.y = 10;

    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        running = false;
    }

    SDL_Surface *screen;

    screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);

    SDL_WM_SetCaption("This is a title", NULL);

    if(screen == NULL)
    {
        running = false;
    }

    while(running == true)
    {
        SDL_PollEvent(&occur);

        if(occur.type == SDL_QUIT)
        {
            running = false;
        }

        if(keyStates[SDLK_w])
        {
            offset.y -= 1;
        }
        if(keyStates[SDLK_a])
        {
            offset.x -= 1;
        }
        if(keyStates[SDLK_s])
        {
            offset.y += 1;
        }
        if(keyStates[SDLK_d])
        {
            offset.x += 1;
        }

        KeyInput(offset);

        SDL_FillRect(screen, NULL, 0);

        SDL_BlitSurface(image, NULL, screen, &offset);

        SDL_Flip(screen);
    }

    SDL_Quit();
    return 0;
}
You use functions like SDL_SetColorKey() and SDL_SetAlpha() before initializing SDL. So you should put lines 24-26 somewhere before you use one of that SDL functions.
@Fransje
When you define the functions like that the pointers that is passed to the functions will be copies inside the functions. Changes made to the copies will not affect the original pointers that was passed to the function.

@Ch1156
I recommend that you wrap these kind of things inside classes but you are clearly not on that level yet so consider returning pointers from functions instead of letting the function set the pointer. Look at how the load_image function is implemented in the Lazy Foo SDL tutorial.
http://lazyfoo.net/SDL_tutorials/lesson02/index.php
Hm, so it should be implemented like this: void CreateWindow(SDL_Surface* &S) ?
I know how to use classes, sort of. I wouldnt say my knowledge of classes is concrete but i can set one up effectively, now pointers is where things get shaky for me as i just started learning them a week ago. I know how pointers work and the basic concept of them though but as for C++ i have been studying it for 2 years self taught. Im just going to follow the tutorials in the link you gave me and hopefully i can get the hang of it. Thanks.
Topic archived. No new replies allowed.