Skip to content

[WIP] Wayland #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
60cface
Snake and gradient examples
versionpatch Jun 8, 2022
953d84a
Implemented wl_surface.c
versionpatch Jun 13, 2022
cdc804e
Wayland backend
versionpatch Jun 14, 2022
10135a0
Made surface object store a pointer to the SHM object to avoid needin…
versionpatch Jun 15, 2022
5282488
Wayland mouse and keyboard support.
versionpatch Jun 15, 2022
f45952f
Implemented window hiding. Fixed some formatting issues.
versionpatch Jun 15, 2022
03eb44e
Simple client side decoration setup
versionpatch Jun 16, 2022
a0c474d
Decoration title rendering
versionpatch Jun 17, 2022
acd94b2
Refactored some parts common between surface and decoration
versionpatch Jun 17, 2022
59c2556
Added close button
versionpatch Jun 17, 2022
51eae1b
Resize WIP
versionpatch Jun 21, 2022
fe0ff2d
Fixed resize memory problems
versionpatch Jun 21, 2022
3df780b
Decorations react to resize
versionpatch Jun 21, 2022
627d783
Moved resize time from buffer release to before drawing
versionpatch Jun 22, 2022
1ea160b
Allow disabling of decorations
versionpatch Jun 22, 2022
2c642b9
Added resize direction checks
versionpatch Jun 22, 2022
6c98065
Removed dependency on the decoration close button, a dummy buffer is …
versionpatch Jun 22, 2022
0be4c71
Cursor changes in resize region
versionpatch Jun 22, 2022
5c7906d
Backend stop, focus, middle mouse button
versionpatch Jun 23, 2022
0d18603
Added XDG decoration protocol
versionpatch Jun 23, 2022
7f08134
Added server side decorations
versionpatch Jun 23, 2022
437ee62
Linking
versionpatch Jun 23, 2022
d5c879a
Server side decoration fixes
versionpatch Jun 23, 2022
86f15be
Min Max buttons
versionpatch Jun 23, 2022
d14989a
Removed examples from wayland branch
versionpatch Jun 24, 2022
9f80d01
Refactored surface destruction
versionpatch Jun 27, 2022
fd06b92
Added asserts and the HAS_WAYLAND conditional to xdg-decor-protocol
versionpatch Jun 27, 2022
2fd0963
Modifiers + more fixes
versionpatch Jun 27, 2022
6f39813
Refactored keyboard file
versionpatch Jun 27, 2022
b9ea6f3
Frameless window fix
versionpatch Jun 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions examples/Gradients.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
open OcamlCanvas
open Float


let interpInt x1 x2 t =
int_of_float ((1. -. t)*.(float_of_int x1) +. t*.(float_of_int x2))
let interpColor c1 c2 t =
let r1, g1, b1 = Color.to_rgb c1 and r2, g2, b2 = Color.to_rgb c2 in
Color.of_rgb (interpInt r1 r2 t) (interpInt g1 g2 t) (interpInt b1 b2 t)
let hsv_to_rgb h s v =
let c = v *. s in
let m = v -. c in
let x = c *. (1. -. abs( ( (rem (h /. 60.) 2.) -. 1.))) in
let r,g,b = match(h) with
|a when a < 60. -> c,x,0.
|a when a < 120. -> x,c,0.
|a when a < 180. -> 0.,c,x
|a when a < 240. -> 0.,x,c
|a when a < 300. -> x,0.,c
|_ -> c,0.,x
in Color.of_rgb (int_of_float((r +. m)*.255.)) (int_of_float((g +. m)*.255.)) (int_of_float((b +. m)*.255.))

open Int64

let () =
Backend.(init default_options);
let c = Canvas.createFramed "test" ~pos:(960-640,540-360) ~size:(1280,720) in
Canvas.setFillColor c Color.white;
Canvas.fillRect c ~pos:(0.,0.) ~size:(1280.,720.);
Canvas.show c;
let r = ref (-1.) in
Backend.run(function
|Frame { canvas = c; timestamp = _ } ->
r := !r +. 1. /. 60.;
Canvas.setFillColor c (hsv_to_rgb (!r *. 36.) 1. 1.);
Canvas.fillRect c ~pos:(128. *. !r,0.) ~size:(128., 360.);
Canvas.setFillColor c (interpColor Color.black Color.white (!r *. 0.1));
Canvas.fillRect c ~pos:(128. *. !r,360.) ~size:(128., 360.);
true;
|_ ->
false;
)(function () ->
Printf.printf "Goodbye !\n"
)
83 changes: 83 additions & 0 deletions examples/SimpleSnakeGame.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
open OcamlCanvas
open Int64
open Random

