I was following this example (Tutorials/TextInput - SDL Wiki') but strcat text_input crashes the program for some reason, any ideas?
Code:char *text_input; ... case SDL_TEXTINPUT: { strcat(text_input, event.text.text); } break;
I was following this example (Tutorials/TextInput - SDL Wiki') but strcat text_input crashes the program for some reason, any ideas?
Code:char *text_input; ... case SDL_TEXTINPUT: { strcat(text_input, event.text.text); } break;
In order to strcat to text_input it must be pointing to some allocated memory. It also must contain a valid (possibly zero-length) C-style (i.e., zero-terminated) string.
You would also need to keep track of how much text has been stored in it so far and realloc as necessary, and eventually free the memory. Usually you would learn this kind of thing with console programs and then move on to SDL.Code:text_input = malloc(200); *text_input = 0; // set first byte to '\0'
The example from your link is obviously incomplete. Note the /* ... */. It's also noteworthy that the variables are declared extern, usually indicating that they are defined in another source/object file.
Thanks that worked. There is another case of pointer trickery if you wouldn't mind helping.
This is a shared resource: load_texture("textures/btn_gray_active.bmp", &texture_gray_btn);
Now i want to save texture_gray_btn inside a pointer in a struct, i.e. just the reference so i can free it independantly from the button struct. In this case i want to save it here: SDL_Texture** P_texture_selected;
I'm not sure how to allocate and deallocate the button and how to properly Query the texture pointer. Any direction would be much appreciated.
and a test would call the function like so:Code:typedef struct button { SDL_Texture** p_texture_normal; SDL_Texture** P_texture_selected; SDL_Texture* texture_btn_txt; SDL_Rect rect_box; SDL_Rect rect_text; bool selected; } struct_button; struct_button createButton(SDL_Texture** tex_norm, SDL_Texture** tex_select, char* label, TTF_Font *font, int font_size, int x, int y) { struct_button this; this.rect_box.x = x; this.rect_box.y = y; this.p_texture_normal = tex_norm; SDL_QueryTexture(/* The pointer goes here*/, NULL, NULL, &this.rect_box.w, &this.rect_box.h); this.selected = false; return this; } void destroyButton(struct_button this) { }
I'm thinking this is the correct way of doing it, but I'm not sure.Code:struct_button someButton = createButton(&texture_main_menu, NULL, "\0", NULL, 0, 0, 0); printf("%d %d\n", someButton.rect_box.w, someButton.rect_box.h);
Code:this.p_texture_normal = *tex_norm;
Last edited by Tiago Redaelli; 01-11-2017 at 07:03 PM.
It seems to be the double-pointers that are confusing you, but I don't understand why the textures are double pointers in the first place. They should probably be single pointers. Maybe you're confused by the double-pointer that is used in the load_texture function, but it uses a double pointer there so that it can modify the pointer in the caller.
You could even get rid of the double-pointer in load_texture by passing back a pointer instead of passing in a double-pointer. This seems more natural to me:
BTW,Code:SDL_Texture *load_texture(const char *name) { SDL_Texture *t; ... return t; } SDL_Texture *texture_gray_btn = load_texture("textures/btn_gray_active.bmp");
Calling your button "struct_button" eliminates the advantage of the typedef since without the typedef it would be called "struct button" (exactly the same but without the underscore). Structs are usually named with the first letter of each word in uppercase, so Button would be a good name:
Also, you don't need to write "\0". A plain old "" will do since a string literal adds a zero-byte at the end automatically.Code:typedef struct Button { ... } Button; // it's okay for this to be the same as the struct tag.
Thanks for the pointers.
The reasoning behind double pointers, is that i want to be able to free the texture idependantly of the Button or if multiple Buttons share the same texture, but as you say they are pointers. maybe... it would already work like that.