Image not loading ?

I’m pretty new to SDL, wanted to make a game from what i’ve learned (forgot 95% so i look up in the github and google things), my main problem i don’t know why the image isn’t loading, i checked the path and it’s correct. I can’t find a solution :(.
Also i don’t know if this is the correct way of doing thins and organizing code and stuff, some functions are copied from a youtube SDL tutorial series, (altought i don’t understand them that well…).

here is my code : (Apologies for the formating)

Utility.h

#pragma once
#include
#include <SDL.h>
class Utility
{

private:
SDL_Rect rect;

public:
SDL_Texture* LoadImage(std::string target, SDL_Renderer* render, int size_x, int size_y);
//void DestroyImage(std::string target, SDL_Renderer* render); (will be implemented later)
void Draw(SDL_Texture* texture, SDL_Renderer* renderer):
};

Utility.cpp

#include “Utility.h”
#include “SDL_image.h”

SDL_Texture* Utility::LoadImage(std::string target, SDL_Renderer* render, int size_w, int size_h)
{
rect.w = size_w;
rect.h = size_h;
SDL_Texture* texture_image = nullptr;
SDL_Surface* image = IMG_Load(target.c_str());
if (image < 0)
{
std::cout << "Error loading image : " << SDL_GetError() << std::endl;
}
else
{
texture_image = SDL_CreateTextureFromSurface(render, image);
}
SDL_FreeSurface(image);
SDL_QueryTexture(texture_image, nullptr, nullptr, &rect.w, &rect.h);
return texture_image;
}

void Utility::Draw(SDL_Texture* texture, SDL_Renderer* renderer)
{
SDL_RenderCopy(renderer, texture, &rect, 0);
}

main.cpp

#include
#include <SDL.h>
#include <SDL_image.h>
#include “Utility.h”

int main(int argc, char** argv)
{
Utility util;
bool run = true;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Event event;
int img_init = IMG_Init(IMG_INIT_JPG && IMG_INIT_PNG);

SDL_Window* win = SDL_CreateWindow(“Runner Game”, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* main_renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture* texture = util.LoadImage(“E:/Downloads/image.png”, main_renderer, 640, 480);
if (SDL_INIT_VIDEO < 0)
{
std::cout << "Unable to initalize video : " << SDL_GetError() << std::endl;
}
if (img_init < 0)
{
std::cout << "Unable to initialize image : " << SDL_GetError() << std::endl;
}
if (win == nullptr)
{
std::cout << "Unable to load window : " << SDL_GetError() << std::endl;
}
while (run)
{
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT)
{
run = false;
}
}
//SDL_SetRenderDrawColor(main_renderer, 255, 240, 134, 255); (i did this test this and it works)
SDL_RenderClear(main_renderer);
util.Draw(texture, main_renderer);
SDL_RenderPresent(main_renderer);
}
//SDL_Delay(3000);
return 0;
SDL_Quit();
}

Thanks in advance !.

Where you have if (image < 0), that doesn’t look right to me. image here is a pointer; you should be checking if (image == NULL) instead.

done, but still the problem persists.
also, isn’t 0 the same as NULL ?.

Might be that you’re using AND(" && “) to define the flags of IMG_Init, when you’d likely want to use bitwise OR(” | "). There’s an example in the wiki if you’d like to check it.

unfortunatly that doesn’t work either.

Well your code has multiple problems — you’re going to have to fix them all before things start working. And to get better help, you need to give us (and yourself) more information than “it doesn’t work.” If I took my car to the mechanic with the complaint “it doesn’t work,” how would he even know where to begin?

To answer your question, yes, 0 is the same as NULL. But you weren’t checking whether image is 0. You were checking whether image was less than 0. Very much not the same thing. When IMG_Load fails, it returns NULL, not something less than NULL, so your error trap would fail to catch the error.

I would suggest for now you simply print out the result of SDL_GetError() after every step, along with a note about which step you’re on and, where you can, the result that you got. Then you can start at the top and figure out what’s going wrong step by step. SDL2’s error reporting is quite good, in my experience, so it’s worth paying attention to what it says.

1 Like

Thanks. but like i said in discord someone, already provided me a solution.

But thanks by the way.