diff --git a/test/add.ul b/test/add.ul index b947588..7d497af 100644 --- a/test/add.ul +++ b/test/add.ul @@ -5,10 +5,6 @@ const str nl = "\n"; plex Terminal { nat handle; - - init() { - handle = open("/dev/term/0", 0); - } } /** @@ -29,7 +25,7 @@ function add(int a, int b) int { * Print with a newline */ function pln(str message) { - Terminal term(); + Terminal term = open("/dev/term/0", 0); write(term, message, message.length); write(term, nl, nl.length); } diff --git a/test/fib.ul b/test/fib.ul index 5e3ef6c..71ad0a7 100644 --- a/test/fib.ul +++ b/test/fib.ul @@ -5,10 +5,6 @@ const str nl = "\n"; plex Terminal { nat handle; - - init() { - handle = open("/dev/term/0", 0); - } } /** @@ -30,7 +26,7 @@ function fib(int n) int { * Print with a newline */ function pln(str message) { - Terminal term(); + Terminal term = open("/dev/term/0", 0); write(term, message, message.length); write(term, nl, nl.length); } diff --git a/test/hello.ul b/test/hello.ul index c16d46f..396daf5 100644 --- a/test/hello.ul +++ b/test/hello.ul @@ -5,10 +5,6 @@ const str nl = "\n"; plex Terminal { nat handle; - - init() { - handle = open("/dev/term/0", 0); - } } /** @@ -22,7 +18,7 @@ function main() { * Print with a newline */ function pln(str message) { - Terminal term(); + Terminal term = open("/dev/term/0", 0); write(term, message, message.length); write(term, nl, nl.length); } diff --git a/test/loop.ul b/test/loop.ul index 2d1602f..8c75fb8 100644 --- a/test/loop.ul +++ b/test/loop.ul @@ -5,17 +5,13 @@ const str nl = "\n"; plex Terminal { nat handle; - - init() { - handle = open("/dev/term/0", 0); - } } /** * Main function */ function main() { - Terminal term(); + Terminal term = open("/dev/term/0", 0); real a = 5.0; do (int i = 5000, 0, -1) { a = a + 5.0; diff --git a/test/malloc.ul b/test/malloc.ul index 1014ca2..6c81ea3 100644 --- a/test/malloc.ul +++ b/test/malloc.ul @@ -1,23 +1,18 @@ /** * Constants */ -const str prompt = "Enter a string: "; const str nl = "\n"; plex Terminal { nat handle; - - init() { - handle = open("/dev/term/0", 0); - } } /** * Main function */ function main() { - Terminal term(); - pln(term, prompt); + Terminal term = open("/dev/term/0", 0); + pln(term, "Enter a string: "); pln(term, term.read(32)); return 0; } diff --git a/test/paint-bw.ul b/test/paint-bw.ul index fc5e8f1..cfe6c80 100644 --- a/test/paint-bw.ul +++ b/test/paint-bw.ul @@ -18,10 +18,12 @@ plex Screen implements Device { nat width; nat height; nat buffer_size; - byte[] screen_buffer; + byte[] buffer; - init() { - this.handle = open("/dev/screen/0", 0); + draw() { + unsafe { + write(this, this.buffer, this.buffer_size); + } } } @@ -33,26 +35,21 @@ plex Mouse implements Device { bool right; bool middle; bool btn4; - nat size; - - init() { - this.handle = open("/dev/mouse/0", 0); - } } /** * Main function */ function main() { - Screen screen(); - Mouse mouse(); + Screen screen = open("/dev/screen/0", 0); + Mouse mouse = open("/dev/mouse/0", 0); - outline_swatch(screen, BLACK, 1, 1, 8); - outline_swatch(screen, WHITE, 21, 1, 8); + outline_swatch(screen, BLACK, 1, 1); + outline_swatch(screen, WHITE, 21, 1); screen.draw(); loop { - mouse.read(); + mouse.refresh(); if (not mouse.left) continue; int box_size = 20; @@ -68,7 +65,7 @@ function main() { set_color(box_size, x, y, mouse.x, mouse.y, color); screen.draw(); - draw_box(screen, selected_color, x, y, 5, 5); + rectangle(screen, selected_color, x, y, 5, 5); } exit(0); } @@ -76,16 +73,18 @@ function main() { /** * Checks if the click is within the bound and update the selected color if so. */ -function set_color(int mouse_x, int mouse_y, int box_x, int box_y, nat color, nat box_size) { - int right = box_x + box_size; - int bottom = box_y + box_size; +function set_color(int box_size, int bx, int by, int mx, int my, byte color) { + int right = bx + box_size; + int bottom = by + box_size; - if (mouse_x < box_x) return; - if (mouse_x > right) return; - if (mouse_y < box_y) return; - if (mouse_y > bottom) return; + if (mx < bx) return; + if (mx > right) return; + if (my < by) return; + if (my > bottom) return; selected_color = color; + + return; } /** @@ -97,22 +96,24 @@ function outline_swatch(Device screen, byte color, int x, int y) { bg_color = DARK_GRAY; } - draw_box(screen, bg_color, x, y, 20, 20); - draw_box(screen, color, x + 2, y + 2, 17, 17); + rectangle(screen, bg_color, x, y, 20, 20); + rectangle(screen, color, x + 2, y + 2, 17, 17); + return; } /** - * Draw a box + * Draw a rectangle */ -function draw_box(Device screen, int screen_width, byte color, int x, int y, int width, int height) { - // we need unsafe because we are using `ptr` and `memset` directly +function rectangle(Device screen, byte color, int x, int y, int width, int height) { + // we need unsafe because we are using pointers `&` and `memset` directly // unsafe takes the guardrails off and allows you to access/modify memory directly unsafe { - int pixel = y * screen_width + x + screen.buffer.ptr + 4; + int pixel = y * width + x + &screen.buffer + 4; do (int i = height; i > 0; i--) { int row = pixel + width; - memset(screen.buffer.ptr, row, color, width); - pixel += screen_width; + memset(screen.buffer, row, color, width); + pixel += width; } } + return; } diff --git a/test/paint.ul b/test/paint.ul index 0bf6aa8..cfe6c80 100644 --- a/test/paint.ul +++ b/test/paint.ul @@ -18,11 +18,11 @@ plex Screen implements Device { nat width; nat height; nat buffer_size; - byte[] screen_buffer; + byte[] buffer; draw() { unsafe { - write(this.handle, &this.screen_buffer, this.buffer_size); + write(this, this.buffer, this.buffer_size); } } } @@ -105,13 +105,13 @@ function outline_swatch(Device screen, byte color, int x, int y) { * Draw a rectangle */ function rectangle(Device screen, byte color, int x, int y, int width, int height) { - // we need unsafe because we are using `ptr` and `memset` directly + // we need unsafe because we are using pointers `&` and `memset` directly // unsafe takes the guardrails off and allows you to access/modify memory directly unsafe { - int pixel = y * width + x + screen.buffer.ptr + 4; + int pixel = y * width + x + &screen.buffer + 4; do (int i = height; i > 0; i--) { int row = pixel + width; - memset(screen.buffer.ptr, row, color, width); + memset(screen.buffer, row, color, width); pixel += width; } } diff --git a/test/simple.ul b/test/simple.ul index ff59272..7f7b697 100644 --- a/test/simple.ul +++ b/test/simple.ul @@ -7,10 +7,6 @@ const real y = 1.0; plex Terminal { nat handle; - - init() { - this.handle = open("/dev/term/0", 0); - } } /** @@ -24,7 +20,7 @@ function main() { * Print with a newline */ function pln(str message) { - Terminal term(); + Terminal term = open("/dev/term/0", 0); write(term, message, message.length); write(term, nl, nl.length); } diff --git a/test/window.ul b/test/window.ul index 22e6fa9..7a6fd91 100644 --- a/test/window.ul +++ b/test/window.ul @@ -9,10 +9,6 @@ const nat WHITE = 255; */ plex Terminal { nat handle; - - init() { - handle = open("/dev/term/0", 0); - } } plex Screen { @@ -22,12 +18,8 @@ plex Screen { nat buffer_size; byte[] screen_buffer; - init() { - this.handle = open("/dev/screen/0", 0); - } - draw() { - write(this.handle, this.screen_buffer, this.buffer_size); + write(&this, this.screen_buffer, this.buffer_size); } } @@ -40,23 +32,19 @@ plex Mouse { bool middle; bool btn4; nat size; - - init() { - this.handle = open("/dev/mouse/0", 0); - } } /** * Main function */ function main() { - Screen screen(); + Screen screen = open("/dev/screen/0", 0); pln(screen.handle.str); pln(screen.width.str); pln(screen.size.str); pln(screen.screen_buffer.ptr.str); - Mouse mouse(); + Mouse mouse = open("/dev/mouse/0", 0); screen.draw(); loop {