Hashlink SDL

I would love to use SDL directly in my hashlink program. My needs are very minimal so I don’t want to involve the whole Heaps in it. I just need to be able to render a surface, capture some user input, play some PCM audio, basic stuff. I struggle to figure out how to actually do it due to lack of any documentation or examples. Looks like the only resource of learning the ways of interacting with SDL is Heaps source code but it’s also not an easy task to parse though all the abstractions involved there.

Any suggestion? Maybe somebody has already made some simple SDL applications using only the bare Hashlink?

So far I managed to create a window and an event catching loop but not much else. I see there are methods to create Surfaces but can’t find anything on how to actually render them.

I also tried to get a use of the OpenGL context and to at least see the clearColor on my screen but no luck as well.

Sdl.init();

final window = new Window('Window', 512, 512);
window.renderTo();

GL.init();
GL.clearColor(1.0, 0.0, 0.0, 1.0);

final pixels = Bytes.alloc(512 * 512 * 4);

for (n in 0...pixels.length) {
	pixels.set(n, Math.floor(Math.random() * 256));
}

final surf = Surface.fromBGRA(pixels, 512, 512);

while (Sdl.processEvents((event) -> {
	switch (event.type) {
		case Quit:
			return false;
		case _:
	}
	return true;
})) {
	GL.clear(GL.COLOR_BUFFER_BIT);
	Sdl.delay(Std.int(1000 / 60));
}

Sdl.quit();

Hello,

your event handler is wrong, it should return true for processed state, or false for unprocessed (bubbling). In short

 (Sdl.processEvents((event) -> {
	return true; // acknowledged, no further processing needed
})) { ... }
1 Like

Oh! Thanks for the hint! Another little piece of the puzzle :slight_smile:

Still can’t get anything on the screen though. There probably should be something similar to GL.swapWindow() or some other way to make the frame buffer to actually appear on the window…

Try this (in continuation to your code;)

import sdl.Cursor;
...
...
var cursor = Cursor.create(surf,0,0);
cursor.set();

while (Sdl.processEvents((event) -> {
	return true;
})) {
	GL.clear(GL.COLOR_BUFFER_BIT);

	window.present();

	Sdl.delay(Std.int(1000 / 60));
}

Sdl.quit();

1 Like

Magnificent :slight_smile:

Thank you! That’s actually a great progress for me. Now when I have a working OpenGL context I’ll be able to use it for all my needs in terms of graphics.

1 Like