Module: Doom::Render::SpinelNative::Kernel

Extended by:
Spinel::Native
Defined in:
lib/doom/render/spinel_native/kernel.rb

Overview

The classic renderer's hot path, ported to spinel_native. Each method mirrors a method of lib/doom/render/renderer.rb; the object graph is replaced by flat Integer/Float arrays kept as module state INSIDE the compiled kernel (native_state), exactly like renderer.rb keeps @map / map/textures once; render then draws a frame per call and returns the framebuffer -- no per-frame re-copy of the map across the boundary.

Visplanes are filled inline (sn_flat_span / sn_sky_span) instead of being deferred through visplane objects -- the per-pixel sampling is identical, so the output matches the reference renderer.

The whole module is one kernel (native_state), so every native method carries a signature. With SPINEL_NATIVE=off the same bodies run as plain Ruby against the module's own ivars, which is how the port is verified against renderer.rb before it is compiled.

Constant Summary collapse

PROJECTION =
160.0
SKY_XSCALE =
512.0 / Math::PI
SKY_YSCALE =
200.0 / 240.0

Instance Method Summary collapse

Instance Method Details

#get_cclipObject



67
68
69
70
71
72
73
74
75
# File 'lib/doom/render/spinel_native/kernel.rb', line 67

def get_cclip
  out = Array.new(320, 0)
  i = 0
  while i < 320
    out[i] = @cclip[i]
    i += 1
  end
  out
end

#get_fclipObject



78
79
80
81
82
83
84
85
86
# File 'lib/doom/render/spinel_native/kernel.rb', line 78

def get_fclip
  out = Array.new(320, 0)
  i = 0
  while i < 320
    out[i] = @fclip[i]
    i += 1
  end
  out
end

#get_wdepthObject



89
90
91
92
93
94
95
96
97
# File 'lib/doom/render/spinel_native/kernel.rb', line 89

def get_wdepth
  out = Array.new(320, 0.0)
  i = 0
  while i < 320
    out[i] = @wdepth[i]
    i += 1
  end
  out
end

#load_map(nodes, verts, segs, subs, lines, sides, secs, wpx, wtab, fpx, cmap, ncount, skytex) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/doom/render/spinel_native/kernel.rb', line 104

def load_map(nodes, verts, segs, subs, lines, sides, secs, wpx, wtab, fpx, cmap, ncount, skytex)
  @nodes = nodes; @verts = verts; @segs = segs; @subs = subs
  @lines = lines; @sides = sides; @secs = secs
  @wpx = wpx; @wtab = wtab; @fpx = fpx; @cmap = cmap
  @ncount = ncount; @skytex = skytex
  @nodes.length
end

#render(px, py, pz, angle) ⇒ Object



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
# File 'lib/doom/render/spinel_native/kernel.rb', line 114

def render(px, py, pz, angle)
  @px = px.to_f; @py = py.to_f; @pz = pz.to_f
  @ang = angle
  @sina = Math.sin(angle)
  @cosa = Math.cos(angle)
  i = 0
  while i < 76800
    @fb[i] = 0
    i += 1
  end
  i = 0
  while i < 320
    @cclip[i] = -1
    @fclip[i] = 240
    @wdepth[i] = 1.0e30
    i += 1
  end
  sn_precompute
  sn_bsp(@ncount - 1)
  # Return a locally-built copy with a concrete Array[Integer] type;
  # spinel will not cross the boundary with an ivar of inferred (poly) type.
  out = Array.new(76800, 0)
  j = 0
  while j < 76800
    out[j] = @fb[j]
    j += 1
  end
  out
end

#sn_bsp(root) ⇒ Object



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
# File 'lib/doom/render/spinel_native/kernel.rb', line 161

