• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Aztral

Render OpenGL text

3 posts in this topic

Hi, I am using a bunch of code I found in a different thread on these forums (that I seem to have lost track of) attempting to render text with openGL and SDL. I've been staring at it for who knows how long and I can't figure out what I'm missing. Nothing at all is drawn. Thanks very much for any pointers. The code to load a font.
void openFont()
{
  string fontPath = "Fonts/" + m_sFontName + ".ttf";
  m_font = TTF_OpenFont(fontPath.c_str(), m_nPtSize);

  if(!m_font)
    {
      cerr<<"Could not open font '"<<m_sFontName<<"': "<<TTF_GetError()<<endl;
      // Exit? Log?
    }
}//END openFont


The code to load the texture.
GLuint loadTexture(SDL_Surface* surface)
{
  GLuint texture = NULL;
  if(surface)
    {
      glGenTextures(1, &texture);
      glBindTexture(GL_TEXTURE_2D, texture);
      
      glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0,
		   GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
      
      // Again using linear filtered textures for now
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      
      SDL_FreeSurface(surface);
    }
  else // Handle null surface* as param
    cerr<<"The surface pointer passed into loadTexture is null.\n";
  
  return texture;
  
}//END loadTexture (SDL_Surface*)


The code to render the text.
void render()
{

  if(m_txtImg)
    SDL_FreeSurface(m_txtImg);


  SDL_Surface* initialTxtImg = TTF_RenderText_Blended(m_font, m_sText.c_str(), m_fg);
  
  if(!initialTxtImg)
    {
      cerr<<"Could not render: "<<m_sText<<" because: "<<TTF_GetError()<<endl;
    }
  

  m_nWidth = nextPowerOfTwo(initialTxtImg->w);
  m_nHeight = nextPowerOfTwo(initialTxtImg->h);
  
 
  m_txtImg = SDL_CreateRGBSurface(0, m_nWidth, m_nHeight, 32, 
						    0x00ff0000, 0x0000ff00,
						    0x000000ff, 0xff000000);
  
  if(!m_txtImg)
    {
      cerr<<"Could not render intermediate text image: "<<TTF_GetError()<<endl;

    }

  SDL_BlitSurface(initialTxtImg, 0, m_txtImg, 0);
  
  m_nTexture = loadTexture(m_txtImg);
}//END render


The code to draw the rendered image.
void draw()
{
  glBindTexture(GL_TEXTURE_2D, m_nTexture);

  glBegin(GL_QUADS);

  glNormal3f(0.0f, 0.0f, 1.0f);// Normal facing us

  // Loop through verts.
  for(int i = 0; i < 4; i++)
    {
      glTexCoord2f(m_texCoords[i].x, m_texCoords[i].y);
      glVertex3f(m_verts[i].x, m_verts[i].y, m_verts[i].z);
    }
  
  glEnd();
}//END draw


The loop actually calling the draw function.
void execGameLoop()
{
  SDL_Event e;

  Sprite glass(75, 75, 200, 300, "Graphics/Glass.bmp");
  
  SDL_Color fg = {150, 150, 150};

  DrawableString testString("Hello", "AbductionIII", 12, fg, 500, 500);

  while(!m_done)
    {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();

      glass.draw();
      testString.draw();

      SDL_PollEvent(&e);
            
      handleEvents(e);

      SDL_GL_SwapBuffers();
    }
}//END drawScene


0

Share this post


Link to post
Share on other sites
Little off topic but I would probably work a little on cleaning up some of the names in your code. For example, you've got a function called draw() and another called render(). What's the difference?

On to your dilemma. I haven't used SDL_ttf too extensively so I can't be of too much aid on why the text isn't drawing, but I can say that from the looks of it, it's probably a setting. Are you rendering to the correct, visible coordinates? Does m_fg contain a 4 floating point values and is the alpha component at least 0.1? Little things like that are usually a good place to start. Another thing I noticed is that you don't have any calls to glEnable(GL_TEXTURE_2D). Without that you won't render whatever texture you have loaded in your loadTexture function.
0

Share this post


Link to post
Share on other sites
Thanks for the response - I can see how that would be confusing and I'll clean it up.

I am quite sure I'm rendering to the proper coordinates because I can render a regular texture based off of a bmp or png just fine to the same location.

m_fg is currently just {0xFF, 0xFF, 0xFF} which should be white text on a black background. I don't give it an alpha value.

I make the call to glEnable(GL_TEXTURE_2D) much earlier and like I said I can render normal textures based on bmp's or png's without issue so I don't think thats the problem

Hmm

Thanks again for the response.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0