Luca
Luca

Reputation: 1756

SDL2 SLD_RenderCopy doesn't display anything

I'm using SDL2 to learn how to make a simple 2D platform. I'm beginning to read SDL game development and the Lazy Foo's tutorial and I'm able to open a window and draw on a surface and blit that surface on the window (as it's done in legacy SDL), but if I try to do the same with hardware acceleration using the Renderer functions it doesn't display a thing! I'm running on windows 10,and when I launch the application it just shows the empty window. I tried to print some debug lines and it seems to open the bitmap, create the texture,but when I print the dimensions of the rectangle I get with QueryTexture it returns 0 and 0 !

//Game.cpp:
#include <SDL.h>
#include <SDL_image.h>
#include "Game.h"
#include <iostream>

bool Game::init(const char *title,int x,int y,int width,int height,int flags)
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
        return false;
    m_pWindow = SDL_CreateWindow(title,x,y,width,height,flags);
    if (m_pWindow == NULL)
        return false;
    m_pRenderer = SDL_CreateRenderer(m_pWindow,-1,0);
    if (m_pRenderer == NULL)
        return false;
    SDL_SetRenderDrawColor(m_pRenderer,0,0,0,255);
    return true;
}

void Game::handle_events()
{
    SDL_Event event;
    while (SDL_PollEvent(&event) != 0)
    {
        switch (event.type)
        {
            case SDL_QUIT:
                m_bRunning = false;
                break;
            default:
                break;
        }
    }
}

void Game::render()
{
    SDL_RenderClear(m_pRenderer);
    SDL_RenderCopy(m_pRenderer,m_pTexture,&m_sourceRectangle,&m_destinationRectangle);
    SDL_RenderPresent(m_pRenderer);
}

void Game::clean()
{
    SDL_DestroyWindow(m_pWindow);
    SDL_DestroyRenderer(m_pRenderer);
    SDL_Quit();
}

bool Game::loadImage()
{
    SDL_Surface *surface = SDL_LoadBMP("logo.bmp");
    if (surface == NULL)
        return false;
    SDL_Texture *texture =  SDL_CreateTextureFromSurface(m_pRenderer,surface);
    if (texture == NULL)
        return false;
    SDL_FreeSurface(surface);
    SDL_QueryTexture(m_pTexture,NULL,NULL,&m_sourceRectangle.w,&m_sourceRectangle.h);
    m_destinationRectangle.x = m_sourceRectangle.x = 0;
    m_destinationRectangle.y = m_sourceRectangle.y = 0;
    m_destinationRectangle.w = m_sourceRectangle.w;
    m_destinationRectangle.h = m_sourceRectangle.h;
    return true;
}



// main.cpp:
#include <SDL.h>
#include "Game.h"
#include <iostream>
#include <Windows.h>

Game *g_game = NULL;

int main(int argc,char **argv)
{
    AllocConsole(); 
    freopen("CON","w", stdout);

    g_game = new Game();
    if (g_game->init("my 2D platform game!",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,800,600,SDL_WINDOW_SHOWN) != true)
        return 1;
    if (g_game->loadImage() != true)
        return 2;
    g_game->render();

    while (g_game->running())
    {
        g_game->handle_events();
        g_game->update();
    }

    g_game->clean();

    return 0;
}

Upvotes: 0

Views: 474

Answers (1)

Meyer
Meyer

Reputation: 1702

The problem is that you do not assign the texture correctly in the Game::loadImage() function. In the line

SDL_Texture *texture = SDL_CreateTextureFromSurface(m_pRenderer,surface);

the created texture is assigned to a local pointer variable, which goes out of scope at the end of the function. Instead, you need to assing the texture to the class variable:

m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer,surface);

As a note, you are not making good use of the C++ capabilities to manage the resources. I recommend that you invest some time in learning about recommended practices, for example from the C++ Core Guidelines.

Upvotes: 2

Related Questions