SDL Trouble

I'm not sure if it's appropriate to post SDL problems here but I thought I'd try. I don't know much about SDL but I'm trying to simply blit something on the screen but it crashes before it applies the surface. Here is the code with a status log I created to show what the last action the program succeeded before it failed. When it runs it loads the files then fails.

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include <fstream>

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;

SDL_Event event;

SDL_Surface *load_image(std::string filename)
{
    SDL_Surface *loadedImage = NULL;
    SDL_Surface *optimizedImage = NULL;

    loadedImage = IMG_Load(filename.c_str());

    if (loadedImage != NULL)
    {
        optimizedImage = loadedImage;

        SDL_FreeSurface(loadedImage);
    }

    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface( source, NULL, destination, &offset );
}

bool init()
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
    return false;

    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

    if (screen == NULL)
    return false;

    SDL_WM_SetCaption("My First SDL App", NULL);

    return true;
}

bool load_files()
{
    image = load_image("first.png");

    if (image == NULL)
    return false;

    return true;
}

void clean_up()
{
    SDL_FreeSurface(image);

    SDL_Quit();
}

int main(int argc, char* args[])
{
    std::fstream statuslog;
    statuslog.open ("statuslog.txt", std::fstream::out);
    statuslog << "Opening log...\n";
    statuslog.close();

    bool quit = false;

    statuslog.open ("statuslog.txt", std::fstream::out);
    statuslog << "Beginning statuslog...\n";
    statuslog.close();

    if (init() == false)
    return 1;

    statuslog.open ("statuslog.txt", std::fstream::out);
    statuslog << "System has been intialized\n";
    statuslog.close();

    if (load_files() == false)
    return 1;

    statuslog.open ("statuslog.txt", std::fstream::out);
    statuslog << "Files have been loaded\n";
    statuslog.close();

    apply_surface(0, 0, image, screen);

    statuslog.open ("statuslog.txt", std::fstream::out);
    statuslog << "Surface has been applied\n";
    statuslog.close();

    if (SDL_Flip(screen) == -1)
    return 1;

    statuslog.open ("statuslog.txt", std::fstream::out);
    statuslog << "Screen has been flipped\n";
    statuslog.close();

    while (quit == false)
    {
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            quit = true;
        }
    }

    statuslog.open ("statuslog.txt", std::fstream::out);
    statuslog << "The process has been completed successfully\n";
    statuslog.close();

    clean_up();

    return 0;
}


If you have any idea on what I'm doing wrong or where else I can post this question it would be appreciated.

Look at your load_image function.

You see if the loaded surface is valid, and then you set the address of another surface to the address of the loaded surface, then you free that surface. You are effectively deleting both surfaces at the same time(both of them point to the same surface). After that you try to use a surface that does not exist.

I think what you want to do is this:

optimizedimage = SDL_DisplayFormat(loadedimage);

This will optimize the blitting of sprites because SDL does not have to convert the surface to a valid format when blitting.

Not this:
optimizedImage = loadedImage;


You might also want to read this:
http://lazyfoo.net/SDL_tutorials/lesson02/index.php
Thanks a lot. I have been using those tutorials but tried to do without looking at the source just to try. I forgot about that part. Thanks again.
Just as another suggestion, I am pretty sure you do not need to reopen your file stream each time you need to write to the file. Just open it once at the beginning of your program, and close once it at the end.
I actually do need to because if there is a crash it wouldn't have worked because the file was never successfully closed. I close it each time so if there is a crash the log is still updated. Otherwise I would be wasting a lot of time :)
Oh, ok.

I didn't know that fstream updates the file after it is closed.
Just flush it, that's what std::flush is for. Opening/closing files like that is a waste of time.
Topic archived. No new replies allowed.