let buildBackground c =
Canvas.setFillColor c Color.black;
Canvas.fillRect c ~pos:(0.,0.) ~size:(500.,500.);
Canvas.setFillColor c Color.white;
Canvas.fillRect c ~pos:(0.,0.) ~size:(500.,10.);
Canvas.fillRect c ~pos:(0.,490.) ~size:(500.,10.);
Canvas.fillRect c ~pos:(0.,0.) ~size:(10.,500.);
Canvas.fillRect c ~pos:(490.,0.) ~size:(10.,500.);
()

let placeBlock c (x,y) col =
Canvas.setFillColor c col;
Canvas.fillRect c ~pos:(x *. 10. , y *. 10.) ~size:(10.,10.);
()

let rec drawSnake c s = match(s) with
|[] -> ()
|h::t -> placeBlock c h Color.orange; drawSnake c t

let rec moveSnake s p = match(s) with
|[] -> []
|h::t -> p::(moveSnake t h)

let sumCoord (a,b) (c,d) = (a +. c,b +. d)

let moveSnakeDirection s d = match(s) with
|[] -> []
|h::t -> moveSnake s (sumCoord d h)

let snakeHitSelf s = match(s) with
|[] -> false
|h::t -> (List.mem h t)
let snakeHitWall s = let h::t = s in let (x,y) = h in (x < 1.) or (x > 48.) or (y < 1.) or (y > 48.)


let () =
Random.self_init();
Backend.(init default_options);
let c = Canvas.createFramed "test" ~pos:(960-250,540-250) ~size:(500,500) in
Canvas.show c;
let r = ref (-1.) in
let snake = ref [(6.,8.);(6.,7.)] and currentDirection = ref (0.,1.) and foodLocation = ref(24.,24.) in
Backend.run(function
|KeyAction { canvas = c; timestamp = _;
key = KeyUpArrow; char; flags = _; state = Down } ->
let (x,y) = !currentDirection in
if (y = 0.) then currentDirection := (0.,-1.);
true;
|KeyAction { canvas = c; timestamp = _;
key = KeyDownArrow; char; flags = _; state = Down } ->
let (x,y) = !currentDirection in
if (y = 0.) then currentDirection := (0.,1.);
true;
|KeyAction { canvas = c; timestamp = _;
key = KeyLeftArrow; char; flags = _; state = Down } ->
let (x,y) = !currentDirection in
if (x = 0.) then currentDirection := (-1.,0.);
true;
|KeyAction { canvas = c; timestamp = _;
key = KeyRightArrow; char; flags = _; state = Down } ->
let (x,y) = !currentDirection in
if (x = 0.) then currentDirection := (1.,0.);
true;

|Frame { canvas = c; timestamp = _ } ->
r := !r +. 1. /. 60.;
buildBackground c;
let h::t = !snake in
if ((sumCoord h !currentDirection) = !foodLocation) then (snake := !foodLocation::!snake; foodLocation := (2. +. float_of_int (Random.int 47),2. +. float_of_int (Random.int 47)));
if !r > 0.033 then (r := 0.; snake := moveSnakeDirection !snake !currentDirection);
if (snakeHitSelf !snake or snakeHitWall !snake) then Backend.stop();
drawSnake c !snake;
placeBlock c !foodLocation Color.green;
true;
|_ ->
false;
)(function () ->
Printf.printf "Goodbye !\n"
)
16 changes: 16 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@
(modules test_canvas)
(libraries ocaml-canvas)
)

(executable
(name Gradients)
(public_name Gradients)
(modes byte_complete native js)
(modules Gradients)
(libraries ocaml-canvas)
)

(executable
(name SimpleSnakeGame)
(public_name Snake)
(modes byte_complete native js)
(modules SimpleSnakeGame)
(libraries ocaml-canvas)
)
2 changes: 1 addition & 1 deletion src/implem/backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ backend_init(
case_GDI(result = gdi_backend_init());
case_QUARTZ(result = qtz_backend_init());
case_X11(result = x11_backend_init());
case_WAYLAND(/*result = wl_backend_init()*/);
case_WAYLAND(result = wl_backend_init());
default_ignore();
}

Expand Down
14 changes: 12 additions & 2 deletions src/implem/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ surface_destroy(
}

if (s->data) {
free(s->data);
switch_IMPL() {
case_WAYLAND(munmap(s->data,s->width * s->height * 4));
default:
free(s->data);
break;
}
}

free(s);
Expand Down Expand Up @@ -191,7 +196,12 @@ surface_resize(

_surface_copy_to_buffer(s, data, width, height);

free(s->data);
switch_IMPL() {
case_WAYLAND(munmap(s->data,s->width * s->height * 4));
default:
free(s->data);
break;
}

} else {

Expand Down
Loading