c++ - SDL2 Image to screen issue -
i've been on hunt trying figure out how execute properly. i've been having trouble finding documentation on sdl (if has necessary goto love check out).
so, after finding out sdl_flip
, sdl_setvideomode
extinct while amidst in lazy foo's tutorials , finding out it's windows now. can't seem image screen still. i'm new sdl (obviously). so, compiles fine. i'm compiling in source directory image.bmp located, reason image pointer returns null
, blank screen when program executes.
one more thing. error sdl_geterror
: "passed null surface". because ever reason image returning null
or can't opened?
here source:
#include "sdl2\sdl.h" #include <stdio.h> #include <stdlib.h> int main(int argc, char* args[]){ sdl_surface *image = null; sdl_window *window = null; sdl_surface *screen = null; sdl_init(sdl_init_video ); window = sdl_createwindow( "img.cc", sdl_windowpos_undefined, sdl_windowpos_undefined, 640, 480, 0); screen = sdl_getwindowsurface(window); image = sdl_loadbmp("image.bmp"); sdl_blitsurface( image, null, screen, null ); sdl_freesurface( image ); sdl_updatewindowsurface(window); if(sdl_geterror != null) { fprintf(stdout,"sdl_geterror: %s\n",sdl_geterror()); } if(image == null) { fprintf(stdout,"image null"); } //pause sdl_delay( 2000 ); //free loaded image sdl_destroywindow( window ); //quit sdl sdl_quit(); return 0; }
when run command,
image = sdl_loadbmp("image.bmp");
it loads image current directory perspective of application. may not directory compiled in!
add error checking
first, check errors, if it's assert()
…
image = sdl_loadbmp("image.bmp"); if (!image) { fail("could not load image"); }
define fail()
...
#include <string> void fail(const char *msg) { std::string text = msg; const char *err = sdl_geterror(); if (err) { text.append(": "); text.append(err); } sdl_showsimplemessagebox( sdl_messagebox_error, "an error occurred", text.c_str(), nullptr); std::exit(1); }
since said image
null, guess print out "file not found" message.
finding files
there couple common ways locate files:
if need keep data files application, consider using
sdl_getbasepath()
find them. using means have put files in known location relative application binary (not source code).you can load files relative current directory, need make sure current directory 1 want when run game. example, if using ide visual studio or xcode, have set current directory in debug settings project.
since didn't tell platform developing on, it's bit hard give more specific advice.
Comments
Post a Comment