Files
  • main.cpp
  • sixel.h
  • xterm.h
main.cpp
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
#include <iostream>
#include <math.h>
#include "sixel.h"
#include "xterm.h"

class Point {
  public:
  Point(double x, double y) {
    this->x = x;
    this->y = y;
  }
  double x, y;
};

class Renderer {
  public:
    Renderer() {
      init_xterm();
      sixel.init(102, 102);
    }

    void setAngle(double angle) {
      this->angle = angle;
    }

    void setPosition(double x, double y) {
      this->x = x;
      this->y = y;
    }

    void render(int map[][10]) {
      for(double col = 0; col < 102; col++) {
        double a = angle + col / (double) 102 * fov - fov / 2;
        double dir = tan(a);
        Point p(x, y);
        int dis = 1;
        while(true) {
          p.x += dir * 0.01;
          p.y += 0.01;
          bool brk = false;
          for(int i = 0; i < 10; i++) {
            for(int j = 0; j < 10; j++) {
              if(map[i][j] == 1) 
                if(p.x >= j && p.x <= j + 1 && p.y >= i && p.y <= i + 1) {
                  brk = true;
                  break;
                }
            }
            if(brk) break;
          }
          if(brk) {
            int length = 102 / (dis * 0.01);
            if(length % 2 == 1) length--;
            for(int i = 51 - length / 2; i < 51 + length / 2; i++) {
              sixel.set_pixel(col, i, 255, 0, 0);
            }
            break;
          }
          dis++;
          
        }
      }
      sixel.render();
	    sixel.compress();
      sixel.draw("\e[H\e[2J");
    }

    Sixel sixel;
    double fov = 2;
    double angle = 0;
    double x = 5;
    double y = 3;
};

int main() {
  Renderer renderer;
  init_xterm();

  int map[][10] = {
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 1, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 0, 1, 0, 0, 1, 1},
    {1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  };


  renderer.render(map);
  while (true) {
		std::string key = read_key();
		if (key == "w") {
			renderer.y += 0.1;
			renderer.render(map);
		} else if (key == "s") {
			renderer.y -= 0.1;
			renderer.render(map);
		}
  }
}