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;
}