undar-lang/test/window.ul.vuir

96 lines
2.1 KiB
Plaintext

global str screen_namespace = "/dev/screen/0"
global str mouse_namespace = "/dev/mouse/0"
global str terminal_namespace = "/dev/term/0"
global str new_line = "\n"
global byte WHITE = 255
/**
* Devices
*/
plex Terminal
nat handle
plex Screen
nat handle
nat width
nat height
byte[] buffer
plex Mouse
nat handle
nat x
nat y
bool left
bool right
bool middle
bool btn4
nat size
function main ()
// open screen
// use load immediate because it is a pointer to a string, not a value
nat tmp_ptr = &screen_namespace
int mode = 0
ptr screen = open(tmp_ptr, mode)
nat screen_handle = screen.handle
str tmp_str = nat_to_string(screen_handle)
pln(tmp_str)
nat width = screen.width
tmp_str = nat_to_string(width)
pln(tmp_str)
nat buffer_size = screen.buffer
tmp_str = nat_to_string(buffer_size)
pln(tmp_str)
nat offset_temp = 16
nat screen_buffer = add_nat(screen, offset_temp)
tmp_str = nat_to_string(screen_buffer)
pln(tmp_str)
// open mouse
tmp_ptr = &mouse_namespace
ptr mouse = open(tmp_ptr, mode)
write(screen, screen_buffer, buffer_size) // redraw
loop draw_loop
// load mouse click data
stat(mouse)
byte left_down = mouse.left // load btn1 pressed
jump_eq_nat(&draw_loop, left_down, mode) // mode is 0 which is an alias for false
nat x = mouse.x
nat y = mouse.y
// Compute start address: y*width + x
nat pixel_pos = mul_nat(y, width)
pixel_pos = add_nat(x, pixel_pos)
pixel_pos = add_nat(screen_buffer, pixel_pos)
nat fat_ptr_size = 4 // need to add offset for fat pointer size
pixel_pos = add_nat(pixel_pos, fat_ptr_size)
byte color = WHITE
screen.buffer[pixel_pos] = color // draw color at screen [x,y]
write(screen, screen_buffer, buffer_size) // redraw
jump(&draw_loop)
exit 0
function pln (str message)
nat term_ns = &terminal_namespace // get terminal device
int mode = 0
ptr term = open(term_ns, mode)
int msg_length = strlen(message)
write(term, message, msg_length)
str nl = &new_line
int nl_length = strlen(nl)
write(term, nl, nl_length)
return