So I’m trying to create an application that has user text input, so I went to this link: https://wiki.libsdl.org/Tutorials/TextInput and followed what’s in there but I still seems to be missing something.
This is my code:
#include <stdio.h>
#include <string.h>
#include <SDL2/SDL.h>
extern void InitVideo();
extern void Redraw();
extern char *text;
extern char *composition;
extern Sint32 cursor;
extern Sint32 selection_len;
int main(int argc, char **argv)
{
SDL_bool done = SDL_FALSE;
InitVideo();
/* ... */
SDL_StartTextInput();
while (!done) {
SDL_Event event;
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
/* Quit */
done = SDL_TRUE;
break;
case SDL_TEXTINPUT:
/* Add new text onto the end of our text */
strcat(text, event.text.text);
break;
case SDL_TEXTEDITING:
/*
Update the composition text.
Update the cursor position.
Update the selection length (if any).
*/
composition = event.edit.text;
cursor = event.edit.start;
selection_len = event.edit.length;
break;
}
}
Redraw();
}
SDL_Quit();
return 0;
}
I linked the sdl libraries and also included the headers but now I don’t know what am I doing wrong. It always gives me the ‘referenced in function SDL_MAIN’ errors in the line with the ‘extern’ parts.
Was I supposed to define those extern variables my self?
That code example isn’t complete. I think it’s expected you add the memory allocation for the text yourself. Here’s a more complete example.
textinput.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
typedef struct Text {
char * string;
size_t size; /* Size of allocated memory. */
size_t length; /* Length of the actual string. */
} Text;
static int GrowText(Text * text, size_t new_size)
{
char * new_text;
text->size += new_size;
new_text = SDL_realloc(text->string, text->size);
if (!new_text) {
SDL_free(text->string);
return 1;
}
text->string = new_text;
return 0;
}
static int AppendText(Text * text, char * text_to_add)
{
size_t tta_length = SDL_strlen(text_to_add);
/* Check if the text (including the terminating zero) even fits into the allocated memory. */
if (text->size - text->length < tta_length + 1) {
/* It doesn't fit. We have to allocate more memory. */
/* Let's give it some extra bytes in case it's a short text. */
size_t new_size = tta_length < 512 ? 512 : tta_length;
if (GrowText(text, new_size)) {
SDL_Log("Out of memory\n");
return 1;
}
}
text->length = SDL_strlcat(text->string, text_to_add, text->size);
return 0;
}
static void RemoveCharacter(Text * text)
{
/* Removing multi-byte characters from the UTF-8 string. */
while (text->length && text->string[text->length - 1] < -64) {
text->length--;
}
if (text->length) {
text->length--;
}
text->string[text->length] = 0;
}
int main(int argc, char *argv[])
{
int run = 1;
Text text = {NULL, 0, 0};
SDL_Window * window;
if (GrowText(&text, 16)) {
SDL_Log("Out of memory\n");
return 1;
}
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0 ) {
return 1;
}
window = SDL_CreateWindow("TextInput", 50, 50, 50, 50, SDL_WINDOW_SHOWN);
if (!window) {
SDL_Quit();
return 2;
}
while (run) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
run = 0;
} else if (e.type == SDL_TEXTINPUT) {
if (AppendText(&text, e.text.text)) {
run = 0;
break;
}
SDL_Log("%s", text.string);
} else if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_ESCAPE) {
run = 0;
} else if (e.key.keysym.sym == SDLK_BACKSPACE) {
if (text.length > 0) {
RemoveCharacter(&text);
SDL_Log("%s", text.string);
}
}
} else if (e.type == SDL_KEYUP) {
if (e.key.keysym.sym == SDLK_RETURN || e.key.keysym.sym == SDLK_KP_ENTER) {
text.string[0] = 0;
text.length = 0;
SDL_Log("-----------");
}
}
}
SDL_Delay(1);
}
SDL_free(text.string);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Welp, I was expecting some textbox showing characters inputted by the user appearing, but anyways, thanks, it worked!