SDL String Input Problem

After my second term of C++ programming I thought I had finally escaped the clutches of SDL, turns out it reared its ugly head again. I have used SDL before but never had to receive String Inputs from the user. Unfortunately, that is the primary function of this GUI that I am building for another project that I finished up (amazing how much more quick it is to build a nice clean program that runs in a little DOS box than it is to build even a very simple GUI). All of this programs functionality has not been coded out yet, but basically for some reason the unicode based solution for string input I got from the fine tutorials at lazyfoo,net (see tutorial #23, Handling String Input) works when in its own project, but does not work when included with the larger GUI, here is the code for my GUI,

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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include "class.h"
#include "equipment.h"
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>

const int SCREEN_WIDTH = 682;
const int SCREEN_HEIGHT = 536;
const int SCREEN_BPP = 32;

bool quit        = false;
bool nameEntered = false;
bool rfidEntered = false;

int x = 0;
int y = 0;

ofstream outputFile;

// The Various Messages
SDL_Surface *message    = NULL;
SDL_Surface *message1   = NULL;
SDL_Surface *message2   = NULL;
SDL_Surface *message3   = NULL;

// The Menus
SDL_Surface *main_menu  = NULL;
SDL_Surface *accnt_menu = NULL;
SDL_Surface *cash_menu  = NULL;
SDL_Surface *lost_card  = NULL;

SDL_Surface *screen = NULL;

SDL_Event event;
SDL_Event event2;

TTF_Font *font = NULL;
SDL_Color color;

SDL_Color textColor = { 255, 255, 255 };

void accnt_men();

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 = SDL_DisplayFormat( loadedImage );

        SDL_FreeSurface( loadedImage );
    }

    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 );
}

class StringInput
{
    private:

    SDL_Surface *text;

    public:

    std::string str;

    StringInput();

    ~StringInput();

    std :: string get_string();

    void handle_input();

    void show_centered();

};

StringInput::StringInput()
{
    str = "well this sucks";

    text = NULL;

    SDL_EnableUNICODE( SDL_ENABLE );
}

StringInput::~StringInput()
{
    SDL_FreeSurface( text );

    //SDL_EnableUNICODE( SDL_DISABLE );
}

std :: string StringInput::get_string()
{
    return str;
};

void StringInput::handle_input()
{
	if( event.type == SDL_KEYDOWN )
	{
		std::string temp = str;

		if( str.length() <= 16 )
		{
			if( event.key.keysym.unicode == (Uint16)' ' )
			{
				str += (char)event.key.keysym.unicode;
			}

			else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
			{
				str += (char)event.key.keysym.unicode;
			}

			else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
			{
				str += (char)event.key.keysym.unicode;
			}

			else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
			{
				str += (char)event.key.keysym.unicode;
			}

		}

		if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
		{
			str.erase( str.length() - 1 );
		}

		if( str != temp )
		{
			SDL_FreeSurface( text );

			text = TTF_RenderText_Solid( font, str.c_str(), color );
		}
	}
}

void StringInput::show_centered()
{
    if( text != NULL )
    {
        apply_surface( ( SCREEN_WIDTH - text->w ) / 2, ( SCREEN_HEIGHT - text->h ) / 2, text, screen );

    }

}


StringInput name;

int main( int argc, char* args[] )
{
    color.r = 255;
    color.g = 0;
    color.b = 0;

    outputFile.open("accounts.txt");

    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;
    }

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

    if( screen == NULL )
    {
        return 1;
    }

    TTF_Init();

    //Set the window caption
    SDL_WM_SetCaption( "Grimshaw Automation", NULL );

    main_menu  = load_image( "Main_Menu.bmp" );
    accnt_menu = load_image( "Account_Menu.png");

    font = TTF_OpenFont( "FreeSerif.ttf", 28 );

    message = TTF_RenderText_Solid( font, "Please Enter The New RFID Number: ", color);

    while(quit == false)
    {
        apply_surface( 0, 0, main_menu, screen );

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

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

            else if( event.type == SDL_MOUSEBUTTONDOWN )
            {
                if(event.button.button == SDL_BUTTON_LEFT)
                {
                    x = event.button.x;
                    y = event.button.y;

                    // Add New Account Page

                    if(x >= 0 && x <= 361 )
                    {
                        if(y <= 221 && y >= 173)
                        {
                            accnt_men();

                        }

                        else if(y <= 258 && y >= 222)
                        {
                            // Add Cash Page
                        }

                        else if(y <= 301 && y >= 259)
                        {
                            // Lost Card Page
                        }

                        else if(y <= 373 && y >= 302)
                        {
                            // Activity Reports And Diagnostics
                        }

                        else if(y <= 441 && y >= 374)
                        {
                            // Enter Rates and Hours
                        }

                        else if(y <= 535 && y >= 442)
                        {
                            //Equipment Settings Table
                        }
                    }
                }
            }
        }
    }

    //Free the surfaces
    SDL_FreeSurface( main_menu  );
    SDL_FreeSurface( accnt_menu );
    SDL_FreeSurface( cash_menu  );
    SDL_FreeSurface( lost_card  );

    //Freeing the Messages
    SDL_FreeSurface( message );

    TTF_CloseFont( font );

    TTF_Quit();

    //Quit SDL
    SDL_Quit();

    outputFile.close();

    return 0;

}

void accnt_men()
{
    rfidEntered = false;

    apply_surface(0, 0, accnt_menu, screen);

    apply_surface(150, 250, message, screen);

    SDL_Flip(screen);

    while( rfidEntered == false )
    {
        while(SDL_PollEvent(&event))
        {
            name.handle_input();

            if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN ) )
            {
                rfidEntered = true;
            }
        }

    }

    outputFile << name.str;
}


To recap: code works in its own project, but not when included in another, to get code in question see Tutorial #23 on lazyfoo.net, thanks for the help!
Last edited on
Very Hopeful Bump
does not work

How doesn't it work? Can you provide a particular compiler or linker error? Or a runtime error? Or what is not happening as you expect. Try to step through with a debugger to see where the code stops doing what you want it to.
Sure Thing, basically , the string that is supposed to receive the input isn't receiving any input, from what I can tell in debugging, it gets into the code block where the unicode checks are in place, but none of the the unicode if statements are getting triggered, even though they should be.
Also, i just went ahead and took out the code that disables unicode, so it can't be that im accidentally turning off unicode or something like that.
Topic archived. No new replies allowed.