Files
kryon/tests/window_runtime_test.zi
T

99 lines
4.7 KiB
Ziran

Window :: #import "window_runtime";
Desktop :: #import "desktop_runtime";
Raster :: #import "cairo_raster";
Event :: #import "sdl_event";
#import "drawing_props"
sdl :: #system_library "SDL2";
GetRenderer :: (window: *void) -> *void #foreign sdl "SDL_GetRenderer";
ReadPixel :: (renderer: *void, bounds: *PixelBounds, format: u32, pixels: *u32, pitch: s32) -> s32 #foreign sdl "SDL_RenderReadPixels";
ReadRgb :: (pixel: u32, format: *void, red: *u8, green: *u8, blue: *u8) #foreign sdl "SDL_GetRGB";
Minimize :: (window: *void) #foreign sdl "SDL_MinimizeWindow";
Restore :: (window: *void) #foreign sdl "SDL_RestoreWindow";
Flags :: (window: *void) -> u32 #foreign sdl "SDL_GetWindowFlags";
WindowId :: (window: *void) -> u32 #foreign sdl "SDL_GetWindowID";
Push :: (event: *Event.Event) -> s32 #foreign sdl "SDL_PushEvent";
cairo :: #system_library "cairo";
SavePaint :: (paint: *void) #foreign cairo "cairo_save";
RestorePaint :: (paint: *void) #foreign cairo "cairo_restore";
PaintRectangle :: (paint: *void, x: float64, y: float64, width: float64, height: float64) #foreign cairo "cairo_rectangle";
ClipPaint :: (paint: *void) #foreign cairo "cairo_clip";
PixelBounds :: struct { x: s32; y: s32; width: s32; height: s32; }
Pixel :: (x: s32, y: s32) -> u32 {
pixels := Raster.RasterSurfaceData()
row := cast(*u32)*pixels[cast(s64)y * cast(s64)Raster.RasterSurfaceStride()]
return row[x]
}
// Read the actual presentation target. SDL's software window path has no
// renderer: its borrowed surface is the displayed buffer, not a texture.
PresentedPixel :: (x: s32, y: s32, pixel: *u32) -> bool {
window := Desktop.DesktopWindow()
renderer := GetRenderer(window)
if renderer != null {
area := PixelBounds.{x, y, 1, 1}
return ReadPixel(renderer, *area, cast(u32)0x16362004, pixel, 4) == 0
}
surface := cast(*Desktop.DesktopWindowSurface)Desktop.GetWindowSurface(window)
if surface == null || surface.format == null || surface.pixels == null ||
surface.format.bytes != 4 || x < 0 || y < 0 ||
x >= surface.width || y >= surface.height { return false }
row := cast(*u32)*surface.pixels[cast(s64)y * surface.pitch]
red: u8
green: u8
blue: u8
ReadRgb(row[x], cast(*void)surface.format, *red, *green, *blue)
pixel.* = cast(u32)0xff000000 | (cast(u32)red << 16) |
(cast(u32)green << 8) | cast(u32)blue
return true
}
#program_export
main :: () -> s32 {
if !Desktop.OpenDesktopSized(64, 48) { return 1 }
if Window.WindowWidth() != 64 || Window.WindowHeight() != 48 ||
!Window.WindowHidden() || Window.WindowMinimized() || Window.WindowClosing() { return 2 }
Window.BeginWindowFrame()
Window.ClearWindowFrame(.{11, 22, 33, 255})
Window.EndWindowFrame()
if Window.WindowHidden() || Pixel(63, 47) != cast(u32)0xff0b1621 { return 3 }
presented: u32
if !PresentedPixel(63, 47, *presented) ||
presented != cast(u32)0xff0b1621 { return 4 }
if !Desktop.ResizeDesktop(80, 40) || Window.WindowWidth() != 64 { return 5 }
Window.BeginWindowFrame()
if Window.WindowWidth() != 80 || Window.WindowHeight() != 40 { return 6 }
// Clearing replaces the whole surface despite a caller's clip, then
// restores the clip for later drawing. Alpha is Cairo's premultiplied ARGB.
SavePaint(Raster.paint)
PaintRectangle(Raster.paint, 0.0, 0.0, 4.0, 4.0)
ClipPaint(Raster.paint)
Window.ClearWindowFrame(.{200, 100, 50, 128})
Raster.RasterRoundedRectangle(Desktop.DesktopViewport(), 0.0, 0, .{10, 20, 30, 255})
RestorePaint(Raster.paint)
Window.EndWindowFrame()
if Pixel(0, 0) != cast(u32)0xff0a141e || Pixel(79, 39) != cast(u32)0x80643219 { return 7 }
Desktop.WindowSetVisibleHost(false)
if !Window.WindowHidden() { return 8 }
Window.BeginWindowFrame()
Window.ClearWindowFrame(.{255, 255, 255, 0})
Window.EndWindowFrame()
if Pixel(0, 0) != cast(u32)0 || Pixel(79, 39) != cast(u32)0 { return 9 }
Desktop.WindowSetVisibleHost(true)
Minimize(Desktop.DesktopWindow())
if Window.WindowMinimized() != ((Flags(Desktop.DesktopWindow()) & cast(u32)0x40) != cast(u32)0) { return 10 }
Restore(Desktop.DesktopWindow())
if !Desktop.WindowInterceptCloseHost(true) { return 11 }
event: Event.Event
event.kind = cast(u32)0x200
event.window_id = WindowId(Desktop.DesktopWindow())
event.which = cast(u32)14
if Push(*event) != 1 || Window.WindowClosing() { return 12 }
if !Desktop.PollDesktop() || !Window.WindowClosing() || !Window.WindowClosing() { return 13 }
if !Desktop.WindowTakeCloseRequestHost() || Window.WindowClosing() { return 14 }
Window.CloseNativeWindow()
if Desktop.DesktopWindow() != null || !Window.WindowHidden() || !Window.WindowClosing() { return 15 }
Window.CloseNativeWindow()
return 0
}