My best try using SDL__SetTextInputRect is in the program listed below. It doesn’t work. For one thing I don’t know what to do about setting the font. The printf statement after the WHILE loop shows that, yes indeed, the text was input while the program was in the loop. But it never shows in the rectangle. Can someone here show me how to get it to put text in the rectangle? Any example will do. TIA Bill S.
Code:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string.h>
int main( int argc, char* args[] ){
int done = 0;
char text[32];
SDL_Init(SDL_INIT_EVERYTHING);
SDL_DisplayMode vmode;
SDL_GetDisplayMode(0, 0, &vmode);
SDL_Window *win1 = NULL;
win1 = SDL_CreateWindow("", 0, 0, vmode.w, vmode.h, SDL_WINDOW_SHOWN);
SDL_Renderer* ren1 = SDL_CreateRenderer(win1, -1, 0);
SDL_SetRenderDrawColor(ren1, 255, 255, 255, 255);
SDL_RenderClear(ren1); // fill the scene with white
SDL_Rect rect1;
rect1.x = 50;
rect1.y = 50;
rect1.w = 200;
rect1.h = 32;
SDL_RenderDrawRect(ren1, &rect1);
SDL_SetRenderDrawColor(ren1, 255, 0, 0, 255); // the rect color (solid red)
SDL_RenderFillRect(ren1, &rect1);
SDL_RenderPresent(ren1); // copy to screen
SDL_StartTextInput();
SDL_SetTextInputRect(&rect1);
while (!done)
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
done = 1;
break;
case SDL_TEXTINPUT:
/* Add new text onto the end of our text */
strcat(text, event.text.text);
break;
}
}
SDL_RenderDrawRect(ren1, &rect1);
SDL_RenderPresent(ren1);
}
SDL_StopTextInput();
SDL_Delay(5000);
SDL_DestroyWindow(win1);
SDL_DestroyRenderer(ren1);
SDL_Quit();
printf("\nthe text input is: %s\n\n",text);
return 0;
}