Thread: SDL_TTF Function and SDL_Mixer Initialisation problems

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    7

    Question SDL_TTF Function and SDL_Mixer Initialisation problems

    I have wrote some code for a graphical Tic-Tac-Toe game and, upon running the code, get several error messages of the form "Could not generate text texture "ABCD". Failed loading SHCORE.DLL: The specified module could not be found." Upon providing the aforementioned DLL (and, subsequently, one that a pop-up alert stated was not found), the error remained the same, only with "The specified module could not be found." being replaced with a string of symbols.

    I treid to use the debugger provided with Code::Blocks to diagnose the bug, but the debugger stopped after attempting to evaluate
    Code:
    if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
    This is not, as far as I am aware, an issue of programming libraries or SDL-related DLLs not being installed correctly - I compiled some programs from the LazyFoo SDL2 Tutorials and they worked exactly as they should.

    The code in question (irrelevant parts redacted):
    Code:
    #include <SDL.h>
    #include <SDL_ttf.h>
    #include <SDL_mixer.h>
    #include <SDL_image.h>
    #include <iostream>
    #include <string>
    
    class texture
    {
        public:
            texture() : tex(nullptr), width(0), height(0) {}; //Initialises as having no texture
            void load_texture(std::string file_name); //Loads texture from file 'file_name'
            void delete_texture(); //Deletes texture contained by class
            void render(int x, int y, int wid, int high); //Renders the entire texture to the point specified by x ad y (from the top left of the texture). Optionally with different width 'wid' and height 'high' (if they are specified as 0, it is rendered as its standard width/height)
            void set_as_render_target(); //Sets the texture as the rendering target of 'game_renderer'
            void generate_text_texture(std::string text); //Generates a texture of solid text, displaying 'text'. The text is in black
            bool has_texture(); //Returns 'true' if an SDL_Texture is stored by the object of the class, 'false' otherwise
            int get_width() {return width;}
            int get_height() {return height;}
        private:
            SDL_Texture* tex; //The texture itself
            int width; //The width of the texture
            int height; //The height of the texture
    
    } select_difficulty, easy, medium, hard, select_first_player, x, o, board, message_to_player;
    
    const int SCREEN_HEIGHT = 640;
    const int SCREEN_WIDTH = 480;
    
    SDL_Window* game_window = nullptr;
    SDL_Renderer* game_renderer = nullptr;
    Mix_Chunk* win = nullptr;
    Mix_Chunk* lose = nullptr;
    TTF_Font* text_font = NULL;
    
    /* Initialises all subsystems, the window and the renderer and sets the render draw colour and window as white */
    bool init()
    {
        bool success = true;
    
        if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
        {
            std::cout << "Could not initialise. " << SDL_GetError() << "\n\n";
            success = false;
        }
        else
        {
            game_window = SDL_CreateWindow("Noughts and Crosses", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 480, 640, SDL_WINDOW_SHOWN);
            if(game_window == NULL)
            {
                std::cout << "Could not initialise. " << SDL_GetError() << "\n\n";
                success = false;
            }
            else
            {
                game_renderer = SDL_CreateRenderer(game_window, -1, SDL_RENDERER_ACCELERATED);
                if(game_renderer == NULL)
                {
                    std::cout << "Could not initialise. " << SDL_GetError() << "\n\n";
                    success = false;
                }
            }
        }
    
        if(!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG))
        {
            std::cout << "Could not initialise SDL Image. " << IMG_GetError() << "\n\n";
            success = false;
        }
    
        if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
        {
            std::cout << "SDL Mixer could not initialise! " << Mix_GetError() << "\n\n";
            success = false;
        }
    
        if(TTF_Init() == -1)
        {
            std::cout << "Could not initialise SDL TTF. " << TTF_GetError() << "\n\n";
            success = false;
        }
    
        SDL_SetRenderDrawColor(game_renderer, 0xFF, 0xFF, 0xFF, 0xFF);
        SDL_RenderClear(game_renderer);
    
        return success;
    }
    
    /* Loads the images/sounds for the game (returns true for all successful, false otherwise). Prints load errors to console */
    bool load_media()
    {
        bool success = true;
    
        text_font = TTF_OpenFont("FantasqueSansMono-Bold.ttf", 106);
        if(!text_font)
        {
            std::cout << "Error: could not open font. " << TTF_GetError() << "\n\n";
            success = false;
        }
    
        select_difficulty.generate_text_texture("Select the difficulty");
        if(select_difficulty.has_texture() == false)
        {
            std::cout << "Could not generate text texture \"Select Difficulty\". " << TTF_GetError() << "\n\n";
            success = false;
        }
    
        easy.generate_text_texture("Easy");
        if(easy.has_texture() == false)
        {
            std::cout << "Could not generate text texture \"Easy\". " << TTF_GetError() << "\n\n";
            success = false;
        }
    
        board.load_texture("board.png");
        if(board.has_texture() == false)
        {
            std::cout << "Could not load texture. " << IMG_GetError() << "\n\n";
            success = false;
        }
    
        win = Mix_LoadWAV("win.wav");
        if(!win)
        {
            std::cout << "Error: could not load \"win\" sound. " << Mix_GetError() << "\n\n";
            success = false;
        }
    
        lose = Mix_LoadWAV("lose.wav");
        if(!lose)
        {
            std::cout << "Error: could not load \"lose\" sound. " << Mix_GetError() << "\n\n";
            success = false;
        }
    
        return success;
    }
    
    /* DEFINITION OF 'texture::generate_text_texture' */
    void texture::generate_text_texture(std::string text)
    {
        SDL_Color black = {0x00, 0x00, 0x00, 0xFF};
        SDL_Surface* texture_to_be = TTF_RenderText_Solid(text_font, text.c_str(), black);
        if(texture_to_be == NULL)
        {
            std::cout << "Error generating text texture!\n\n";
            tex = nullptr;
        }
    
        SDL_CreateTextureFromSurface(game_renderer, texture_to_be);
        width = texture_to_be->w;
        height = texture_to_be->h;
    
        SDL_FreeSurface(texture_to_be);
    
    }
    /* END OF FUNCTION DEFINITION */
    
    int main(int argc, char* args[])
    {
        if(!init())
        {
            std::cout << "Could not initialise. Aborting program.\n\n";
            return 1;
        }
        else
            if(!load_media())
            {
                std::cout << "Could not load media. Aborting program.\n\n";
                return 2;
            }
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    No need for replies - fixed it.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    Good to know, but posting your solution would help someone with the same problem in future, if they happen to arrive here after a similar search.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-08-2013, 10:06 AM
  2. SDL-displaying variable with SDL_ttf
    By nobo707 in forum Game Programming
    Replies: 4
    Last Post: 11-12-2009, 02:43 AM
  3. Help with SDL_ttf.
    By tuxinator in forum Linux Programming
    Replies: 6
    Last Post: 01-03-2006, 12:34 AM
  4. SDL_ttf and OpenGL
    By sand_man in forum Game Programming
    Replies: 2
    Last Post: 12-01-2004, 06:06 PM
  5. Generic function for initialisation
    By foniks munkee in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 07:08 PM

Tags for this Thread