From c02ad6d7fb3769498fabc9479293b6faea07385f Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sat, 28 Dec 2019 01:41:32 +0100 Subject: [PATCH] Added DisplayMode and PixelFormat. --- display.go | 33 +++++++++++++++++++++++++++++++-- pixelformat.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 pixelformat.go diff --git a/display.go b/display.go index 230e424..61925db 100644 --- a/display.go +++ b/display.go @@ -5,14 +5,43 @@ import "C" import ( "errors" + "sort" "unsafe" ) -// Display represents a display +// Display represents a display. type Display struct { display *C.ALLEGRO_DISPLAY } +// DisplayMode contains information about a fullscreen display mode. +type DisplayMode struct { + Width int + Height int + Format PixelFormat + RefreshRate int +} + +// DisplayMode returns all available fullscreen display modes. +func DisplayModes() []DisplayMode { + n := int(C.al_get_num_display_modes()) + var modes []DisplayMode + var mode C.ALLEGRO_DISPLAY_MODE + for i := 0; i < n; i++ { + C.al_get_display_mode(C.int(i), &mode) + modes = append(modes, DisplayMode{ + Width: int(mode.width), + Height: int(mode.height), + Format: PixelFormat(mode.format), + RefreshRate: int(mode.refresh_rate), + }) + } + weight := func(m DisplayMode) int { return (m.Width*m.Height)*10000 + (m.RefreshRate * 100) + int(m.Format) } + sort.Slice(modes, func(i, j int) bool { return weight(modes[i]) < weight(modes[j]) }) + return modes +} + +// NewDisplayOptions sets options for creating a new display. type NewDisplayOptions struct { Fullscreen bool Resizable bool @@ -24,7 +53,7 @@ type NewDisplayOptions struct { OpenGL bool } -// NewDisplay creates a display +// NewDisplay creates a display. func NewDisplay(width, height int, options NewDisplayOptions) (*Display, error) { var flags C.int = C.ALLEGRO_WINDOWED if options.Fullscreen { diff --git a/pixelformat.go b/pixelformat.go new file mode 100644 index 0000000..70988d8 --- /dev/null +++ b/pixelformat.go @@ -0,0 +1,41 @@ +package allg5 + +// #include +import "C" + +// PixelFormat describes the components a pixel. +type PixelFormat int + +const ( + PixelFormatAny PixelFormat = C.ALLEGRO_PIXEL_FORMAT_ANY + PixelFormatAnyNoAlpha = C.ALLEGRO_PIXEL_FORMAT_ANY_NO_ALPHA + PixelFormatAnyAlpha = C.ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA + PixelFormatAny15NoAlpha = C.ALLEGRO_PIXEL_FORMAT_ANY_15_NO_ALPHA + PixelFormatAny16NoAlpha = C.ALLEGRO_PIXEL_FORMAT_ANY_16_NO_ALPHA + PixelFormatAny16Alpha = C.ALLEGRO_PIXEL_FORMAT_ANY_16_WITH_ALPHA + PixelFormatAny24NoAlpha = C.ALLEGRO_PIXEL_FORMAT_ANY_24_NO_ALPHA + PixelFormatAny32NoAlpha = C.ALLEGRO_PIXEL_FORMAT_ANY_32_NO_ALPHA + PixelFormatAny32Alpha = C.ALLEGRO_PIXEL_FORMAT_ANY_32_WITH_ALPHA + PixelFormatARGB8888 = C.ALLEGRO_PIXEL_FORMAT_ARGB_8888 + PixelFormatRGBA8888 = C.ALLEGRO_PIXEL_FORMAT_RGBA_8888 + PixelFormatARGB4444 = C.ALLEGRO_PIXEL_FORMAT_ARGB_4444 + PixelFormatRGB888 = C.ALLEGRO_PIXEL_FORMAT_RGB_888 + PixelFormatRGB565 = C.ALLEGRO_PIXEL_FORMAT_RGB_565 + PixelFormatRGB555 = C.ALLEGRO_PIXEL_FORMAT_RGB_555 + PixelFormatRGBA5551 = C.ALLEGRO_PIXEL_FORMAT_RGBA_5551 + PixelFormatARGB1555 = C.ALLEGRO_PIXEL_FORMAT_ARGB_1555 + PixelFormatABGR8888 = C.ALLEGRO_PIXEL_FORMAT_ABGR_8888 + PixelFormatXBGR8888 = C.ALLEGRO_PIXEL_FORMAT_XBGR_8888 + PixelFormatBGR888 = C.ALLEGRO_PIXEL_FORMAT_BGR_888 + PixelFormatBGR565 = C.ALLEGRO_PIXEL_FORMAT_BGR_565 + PixelFormatBGR555 = C.ALLEGRO_PIXEL_FORMAT_BGR_555 + PixelFormatRGBX8888 = C.ALLEGRO_PIXEL_FORMAT_RGBX_8888 + PixelFormatXRGB8888 = C.ALLEGRO_PIXEL_FORMAT_XRGB_8888 + PixelFormatABGRF32 = C.ALLEGRO_PIXEL_FORMAT_ABGR_F32 + PixelFormatABGR8888LE = C.ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE + PixelFormatRGBA4444 = C.ALLEGRO_PIXEL_FORMAT_RGBA_4444 + PixelFormatSingleChannel8 = C.ALLEGRO_PIXEL_FORMAT_SINGLE_CHANNEL_8 + PixelFormatCompressedRGBADXT1 = C.ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT1 + PixelFormatCompressedRGBADXT3 = C.ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT3 + PixelFormatCompressedRGBADXT5 = C.ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT5 +)