sstream, type conversion, SDL, and bleh.

Hey guys,

Basically, what I'm trying to do is use SDL to make a window, then use SDL_ttf to print out the xy coordinates of the mouse in said window.

Now the 1st tricky part was converting the xy coordinates from integer values to const char*, as this is what SDL_ttf requires as its printing parameters, but I believe I achieved this through sstream.

Now the issue is that instead of the number being printed on the screen updating when I move the mouse, it just adds more numbers to the value of the string/constchar*. As you can see, I added SDL_FreeSurface to the beginning of the loop so that whenever the mouse moves, it SHOULD clear the original text... but it doesn't. My brain is tired and I'm hoping someone can help me finish this piece of code.

EDIT : So sorry, I guess I should have stated where the piece of code was that is giving me troubles. Lines 117 - 132.

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
#include <iostream>
#include <string>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <sstream>


using namespace std;

bool quit = false;

engine eng;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

const int FRAMES_PER_SECOND = 60;

SDL_Surface* screen = NULL;
SDL_Surface* Mousecoords;
TTF_Font* font = NULL;
SDL_Color textColor = {255,255,255};

SDL_Event event;

SDL_Rect camera = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT};

SDL_Surface *load_image(std::string filename)
{
    SDL_Surface* loadedimage = NULL;
    SDL_Surface* optimizedimage = NULL;
    loadedimage = IMG_Load(filename.c_str());
    if( loadedimage != NULL )
    {
        //Create an optimized surface
        optimizedimage = SDL_DisplayFormat( loadedimage );

        //Free the old surface
        SDL_FreeSurface( loadedimage );

        //If the surface was optimized
        if( optimizedimage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedimage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedimage->format, 0, 0xFF, 0xFF ) );
        }
    }
    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);
}

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;
    }
    if(TTF_Init()==-1)
    {
        return false;
    }
    SDL_WM_SetCaption("Where's my mouse?", NULL);
    return true;
}

bool load_files()
{
    font = TTF_OpenFont("Ubuntu-L.ttf",28);
    if(font == NULL)
    {
        return false;
    }
    return true;
}

void clean_up()
{
    SDL_FreeSurface(screen);
    SDL_FreeSurface(Mousecoords);
    TTF_CloseFont(font);
    TTF_Quit;
    SDL_Quit;
}

int main(int argc, char* args[])
{
    if(init()==false)
    {
       return 1;
    }
    if(load_files()==false)
    {
        return 2;
    }
    std::stringstream xcoord;
    std::stringstream ycoord;
    while(quit == false)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_MOUSEMOTION)
            {
                SDL_FreeSurface(Mousecoords);
                int x = 0;
                int y = 0;
                x = event.motion.x;
                y = event.motion.y;
                xcoord << x;
                ycoord << y;
                Mousecoords = TTF_RenderText_Solid(font, xcoord.str().c_str(), textColor);
                apply_surface(0,0,Mousecoords,screen);
                if(SDL_Flip(screen) == -1)
                {
                    return 0;
                }
            }
            if(event.type == SDL_QUIT)
            {
                quit = true;
            }
        }
        if(SDL_Flip(screen) == -1)
        {
            return 0;
        }

    }
    clean_up();

    return 0;
}
Last edited on
The problem is that you reuse the same stringstream each time so you will add more and more content to the stream. If you instead define the stringstream objects somewhere between line 118-124 you will get new empty stringstreams each time which is what you want.
Awesome, that worked great!

Applied, and found that the numbers were meshing together so I applied a background. Now working as intended. Thank you very much!
Topic archived. No new replies allowed.