SDL-Image: Couldn't open image, only error msg - c++

Game Engine Creation course work, questions about SDL, currently learning C++ SDL.
Met a question about SDL_Image, Couldn't open image.
When I run the code it closed instantly
bool InitSDL()
{
//Setup SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "SDL did not initialise. Error: " << SDL_GetError();
return false;
}
else
{
gWindow = SDL_CreateWindow(
"GEC_SDL",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN
);
if (gWindow == NULL)
{
cout << "Window was not created. Error: " << SDL_GetError();
return false;
}
}
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer != NULL)
{
cout << "Renderer initialised." << endl;
//Initialise PNG loading.
int imageFlags = IMG_INIT_PNG;
if (!(IMG_Init(imageFlags) & imageFlags))
{
cout << "SDL_Image could not initialise. Error: " << IMG_GetError;
return false;
}
else {
cout << "SDL_Image initialised." << endl;
}
}
else
{
cout << "Renderer could not initialise. Error: " << SDL_GetError;
return false;
}
gTexture = LoadTextureFromFile("D:/_a'Programs/School/GEC_SDL/GEC_SDL/Images/text.bmp");
if (gTexture == NULL)
{
return false;
}
return true;
}
Error Messgae
Tried full path: D:_a'Programs\School\GEC_SDL\GEC_SDL\Images
Error Messgae
Load texture from file function
SDL_Texture* LoadTextureFromFile(string path)
{
FreeTexture();
SDL_Texture* pTexture = NULL;
//Load the image.
SDL_Surface* pSurface = IMG_Load( path.c_str() );
if (pSurface != NULL)
{
//Create the texture from the pixels on the surface.
pTexture = SDL_CreateTextureFromSurface(gRenderer, pSurface);
if (pTexture == NULL)
{
cout << "Unable to create texture from surface. Error: " << SDL_GetError() << endl;
}
SDL_FreeSurface(pSurface);
}
else
{
cout << "Unable to create texture from surface. Error: " << IMG_GetError() << endl;
}
return pTexture;
}
I searched similar question in the forum and tried their ways, none of them works, please help.

OMG, yes it's ''s problem, I rename the folder, now it works...
PS: stupid character sets

Related

my SDL_Renderer* seem to not work when I pass it into a function

