C++ & SDL : No error but Images won't draw -
i have been following tutorial on sdl game development c++. , yesterday ran odd error. my image should drawn window. doesn't appear in form. i did bit of debugging , image in fact loaded program. won't draw. decided come today.
after digging, seem sdl rectangles used in game class problem. i figured out printing width of destination rectangle , came out 0. perhaps missed something. have pasted link project game class consists of header , cpp. can see went wrong?
https://app.box.com/s/fa7nvji36bulcmb36p72
not going gold medal programming, trying things work can continue rest of chapter.
any kindly appreciated. have posted code related image below.
---game.h---
#ifndef __game__ #define __game__ #include "sdl.h" class game { public: game(); ~game(); bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); void render(); void update(); void maingameloop(); void clean(); void loadimages(); bool running(); private: bool g_running; sdl_window* window; sdl_renderer* renderer; sdl_texture* m_ptexture; // new sdl_texture variable sdl_rect m_sourcerectangle; // first rectangle sdl_rect m_destinationrectangle; // rectangle }; #endif /* defined(__game__) */ ---game.cpp (extracts)---
void game::loadimages() //called in constructor (testing purposes) { sdl_surface* ptempsurface = sdl_loadbmp("assets/sprites/rider.bmp"); //loads image if (ptempsurface == null) cout << "error" << endl; else cout << "image loaded" << endl; m_ptexture = sdl_createtexturefromsurface(renderer, ptempsurface); //creates texture//check sdl_freesurface(ptempsurface); //frees surface sdl_querytexture(m_ptexture, null, null,&m_sourcerectangle.w, &m_sourcerectangle.h); //sets width , height of source rectangle m_sourcerectangle.x = 0; m_sourcerectangle.y = 0; m_destinationrectangle.x = 0; //prepares destination , source co-ordinates m_destinationrectangle.y = 0; m_destinationrectangle.w = m_sourcerectangle.w; m_destinationrectangle.h = m_sourcerectangle.h; cout << m_destinationrectangle.w << endl; //proves problem here. } void game::render() { // set black sdl_setrenderdrawcolor(renderer, 0, 0, 0, 255); // clear window black sdl_renderclear(renderer); sdl_rendercopy(renderer, m_ptexture, &m_sourcerectangle,&m_destinationrectangle); //draws sprite // show window sdl_renderpresent(renderer); } perhaps else can see failing see.
the error isn't think @ all, due fact thegame::loadimagesfunction executed before sdl , rendering context have been initialized, when try load create texture surface, renderer invalid. if had checked return value thesdl_createtexturefromsurface()call sdl_error()function have told that.
what need either move call game::init() have in main constructor of game class, or maybe move call game::loadimages() to game::init() function.
in case need initialize sdl before start load images.
Comments
Post a Comment