DEV Community

Cover image for Drawing a bitmap in the window (Leaf-SDL #2)
edA‑qa mort‑ora‑y
edA‑qa mort‑ora‑y

Posted on • Originally published at mortoray.com

Drawing a bitmap in the window (Leaf-SDL #2)

Read Update #1

Syntax Error

I addressed two problems since last update: A bad error message, and an image loading error.

The bad error message was triggered when an unknown symbol was used in a type, for example:

var a : wacky
Enter fullscreen mode Exit fullscreen mode

The logging system could already deal with symbols, but this conversion code wasn't passing the value up properly. After a bit of plumbing I got it to produce a new error message properly in the logger.

Unknown identifier `wacky`
Enter fullscreen mode Exit fullscreen mode

Image Loading and as_abi_ptr

In this snippet of code the check_sdl for sdl_blit_surface was reporting an error:

var image = sdl_load_bmp_rw( f, 1 )
//TODO: Check null `image`

q = sdl_blit_surface( image, 0, surface, 0 )
check_sdl( "sdl_blit_surface", q )
Enter fullscreen mode Exit fullscreen mode

Ignore that TODO for a second... I added code to print the SDL error messages. We don't have a library facility yet to print, or convert, ABI null-terminated strings; a quick loop will do:

defn print_sdl_error = -> {
    var sx = sdl_get_error()
    var i = 0
    while sx#i != 0 {
        std.print( char_val(sx#i) )
        i += 1
    }
    std.println([])
}
Enter fullscreen mode Exit fullscreen mode

The error was SDL_UpperBlit: passed a NULL surface. The code looked right, and I thought the values were okay, but I had no way to test them. That's what the TODO bit was about. When calling sdl_load_bmp_rw I couldn't inspect the result. To see if it was valid I tried printing the size of the image:

std.println(["image ", image.w, "x", image.h])
Enter fullscreen mode Exit fullscreen mode

That gave a segfault, indicating image was definitely not valid.

To test for null though I'd need a way to look at this value. In Leaf a value_ptr is an extrinsic property, disallowing any kind of direct manipulation (like comparison).

I introduced the as_abi_ptr builtin function to convert a value_ptr into a abi_ptr, which is a binary type. We can compare those, thus enabling this function:

defn check_sdl_ptr = ( where : array「char」, item : abi_ptr ) -> {
    item != 0 then return
    std.println([where, " failed"])
    print_sdl_error()
    fail string_tag( "sdl-fail" )
}
Enter fullscreen mode Exit fullscreen mode

Called as:

var image = sdl_load_bmp_rw( f, 1 )
check_sdl_ptr( "sdl_load_bmp_rw", as_abi_ptr(image) )
Enter fullscreen mode Exit fullscreen mode

I could now see the mysterious File is not a Windows BMP file error. I resaved the file in Gimp and it just went away, leaving us with the result shown below.

Screenshot

For next time hopefully I can do a bit more drawing, with rectangles.

Join in the fun by checking out the SDL Branch of Leaf. The demo code is on github, though I can't promise what the current state is 🙈. Be sure to follow me on Twitch as well.

Top comments (0)