aboutsummaryrefslogtreecommitdiff
path: root/content/drawing-pixels-in-plan9.md
blob: 473ccaf68ae2c2bb9aa3d66f8507056d41e0b623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
title: "Drawing Pixels in Plan9"
url: drawing-pixels-in-plan9.html
date: 2023-05-27T17:41:33+02:00
type: note
draft: false
tags: [plan9, graphics]
---

I have started exploring Plan9's graphics capabilities. This is a hello world
alternative for drawing that draws a yellow square on a blue background.

More information:

- [draw.h header file](https://github.com/0intro/plan9/blob/main/sys/include/draw.h)
  contains all the drawing functions
- [draw man page](https://9fans.github.io/plan9port/man/man3/draw.html)
  has a bit more digestable descriptions of the draw functions
- [graphics man page](https://9fans.github.io/plan9port/man/man3/graphics.html)
  has a bit more digestable descriptions of the graphics functions
- [all man pages](https://9fans.github.io/plan9port/man/man3/)
  can be a valuable resource for learning about the system

![Plan9 Howdy World!](/notes/plan9-pixels.png)

```c
// main.c
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <cursor.h>

void
main()
{
  ulong co;
  Image *im, *bg;
  co = 0x0000FFFF;

  if (initdraw(nil, nil, argv0) < 0)
  {
    sysfatal("%s: %r", argv0);
  }

  im = allocimage(display, Rect(0, 0, 300, 300), RGB24, 0, DYellow);
  bg = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, co);

  if (im == nil || bg == nil)
  {
    sysfatal("not enough memory");
  }

  draw(screen, screen->r, bg, nil, ZP);
  draw(screen, screen->r, im, nil, Pt(-40, -40));

  flushimage(display, Refnone);

  // Wait 10 seconds before exiting.
  sleep(10000);

  exits(nil);
}
```

And then compile with `mk` (mkfile below):

```makefile
# mkfile
</$objtype/mkfile

RC=/rc/bin
BIN=/$objtype/bin
MAN=/sys/man

main:
	$CC $CFLAGS main.c
	$LD $LDFLAGS -o main main.$O
```

And run with `./main`. To exit the program, press `Delete key` (strange but this
is the alternative for Ctrl+C).

*This is **very cool** indeed!*