Question

I am doing a big project and I want to show user the information which looks nice.

All that I need it's to be able to draw square and text inside.

I drew text and squares, but I cannot put them together. I even detected a place where problem is. It's function SDL_SetVideoMode(mwidth,mheight,bpp,flags).

This place is highlighted. Thanks in advance!

using namespace std;
#define SPACING 0
TTF_Font *font=0;
int  font_size=16;
SDL_Surface *screen;)



void draw_text_at(SDL_Surface *scr,char *msg, int x0, int y0)
{
   int h;
   SDL_Rect r;

   SDL_Color fg={0,0,0,255};
   h=TTF_FontLineSkip(font);

    r.x=x0;
    r.y=y0-h;
    r.x=x0-TTF_GetFontOutline(font);
    r.y+=h+SPACING-TTF_GetFontOutline(font);

    SDL_Surface *surf=TTF_RenderText_Blended(font,msg,fg);

      if(surf)
      {   SDL_BlitSurface(surf,0,scr,&r);
          SDL_FreeSurface(surf);
      }
}

void window::handle_key_down(SDL_keysym * keysym)
{
  switch(keysym->sym)
  {
    case SDLK_ESCAPE:
     exit(0);
     break;

    default:
     break;
  }
}


 void window::setup_opengl()
 {
  float ratio=(float)mwidth/(float)mheight;
  glShadeModel(GL_SMOOTH);
  glCullFace(GL_BACK);
  glFrontFace(GL_CCW);

  glEnable(GL_CULL_FACE);
  glClearColor(1.0f,1.0f,1.0f,1.0f);
  glViewport(0,0,mwidth,mheight);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  gluPerspective(60.0,ratio,1.0,1024.0);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
 }


 void window::draw_screen()
 {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();  // set  system of coordinates to the center of the screen
  glTranslatef(0.0,0.0,-10.0);
  glColor4f(0.0,1.0,1.0,1.0);

  glBegin(GL_QUADS);
   glVertex3f(-8, 0-2 ,0);
   glVertex3f(-2+-8,0-2 ,0);
   glVertex3f(-2+-8,-2-2,0);
   glVertex3f(0+-8, -2-2,0); 
  glEnd();

  glBegin(GL_QUADS);
   glVertex3f(-8+3, 0-2 ,0);
   glVertex3f(-2+-8+3,0-2 ,0);
   glVertex3f(-2+-8+3,-2-2,0);
   glVertex3f(0+-8+3, -2-2,0);
  glEnd ();

  SDL_GL_SwapBuffers( );
 }


void window(int quantity_of_disks, hard_disk &disk)
 {
   if((SDL_Init(SDL_INIT_VIDEO)<0))
    {printf("Could not initialize SDL: %s.\n", SDL_GetError());
    exit(-1); //return -1;
    }

   if(TTF_Init()==-1)
    {   printf("TTF_Init: %s\n", TTF_GetError());
        exit(-2);
    }

   const SDL_VideoInfo *info=NULL;
   int flags=0,bpp=0;
   info=SDL_GetVideoInfo();

   if(!info)
    {   fprintf(stderr,"Video query failed");
        SDL_Quit();
        exit(0);
    }

   bpp=info->vfmt->BitsPerPixel;

   SDL_GL_SetAttribute(SDL_GL_RED_SIZE,5);
   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,5);
   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,5);
   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

HERE IF I set flag to zero, I see text
If flag is set to SDL_OPENGL or SDL_OPENGLBLIT , I see squares

flas is transfered to function SDL_SetVideoMode But I want see both. I tried change it, but..

  flags= 0;
 //   SDL_ANYFORMAT | SDL_OPENGLBLIT  | SDL_ASYNCBLIT

This moment is here:

     screen = SDL_SetVideoMode(mwidth,mheight,bpp,flags);

    if ( screen == NULL )
     { fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
      exit(-2); //return -2;
      }
     SDL_WM_SetCaption ("MBR informant",NULL);
     setup_opengl();

     glMatrixMode(GL_MODELVIEW);

    char *path_to_font="freefont-ttf/sfd/FreeMono.ttf";
   load_font(path_to_font, font_size);

   SDL_FillRect(screen,0,~0);

   while (1)
     {  draw_screen();
        char *msg="we typed it";
        draw_text_at(screen,msg,50,50);
        SDL_Flip(screen);

       SDL_Delay(1000);
       process_events ();
      }

     SDL_Quit();
     }

It looks like:

when variable flag = 0 when variable flag = 0

when variable flag = SDL_OPENGL enter image description here

Due to big size of code I left only functions that have relation to painting. Full file: http://rghost.net/37518422

SOLUTION

Thanks to jrok I 've solved this problem so: I defined rectangle. SDL_Rect rect; rect.x=0; rect.y=0; rect.w=150; rect.h=140;

and I modified one part:

     while (1)
       {  draw_screen();
          char *msg="we typed it";
          draw_text_at(screen,msg,50,50);
          **SDL_FillRect (screen,&rect,100);**
          SDL_Flip(screen);
         SDL_Delay(1000);
        process_events ();
      }

and I got what I wanted: enter image description here

Was it helpful?

Solution

You're blitting the text with SDL_BlitSurface(). Exactly why it goes wrong I don't know, but SDL wiki cleary says (SDL_BlitSurface):

Like most surface manipulation functions in SDL, it should not be used together with OpenGL.

If you wish to stick with OpenGL, this question covers some possibilities for rendering text:

How to do OpenGL live text-rendering for a GUI?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top