JimmyD
JimmyD

Reputation: 2759

Image doesn't show in SDL 2.0

I'm writing a OpenGl Game using SDL 2.0. Now I have one problem. I will draw a image on the screen. Now I create the window and initialize everything. The image does load without errors but does not show on the screen. Below are a few code samples. All this code is needed for drawing a bitmap. No other code is shown. I get an black window with this code. And no image :(. (Update the code to main and a function)

 void Bitmap(const tstring& nameRef, int xPos, int yPos, int width, int height)
{
SDL_Surface* image;
     SDL_Rect rect;
    rect.x = xPos;
    rect.y = yPos;
    rect.h = height;
    rect.w = width;

    image = SDL_LoadBMP(("./GameData/Bitmap/" + m_FileName).c_str());

    SDL_BlitSurface(image , NULL , SDL_GetWindowSurface(GAME_ENGINE->GetMainWindow()) , &rect);

}


int main(int argc, char** argv)
{
SDL_Window * m_MainWindowPtr =nullpr;

//Initialize SDL
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        return false;
    }

    //Create Window
    if(m_Fullscreen == false)
    {
        m_MainWindowPtr = SDL_CreateWindow(m_WindowName.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_ScreenWidth, m_ScreenHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
    }
    else if(m_Fullscreen == true)
    {
        m_MainWindowPtr = SDL_CreateWindow(m_WindowName.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_ScreenWidth, m_ScreenHeight, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
    }

    //check if the window is made, else quit the game
    if(m_MainWindowPtr == nullptr)
    {
        return false;
    }

    //Enable unicode
    //SDL_EnableUNICODE( SDL_TRUE );

    // NEEDED TO DRAW OPENGL 
    SDL_GLContext glcontext = SDL_GL_CreateContext(m_MainWindowPtr); 

    //LIKE THE OLD SWAP BUUFERS FUNCTION 
    SDL_GL_SwapWindow(m_MainWindowPtr); 


Bitmap("test.bmp", 500, 500, 247, 360);
}

Upvotes: 1

Views: 2221

Answers (2)

user2448027
user2448027

Reputation: 1638

Try SDL_RenderPresent (replaces SDL_Flip and SDL_UpdateRects in SDL 2.0) or the OpenGL solution that adderly posted.

Upvotes: 0

adderly
adderly

Reputation: 192

SDL_UpdateRect ?

Have you correctly set OPEN_GL? viewport?

If you are going to use opengl for rendering, i dont see any opengl code.

Try the example here and tell us how did it go?

http://wiki.libsdl.org/moin.fcg/SDL_GL_CreateContext

Upvotes: 1

Related Questions