Search code examples
c#sdl-2

How do I blit to a surface?


I have recently tried to learn c# and after I learned some of that I follow a tutorial(https://jsayers.dev/category/c-sdl-tutorial-series/) and i tried to use sdl_blitsurface and it doesn't give any errors but it just doesn't make the image show up. heres source code btw https://pastebin.com/iVkhkjsq

    var SourceRect = new SDL.SDL_Rect
    {
        x = 0,
        y = 0,
        w = 16,
        h = 16
    };
 
    var DestRect = new SDL.SDL_Rect
    {
        x = 20,
        y = 200
    };
 
   SDL.SDL_BlitSurface(surf, ref SourceRect, renderer, ref DestRect);

I tried sdl_blitsurface and it just isn't showing up


Solution

  • I had this same problem. You are using the newer SDL functions that operate on a renderer. You then have to use IMG_LoadTexture and SDL_RenderCopy. Also note that the destination rect does require a width and height otherwise it does not show. eg:

    var SourceRect = new SDL.SDL_Rect
    {
        x = 0,
        y = 0,
        w = imgw,
        h = imgh
    };
    
    var DestRect = new SDL.SDL_Rect
    {
        x = (int)circle.x,
        y = (int)circle.y,
        w = imgw,
        h = imgh
    };
    
    var img2 = SDL_image.IMG_LoadTexture(renderer,@"Examples\dog.jpg");
    SDL.SDL_RenderCopy(renderer, img2, ref SourceRect, ref DestRect);