Пример #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
		}
	}
}
Пример #2
0
func main() {
	var zoom float32 = 1.0
	redraw := true
	filename := "data/mysha.pcx"

	if !allegro.Init() {
		log.Fatalln("Could not init Allegro.")
	}

	if !allegro.InstallMouse() {
		log.Fatalln("Could not init Mouse.")
	}
	if !allegro.InstallKeyboard() {
		log.Fatalln("Could not init Keyboard.")
	}

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

	display := allegro.CreateDisplay(640, 480)
	defer display.Destroy()
	if display == nil {
		log.Fatalln("Could not create Display.")
	}
	display.SetWindowTitle(filename)

	t := time.Now()
	bitmap := allegro.LoadBitmap(filename)
	defer bitmap.Destroy()
	duration := time.Since(t)
	if bitmap == nil {
		log.Fatalf("%s not found or failed to load\n", filename)
	}
	fmt.Printf("Loading took %s\n", duration.String())

	timer := allegro.CreateTimer(1.0 / 30)
	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(allegro.GetMouseEventSource())
	queue.RegisterEventSource(display.GetEventSource())
	queue.RegisterEventSource(timer.GetEventSource())

	timer.Start()

	for {
		event := queue.WaitForEvent()

		if event.Type() == allegro.EventDisplayOrientation {
			o := event.DisplayOrientation()
			if o == allegro.DisplayOrientation0Degrees {
				fmt.Println("0 degrees")
			} else if o == allegro.DisplayOrientation90Degrees {
				fmt.Println("90 degrees")
			} else if o == allegro.DisplayOrientation180Degrees {
				fmt.Println("180 degrees")
			} else if o == allegro.DisplayOrientation270Degrees {
				fmt.Println("270 degrees")
			} else if o == allegro.DisplayOrientationFaceUp {
				fmt.Println("Face up")
			} else if o == allegro.DisplayOrientationFaceDown {
				fmt.Println("Face down")
			}
		}
		if event.Type() == allegro.EventDisplayClose {
			fmt.Println("Closing.")
			break
		}
		if event.Type() == allegro.EventMouseButtonDown {
			fmt.Println("Mouse down + ", event.MouseButton())
		}
		if event.Type() == allegro.EventMouseButtonUp {
			fmt.Println("Mouse up + ", event.MouseButton())
		}
		if event.Type() == allegro.EventKeyChar {
			k := event.KeyboardKeycode()
			u := event.KeyboardUnichar()
			fmt.Println("Keycode + ", k)
			fmt.Println("Unichar + ", u)

			if k == allegro.KeyEscape {
				fmt.Println("Closing (Escape key).")
				break
			}
			if u == '1' {
				zoom = 1
			}
			if u == '+' {
				zoom *= 1.1
			}
			if u == '-' {
				zoom /= 1.1
			}
			if u == 'f' {
				zoom = float32(display.GetWidth()) / float32(bitmap.GetWidth())
			}
		}
		if event.Type() == allegro.EventTimer {
			redraw = true
		}

		if redraw && queue.IsEmpty() {
			redraw = false
			allegro.ClearToColor(allegro.MapRgb(255, 255, 255))
			if zoom == 1 {
				bitmap.Draw(0, 0, 0)
			} else {
				bitmap.DrawScaledRotated(0, 0, 0, 0, zoom, zoom, 0, 0)
			}
			allegro.FlipDisplay()
		}
	}
}