I have my main func:
int main(int argc, char* argv[])
{
int broad[40][20] = {0};
SDL_Renderer* renderer = NULL;
SDL_Window* window = NULL;
srand(time(NULL));
SDL_Event e;
bool quit=false;
if (!Init(renderer,window))
{
cout << "Can't INIT Window Screen. ";
return 0;
}
else
{
if (!load_block(renderer))
{
cout << "Can't load BLOCK. ";
}
else
I create renderer in main than pass it in Init and load_block.
this is my load_block:
bool load_block(SDL_Renderer* renderer)
{
bool success = true;
color_block[RED] = loadTexture(renderer,"red_block.bmp");
if (color_block[RED] == NULL)
{
cout << "Unable to render IMG. SDL Error: \n" << SDL_GetError();
success = false;
}
I continue pass my SDL_Renderer* variable into my loadTexture func:
SDL_Texture* loadTexture(SDL_Renderer* renderer,string path)
{
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
if (loadedSurface == NULL)
{
cout << "Unable to load image. Error: \n " << SDL_GetError();
}
else
{
//SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0x00, 0xFF, 0xFF ) );
newTexture = SDL_CreateTextureFromSurface(renderer,loadedSurface);
if (newTexture == NULL)
cout << "Unable to create texture from " << path << " Error: \n" << SDL_GetError();
SDL_FreeSurface(loadedSurface);
}
return newTexture;
}
But it seem to not work.
It return me the error that: invalid renderer ?
what wrong with my renderer variable ?
Can I fix it ? I don't want to create my image in main func, and extern renderer seem to not work too, btw I don't want to use a global variable.
my console
this is my function Init:
bool Init(SDL_Renderer* renderer, SDL_Window* window)
{
bool success = true;
window = SDL_CreateWindow("SDL Tetris", SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
if ( window == NULL )
{
cout << "Window could not be created. Error: \n" << SDL_GetError();
success = false;
}
else
{
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
cout << "Renderer could not be created. Error: \n" << SDL_GetError();
success = false;
}
else
SDL_SetRenderDrawColor(renderer,0xFF,0xFF,0xFF,0xFF);
}
return success;
}
And I got nothing like "Renderer couldn't be created". So I think my renderer had been created successful.

SDL2 Window Not Opening [duplicate]

I'm trying to open a window with SDL2 in visual studio 2015. I've set a .bmp image in my code to display to the screen in a window, but when I run my code the program returns 0 and closes without a window. The .bmp image is in the project folder. How do you display the window?
#include <SDL.h>
#include <iostream>
int main(int argc, char* args[])
{
SDL_Window *window = nullptr;
SDL_Surface *windowSurface = nullptr;
SDL_Surface *imageSurface = nullptr;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
std::cout << "Game Initialization error: " << SDL_GetError() << std::endl;
{
window = SDL_CreateWindow("Contrast Beta 0.0.1", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 920, SDL_WINDOW_HIDDEN | SDL_WINDOW_FULLSCREEN);
if (window == NULL)
std::cout << "Window Creation Failed, Error: " << SDL_GetError() << std::endl;
else
{
//Window Created
windowSurface = SDL_GetWindowSurface(window);
imageSurface = SDL_LoadBMP("Background.bmp");
if (imageSurface == NULL)
std::cout << "Error loading background: " << SDL_GetError() << std::endl;
else
{
SDL_BlitSurface(imageSurface, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(2000);
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Remove SDL_WINDOW_HIDDEN; that's all.

TTF_RenderText_Solid() Crashing Game and Visual Studio [c++][sdl ttf]

Stack Trace :
So, I'm trying to create a 2D game and right now I'm trying to display and move a image, however when I Debug the game/run it, visual studio and the game freezes and can't quit, not even when using the task manager to kill it.
The only way to unfreeze it is by logging off or restarting the pc which forces both of them to close.
I also get some weird error :
Unhandled exception at 0x71002A95 (SDL2_ttf.dll) in SDLGame.exe: 0xC0000005: Access violation reading location 0x00000000.
I have no idea what it means and how to fix it, but I'm guessing it has something to do with my code that I need to change.
Here's my code :
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
// Macros
#define pause system("PAUSE"); // Works on windows only, removed in alpha / beta versions.
// Pre "init" of functions
void QuitGame();
int InitGame();
void processInput();
void InitRects();
// Variables
int FramesPassed = 0;
int FramesPerSecond = 0; // Not used yet
SDL_Renderer* renderer = nullptr;
SDL_Window* window = nullptr;
SDL_Event evnt;
SDL_Rect sprite1_Rect;
SDL_Rect FPS_Text_Rect;
TTF_Font* Sans = TTF_OpenFont("Fonts/Aaargh.ttf", 40);
SDL_Color Color_White = { 255, 255, 255 };
SDL_Surface* FPS_Text_Surface = nullptr;
SDL_Texture* FPS_Text = nullptr;
SDL_Texture* testImg = nullptr;
static bool isRunning = true;
int SDL_main(int argc, char* argv[])
{
InitGame();
InitRects();
std::cout << "Displaying text on screen using SDL TTF doesn't work" << std::endl;
std::cout << "This happens when the TTF Surface is being rendered on screen" << std::endl;
std::cout << "check line : 123 and 124." << std::endl;
while (isRunning)
{
FramesPassed++;
processInput();
SDL_RenderClear(renderer); // Clears the last/current frame?
// Render testImage on screen. (needs to be between render present and clear.)
SDL_RenderCopy(renderer, testImg, NULL, &sprite1_Rect);
SDL_RenderCopy(renderer, FPS_Text, NULL, &FPS_Text_Rect);
SDL_RenderPresent(renderer); // Pretty much draws everything again.
}
return 0;
QuitGame();
}
int InitGame()
{
std::cout << "Game Initializing..." << std::endl;
std::cout << "TTF SDL Initializing..." << std::endl;
if (TTF_Init() < 0)
{
std::cout << "SDL TTF Failed To Initialize : " << TTF_GetError() << std::endl;
pause
QuitGame();
}
else
{
std::cout << "SDL TTF Initialized Successfully" << std::endl;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cout << "SDL Initialization Failed : " << SDL_GetError() << std::endl;
pause
QuitGame();
}
else
{
std::cout << "SDL Initializing" << std::endl;
window = SDL_CreateWindow("Game Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 720, SDL_WINDOW_SHOWN);
if (window == NULL)
{
std::cout << "Window Creation Failed : " << SDL_GetError() << std::endl;
pause
QuitGame();
}
else
{
std::cout << "SDL Initialized Successfully" << std::endl;
std::cout << "Renderer Initializing..." << std::endl;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
std::cout << "Renderer Creation Failed : " << SDL_GetError() << std::endl;
pause
QuitGame();
}
else
{
std::cout << "Renderer Initialized Successfully" << std::endl;
// This line (under) crashes game, and crashes visual studio...
FPS_Text_Surface = TTF_RenderText_Solid(Sans, "Frames Passed : " + FramesPassed, Color_White);
testImg = IMG_LoadTexture(renderer, "images/test.bmp");
FPS_Text = SDL_CreateTextureFromSurface(renderer, FPS_Text_Surface);
}
}
}
}
std::cout << "Game Has Successfully Initialized!" << std::endl;
return 0;
}
void InitRects()
{
sprite1_Rect.h = 32;
sprite1_Rect.w = 32;
sprite1_Rect.x = 10;
sprite1_Rect.y = 10;
FPS_Text_Rect.h = 100;
FPS_Text_Rect.w = 50;
FPS_Text_Rect.x = 2;
FPS_Text_Rect.y = 2;
}
void processInput()
{
if (SDL_PollEvent(&evnt)) {
switch (evnt.type) {
case SDL_QUIT:
QuitGame();
break;
case SDL_KEYDOWN:
switch (evnt.key.keysym.sym) {
case SDLK_a:
sprite1_Rect.x -= 1;
break;
case SDLK_d:
sprite1_Rect.x += 1;
break;
case SDLK_w:
sprite1_Rect.y-= 1;
break;
case SDLK_s:
sprite1_Rect.y += 1;
break;
}
break;
}
}
}
void QuitGame()
{
isRunning = false;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
The problem is that you have the font as a global variable and load it straight away!
You need to call TTF_Init() first and load the font after!
By using a global vairable your loading it before initializing SDL_TTF, and this way TTF_OpenFont() will return a nullptr and if you try to read a nullptr gives an Access violation reading location 0x00000000 error!
Just call TTF_OpenFont() in a function and after TTF_Init() and it will work!
Just a tip you should check that Sans isn't a nullptr before using it!

SDL2 Hello World tutorial closes with error

I'm developing in VS2013 on a Windows 8.1 machine. The window flashes briefly on the screen (despite having a 2 second delay in the code).
Here is all the code:
#include <SDL.h>
#include <iostream>
int main(int argc, char **argv)
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 4;
}
//open a window
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (win == nullptr){
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
return 3;
}
//renderer
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr){
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
return 2;
}
//load bitmap
SDL_Surface *bmp = SDL_LoadBMP("helloworld.jpg");
if (bmp == nullptr){
std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
return 6;
}
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
SDL_FreeSurface(bmp);
if (tex == nullptr){
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
return 5;
}
SDL_RenderClear(ren);
SDL_RenderCopy(ren, tex, NULL, NULL);
SDL_RenderPresent(ren);
SDL_Delay(2000);
return 0;
}
It exits with a code 6. I was thinking it couldn't find the helloworld.jpg file. But, its there. I moved it from location to location hoping I was just an idiot. No luck. Its currently in the same directory as the .exe file.
SDL_LoadBMP will only load BMP formatted pictures, and guessing by the extension you have chosen you look to be using a JPEG formatted image, and your program is refusing to load it.
see SDL_Image for one possible solution.

SDL2 CreateRenderer throwing Invalid Window Error

My current code is as follows:
#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
int main(int argc, char **argv) {
if (SDL_Init(SDL_INIT_EVERYTHING != 0)) {
std::cout << "SDL_Init() Error: " << SDL_GetError() << std::endl;
std::cin.get();
return 1;
}
SDL_Window *win = SDL_CreateWindow("RandomSDL2", 100, 100, 1280, 720, SDL_WINDOW_SHOWN);
if (win = nullptr) {
std::cout << "SDL_CreateWindow() Error: " << SDL_GetError() << std::endl;
std::cin.get();
return 1;
}
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr) {
std::cout << "SDL_CreateRenderer() Error: " << SDL_GetError() << std::endl;
std::cin.get();
return 1;
}
SDL_Surface *img = IMG_Load("res/test.png");
if (img == nullptr) {
std::cout << "IMG_Load() Error: " << IMG_GetError() << std::endl;
std::cin.get();
return 1;
}
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, img);
SDL_FreeSurface(img);
if (tex == nullptr) {
std::cout << "SDL_CreateTextureFromSurface() Error: " << SDL_GetError << std::endl;
std::cin.get();
}
SDL_RenderClear(ren);
SDL_RenderCopy(ren, tex, NULL, NULL);
SDL_RenderPresent(ren);
SDL_Delay(2000);
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
It seems that the CreateRenderer function throws the error Invalid Window, I'm unsure as to why, I'm currently following this tutorial, I've copied the code down to a T besides the resolution and the use of SDL_image, SDL_image shouldn't really effect the creation of the renderer at this stage, I've also tried the resolution specified in the tutorial and it seems to still throw the same error.
Does anyone have any insight on this issue?
if (win = nullptr) {
^ assignment, probably wanted ==
Don't set win to nullptr and it probably won't be nullptr.