Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7862779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:16:46+00:00 2026-06-02T23:16:46+00:00

The SDL documentation say that SDL_BlitSurface() returns -1 when it is not successful, but

  • 0

The SDL documentation say that SDL_BlitSurface() returns -1 when it is not successful, but i can not find why it would fail.

Here is my source code:

main.cpp

#include <iostream>
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "window.cpp"
#include "player.cpp"

int init();
void quit();

int main(int argc,char *args[])
{
    if (init() == -1) 
    {
        std::cerr << "Error:init()" << std::endl;
        return -1;
    }
    window main_window("Juego","img/bg.png",800,600,32);
    player player1(500,500,0,0,5,5,"img/square.png");
    while (!main_window.close)
    {
        main_window.handle_events();
        if ( main_window.draw_background() != true)
        {
            std::cerr << "ERROR->main_window.draw_background()" << std::endl;
            main_window.close = true;
        }
        player1.update(main_window.screen);
        SDL_Flip(main_window.screen);
        SDL_Delay(60/1000);
    }
    quit();
    return 0;
}

int init()
{   
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {   
        std::cerr << "Error while initialising SDL" << std::endl; 
        return -1;
    }
    return 0;
}

void quit()
{
    SDL_Quit();
}

window.cpp

class window
{
    private:
        void load_image(std::string source,SDL_Surface *destination);
        SDL_Surface *window_bg;
        SDL_Event event_queue;
        SDL_Rect window_rect;
    public:
        window(std::string SCREEN_TITLE,std::string SCREEN_BG,int sw,int sh,int sbpp);
        void handle_events();
        bool draw_background();
        SDL_Surface *screen;
        bool close;
};

window::window(std::string SCREEN_TITLE,std::string SCREEN_BG,int sw,int sh,int sbpp)
{
    window_bg = NULL;
    screen = NULL;
    close = false;
    window_rect.x = 0;
    window_rect.y = 0;
    window_rect.w = sw;
    window_rect.h = sh;
    screen = SDL_SetVideoMode(sw,sh,sbpp,SDL_SWSURFACE);
    std::cout << "Screen created." << std::endl;
    SDL_WM_SetCaption(SCREEN_TITLE.c_str(),NULL);
    std::cout << "Window title: " << SCREEN_TITLE << std::endl;
    load_image(SCREEN_BG,window_bg);
}

void window::handle_events()
{
    while (SDL_PollEvent(&event_queue))
    {
        if (event_queue.type == SDL_QUIT)
        {    
            close = true;
        }
    }
}

void window::load_image(std::string source,SDL_Surface *destination)
{
    SDL_Surface *tmp_img = IMG_Load(source.c_str());
    if (tmp_img != NULL)
    {
        if ((destination = SDL_DisplayFormat(tmp_img)) == NULL)
        {
            std::cerr << "ERROR->SDL_DisplayFormat()" << std::endl;
        }
        std::cout << source << " Loaded." << std::endl;
        SDL_FreeSurface(tmp_img);
    }
    else
    {    
        std::cerr << "Could not load: " << source << std::endl;
    }
}

bool window::draw_background()
{
    int error_check = SDL_BlitSurface(window_bg,&window_rect,screen,NULL);
    if (error_check != 0)
    {
        std::cerr << "SDL_BlitSurface() == " << error_check << std::endl;
        return false;
    }
    return true;
}

The error is in window.cpp (i think):

bool window::draw_background()
{
    int error_check = SDL_BlitSurface(window_bg,NULL,screen,NULL);
    if (error_check != 0)
    {
        std::cerr << "SDL_BlitSurface() == " << error_check << std::endl;
        return false;
    }
    return true;
}

The output of my program is:

Screen created.
Window title: Juego
img/bg.png Loaded.
img/square.png Loaded
SDL_BlitSurface() == -1
ERROR->main_window.draw_background()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:16:49+00:00Added an answer on June 2, 2026 at 11:16 pm

    load_image(SCREEN_BG,window_bg); in the window constructor loads into a temporary SDL_Surface and never populates the destination, which I assume is an output parameter. Then when draw_background() uses window_bg, it’s still NULL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SDL app that compiles fine, and the images show up, but
I've successfully built SDL from source using bcc 5.5.1 but any SDL test application
This is an SDL problem, however I have the strong feeling that the problem
I'm using SDL with FASM, and have code that's minimally like the following: format
this is some code that SDL requires in visual studios 2005 in order for
I am using SDL audio to play sounds. SDL_LockAudio tells this : Do not
I'm trying to make an SDL application in Xcode, but I'm having trouble loading
Here is the SDL program: #include <SDL/SDL.h> int main(int argc, char** argv){ SDL_Init(SDL_INIT_VIDEO); SDL_Surface*
Many libraries like SDL, etc, etc have in their tutorials method calls that free
I'm just learning SDL and I would like to know where is the correct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.