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

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`

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 )

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

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])

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 : arraychar」, item : abi_ptr ) -> {
    item != 0 then return
    std.println([where, " failed"])
    fail string_tag( "sdl-fail" )

Called as:

var image = sdl_load_bmp_rw( f, 1 )
check_sdl_ptr( "sdl_load_bmp_rw", as_abi_ptr(image) )

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.


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

Categories: Leaf, Programming

Tagged as: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s