• Lunes 29 de Abril de 2024, 12:21

Autor Tema:  duda en ingreso de texto online con sdl  (Leído 971 veces)

cachiatore

  • Nuevo Miembro
  • *
  • Mensajes: 2
    • Ver Perfil
duda en ingreso de texto online con sdl
« en: Lunes 17 de Noviembre de 2008, 23:05 »
0
hi gente tengo un codigo para ingresar texto desde pantalla con sdl
y funciona bien. usa una cadena char msg.
por favor cambien esto si usan otra fuente
//fuente = TTF_OpenFont("ariblk.ttf",15);

Código: Text
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <SDL/SDL.h>
  5. #include <SDL/SDL_ttf.h>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     SDL_Surface *screen,*ttext;
  10.    SDL_Event event;
  11.    bool done = false;
  12.    SDL_Color colorfuente={255,255,255};
  13.    SDL_Rect rect={0,0,0,0};
  14.    TTF_Font *fuente;
  15.    char msg[64]="";
  16.    
  17.    atexit(SDL_Quit);
  18.    atexit(TTF_Quit);
  19.  
  20.    // Iniciar SDL
  21.    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  22.       printf("No se pudo iniciar SDL: %sn",SDL_GetError());
  23.       exit(1);
  24.    }
  25.  
  26.    // Activamos modo de video
  27.    screen = SDL_SetVideoMode(800,600,24,SDL_HWSURFACE);
  28.    if (screen == NULL) {
  29.       printf("No se puede inicializar el modo gráfico: %sn",SDL_GetError());
  30.       exit(1);
  31.    }
  32.    
  33.    if(TTF_Init() < 0){
  34.         printf("No se pudo iniciar SDL_TTF: %sn",SDL_GetError());
  35.       exit(1);
  36.    }
  37.    
  38.    fuente = TTF_OpenFont("ariblk.ttf",15);
  39.    
  40.    while(!done) {
  41.         SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
  42.         if(msg[0]){
  43.             ttext = TTF_RenderText_Solid(fuente,msg,colorfuente);
  44.             rect.w=ttext->w;
  45.             rect.h=ttext->h;
  46.             SDL_BlitSurface(ttext,NULL,screen,&rect);
  47.         }
  48.        
  49.       while(SDL_PollEvent(&event)){
  50.           switch(event.type)
  51.           {
  52.                 case SDL_KEYDOWN:
  53.                     if(event.key.keysym.sym>='a'&&event.key.keysym.sym<='z'){
  54.                         if(strlen(msg)<64-1)
  55.                             msg[strlen(msg)]=event.key.keysym.sym;
  56.                     }
  57.                     break;
  58.                 case SDL_QUIT:
  59.                     done=true;
  60.             }
  61.       }
  62.       SDL_Flip(screen);
  63.       SDL_Delay(30);
  64.    }
  65.    TTF_CloseFont(fuente);
  66.    SDL_FreeSurface(ttext);
  67.  
  68.    return 0;
  69. }
  70.  
  71.  

el problema es que yo quiero implementarla a mi juego pero en mi caso uso 1 struct con un campo cadena weno hago lo mismo pero me muestra basura por pantalla queria saber porque y si hay alguna forma de arreglarlo ya debo usar un struct

Código: Text
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <SDL/SDL.h>
  5. #include <SDL/SDL_ttf.h>
  6. struct tp
  7. {
  8.   char cad[50];
  9. };
  10. int main(int argc, char *argv[])
  11. {
  12.   tp p;
  13.     SDL_Surface *screen,*ttext;
  14.    SDL_Event event;
  15.    bool done = false;
  16.    SDL_Color colorfuente={255,255,255};
  17.    SDL_Rect rect={0,0,0,0};
  18.    TTF_Font *fuente;
  19.  
  20.    atexit(SDL_Quit);
  21.    atexit(TTF_Quit);
  22.  
  23.    // Iniciar SDL
  24.    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  25.       printf("No se pudo iniciar SDL: %sn",SDL_GetError());
  26.       exit(1);
  27.    }
  28.  
  29.    // Activamos modo de video
  30.    screen = SDL_SetVideoMode(800,600,24,SDL_HWSURFACE);
  31.    if (screen == NULL) {
  32.       printf("No se puede inicializar el modo gráfico: %sn",SDL_GetError());
  33.       exit(1);
  34.    }
  35.  
  36.    if(TTF_Init() < 0){
  37.         printf("No se pudo iniciar SDL_TTF: %sn",SDL_GetError());
  38.       exit(1);
  39.    }
  40.  
  41.    fuente = TTF_OpenFont("times.ttf",15);
  42.  
  43.    while(!done) {
  44.         SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
  45.         if(p.cad[0]){
  46.             ttext = TTF_RenderText_Solid(fuente,p.cad,colorfuente);
  47.             rect.w=ttext->w;
  48.             rect.h=ttext->h;
  49.             SDL_BlitSurface(ttext,NULL,screen,&rect);
  50.         }
  51.  
  52.       while(SDL_PollEvent(&event)){
  53.           switch(event.type)
  54.           {
  55.                 case SDL_KEYDOWN:
  56.                     if(event.key.keysym.sym>='a'&&event.key.keysym.sym<='z'){
  57.                         if(strlen(p.cad)<64-1)
  58.                             p.cad[strlen(p.cad)]=event.key.keysym.sym;
  59.                     }
  60.                     break;
  61.                 case SDL_QUIT:
  62.                     done=true;
  63.             }
  64.       }
  65.       SDL_Flip(screen);
  66.       SDL_Delay(30);
  67.    }
  68.    TTF_CloseFont(fuente);
  69.    SDL_FreeSurface(ttext);
  70.  
  71.    return 0;
  72. }
  73.  
  74.