def sn_bsp(root)
  stack = Array.new(2048, 0)
  sp = 0
  stack[sp] = root
  sp = sp + 1
  while sp > 0
    sp = sp - 1
    ni = stack[sp]
    if (ni & 0x8000) != 0
      sn_subsector(ni & 0x7FFF)
    else
      b = ni * 6
      dx = @px - @nodes[b]
      dy = @py - @nodes[b + 1]
      left_v = dy * @nodes[b + 2]
      right_v = dx * @nodes[b + 3]
      if right_v >= left_v
        stack[sp] = @nodes[b + 5].to_i
        sp = sp + 1
        stack[sp] = @nodes[b + 4].to_i
        sp = sp + 1
      else
        stack[sp] = @nodes[b + 4].to_i
        sp = sp + 1
        stack[sp] = @nodes[b + 5].to_i
        sp = sp + 1
      end
    end
  end
  0
end

#sn_flat_span(x, y1, y2, plane_h, light, flat_idx) ⇒ Object



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/doom/render/spinel_native/kernel.rb', line 681

def sn_flat_span(x, y1, y2, plane_h, light, flat_idx)
  if y1 > y2
    return 0
  end
  ph = plane_h.to_f - @pz
  if ph < 0.0
    ph = -ph
  end
  if ph == 0.0
    return 0
  end
  lnum = light >> 4
  if lnum > 15
    lnum = 15
  end
  if lnum < 0
    lnum = 0
  end
  startmap = (15 - lnum) * 4
  fbase = flat_idx * 4096
  y = y1
  if y < 0
    y = 0
  end
  yy2 = y2
  if yy2 > 239
    yy2 = 239
  end
  while y <= yy2
    dy = y - 120
    if dy != 0
      absdy = dy
      if absdy < 0
        absdy = -absdy
      end
      perp = ph * PROJECTION / absdy.to_f
      rd = perp * @cds[x]
      tx = (@px + rd * @ccos[x]).to_i & 63
      ty = (0.0 - @py - rd * @csin[x]).to_i & 63
      zc = (perp / 16.0).to_i
      if zc > 127
        zc = 127
      end
      if zc < 0
        zc = 0
      end
      lvl = (startmap.to_f - 10.0 / (zc + 1).to_f).to_i
      if lvl < 0
        lvl = 0
      end
      if lvl > 31
        lvl = 31
      end
      @fb[y * 320 + x] = @cmap[lvl * 256 + @fpx[fbase + ty * 64 + tx]]
    end
    y += 1
  end
  0
end

#sn_one_sided(x, ceil_y, floor_y, scale, dist, tex_col, fsec, sd_idx, flags, has_ceil, has_floor) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/doom/render/spinel_native/kernel.rb', line 382

def sn_one_sided(x, ceil_y, floor_y, scale, dist, tex_col, fsec, sd_idx, flags, has_ceil, has_floor)
  f_floor = @secs[fsec * 7]
  f_ceil = @secs[fsec * 7 + 1]
  f_light = @secs[fsec * 7 + 2]
  f_ff = @secs[fsec * 7 + 3]
  f_cf = @secs[fsec * 7 + 4]
  f_csky = @secs[fsec * 7 + 5]
  f_fsky = @secs[fsec * 7 + 6]

  if has_ceil != 0
    mt = @cclip[x] + 1
    mb = ceil_y - 1
    if @fclip[x] - 1 < mb
      mb = @fclip[x] - 1
    end
    if mt <= mb
      if f_csky != 0
        sn_sky_span(x, mt, mb)
      else
        sn_flat_span(x, mt, mb, f_ceil, f_light, f_cf)
      end
    end
  end

  if has_floor != 0
    mt = floor_y + 1
    if @cclip[x] + 1 > mt
      mt = @cclip[x] + 1
    end
    mb = @fclip[x] - 1
    if mt <= mb
      if f_fsky != 0
        sn_sky_span(x, mt, mb)
      else
        sn_flat_span(x, mt, mb, f_floor, f_light, f_ff)
      end
    end
  end

  mid = @sides[sd_idx * 6 + 3]
  yoff = @sides[sd_idx * 6 + 5]
  if (flags & 16) != 0
    th = 128
    if mid >= 0
      th = @wtab[mid * 3 + 2]
    end
    mid_tex_y = yoff + th - (f_ceil - f_floor)
  else
    mid_tex_y = yoff
  end
  sn_wall_column(x, ceil_y, floor_y, mid, dist, f_light, tex_col, mid_tex_y, scale, f_ceil.to_f)

  if dist < @wdepth[x]
    @wdepth[x] = dist
  end
  @cclip[x] = 240
  @fclip[x] = -1
  0
end

#sn_precomputeObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/doom/render/spinel_native/kernel.rb', line 146

def sn_precompute
  x = 0
  while x < 320
    ca = @ang - Math.atan2((x - 160).to_f, PROJECTION)
    @ccos[x] = Math.cos(ca)
    @csin[x] = Math.sin(ca)
    dxx = (x - 160).to_f
    @cds[x] = Math.sqrt(dxx * dxx + PROJECTION * PROJECTION) / PROJECTION
    x += 1
  end
  0
end

#sn_seg(seg_i) ⇒ Object



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
# File 'lib/doom/render/spinel_native/kernel.rb', line 208

def sn_seg(seg_i)
  v1i = @segs[seg_i * 5]
  v2i = @segs[seg_i * 5 + 1]
  dx1 = @verts[v1i * 2].to_f - @px
  dy1 = @verts[v1i * 2 + 1].to_f - @py
  x1 = dx1 * @sina - dy1 * @cosa
  y1 = dx1 * @cosa + dy1 * @sina
  dx2 = @verts[v2i * 2].to_f - @px
  dy2 = @verts[v2i * 2 + 1].to_f - @py
  x2 = dx2 * @sina - dy2 * @cosa
  y2 = dx2 * @cosa + dy2 * @sina

  if y1 <= 0.0 && y2 <= 0.0
    return 0
  end
  near = 1.0
  if y1 < near || y2 < near
    if y1 < near && y2 < near
      return 0
    elsif y1 < near
      t = (near - y1) / (y2 - y1)
      x1 = x1 + t * (x2 - x1)
      y1 = near
    elsif y2 < near
      t = (near - y1) / (y2 - y1)
      x2 = x1 + t * (x2 - x1)
      y2 = near
    end
  end

  sx1 = 160.0 + x1 * PROJECTION / y1
  sx2 = 160.0 + x2 * PROJECTION / y2
  if sx1 >= sx2
    return 0
  end
  if sx2 < 0.0 || sx1 >= 320.0
    return 0
  end
  x1i = sx1.to_i
  if x1i < 0
    x1i = 0
  end
  x2i = sx2.to_i
  if x2i > 319
    x2i = 319
  end
  if x1i > x2i
    return 0
  end

  ldi = @segs[seg_i * 5 + 2]
  dir = @segs[seg_i * 5 + 3]
  sd_idx = dir == 0 ? @lines[ldi * 3] : @lines[ldi * 3 + 1]
  if sd_idx < 0
    return 0
  end
  fsec = @sides[sd_idx * 6]
  flags = @lines[ldi * 3 + 2]
  back_sd = -1
  if (flags & 4) != 0
    bsi = dir == 0 ? @lines[ldi * 3 + 1] : @lines[ldi * 3]
    if bsi >= 0
      back_sd = bsi
    end
  end

  segdx = (@verts[v2i * 2] - @verts[v1i * 2]).to_f
  segdy = (@verts[v2i * 2 + 1] - @verts[v1i * 2 + 1]).to_f
  seg_len = Math.sqrt(segdx * segdx + segdy * segdy)

  sn_seg_range(x1i, x2i, sx1, sx2, y1, y2, fsec, back_sd, sd_idx, flags, seg_i, seg_len)
  0
end

#sn_seg_range(x1, x2, sx1, sx2, dist1, dist2, fsec, back_sd, sd_idx, flags, seg_i, seg_len) ⇒ Object



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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/doom/render/spinel_native/kernel.rb', line 284

def sn_seg_range(x1, x2, sx1, sx2, dist1, dist2, fsec, back_sd, sd_idx, flags, seg_i, seg_len)
  v1i = @segs[seg_i * 5]
  v2i = @segs[seg_i * 5 + 1]
  segdx = (@verts[v2i * 2] - @verts[v1i * 2]).to_f
  segdy = (@verts[v2i * 2 + 1] - @verts[v1i * 2 + 1]).to_f
  pxr = @px - @verts[v1i * 2].to_f
  pyr = @py - @verts[v1i * 2 + 1].to_f
  sin_a = @sina
  cos_a = @cosa
  c1 = cos_a * PROJECTION - sin_a * 160.0
  c2 = sin_a * PROJECTION + cos_a * 160.0
  tex_a = segdy * sin_a + segdx * cos_a
  tex_b = segdy * c1 - segdx * c2
  tex_e = pyr * sin_a + pxr * cos_a
  tex_f = pyr * c1 - pxr * c2
  tex_offset = @segs[seg_i * 5 + 4] + @sides[sd_idx * 6 + 4]

  f_floor = @secs[fsec * 7]
  f_ceil = @secs[fsec * 7 + 1]
  f_csky = @secs[fsec * 7 + 5]
  has_ceil = 0
  if f_ceil.to_f > @pz
    has_ceil = 1
  end
  if f_csky != 0
    has_ceil = 1
  end
  has_floor = 0
  if f_floor.to_f < @pz
    has_floor = 1
  end

  x = x1
  while x <= x2
    if @cclip[x] < @fclip[x] - 1
      t = 0.0
      if sx2 != sx1
        t = (x.to_f - sx1) / (sx2 - sx1)
      end
      if t < 0.0
        t = 0.0
      end
      if t > 1.0
        t = 1.0
      end
      dist = dist1
      if dist1 > 0.0 && dist2 > 0.0
        inv = (1.0 - t) / dist1 + t / dist2
        dist = 1.0 / inv
      else
        if dist1 > 0.0
          dist = dist1
        else
          dist = dist2
        end
      end
      denom = tex_a * x.to_f + tex_b
      ss = 0.0
      da = denom
      if da < 0.0
        da = -da
      end
      if da >= 0.001
        ss = (tex_e * x.to_f + tex_f) / denom
      end
      tex_col = (tex_offset.to_f + ss * seg_len).to_i
      if dist >= 1.0
        scale = PROJECTION / dist
        front_floor = f_floor.to_f - @pz
        front_ceil = f_ceil.to_f - @pz
        fcf = 120.0 - front_ceil * scale
        front_ceil_y = fcf.to_i
        if fcf > front_ceil_y.to_f
          front_ceil_y = front_ceil_y + 1
        end
        front_floor_y = (120.0 - front_floor * scale).to_i
        ceil_y = front_ceil_y
        if @cclip[x] + 1 > ceil_y
          ceil_y = @cclip[x] + 1
        end
        floor_y = front_floor_y
        if @fclip[x] - 1 < floor_y
          floor_y = @fclip[x] - 1
        end
        if back_sd >= 0
          sn_two_sided(x, ceil_y, floor_y, scale, dist, tex_col, fsec, back_sd, sd_idx, flags, has_ceil, has_floor)
        else
          sn_one_sided(x, ceil_y, floor_y, scale, dist, tex_col, fsec, sd_idx, flags, has_ceil, has_floor)
        end
      end
    end
    x += 1
  end
  0
end

#sn_sky_span(x, y1, y2) ⇒ Object



743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'lib/doom/render/spinel_native/kernel.rb', line 743

def sn_sky_span(x, y1, y2)
  if y1 > y2
    return 0
  end
  off = @wtab[@skytex * 3]
  sw = @wtab[@skytex * 3 + 1]
  sh = @wtab[@skytex * 3 + 2]
  ca = @ang - Math.atan2((x - 160).to_f, PROJECTION)
  sx = (ca * SKY_XSCALE).to_i % sw
  if sx < 0
    sx = sx + sw
  end
  y = y1
  if y < 0
    y = 0
  end
  yy2 = y2
  if yy2 > 239
    yy2 = 239
  end
  while y <= yy2
    ty = (100.0 + (y - 120).to_f * SKY_YSCALE).to_i % sh
    if ty < 0
      ty = ty + sh
    end
    c = @wpx[off + sx * sh + ty]
    if c >= 0
      @fb[y * 320 + x] = c
    end
    y += 1
  end
  0
