• 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
Wraithe

SDL_ttf runtime error

8 posts in this topic

Hello all,

 

Looking for a little help. I have used SDL with TTF quite a bit and I am fairly comfortable with it at this point. This error has me dumbfounded however, and I have no more ideas of how to proceed. I have tested the Font loading and TTF_RenderText_Solid. The surface returned from the rendertext function is not a null pointer, but it must be the issue, as my program crashes anytime I try to blit a text surface. I have used the same blitting function for standard images, and it works just fine.

 

I have also checked all my compiler linking (I am using code::blocks), it is fine. I have loaded up old programs, recompiled them, and they work just fine. I have no idea where to go from here. My button class is where I am trying to utilize text, so that is what I will be showing.

 

Edit: In case anyone ends up asking, I did call TTF_Init();

 
#include "text.h"
 
std::map <std::string, SDL_Color> Text::mColors;

std::map <std::string, TTF_Font *> Text::mFonts;



SDL_Surface * Text::applyText( std::string strText, std::string strFont, std::string strColor, int * w, int * h )

{

    SDL_Surface * surface = NULL;



    surface = TTF_RenderText_Solid( mFonts[ strFont ], strText.c_str(), mColors[ strColor ] );



    TTF_SizeText( mFonts[ strFont ], strText.c_str(), w, h );



    return surface;

}
 
 
 
#ifndef TEXT_H

#define TEXT_H



#include <map>

#include <string>



#include "SDL/sdl_ttf.h"



class Text

{

    public:

        static SDL_Surface * applyText( std::string strText, std::string strFont, std::string strColor, int * w, int * h );



        static std::map <std::string, SDL_Color> mColors;

        static std::map <std::string, TTF_Font *> mFonts;

};



#endif // TEXT_H
 
 
 
// inside of Display.cpp
 
void Display::loadFonts()

{

    initInput( "Resources/ResourceListFonts.txt" );



    while( !isFile.eof() )

    {

        readLine();



        if( !strData.empty() )

        {

            std::string strFont = strData;



            readLine();



            std::string strFontPath = strPath + strData;



            int fontSize = readValue();



            TTF_Font * newFont = NULL;



            newFont = TTF_OpenFont( strFontPath.c_str(), fontSize );



            mFonts.insert( std::pair <std::string, TTF_Font *> ( strFont, newFont ) );

        }

    }



    freeInput();

}
 
 
// in button.cpp
 
Button::Button( int iX, int iY, std::string strSurf, std::string strFunc, std::string strText )

{

    x = iX;

    y = iY;

    w = Display::getImageWidth( strSurf );

    h = Display::getImageHeight( strSurf );



    iTextW = 0;

    iTextH = 0;



    strFunction = strFunc;

    strSurface = strSurf;



    surfaceText = NULL;



    if( !strText.empty() )

    {

        strSurfaceText = strText;



        surfaceText = Display::applyText( strSurfaceText, "monospaceTypewriter12", "black", &iTextW, &iTextH );

    }



    sClipX = 0;

    sClipY = 0;

}
 
 
void Button::render()

{

    Display::applySurface( x, y, strSurface, sClipX, sClipY );



    if( surfaceText != NULL )

    {

        Display::applySurface( ( x + ( iTextW / 2 ) ), ( y + ( iTextH / 2 ) ), surfaceText );

    }

}
 
Edited by Wraithe
0

Share this post


Link to post
Share on other sites
Are you sure TTF_OpenFont does not return a null pointer?

Are you sure mFonts[strFont] does not return a null pointer?
0

Share this post


Link to post
Share on other sites

You need to have error checking around all calls. Almost all SDL calls can return status codes. The convention in SDL is that functions which return pointers can return nullptr on failure, and functions which return integer values return negative values on failure. You can use SDL_GetError() (or for TTF functions,  TTF_GetError()) to get a reasonably human readable description of the error.

0

Share this post


Link to post
Share on other sites

Thanks for the replies! As far as I can tell (with error checking) the pointers are valid. I set up error checking and not a single error occurs. The program just crashes when a text rendered surface hits the blit function.

 

 

Edit:


Are you sure mFonts[strFont] does not return a null pointer?

 

 

Yes, I have even tried filling in all the render text solid parameters with solid values, generated IN that function, and the crash still occured at the blit function.

Edited by Wraithe
0

Share this post


Link to post
Share on other sites

I have also tried freeing the rendered text surface immediately after creation, and the program crashes, yet the pointer is not NULL. Again, my other programs work just fine with TTF, what on earth could the issue be?

 

Edit: Finally got a crash report (Only does it once out of every 10 runs )

 

Problem signature:
  Problem Event Name:    APPCRASH
  Application Name:    Space Invaders.exe
  Application Version:    0.0.0.0
  Application Timestamp:    51e53cc5
  Fault Module Name:    ntdll.dll
  Fault Module Version:    6.1.7600.16385
  Fault Module Timestamp:    4a5bdb3b
  Exception Code:    c0000005
  Exception Offset:    000335f2
  OS Version:    6.1.7600.2.0.0.256.1
  Locale ID:    1033
  Additional Information 1:    e3cc
  Additional Information 2:    e3cc0ee538989a7b66ab90686b3c00a6
  Additional Information 3:    d22a
  Additional Information 4:    d22a3993b2d48ddd96705e026b50390c

 

 

  Fault Module Name:    ntdll.dll  <-- What is this?

Edited by Wraithe
0

Share this post


Link to post
Share on other sites

Sorry to post so many times in a row, but I found a solution.

 

The program only crashes when I use a vector of my button class. If I make it a vector of pointers, it works. Anyone know why this is?  (Not a big deal, pointers are fine, I simply don't understand why that would cause a crash).

0

Share this post


Link to post
Share on other sites
In certain situations the vector will copy objects stored in the vector so if you have not implemented the Button class to handle copying correctly that is probably the problem.

Also see the rule of three.
0

Share this post


Link to post
Share on other sites

Were you not running your program in a debugger? It would be a lot easier to diagnose the cause of the crash using a debugger.

0

Share this post


Link to post
Share on other sites

Thank you both again for the replies!

 

I do not ever think to use the debugger =(, will do my best to remember.

 

There was some error about std::mem_copy or something similar, so it must have been vector copying errors.

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