Twinklebear's SDL2 tutorial lesson 1 segmentation fault |
richardm
|
I'm trying to follow Twinklebear's SDL2 tutorial. Lesson 0 compiled and ran properly so I assume I set up SDL2 correctly. Now I'm trying to do Lesson 1
http://www.willusher.io/sdl2%20tutorials/2013/08/17/lesson-1-hello-world and the program crashes with a segmentation fault. It compiles without errors or warnings. https://github.com/Twinklebear/TwinklebearDev-Lessons make and compile step g++ -o Lesson1 Lesson1.c++ -std=c++11 `sdl-config --libs` -lSDL_ttf -lSDL2 -ILessons/include output when I run is: after window after renderer imagePath /home/rm/Lessons/res/Lesson1/hello.bmp after load bmp [2]+ Segmentation fault (core dumped) ./Lesson1 I get similar errors when I try to run Lesson6. It seems that a call to SDL_CreateTextureFromSurface is responsible, but the two pointers given in the parameters are not nullptr. I don't understand how I could proceed from here on. This was posted before but the issue was never resolved. code /* * Lesson 1: Hello World! */ int main(int, char**){ //First we need to start up SDL, and make sure it went ok if (SDL_Init(SDL_INIT_VIDEO) != 0){ std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; return 1; } //Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN); //Make sure creating our window went ok if (win == nullptr){ std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; return 1; } std::cout << "after window " << std::endl; //Create a renderer that will draw to the window, -1 specifies that we want to load whichever //video driver supports the flags we're passing //Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering //SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be //synchronized with the monitor's refresh rate SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (ren == nullptr){ SDL_DestroyWindow(win); std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; SDL_Quit(); return 1; } std::cout << "after renderer" << std::endl; //SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface //this lets us choose when to upload or remove textures from the GPU std::string imagePath = getResourcePath("Lesson1") + "hello.bmp"; std::cout << "imagePath " << imagePath << std::endl; SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str()); if (bmp == nullptr){ SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl; SDL_Quit(); return 1; } std::cout << "after load bmp " << std::endl; //To use a hardware accelerated texture for rendering we can create one from //the surface we loaded SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp); //We no longer need the surface SDL_FreeSurface(bmp); if (tex == nullptr){ SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl; SDL_Quit(); return 1; } std::cout << "after texture " << std::endl; //A sleepy rendering loop, wait for 3 seconds and render and present the screen each time for (int i = 0; i < 3; ++i){ std::cout << "renderer loop " << std::endl; //First clear the renderer SDL_RenderClear(ren); //Draw the texture SDL_RenderCopy(ren, tex, NULL, NULL); //Update the screen SDL_RenderPresent(ren); //Take a quick break after all that hard work SDL_Delay(1000); } //Clean up our objects and quit SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); return 0; } |
|||||||||||
|