1
votes

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);
}
2
How do you know the image loaded properly?user1944441
If the images is not properly loaded the SDL_LoadBMP returns a NULL pointer. And SDL_LoadBMP does not return any NULL pointer.JimmyD
Then the problem is elsewhere not shown. Posting a Short, Self Contained, Correct Example, instead of parts of code, would be better.user1944441
This is all code for the image. I have other code in the project but that's for playing audio and so. This has nothing to do with images.JimmyD

2 Answers

1
votes

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

0
votes

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