Home > Software design >  SDL doesn't render BMP image on mac
SDL doesn't render BMP image on mac

Time:09-19

I have the following code in main.cpp:

#include <SDL2/SDL.h>
#include <iostream>
int main(){
    SDL_Init(SDL_INIT_VIDEO);
    bool quit = false;
    SDL_Event event;
    SDL_Window * window = SDL_CreateWindow("Chess",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 720, 640, 0);
    SDL_Delay(100);
    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    SDL_Surface * board = SDL_LoadBMP("board.bmp");
    if (board == NULL) {    
        std::cout << "The image 'board.bmp' could not be loaded due to the following SDL error: "        << SDL_GetError() << std::endl;    
        return 1;
    }
    else {
        std::cout << "The image 'board.bmp' was loaded successfully" << std::endl;
    }
    SDL_Texture * board_texture = SDL_CreateTextureFromSurface(renderer, board);
    if (board_texture == nullptr){
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }
    SDL_RenderCopy(renderer, board_texture, NULL, NULL);
    while (!quit)
    {
        SDL_WaitEvent(&event);
        SDL_RenderPresent(renderer);
        switch (event.type)
        {
        case SDL_QUIT:
            quit = true;
            break;
        }
        SDL_Delay(15);
    }
    SDL_DestroyTexture(board_texture);
    SDL_FreeSurface(board);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

Both error checks return nothing, and I can't figure out why. Also, I build using:

g   chess.cpp -o chess -I include -L lib -l SDL2-2.0.0 

This seems to work for my Windows PC, but not on my Intel Macbook Pro. Are there any solutions/workarounds available?

CodePudding user response:

As documentation says

The backbuffer should be considered invalidated after each present; do not assume that previous contents will exist between frames. You are strongly encouraged to call SDL_RenderClear() to initialize the backbuffer before starting each new frame's drawing, even if you plan to overwrite every pixel.

So it should be:

SDL_WaitEvent(&event);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, board_texture, NULL, NULL);
SDL_RenderPresent(renderer);
  • Related