Ejemplo n.º 1
0
func main() {
	done := false
	redraw := true

	if !allegro.Init() {
		log.Fatalln("Failed to init Allegro.")
	}

	if !imageio.Init() {
		log.Fatalln("Failed to init IIO addon.")
	}

	font.InitFont()

	display := allegro.CreateDisplay(640, 480)
	defer display.Destroy()
	if display == nil {
		log.Fatalln("Error creating display.")
	}

	if !allegro.InstallKeyboard() {
		log.Fatalln("Error installing keyboard.")
	}

	font := font.LoadFont("data/fixed_font.tga", 0, 0)
	if font == nil {
		log.Fatalln("Error loading data/fixed_font.tga")
	}

	bmp := allegro.LoadBitmap("data/mysha.pcx")
	dispBmp := bmp
	defer bmp.Destroy()
	if bmp == nil {
		log.Fatalln("Error loading data/mysha.pcx")
	}
	text := "Display bitmap (space to change)"

	allegro.SetNewBitmapFlags(allegro.MemoryBitmap)
	memBmp := allegro.LoadBitmap("data/mysha.pcx")
	defer memBmp.Destroy()
	if memBmp == nil {
		log.Fatalln("Error loading data/mysha.pcx")
	}

	timer := allegro.CreateTimer(INTERVAL)
	defer timer.Destroy()
	if timer == nil {
		log.Fatalln("Could not create Timer.")
	}

	queue := allegro.CreateEventQueue()
	defer queue.Destroy()
	if queue == nil {
		log.Fatalln("Could not create Queue.")
	}

	queue.RegisterEventSource(allegro.GetKeyboardEventSource())
	queue.RegisterEventSource(display.GetEventSource())
	queue.RegisterEventSource(timer.GetEventSource())

	timer.Start()

	allegro.SetBlender(allegro.Add, allegro.One, allegro.InverseAlpha)

	for !done {
		if redraw && queue.IsEmpty() {
			update(bmp)
			allegro.ClearToColor(allegro.MapRgbF(0, 0, 0))
			bmp.DrawTinted(allegro.MapRgbaF(1, 1, 1, 0.5), bmpX, bmpY, bmpFlag)
			font.DrawText(allegro.MapRgbaF(1, 1, 1, 0.5), 0, 0, 0, text)
			allegro.FlipDisplay()
			redraw = false
		}

		event := queue.WaitForEvent()
		switch event.Type() {
		case allegro.EventKeyDown:
			if event.KeyboardKeycode() == allegro.KeyEscape {
				done = true
			} else if event.KeyboardKeycode() == allegro.KeySpace {
				if bmp == memBmp {
					bmp = dispBmp
					text = "Display bitmap (space to change)"
				} else {
					bmp = memBmp
					text = "Memory bitmap (space to change)"
				}
			}
		case allegro.EventDisplayClose:
			done = true
		case allegro.EventTimer:
			redraw = true
		}
	}
}
Ejemplo n.º 2
0
func main() {
	if !allegro.Init() {
		log.Fatalln("Could not init Allegro.")
	}

	if !allegro.InstallKeyboard() {
		log.Fatalln("Could not init Keyboard.")
	}
	if !imageio.Init() {
		log.Fatalln("Could not init ImageIO.")
	}

	allegro.SetNewDisplayFlags(allegro.Resizable | allegro.GenerateExposeEvents)
	display := allegro.CreateDisplay(640, 480)
	defer display.Destroy()
	if display == nil {
		log.Fatalln("Unable to set any graphic mode")
	}

	allegro.SetNewBitmapFlags(allegro.MemoryBitmap)
	bmp := allegro.LoadBitmap("data/mysha.pcx")
	defer bmp.Destroy()
	if bmp == nil {
		log.Fatalln("Unable to load image")
	}

	queue := allegro.CreateEventQueue()
	defer queue.Destroy()
	if queue == nil {
		log.Fatalln("Could not create Queue.")
	}

	queue.RegisterEventSource(allegro.GetKeyboardEventSource())
	queue.RegisterEventSource(display.GetEventSource())

	redraw := true
	for {
		if redraw && queue.IsEmpty() {
			allegro.ClearToColor(allegro.MapRgb(255, 0, 0))
			bmpW := float32(bmp.GetWidth())
			bmpH := float32(bmp.GetHeight())
			displayW := float32(display.GetWidth())
			displayH := float32(display.GetHeight())
			bmp.DrawScaled(0, 0, bmpW, bmpH, 0, 0, displayW, displayH, 0)
			allegro.FlipDisplay()
			redraw = false
		}

		event := queue.WaitForEvent()
		if event.Type() == allegro.EventDisplayResize {
			display.AcknowledgeResize()
			redraw = true
		}
		if event.Type() == allegro.EventDisplayExpose {
			redraw = true
		}
		if event.Type() == allegro.EventKeyDown && event.KeyboardKeycode() == allegro.KeyEscape {
			break
		}
		if event.Type() == allegro.EventDisplayClose {
			break
		}
	}
}