C++ SDL display texture
Jul 14, 2015 at 5:45pm UTC
Hi, I just started learning SDL. Im reading the book SDL Game Development and I have an error in my code. I don't know if I can ask about SDL here, but I think it has much to do with C++. Heres the relevant code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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;
}
void Game::render()
{
SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle,
&m_destinationRectangle);
SDL_RenderPresent(m_pRenderer); // draw to the screen
}
can someone help me find out whats wrong?
Jul 14, 2015 at 6:22pm UTC
What error message do you get? The code that you have posted is not a complete program.
Last edited on Jul 14, 2015 at 6:23pm UTC
Jul 14, 2015 at 6:28pm UTC
I don't get any error messages, but the screen ends up black. Sorry forgot to add it
Jul 14, 2015 at 6:34pm UTC
Are you sure the surface/texture loads correctly? If SDL_LoadBMP or SDL_CreateTextureFromSurface fails they return NULL.
Jul 14, 2015 at 6:37pm UTC
I just tested it and I didnt get any errors. I've checked the file I load it from and it is correct. Mayby the wrong thing is in the main.cpp here is it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include "Game.hpp"
Game* g_game = 0;
int main(int argc, char * argv[])
{
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->render();
}
g_game->clean();
return 0;
}
Last edited on Jul 14, 2015 at 6:52pm UTC
Topic archived. No new replies allowed.