Skip to content

Instantly share code, notes, and snippets.

@Quintus
Last active September 24, 2017 11:08
Show Gist options
  • Save Quintus/2d226f69350c436ffc71ac52192a320a to your computer and use it in GitHub Desktop.
Save Quintus/2d226f69350c436ffc71ac52192a320a to your computer and use it in GitHub Desktop.
A working example of a simple use of the Allegro C library (liballeg.org) in Go
package main
/*
#cgo LDFLAGS: -lallegro
#include <stdio.h>
#include <stdlib.h>
#include <allegro5/allegro.h>
inline ALLEGRO_EVENT_TYPE extract_event(const ALLEGRO_EVENT* p_evt)
{
return p_evt->type;
}
inline const ALLEGRO_KEYBOARD_EVENT* extract_keyboard_event(const ALLEGRO_EVENT* p_evt)
{
return &(p_evt->keyboard);
}
*/
import "C"
import "fmt"
func main() {
C.al_install_system(C.ALLEGRO_VERSION_INT, nil)
C.al_install_keyboard()
defer C.al_uninstall_system()
fmt.Println("Start")
p_display := C.al_create_display(640, 480)
p_evqueue := C.al_create_event_queue()
defer C.al_destroy_display(p_display)
defer C.al_destroy_event_queue(p_evqueue)
C.al_register_event_source(p_evqueue, C.al_get_display_event_source(p_display))
C.al_register_event_source(p_evqueue, C.al_get_keyboard_event_source())
run := true
for run {
// Handle events
var evt C.ALLEGRO_EVENT
for C.al_get_next_event(p_evqueue, &evt) {
switch (C.extract_event(&evt)) {
case C.ALLEGRO_EVENT_DISPLAY_CLOSE:
run = false
case C.ALLEGRO_EVENT_KEY_DOWN:
if C.extract_keyboard_event(&evt).keycode == C.ALLEGRO_KEY_ESCAPE {
run = false
}
}
}
C.al_clear_to_color(C.al_map_rgb(0, 0, 0))
C.al_flip_display()
}
fmt.Println("Finish")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment