about summary refs log tree commit diff stats
path: root/apps/tile.mu
blob: 6ac49d3974d3637e81d5df2c856e1b17bc9a13b3 (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Randomly tile from a given set of tiles
#
# To build:
#   $ git clone https://github.com/akkartik/mu
#   $ cd mu
#   $ ./translate apps/tile.mu
# To run:
#   $ qemu-system-i386 code.img

type tile {
  data: (handle array handle array int)
}

fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk) {
  # basic maze
  # 10 PRINT CHR$(205.5 + RND(1))
  # GOTO 10
#?   var tiles-storage: (array tile 2)
#?   var tiles/esi: (addr array tile) <- address tiles-storage
#?   set-tile tiles, 0/idx, 1 0 0, 0 1 0, 0 0 1
#?   set-tile tiles, 1/idx, 0 0 1, 0 1 0, 1 0 0

  # https://pbat.ch/wiki/trikuf
  var tiles-storage: (array tile 0x62)
  var tiles/esi: (addr array tile) <- address tiles-storage
  set-tile tiles, 0x00/idx, 1 1 1, 0 1 0, 0 0 0
  set-tile tiles, 0x01/idx, 0 1 1, 1 1 0, 0 0 0
  set-tile tiles, 0x02/idx, 1 1 1, 1 0 1, 0 0 0
  set-tile tiles, 0x03/idx, 1 1 0, 0 1 1, 0 0 0
  set-tile tiles, 0x04/idx, 0 0 0, 1 1 1, 0 0 0
  set-tile tiles, 0x05/idx, 1 0 0, 1 1 1, 0 0 0
  set-tile tiles, 0x06/idx, 0 1 0, 1 1 1, 0 0 0
  set-tile tiles, 0x07/idx, 0 0 1, 1 1 1, 0 0 0
  set-tile tiles, 0x08/idx, 1 0 1, 1 1 1, 0 0 0
  set-tile tiles, 0x09/idx, 0 1 1, 1 1 0, 1 0 0
  set-tile tiles, 0x0a/idx, 0 1 1, 0 0 1, 1 0 0
  set-tile tiles, 0x0b/idx, 1 1 1, 0 0 1, 1 0 0
  set-tile tiles, 0x0c/idx, 1 1 1, 1 0 1, 1 0 0
  set-tile tiles, 0x0d/idx, 0 0 0, 1 1 1, 1 0 0
  set-tile tiles, 0x0e/idx, 1 0 0, 1 1 1, 1 0 0
  set-tile tiles, 0x0f/idx, 0 1 0, 1 1 1, 1 0 0
  set-tile tiles, 0x10/idx, 0 0 1, 1 1 1, 1 0 0
  set-tile tiles, 0x11/idx, 1 0 1, 1 1 1, 1 0 0
  set-tile tiles, 0x12/idx, 1 0 1, 0 0 0, 0 1 0
  set-tile tiles, 0x13/idx, 1 1 1, 0 0 0, 0 1 0
  set-tile tiles, 0x14/idx, 1 1 1, 0 1 0, 0 1 0
  set-tile tiles, 0x15/idx, 0 1 1, 1 1 0, 0 1 0
  set-tile tiles, 0x16/idx, 1 1 0, 0 1 1, 0 1 0
  set-tile tiles, 0x17/idx, 0 0 0, 1 1 1, 0 1 0
  set-tile tiles, 0x18/idx, 1 0 0, 1 1 1, 0 1 0
  set-tile tiles, 0x19/idx, 0 1 0, 1 1 1, 0 1 0
  set-tile tiles, 0x1a/idx, 0 0 1, 1 1 1, 0 1 0
  set-tile tiles, 0x1b/idx, 1 0 1, 1 1 1, 0 1 0
  set-tile tiles, 0x1c/idx, 1 0 1, 0 0 0, 1 1 0
  set-tile tiles, 0x1d/idx, 0 1 1, 0 0 0, 1 1 0
  set-tile tiles, 0x1e/idx, 1 1 1, 0 0 0, 1 1 0
  set-tile tiles, 0x1f/idx, 0 0 1, 1 0 0, 1 1 0
  set-tile tiles, 0x20/idx, 1 0 1, 1 0 0, 1 1 0
  set-tile tiles, 0x21/idx, 1 1 1, 1 0 0, 1 1 0
  set-tile tiles, 0x22/idx, 0 1 1, 0 1 0, 1 1 0
  set-tile tiles, 0x23/idx, 1 1 1, 0 1 0, 1 1 0
  set-tile tiles, 0x24/idx, 0 0 0, 0 1 1, 1 1 0
  set-tile tiles, 0x25/idx, 0 1 0, 0 1 1, 1 1 0
  set-tile tiles, 0x26/idx, 1 1 0, 0 1 1, 1 1 0
  set-tile tiles, 0x27/idx, 0 0 1, 0 1 1, 1 1 0
  set-tile tiles, 0x28/idx, 1 1 0, 1 0 0, 0 0 1
  set-tile tiles, 0x29/idx, 1 1 1, 1 0 0, 0 0 1
  set-tile tiles, 0x2a/idx, 1 1 1, 1 0 1, 0 0 1
  set-tile tiles, 0x2b/idx, 1 1 0, 0 1 1, 0 0 1
  set-tile tiles, 0x2c/idx, 0 0 0, 1 1 1, 0 0 1
  set-tile tiles, 0x2d/idx, 1 0 0, 1 1 1, 0 0 1
  set-tile tiles, 0x2e/idx, 0 1 0, 1 1 1, 0 0 1
  set-tile tiles, 0x2f/idx, 0 0 1, 1 1 1, 0 0 1
  set-tile tiles, 0x30/idx, 1 0 1, 1 1 1, 0 0 1
  set-tile tiles, 0x31/idx, 0 1 0, 0 0 0, 1 0 1
  set-tile tiles, 0x32/idx, 1 1 0, 0 0 0, 1 0 1
  set-tile tiles, 0x33/idx, 0 1 1, 0 0 0, 1 0 1
  set-tile tiles, 0x34/idx, 1 1 1, 0 0 0, 1 0 1
  set-tile tiles, 0x35/idx, 1 1 0, 1 0 0, 1 0 1
  set-tile tiles, 0x36/idx, 1 1 1, 1 0 0, 1 0 1
  set-tile tiles, 0x37/idx, 0 1 1, 0 0 1, 1 0 1
  set-tile tiles, 0x38/idx, 1 1 1, 0 0 1, 1 0 1
  set-tile tiles, 0x39/idx, 1 1 1, 1 0 1, 1 0 1
  set-tile tiles, 0x3a/idx, 0 0 0, 1 1 1, 1 0 1
  set-tile tiles, 0x3b/idx, 1 0 0, 1 1 1, 1 0 1
  set-tile tiles, 0x3c/idx, 0 1 0, 1 1 1, 1 0 1
  set-tile tiles, 0x3d/idx, 0 0 1, 1 1 1, 1 0 1
  set-tile tiles, 0x3e/idx, 1 0 1, 1 1 1, 1 0 1
  set-tile tiles, 0x3f/idx, 1 1 0, 0 0 0, 0 1 1
  set-tile tiles, 0x40/idx, 1 0 1, 0 0 0, 0 1 1
  set-tile tiles, 0x41/idx, 1 1 1, 0 0 0, 0 1 1
  set-tile tiles, 0x42/idx, 1 1 0, 0 1 0, 0 1 1
  set-tile tiles, 0x43/idx, 1 1 1, 0 1 0, 0 1 1
  set-tile tiles, 0x44/idx, 0 0 0, 1 1 0, 0 1 1
  set-tile tiles, 0x45/idx, 1 0 0, 1 1 0, 0 1 1
  set-tile tiles, 0x46/idx, 0 1 0, 1 1 0, 0 1 1
  set-tile tiles, 0x47/idx, 0 1 1, 1 1 0, 0 1 1
  set-tile tiles, 0x48/idx, 1 0 0, 0 0 1, 0 1 1
  set-tile tiles, 0x49/idx, 1 0 1, 0 0 1, 0 1 1
  set-tile tiles, 0x4a/idx, 1 1 1, 0 0 1, 0 1 1
  set-tile tiles, 0x4b/idx, 0 1 0, 0 0 0, 1 1 1
  set-tile tiles, 0x4c/idx, 1 1 0, 0 0 0, 1 1 1
  set-tile tiles, 0x4d/idx, 1 0 1, 0 0 0, 1 1 1
  set-tile tiles, 0x4e/idx, 0 1 1, 0 0 0, 1 1 1
  set-tile tiles, 0x4f/idx, 1 1 1, 0 0 0, 1 1 1
  set-tile tiles, 0x50/idx, 1 1 0, 1 0 0, 1 1 1
  set-tile tiles, 0x51/idx, 0 0 1, 1 0 0, 1 1 1
  set-tile tiles, 0x52/idx, 1 0 1, 1 0 0, 1 1 1
  set-tile tiles, 0x53/idx, 1 1 1, 1 0 0, 1 1 1
  set-tile tiles, 0x54/idx, 0 0 0, 0 1 0, 1 1 1
  set-tile tiles, 0x55/idx, 0 1 0, 0 1 0, 1 1 1
  set-tile tiles, 0x56/idx, 1 1 0, 0 1 0, 1 1 1
  set-tile tiles, 0x57/idx, 0 1 1, 0 1 0, 1 1 1
  set-tile tiles, 0x58/idx, 1 1 1, 0 1 0, 1 1 1
  set-tile tiles, 0x59/idx, 1 0 0, 0 0 1, 1 1 1
  set-tile tiles, 0x5a/idx, 1 0 1, 0 0 1, 1 1 1
  set-tile tiles, 0x5b/idx, 0 1 1, 0 0 1, 1 1 1
  set-tile tiles, 0x5c/idx, 1 1 1, 0 0 1, 1 1 1
  set-tile tiles, 0x5d/idx, 0 0 0, 1 0 1, 1 1 1
  set-tile tiles, 0x5e/idx, 1 0 0, 1 0 1, 1 1 1
  set-tile tiles, 0x5f/idx, 0 0 1, 1 0 1, 1 1 1
  set-tile tiles, 0x60/idx, 1 0 1, 1 0 1, 1 1 1
  set-tile tiles, 0x61/idx, 1 1 1, 1 0 1, 1 1 1

  render-tiles screen, tiles
  {
    var key/eax: byte <- read-key keyboard
    compare key, 0
    loop-if-=
  }
  var step/eax: int <- copy 0
  {
    render-random screen, tiles, 4/pixels-per-cell, step
    {
      var key/eax: byte <- read-key keyboard
      compare key, 0
      loop-if-=
    }
    step <- increment
    loop
  }
}

fn set-tile _tiles: (addr array tile), _idx: int, n0: int, n1: int, n2: int, n3: int, n4: int, n5: int, n6: int, n7: int, n8: int {
  var tiles/eax: (addr array tile) <- copy _tiles
  var idx/ecx: int <- copy _idx
  var tile/edi: (addr tile) <- index tiles, idx
  var rows-ah/eax: (addr handle array handle array int) <- get tile, data
  populate rows-ah, 3
  var _rows/eax: (addr array handle array int) <- lookup *rows-ah
  var rows/edi: (addr array handle array int) <- copy _rows
  var row0-ah/eax: (addr handle array int) <- index rows, 0
  populate row0-ah, 3
  var row0/eax: (addr array int) <- lookup *row0-ah
  var x0/ecx: (addr int) <- index row0, 0
  var src/esi: int <- copy n0
  copy-to *x0, src
  var x1/ecx: (addr int) <- index row0, 1
  var src/esi: int <- copy n1
  copy-to *x1, src
  var x2/ecx: (addr int) <- index row0, 2
  var src/esi: int <- copy n2
  copy-to *x2, src
  var row1-ah/eax: (addr handle array int) <- index rows, 1
  populate row1-ah, 3
  var row1/eax: (addr array int) <- lookup *row1-ah
  var x3/ecx: (addr int) <- index row1, 0
  var src/esi: int <- copy n3
  copy-to *x3, src
  var x4/ecx: (addr int) <- index row1, 1
  var src/esi: int <- copy n4
  copy-to *x4, src
  var x5/ecx: (addr int) <- index row1, 2
  var src/esi: int <- copy n5
  copy-to *x5, src
  var row2-ah/eax: (addr handle array int) <- index rows, 2
  populate row2-ah, 3
  var row2/eax: (addr array int) <- lookup *row2-ah
  var x6/ecx: (addr int) <- index row2, 0
  var src/esi: int <- copy n6
  copy-to *x6, src
  var x7/ecx: (addr int) <- index row2, 1
  var src/esi: int <- copy n7
  copy-to *x7, src
  var x8/ecx: (addr int) <- index row2, 2
  var src/esi: int <- copy n8
  copy-to *x8, src
}

fn render-tiles screen: (addr screen), _tiles: (addr array tile) {
  draw-rect screen, 0 0, 0x400 0x300, 8
  var tiles/esi: (addr array tile) <- copy _tiles
  var num-tiles: int
  var tmp/eax: int <- length tiles
  copy-to num-tiles, tmp
#?   draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, num-tiles, 0x31, 0
  var i/ecx: int <- copy 0
  {
    compare i, num-tiles
    break-if->=
#?     draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, i, 0x31, 0
    var tile/eax: (addr tile) <- index tiles, i
    var start-x/edx: int <- copy i
    start-x <- and 0xf/tiles-per-row-minus-one
    start-x <- shift-left 5  # ceil(log(3 * pixels-per-cell))
    start-x <- add 0x20/left-margin
#?     draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, start-x, 0x31 0
    var start-y/ebx: int <- copy i
    start-y <- shift-right 4/log2-tiles-per-row
    start-y <- shift-left 5  # ceil(log(3 * pixels-per-cell))
    start-y <- add 0x20/top-margin
#?     draw-int32-hex-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, start-x, 0x31, 0
    render-tile screen, tile, start-x, start-y, 8/pixels-per-cell
    i <- increment
    loop
  }
}

fn render-tile screen: (addr screen), _tile: (addr tile), start-x: int, start-y: int, n: int {
  var tile/esi: (addr tile) <- copy _tile
  var y/ecx: int <- copy start-y
  y <- add 4/margin-top
  var rows-ah/eax: (addr handle array handle array int) <- get tile, data
  var rows/eax: (addr array handle array int) <- lookup *rows-ah
  var i/ebx: int <- copy 0
  {
    compare i, 3
    break-if->=
    var x/edx: int <- copy start-x
    x <- add 4/margin-left
    var curr-row-ah/eax: (addr handle array int) <- index rows, i
    var curr-row/eax: (addr array int) <- lookup *curr-row-ah
    var j/edi: int <- copy 0
    {
      compare j, 3
      break-if->=
      var curr/eax: (addr int) <- index curr-row, j
      var color/eax: int <- copy *curr
      color <- shift-left 1
#?       draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, x, 9 0
#?       draw-int32-decimal-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, y, 0xc 0
      draw-rect2 screen, x y, n n, color
      j <- increment
      x <- add n
      loop
    }
    i <- increment
    y <- add n
    loop
  }
}

fn render-random screen: (addr screen), _tiles: (addr array tile), pixels-per-cell: int, seed: int {
  draw-rect screen, 0 0, 0x400 0x300, 8
  var tiles/esi: (addr array tile) <- copy _tiles
  var rand/edi: int <- next-random seed
  var num-tiles/ebx: int <- length tiles
  var y/ecx: int <- copy 0
  {
    compare y, 0x300
    break-if->=
    var x/edx: int <- copy 0
    {
      compare x, 0x400
      break-if->=
      var i/eax: int <- remainder rand, num-tiles
      var tile/eax: (addr tile) <- index tiles, i
      render-tile-without-margin screen, tile, x, y, pixels-per-cell
      x <- add pixels-per-cell
      x <- add pixels-per-cell
      x <- add pixels-per-cell
      rand <- next-random rand
      loop
    }
    y <- add pixels-per-cell
    y <- add pixels-per-cell
    y <- add pixels-per-cell
    loop
  }
}

fn render-tile-without-margin screen: (addr screen), _tile: (addr tile), start-x: int, start-y: int, pixels-per-cell: int {
  var tile/esi: (addr tile) <- copy _tile
  var y/ecx: int <- copy start-y
  var rows-ah/eax: (addr handle array handle array int) <- get tile, data
  var rows/eax: (addr array handle array int) <- lookup *rows-ah
  var i/ebx: int <- copy 0
  {
    compare i, 3
    break-if->=
    var x/edx: int <- copy start-x
    var curr-row-ah/eax: (addr handle array int) <- index rows, i
    var curr-row/eax: (addr array int) <- lookup *curr-row-ah
    var j/edi: int <- copy 0
    {
      compare j, 3
      break-if->=
      var curr/eax: (addr int) <- index curr-row, j
      var color/eax: int <- copy *curr
      color <- shift-left 1
      draw-rect2 screen, x y, pixels-per-cell pixels-per-cell, color
      j <- increment
      x <- add pixels-per-cell
      loop
    }
    i <- increment
    y <- add pixels-per-cell
    loop
  }
}