Is there a way to erase the contents of a stringstream?

closed account (yCf9216C)
I think I need to replace the contents of a stringstream object, because when I run the program,the text which gets put on tothe screen flows off the screen with each subsequent tick. I just need to make the current time appear on the screen.

The code:

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include"stdafx.h"
#include"SDL.h"
#include"SDL_ttf.h"
#include"SDL_image.h"
#include<vector>
#include <string>
#include <sstream>
using namespace std;

const int SCREEN_WIDTH = 1920;
const int SCREEN_HEIGHT = 1080;
const int SCREEN_BPP = 32;

SDL_Surface *background = NULL;
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *boxA = NULL;
SDL_Surface *boxB = NULL;
SDL_Surface *collideMessage = NULL;
SDL_Surface *missMessage = NULL;
SDL_Surface *seconds = NULL;

//The event structure
SDL_Event event;

//The font
TTF_Font *font = NULL;

//The color of the font
SDL_Color textColor = { 0, 0, 0 };

SDL_Rect boxAz;
SDL_Rect boxBz;
bool collide;
bool quit = false;

bool check_collision();

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = SDL_LoadBMP( filename.c_str() );

    //If the image loaded
    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 the optimized surface
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the 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( "Press an Arrow Key", NULL );

    //If everything initialized fine
    return true;
}

bool load_files()
{
    //Load the background image
    background = load_image( "background.bmp" );
	boxA = load_image( "boxA.bmp" );
	boxB = load_image( "boxB.bmp" );

    font = TTF_OpenFont( "lazy.ttf", 20 );
    //Open the font

    //If there was a problem in loading the background
	if( background == NULL || boxA == NULL || boxB == NULL )
    {
        return false;
    }

    //If there was an error in loading the font
    if( font == NULL )
    {
        return false;
    }

    //If everything loaded fine
    return true;
}

void clean_up()
{
    //Free the surfaces
    SDL_FreeSurface( background );
	SDL_FreeSurface( boxA );
    SDL_FreeSurface( boxB );
	SDL_FreeSurface( missMessage );
	SDL_FreeSurface( collideMessage );

    //Close the font
    TTF_CloseFont( font );

    //Quit SDL_ttf
    TTF_Quit();

    //Quit SDL
    SDL_Quit();
}

int main ( int argc, char* args[] ) {
	
	std::stringstream time;

	Uint32 start = 0;
	
	bool running= true;

	start = SDL_GetTicks();

	if (init() == false)  {
		return false;
	}
	if (load_files() == false ) {
		return false;
	}

	collideMessage = TTF_RenderText_Solid( font, "Collision detected!", textColor );
    missMessage = TTF_RenderText_Solid( font, "No collisions detected.", textColor );

	boxAz.x = 300;
	boxAz.y = 300;

	boxBz.x = 1000;
	boxBz.y = 300;
	message = missMessage;

	//While the user hasn't quit
    while( quit == false )
{//If message needs to be displayed
        if( message != NULL )
        {
			check_collision();

			//Apply the background to the screen
            apply_surface( 0, 0, background, screen );

            //Apply the message centered on the screen
            apply_surface( 20,20, message, screen );

			apply_surface ( boxAz.x, boxAz.y, boxA, screen);
			apply_surface (boxBz.x, boxBz.y, boxB, screen);

            //Null the surface pointer
        }        //Update the screen
			//If there's an event to handle
        if( SDL_PollEvent( &event ) )
		{
            //If the user has Xed out the window
             if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
			 }
			 if  (event.key.keysym.sym == SDLK_s )
				 if (running == true ) {
					 running = false;
					 start = 0;
				 }
				 else if (running == false ) {

					 running = true;
					 start = SDL_GetTicks();
				 }
		}

			Uint8 *keystates = SDL_GetKeyState (NULL);
			if ( keystates[ SDLK_UP ] ) {
				boxAz.y -= 20;
			}
			else if ( keystates[ SDLK_DOWN ]) {
				boxAz.y += 20;
			}
			else if( keystates[ SDLK_LEFT]) {
				boxAz.x -= 20;
			}
			else if ( keystates[ SDLK_RIGHT ]) {
				boxAz.x += 20;
			} 

					if (running == true ) {
			time << "Timer: " << SDL_GetTicks() - start; 
		}
		seconds = TTF_RenderText_Solid( font, time.str().c_str(), textColor);

		apply_surface ( 300,20,seconds,screen);

		time << " ";

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

    }

    //Clean up
    clean_up();
	return 0;
}

bool check_collision() { //The sides of the rectangles 
	int leftA,leftB; 
	int rightA, rightB;
	int topA, topB;
	int bottomA, bottomB;
	//Go through the A boxes 
		//Calculate the sides of rect A 
		leftA = boxAz.x; 
		rightA = boxAz.x + boxAz.w; 
		topA = boxAz.y;
		bottomA = boxAz.y + boxAz.h; 
		//Go through the B boxes
			//Calculate the sides of rect B
			leftB = boxAz.x; 
			rightB = boxBz.x + boxBz.w; 
			topB = boxBz.y; 
			bottomB = boxBz.y + boxBz.h;
			//If no sides from A are outside of B 
			if( ( ( bottomA <= topB ) || ( topA >= bottomB ) || ( rightA <= leftB ) || ( leftA >= rightB ) ) == false ) {
				//A collision is detected 
				message = collideMessage;
				return true; } 

	//If neither set of collision boxes touched
			message = missMessage;
				return false; 
}


Keep in mind this is largely pointless, and simply an experiment.

Also I don't entirely understand stringstreams.
Last edited on
I don't know if you can reset stringstreams. What you could do instead is define the stringstream object in the smallest scope possible where it is being used. Here it would be inside the loop while( quit == false ).
closed account (yCf9216C)
Hey, that worked, thank you.
-
Last edited on
@knuth - clear() just clears the error flags. It doesn't affects the content of the stream.
Last edited on
Topic archived. No new replies allowed.