L
L
logan baby2021-09-16 15:41:27
C++ / C#
logan baby, 2021-09-16 15:41:27

Why is the image not loading in SDL?

Why is it that when I load an image (sdl_loadbmp) it loads but has a black background instead? how to fix? 61433b419a0cd257639876.png

program code :

#include <iostream>
#include <SDL.h>

using namespace std;

const int width_screen = 640;
const int height_screen = 480;

SDL_Window* win = NULL;
SDL_Surface* scr = NULL;
SDL_Surface* helloworld = NULL;

bool init()
{
  bool success = true;
  if (SDL_Init(SDL_INIT_VIDEO) < 0)
  {
    cout << "Error in init program", SDL_GetError;
    success = false;
  }

  else
  {
    win = SDL_CreateWindow("Hello world", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
      width_screen, height_screen, SDL_WINDOW_SHOWN);

    if (win == NULL)
    {
      cout << "Error in creating window", SDL_GetError;
      success = false;
    }

    else
    {
      scr = SDL_GetWindowSurface(win);

      if (scr == NULL)
      {
        cout << "Error in getting surface", SDL_GetError();
        success = false;
      }
    }
  }

  return success;
}

bool load_media()
{
  bool success = true;

  helloworld = SDL_LoadBMP("C:/Users/mshun/Downloads/tmb_74342_5656.bmp");

  if (helloworld == NULL)
  {
    cout << "Error in loading media", SDL_GetError();
    success = false;
  }

  return success;
}

void close()
{
  SDL_FreeSurface(helloworld);
  helloworld = NULL;

  SDL_DestroyWindow(win);
  win = NULL;

  SDL_Quit();
}

int main(int argc, char* args[])
{
  if (!init())
  {
    cout << "Error in init... ", SDL_GetError();
  }

  else
  {
    if (!load_media())
    {
      cout << "Error in loading media... ", SDL_GetError();
    }

    else
    {
      SDL_UpdateWindowSurface(win);
      SDL_Delay(200000);
    }
  }

  close();

  return 666;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Antropov, 2021-09-16
@SanceiLaks

So you don't draw it anywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question