Drawing a bitmap in the window (Leaf-SDL #2)
Tags: leaf, coding, compiler, graphics
Published: 2018-03-09 11:53:05

---
title: Drawing a bitmap in the window (Leaf-SDL #2)
published: true
description: Loading a graphic after figuring out some errors
tags: leaf, coding, compiler, graphics
canonical_url: https://mortoray.com/2018/03/09/drawing-a-bitmap-in-the-window-leaf-sdl-2/
cover_image: https://mortoray.files.wordpress.com/2018/03/leaf_coding_export.jpg
---

{% youtube 0N-qs8qv2dk %}

> Read [Update #1](https://mortoray.com/2018/02/28/new-game-a-quest-for-graphics-leaf-sdl-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:

```leaf
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:

```leaf
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:

```leaf
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([])
}
```

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:

```leaf
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:

```leaf
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" )
}
```

Called as:

```leaf
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.

![Screenshot](https://mortoray.files.wordpress.com/2018/03/screenshot_20180309_105012.png)

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


> Join in the fun by checking out the [SDL Branch](https://code.launchpad.net/~eda-qa/leaflang/sdl) of [Leaf](http://leaflang.org). The demo code is [on github](https://github.com/mortoray/leaf-algorithms/tree/master/sdl), though I can't promise what the current state is 🙈. Be sure to follow me on [Twitch](https://www.twitch.tv/mortoray) as well.