-1

So I have three classes Application, DrawMgr and Cube. In Application.cpp is my main loop in which I call DrawMgr::DrawCube in which I call Cube::Draw.

Application.cpp

while (!quit)
{
    //Draw Background
    gDrawMgr.DrawBackground();

    gDrawMgr.DrawCube();

    //UpdateScreen
    gDrawMgr.UpdateScreen();

    //Handle events on queue
    while (SDL_PollEvent(&e) != 0)
    {
        //User requests quit
        if (e.type == SDL_QUIT || e.key.keysym.sym == SDLK_ESCAPE)
        {
            quit = true;
        }
    }
}

DrawMgr.cpp

SDL_Surface* screenSurface;

void DrawMgr::DrawCube()
{
   gCube.Draw(screenSurface);
}

Cube.cpp

void Cube::Draw( SDL_Surface* destination )
{
    SDL_Rect offset;
    offset.x = 100;
    offset.y = 100;

    SDL_FillRect(cube, NULL, SDL_MapRGB( cube->format, 0, 0, 0 ) );

    SDL_BlitSurface( cube, NULL, destination, &offset);
}

When I run the program, the cube doesn't appear, what am I doing wrong?

  • So you're saying this works as long as you don't divide your code in three separate classes? – Martin G Feb 21 '17 at 11:48
  • Yes, it does. I should probably mention that gCube and gDrawMgr are objects which I use globally with extern. – Simeon Godinyachki Feb 21 '17 at 12:50
  • Where are you actually writing to the screen, `Cube::Draw` is drawing onto the surface `screenSurface`. `SDL_Flip` is then required to draw to the display buffer. – Colin Feb 21 '17 at 15:35
  • Where is `screenSurface` initialised? If this is all your code then you're using uninitialised pointers. – Colin Feb 21 '17 at 15:37
  • 1
    Question leaves too many important details out of scope. Where and when does `screenSurface` and `cube` get assigned? What is initialisation order? Make a minimal complete example, it can't be longer than 100 lines. – keltar Feb 22 '17 at 03:01

1 Answers1

0

Are you sure you're using SDL2.0 ? because things have changed, you need a SDL_Renderer and a SDL_Texture. Seems like you're trying to do it in the SDL 1 way.

In SDL2, to update the screen, we use those calls

SDL_RenderClear(sdlRenderer);

SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);

SDL_RenderPresent(sdlRenderer);

RenderClear to wipe the screen content. RenderCopy to draw some texture on it. RenderPresent to draw the final result on the screen.

Go see the migration guide for more details on the new flow.

https://wiki.libsdl.org/MigrationGuide

jordsti
  • 746
  • 8
  • 12
  • It is still possible to blit on window surface if one really wants to. – keltar Feb 22 '17 at 03:02
  • @keltar You can, but you'll have a better FPS with SDL_Texture and those Render calls. – jordsti Feb 22 '17 at 15:43
  • It depends. But anyway, my point was that code in question might be completely valid SDL2, even if to some people it may look "not 2 enough". – keltar Feb 22 '17 at 18:45