SDL troubles

Okay, so I'm not exactly a "1 hour into tuts" beginner, but I am still pretty new and was hoping someone could help me with a little bit of SDL trouble.

SDL of course is the library I'm using to pull graphics up, and my problem in particular is with a sub library of SDL - SDL_ttf.

What's happening - Code runs then returns 3. I ran the debugger, and it pops up 6 call stacks @ line 134 -

I also believe there may be a problem with TTF_Init() (line 76)

Debugger call stacks :

1
2
3
4
5
6
7
Nr 0 | Address 6F4C2A9D| Function TTF_SizeUNICODE()              | File path/to/my/bin/debug/SDL_ttf.dll | Line |
Nr 1 | Address 6F4C2E1F| Function TTF_SizeUNICODE_Solid()        | File path/to/my/bin/debug/SDL_ttf.dll | Line |
Nr 2 | Address 6F4C3253| Function TTF_RenderText_Solid()         | File path/to/my/bin/debug/SDL_ttf.dll | Line |
Nr 3 | Address 004016F3| Function SDL_main(argc=1, args=0x382f98)| File /main.cpp| Line 134|
Nr 4 | Address 00402399| Function                                | File ./src/main/win32/SDL_win32_main.c| Line 210|
Nr 5 | Address 00000000| Function 0x00000001 in ??()             | File                                  | Line |
Nr 6 | Address 00000000| Function -x--392f98 in ??()             | File                                  | Line |


And as for my code itself - main.cpp :

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>


//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The surfaces that will be used
SDL_Surface *image = NULL;
SDL_Surface *background = NULL;
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//TTF - Font
TTF_Font *font = NULL;
//TTF - Color of font
SDL_Color textColor = {255,255,255};
SDL_Surface *load_image( std::string filename )
{
    //Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;
    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;
    //Load the image
    loadedImage = IMG_Load( filename.c_str() );
    //If nothing went wrong in loading the image
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );
         //Free the old image
        SDL_FreeSurface( loadedImage );
        //If the image was optimized just fine
        if(optimizedImage != NULL)
        {
            //Map the color key
            Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0, 0xff, 0xff);
            //Set all pizels of color R 0, G 0xFF, B 0xFF to be transparent
            SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
        }
    }
    //Return the optimized image
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;
    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;
    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}

bool init()
{
    //Initialize all SDL subsystems
    if(SDL_Init(SDL_INIT_EVERYTHING)==-1)
    {
        return false;
    }
    //set up screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP, SDL_SWSURFACE );
    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;
    }
    //Initialize SDL_ttf
    if((TTF_Init()) == -1)
    {
        return false;
    }
    //Set the window caption
    SDL_WM_SetCaption( "TTF - Testing", NULL );
    //If everything initialized fine
    return true;
}

bool load_files()
{
    //Load the image
    image = load_image("hello.bmp");
    background = load_image("grass_sampling.bmp");
    TTF_OpenFont("test.ttf", 28);
    //If there was an error loading the image
    if(image == NULL)
    {
        return false;
    }
    //If there was an error loading the background
    if(background == NULL)
    {
        return false;
    }
    //If everything loaded fine
    return true;
}

void clean_up()
{
    //Free the loaded image
    SDL_FreeSurface(image);
    SDL_FreeSurface(background);
    //Close the font
    TTF_CloseFont(font);
    //Quit SDL_ttf
    TTF_Quit();
    //Quit SDL
    SDL_Quit();
}

int main( int argc, char* args[] )
{
    //Make sure the program waits for a quit
    bool quit = false;
    //Initialize
    if(init()==false)
    {
        return 1;
    }
    //Load the files
    if( load_files() == false )
    {
        return 2;
    }
    //Render the text
    message = TTF_RenderText_Solid( font, "The quick brown fox jumps over the lazy dog", textColor );
    //If there was an error rendering the text
    if(message == NULL)
    {
        return 4;
    }
    //Apply the surface to the screen
    apply_surface( 0, 0, background, screen );
    apply_surface (0,0,image,screen);
    apply_surface (0,0,message,screen);
    //update screen
    SDL_Flip(screen);
    //While the user hasn't quit
    while(quit == false)
    {
        //while there's an event to handle
        while(SDL_PollEvent(&event))
        {
            //If the user has Xed out the window
            if(event.type == SDL_QUIT)
            {
                //Quit the program
                quit = true;
            }
        }
    }
    //Free the surface and quit
    clean_up();

        //Quit SDL
    SDL_Quit();
    return 0;
}

Topic archived. No new replies allowed.