end

#sn_subsector(index) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/doom/render/spinel_native/kernel.rb', line 195

def sn_subsector(index)
  first = @subs[index * 2]
  count = @subs[index * 2 + 1]
  i = 0
  while i < count
    sn_seg(first + i)
    i += 1
  end
  0
end

#sn_two_sided(x, ceil_y, floor_y, scale, dist, tex_col, fsec, back_sd, sd_idx, flags, has_ceil, has_floor) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/doom/render/spinel_native/kernel.rb', line 444

def sn_two_sided(x, ceil_y, floor_y, scale, dist, tex_col, fsec, back_sd, sd_idx, flags, has_ceil, has_floor)
  f_floor = @secs[fsec * 7]
  f_ceil = @secs[fsec * 7 + 1]
  f_light = @secs[fsec * 7 + 2]
  f_ff = @secs[fsec * 7 + 3]
  f_cf = @secs[fsec * 7 + 4]
  f_csky = @secs[fsec * 7 + 5]
  f_fsky = @secs[fsec * 7 + 6]
  bsec = @sides[back_sd * 6]
  b_floor = @secs[bsec * 7]
  b_ceil = @secs[bsec * 7 + 1]
  b_light = @secs[bsec * 7 + 2]
  b_ff = @secs[bsec * 7 + 3]
  b_cf = @secs[bsec * 7 + 4]
  b_csky = @secs[bsec * 7 + 5]

  back_floor = b_floor.to_f - @pz
  back_ceil = b_ceil.to_f - @pz
  back_ceil_y = (120.0 - back_ceil * scale).to_i
  bfy = 120.0 - back_floor * scale
  back_floor_y = bfy.to_i
  if bfy > back_floor_y.to_f
    back_floor_y = back_floor_y + 1
  end

  closed_door = 0
  if b_ceil <= f_floor
    closed_door = 1
  end
  if b_floor >= f_ceil
    closed_door = 1
  end
  both_sky = 0
  if f_csky != 0 && b_csky != 0
    both_sky = 1
  end
  eff_front_ceil = f_ceil
  if both_sky != 0
    eff_front_ceil = b_ceil
  end

  should_mark_ceiling = 0
  should_mark_floor = 0
  if closed_door != 0
    should_mark_ceiling = 1
    should_mark_floor = 1
  else
    if eff_front_ceil != b_ceil
      should_mark_ceiling = 1
    end
    if f_cf != b_cf
      should_mark_ceiling = 1
    end
    if f_light != b_light
      should_mark_ceiling = 1
    end
    if f_floor != b_floor
      should_mark_floor = 1
    end
    if f_ff != b_ff
      should_mark_floor = 1
    end
    if f_light != b_light
      should_mark_floor = 1
    end
  end

  if has_ceil != 0 && should_mark_ceiling != 0
    mt = @cclip[x] + 1
    mb = ceil_y - 1
    if @fclip[x] - 1 < mb
      mb = @fclip[x] - 1
    end
    if mt <= mb
      if f_csky != 0
        sn_sky_span(x, mt, mb)
      else
        sn_flat_span(x, mt, mb, f_ceil, f_light, f_cf)
      end
    end
  end

  if has_floor != 0 && should_mark_floor != 0
    mt = floor_y + 1
    mb = @fclip[x] - 1
    if @cclip[x] + 1 > mt
      mt = @cclip[x] + 1
    end
    if mt <= mb
      if f_fsky != 0
        sn_sky_span(x, mt, mb)
      else
        sn_flat_span(x, mt, mb, f_floor, f_light, f_ff)
      end
    end
  end

  if both_sky == 0 && f_ceil > b_ceil
    upper = @sides[sd_idx * 6 + 1]
    yoff = @sides[sd_idx * 6 + 5]
    if (flags & 8) != 0
      upper_tex_y = yoff
    else
      th = 128
      if upper >= 0
        th = @wtab[upper * 3 + 2]
      end
      upper_tex_y = yoff + b_ceil - f_ceil + th
    end
    sn_wall_column(x, ceil_y, back_ceil_y, upper, dist, f_light, tex_col, upper_tex_y, scale, f_ceil.to_f)
  end

  if f_floor < b_floor
    lower = @sides[sd_idx * 6 + 2]
    yoff = @sides[sd_idx * 6 + 5]
    if (flags & 16) != 0
      lower_tex_y = yoff + f_ceil - b_floor
    else
      lower_tex_y = yoff
    end
    sn_wall_column(x, back_floor_y, floor_y, lower, dist, f_light, tex_col, lower_tex_y, scale, b_floor.to_f)
  end

  if closed_door != 0
    if dist < @wdepth[x]
      @wdepth[x] = dist
    end
    @cclip[x] = 240
    @fclip[x] = -1
  else
    if eff_front_ceil > b_ceil
      if back_ceil_y > @cclip[x]
        @cclip[x] = back_ceil_y
      end
    elsif eff_front_ceil < b_ceil
      if ceil_y - 1 > @cclip[x]
        @cclip[x] = ceil_y - 1
      end
    else
      if f_cf != b_cf || f_light != b_light
        if ceil_y - 1 > @cclip[x]
          @cclip[x] = ceil_y - 1
        end
      end
    end
    if f_floor < b_floor
      if back_floor_y < @fclip[x]
        @fclip[x] = back_floor_y
      end
    elsif f_floor > b_floor
      if floor_y + 1 < @fclip[x]
        @fclip[x] = floor_y + 1
      end
    else
      if f_ff != b_ff || f_light != b_light
        if floor_y + 1 < @fclip[x]
          @fclip[x] = floor_y + 1
        end
      end
    end
  end
  0
