SDL_image loads a black square instead of my image


#1

I am trying to render my image, however it instead creates a black square instead of an image. I am unsure what the cause is as I have created a texture for the image as well. I am using visual studio 2019 on windows. here is my code
//header file
#pragma once
#include “window.h”

class Rect : public Window {
public:
Rect(const Window &window, int w, int h, int x, int y, int r, int g, int b, int a);
Rect(const Window& window, int w, int h, int x, int y, const std::string &image_path);
~Rect();

void draw() const;
void pollEvents(SDL_Event& event);

private:
int _w, _h;
int _x, _y;
int _r, _g, _b, _a;
SDL_Texture* _texture = nullptr;
};
//file to create image and texture.
#include “rect.h”
#include

Rect::Rect(const Window &window, int w, int h, int x, int y, int r, int g, int b, int a) :
Window(window), _w(w), _h(h), _x(x), _y(y), _r®, _g(g), _b(b), _a(a)
{
}
Rect::Rect(const Window &window, int w, int h, int x, int y, const std::string& image_path) :
Window(window), _w(w), _h(h), _x(x), _y(y)
{
auto surface = IMG_Load(image_path.c_str());
if (!surface) {
std::cerr << “Failed to load image\n”;
}
_texture = SDL_CreateTextureFromSurface(_renderer, surface);
if (!_texture) {
std::cerr << “Failed to load image\n”;
}
SDL_FreeSurface(surface);
}

Rect::~Rect() {
SDL_DestroyTexture(_texture);
}

void Rect::draw() const {
SDL_Rect rect = { _x, _y, _w, _h };
if (_texture) {
SDL_RenderCopy(_renderer, _texture, nullptr, &rect);
}
else {
SDL_SetRenderDrawColor(_renderer, _r, _g, _r, _a);
SDL_RenderFillRect(_renderer, &rect);
}
}

void Rect::pollEvents(SDL_Event &event) {
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_LEFT:
_x -= 10;
break;
case SDLK_RIGHT:
_x += 10;
break;
case SDLK_UP:
_y -= 10;
break;
case SDLK_DOWN:
_y += 10;
break;
}
}
}
//Main file
#include “window.h”
#include “rect.h”

void pollEvents(Window& window, Rect& rect) {
SDL_Event event;

if (SDL_PollEvent(&event)) {
	rect.pollEvents(event);
	window.pollEvents(event);
}

}

int main(int argc, char* argv[]) {
Window window(“Window”, 800, 600);
Rect rect(window, 120, 120, 100, 100, “res/hello.png”);

while (!window.isClosed()) {
	pollEvents(window, rect);
	rect.draw();
	window.clear();
}
return 0;

}

the renderer is built on another fie. It first presents the renderer, then clears the renderer.


#2

I need to see what window.clear is doing.
You should follow this flow.

  1. Clear Renderer
  2. Draw things
  3. Present Renderer

#3

window.clear does the following

void Window::clear() const {
SDL_RenderPresent(_renderer);
SDL_SetRenderDrawColor(_renderer, 75, 0, 0, 255);
SDL_RenderClear(_renderer);
}
if i do RenderClear before RenderPresent, the square simply does not appear, and only the background is created.


#4

Try using this block of code to see if your image will render.

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

int main(int argc, char ** argv) {
    bool quit = false;
    SDL_Event event;
    SDL_Rect dest = { 0, 0, 50, 50 };
 
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window * window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    SDL_Surface * image = IMG_Load("test.png");
    SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, image);
    SDL_FreeSurface(image);
 
    while (!quit) {
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
                case SDL_QUIT:
                    quit = true;
                    break;
            }
        }
    
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture, NULL, &dest);
        SDL_RenderPresent(renderer);
    }
 
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
 
    SDL_Quit();
 
    return 0;
}

#5

It still gives me a black screen sadly. If its any help here is what the output says

‘RenderImage.exe’ (Win32): Loaded ‘C:\Users\Shawn\Documents\SDL\RenderImage\Debug\RenderImage.exe’. Symbols loaded.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\ntdll.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\kernel32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\KernelBase.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\ucrtbased.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Users\Shawn\Documents\SDL\RenderImage\Debug\SDL2_image.dll’. Module was built without symbols.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\msvcrt.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\vcruntime140d.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Users\Shawn\Documents\SDL\RenderImage\Debug\SDL2.dll’. Module was built without symbols.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Users\Shawn\Documents\SDL\RenderImage\Debug\SDL2.dll’. Module was built without symbols.
‘RenderImage.exe’ (Win32): Unloaded ‘C:\Users\Shawn\Documents\SDL\RenderImage\Debug\SDL2.dll’
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\advapi32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\sechost.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\rpcrt4.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\sspicli.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\cryptbase.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\bcryptprimitives.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\gdi32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\gdi32full.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\msvcp_win.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\ucrtbase.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\user32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\win32u.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\imm32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\ole32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\combase.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\oleaut32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\setupapi.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\cfgmgr32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\bcrypt.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\shell32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\SHCore.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\windows.storage.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\profapi.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\powrprof.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\shlwapi.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\kernel.appcore.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\cryptsp.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\version.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\winmm.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\winmmbase.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\winmmbase.dll’.
‘RenderImage.exe’ (Win32): Unloaded ‘C:\Windows\SysWOW64\winmmbase.dll’
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\uxtheme.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\msctf.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\dwmapi.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\crypt32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\msasn1.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\clbcatq.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\TextInputFramework.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\CoreMessaging.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\CoreUIComponents.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\ntmarta.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\WinTypes.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\WinTypes.dll’.
‘RenderImage.exe’ (Win32): Unloaded ‘C:\Windows\SysWOW64\WinTypes.dll’
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\d3d9.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\igdumd32.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\ResourcePolicyClient.dll’.
‘RenderImage.exe’ (Win32): Unloaded ‘C:\Windows\SysWOW64\ResourcePolicyClient.dll’
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\hid.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\devobj.dll’.
‘RenderImage.exe’ (Win32): Loaded ‘C:\Windows\SysWOW64\wintrust.dll’.
The thread 0x1038 has exited with code 0 (0x0).
‘RenderImage.exe’ (Win32): Unloaded ‘C:\Windows\SysWOW64\igdumd32.dll’
‘RenderImage.exe’ (Win32): Unloaded ‘C:\Windows\SysWOW64\d3d9.dll’
The thread 0x610 has exited with code 0 (0x0).
The thread 0x4c34 has exited with code 0 (0x0).
The thread 0x2ff4 has exited with code 0 (0x0).
The program ‘[8524] RenderImage.exe’ has exited with code 0 (0x0).


#6

Is the image definitely been loaded ? I think you have print outs.

Or try create a .png file in paint and try render that. Myt be the image is the issue.


#7

I created one in paint and it did not work, so the issue is most likely that it is not loading correctly.


#8

Check whether image is NULL. You can try using a regular BMP image and loading it with SDL_LoadBMP() instead of SDL_IMGLoad().


#9

Move your .png image into the folder where the .exe is located / built for your project. Probably a path issue ?


#10

Thanks for the help! I had to move my c++ file into a different folder. It was in the same folder the solution was in and I believe that was the problem. When I move that and the image into another folder it worked perfectly. Thank you!


#11

Your welcome, good job and let us know how the project goes. Always intrested in seen what people are building :grinning: