Unable to open Image in C++ SDL -
so im trying render image. code worked fine before refactored seperate classes cannot see problem. file path image worked before refactoring have not moved image should still in same place.
the error getting "unable create texture surface. error: couldn't open images/text.bmp" im line of code below messing up.
if (!(gtexture->loadfromfile("images/text.bmp"))) { return false; }
here loadfromfile function code
bool texture2d::loadfromfile(string path) { free(); sdl_texture* ptexture = null; sdl_surface* psurface = img_load(path.c_str()); if (psurface != null) { //sdl_setcolorkey(psurface, sdl_true, sdl_maprgb(psurface->format, 0, 0xff, 0xff)); ptexture = sdl_createtexturefromsurface(mrenderer, psurface); if (ptexture == null) { cout << "unable create texture surface. error: " << sdl_geterror() << endl; } mwidth = psurface->w; mheight = psurface->h; sdl_freesurface(psurface); } else { cout << "unable create texture surface. error: " << img_geterror() << endl; } mtexture = ptexture; return ptexture != null; }
below source.cpp file:
#include <sdl.h> #include <sdl_image.h> #include <sdl_mixer.h> #include "constants.h" #include "texture2d.h" #include "commons.h" #include <iostream> #include <string> using namespace std; //globals sdl_window* gwindow = null; sdl_renderer* grenderer = null; texture2d* gtexture = null; //prototypes bool initsdl(); void closesdl(); bool update(); void render(); int main(int argc, char* args[]) { if(initsdl()) { bool quit = false; while(!quit) { //gtexture->render(vector2d(), sdl_flip_none, 0); render(); quit = update(); } } closesdl(); return 0; } bool initsdl() { if(sdl_init(sdl_init_video) < 0) { cout << "sdl did not initialise. error: " << sdl_geterror(); return false; } else { //its bro gwindow = sdl_createwindow("games engine creation", sdl_windowpos_undefined, sdl_windowpos_undefined, screen_width, screen_height, sdl_window_shown); //did go? if (gwindow == null) { //nuh uh cout << "window not created. error: " << sdl_geterror(); return false; } grenderer = sdl_createrenderer(gwindow, -1, sdl_renderer_accelerated); if (grenderer != null) { //initialise png bby int imageflags = img_init_png; if (!(img_init(imageflags) & imageflags)) { cout << "sdl_image not initialise. error: " << img_geterror(); return false; } } else { cout << "renderer not initialised. error: " << sdl_geterror(); return false; } gtexture = new texture2d(grenderer); if (!(gtexture->loadfromfile("images/text.bmp"))) { return false; } return true; } } void closesdl() { sdl_destroywindow(gwindow); gwindow = null; delete gtexture; gtexture = null; sdl_destroyrenderer(grenderer); grenderer = null; img_quit(); sdl_quit(); } bool update() { sdl_event e; sdl_pollevent(&e); switch(e.type) { case sdl_quit: return true; break; case sdl_keyup: switch(e.key.keysym.sym) { case sdlk_q: return true; break; } break; case sdl_mousebuttondown: return true; break; } return false; } void render() { sdl_setrenderdrawcolor(grenderer, 0xff, 0xff, 0xff, 0xff); sdl_renderclear(grenderer); gtexture->render(vector2d(), sdl_flip_none, 0); //sdl_rect renderlocation = { 0, 0, mwidth, mheight }; //sdl_rendercopyex(mrenderer, mtexture, null, &renderlocation, 0, null, sdl_flip_none); sdl_renderpresent(grenderer); }
Comments
Post a Comment