end

#sn_wall_column(x, y1, y2, tex, dist, light, tex_col, tex_y_start, scale, world_top) ⇒ Object



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'lib/doom/render/spinel_native/kernel.rb', line 610

def sn_wall_column(x, y1, y2, tex, dist, light, tex_col, tex_y_start, scale, world_top)
  if y1 > y2
    return 0
  end
  if tex < 0
    return 0
  end
  clip_top = @cclip[x] + 1
  clip_bot = @fclip[x] - 1
  if y1 < clip_top
    y1 = clip_top
  end
  if y2 > clip_bot
    y2 = clip_bot
  end
  if y1 > y2
    return 0
  end
  base = @wtab[tex * 3]
  tw = @wtab[tex * 3 + 1]
  th = @wtab[tex * 3 + 2]
  wlnum = light >> 4
  if wlnum > 15
    wlnum = 15
  end
  if wlnum < 0
    wlnum = 0
  end
  wstart = (15 - wlnum) * 4
  wsi = 47
  if dist > 0.0
    wsi = (2560.0 / dist).to_i
  end
  if wsi > 47
    wsi = 47
  end
  if wsi < 0
    wsi = 0
  end
  lvl = wstart - wsi / 2
  if lvl < 0
    lvl = 0
  end
  if lvl > 31
    lvl = 31
  end
  tex_x = tex_col % tw
  if tex_x < 0
    tex_x = tex_x + tw
  end
  tex_step = 1.0 / scale
  unclipped_y1 = 120.0 - (world_top - @pz) * scale
  tex_y_at_y1 = tex_y_start.to_f + (y1.to_f - unclipped_y1) * tex_step
  y = y1
  while y <= y2
    off = y - y1
    ty = (tex_y_at_y1 + off.to_f * tex_step).to_i % th
    if ty < 0
      ty = ty + th
    end
    c = @wpx[base + tex_x * th + ty]
    if c >= 0
      @fb[y * 320 + x] = @cmap[lvl * 256 + c]
    end
    y += 1
  end
  0
end