1st SDL Engine Drawing BMP Blank Screen

Started by
2 comments, last by Alio 4 years, 11 months ago

Hello I have bought the book SDL game development from amazon to learn SDL2 game programming on Ubuntu.

I have my first game engine up and running but when I add code to draw a BMP in the top left of the screen I just get a black screen when I run it.

Can anybody help me solve this I would really appreciate it.

Also I am not sure if I have the texture creation code on lines 8-15 in the right place, I tried moving it around but it didn't make a difference.

 

main.cpp

 

<code>

#include <SDL2/SDL.h>
#include "Game.h"
#include "Game.cpp"


int main(int argc, char * args[])
{
Game * g_game = new Game;

g_game->init("Chapter 1", 100,100,640,480,false);

while(g_game->running())
{
    g_game->handleEvents();
    g_game->update();
    g_game->renderer();
}
g_game->clean();

return 0;

}


<code/>

 

Game.cpp

 

<code>

#include "Game.h"
#include <iostream>

bool Game::init(const char* title, int xpos, int ypos, int width,
int height, bool fullscreen)
{
    
    SDL_Surface * pTempSurface = SDL_LoadBMP("assets/rider.bmp");    
    m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer,pTempSurface);
    SDL_FreeSurface(pTempSurface);
    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;    

    int flags = 0;

    if (fullscreen)
    {
    flags = SDL_WINDOW_FULLSCREEN;
    }

    // attempt to initialize SDL
    if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {
    std::cout<<"SDL Init Success\n";
    // init the window
    m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);

    if(m_pWindow != 0) //window init success
    {
    std::cout<<"Window Creation Success\n";
    m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);

        if(m_pRenderer !=0) // render init success
        {
        std::cout<<"Renderer Creation Success\n";
        SDL_SetRenderDrawColor(m_pRenderer, 0,0,0,0);
        }    
        else
        {
        std::cout<<"Renderer Init Fail\n";
        return false; // renderer init fail
        }

    }
    
    else
    {
    std::cout<<"Window Init Fail\n";
    return false; //window init fail
    }

}

else
{
std::cout<<"SDL Init Fail\n";
return false; // SDL init fail
}


std::cout<<"Init success\n";
m_bRunning = true; // everything inited successfully
// start the main loop

return true;

}

 

void Game::renderer()
{
SDL_RenderClear(m_pRenderer); // clears the renderer
// to the draw colour

SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle,&m_destinationRectangle);

SDL_RenderPresent(m_pRenderer); // draw to the screen
}


void Game::update()
{
}

void Game::clean()
{
std::cout<<"Cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}

 


void Game::handleEvents()
{
SDL_Event event;
    if(SDL_PollEvent(&event))
    {
        switch (event.type)
        {
        case SDL_QUIT:
         m_bRunning = false;
        break;
        }
    }
}

 

<code/>

 

game.h

 

<code>

 

#ifndef __Game__
#define __Game__

class Game
{
public:
Game() {}
~Game() {}

 

bool init(const char* title, int xpos, int ypos, int width,
int height, bool fullscreen);
void renderer();
void update();
void handleEvents();
void clean();

// a function to access the private running variable
bool running() {return m_bRunning;}

private:

SDL_Window * m_pWindow;
SDL_Renderer * m_pRenderer;
SDL_Texture * m_pTexture; //the new SDL Texture variable
SDL_Rect m_sourceRectangle; // the first rectangle
SDL_Rect m_destinationRectangle;// another rectangle

bool m_bRunning;
};


#endif /* defined(__Game_h__) */

 

<code/>

Advertisement

Start by checking for errors wherever you have SDL calls, especially where you're loading and creating the texture.

Check the return value, then call SDL_GetError() if needed. Use the documentation to determine how to properly check for errors; the easiest way is to just type in the function call name in the search box, top left.

 

For example: Your very first call is SDL_LoadBMP. The documentation for this call says that this returns null if there was an error.

Thus, check the return value to see if it's null before going continuing on.

Hello to all my stalkers.

thanks I did that and got it running. Thanks

This topic is closed to new replies.

Advertisement