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 !.