source: trunk/tools/neercs/term/term.cpp @ 1962

Last change on this file since 1962 was 1962, checked in by rez, 8 years ago

NEERCS: fixed setup value bar length / fixed copper setup

File size: 6.7 KB
Line 
1//
2// Neercs
3//
4
5#if defined HAVE_CONFIG_H
6#   include "config.h"
7#endif
8
9#if defined _XBOX
10#   define _USE_MATH_DEFINES /* for M_PI */
11#   include <xtl.h>
12#elif defined _WIN32
13#   define _USE_MATH_DEFINES /* for M_PI */
14#   define WIN32_LEAN_AND_MEAN
15#   include <windows.h>
16#endif
17
18#include "core.h"
19
20using namespace std;
21using namespace lol;
22
23#include "../neercs.h"
24#include "term.h"
25
26Term::Term(ivec2 size)
27  : m_pty(0),
28    m_caca(caca_create_canvas(size.x, size.y)),
29    m_size(size),
30    m_title(0),
31    m_time(0.f)
32{
33#if defined HAVE_PTY_H || defined HAVE_UTIL_H || defined HAVE_LIBUTIL_H
34    m_pty = new Pty();
35    char const *shell = getenv("SHELL");
36    if (!shell)
37        shell = "/bin/sh";
38    m_pty->Run(shell, size);
39#endif
40}
41
42void Term::TickGame(float seconds)
43{
44    Entity::TickGame(seconds);
45
46#if defined HAVE_PTY_H || defined HAVE_UTIL_H || defined HAVE_LIBUTIL_H
47    bool have_ctrl = Input::GetStatus(Key::LeftCtrl)
48                      || Input::GetStatus(Key::RightCtrl);
49    bool have_shift = Input::GetStatus(Key::LeftShift)
50                       || Input::GetStatus(Key::RightShift);
51
52    for (int i = 0x0; i < 0x7f; ++i)
53    {
54        if (Input::WasPressed((Key::Value)i))
55        {
56            if (have_ctrl && i >= 'a' && i <= 'z')
57            {
58                char c = i + 1 - 'a';
59                m_pty->WriteData(&c, 1);
60            }
61            else if (have_shift && i >= 'a' && i <= 'z')
62            {
63                char c = i + 'A' - 'a';
64                m_pty->WriteData(&c, 1);
65            }
66            else
67            {
68                char c = i;
69                m_pty->WriteData(&c, 1);
70            }
71        }
72    }
73
74    /* This is the real terminal code */
75    /* XXX: for now we draw fancy shit */
76    m_time += seconds;
77
78    m_pty->SetWindowSize(ivec2(caca_get_canvas_width(m_caca),
79                               caca_get_canvas_height(m_caca)));
80
81    size_t total = 0;
82    for (;;)
83    {
84        char buf[BUFSIZ];
85        size_t current = m_pty->ReadData(buf, BUFSIZ);
86        if (current <= 0)
87            break;
88        total += current;
89        size_t processed = ReadAnsi(buf, current);
90        if (processed < current)
91            m_pty->UnreadData(buf + processed, current - processed);
92        if (current < BUFSIZ)
93            break;
94//        if (total > 10000)
95//            break;
96    }
97#else
98    /* Unsupported platform - draw some fancy shit instead */
99    m_time += seconds;
100    DrawFancyShit();
101#endif
102}
103
104void Term::TickDraw(float seconds)
105{
106    Entity::TickDraw(seconds);
107}
108
109Term::~Term()
110{
111#if defined HAVE_PTY_H || defined HAVE_UTIL_H || defined HAVE_LIBUTIL_H
112    delete m_pty;
113#endif
114    caca_free_canvas(m_caca);
115}
116
117/*
118 * XXX: fancy shit below
119 */
120
121static uint32_t hex_color(float r, float g, float b)
122{
123    return ((uint32_t)(r * 15.99f) << 8) |
124           ((uint32_t)(g * 15.99f) << 4) |
125           (uint32_t)(b * 15.99f);
126}
127
128void Term::DrawFancyShit()
129{
130    /* draw something awesome */
131    int bg_color = 0x222;
132    int w = caca_get_canvas_width(m_caca);
133    int h = caca_get_canvas_height(m_caca);
134
135    caca_set_color_argb(m_caca, 0xfff, bg_color);
136    caca_clear_canvas(m_caca);
137
138    caca_set_color_argb(m_caca, 0xfff, bg_color);
139    for(int i = 0; i < h; i++)
140    {
141        float a = M_PI / 180 * i * 16 + m_time * 4;
142        float b = M_PI / 180 * i * 12;
143        int x = w / 2  - 14 + w / 4 * lol::cos(a) + w / 4 * lol::sin(b);
144        caca_put_str(m_caca, x, i, "LOL WUT! NEERCS SI TEH RULEZ");
145    }
146
147    caca_set_color_argb(m_caca, 0x444, bg_color);
148    for(int i = 0; i < w; i++)
149    {
150        float a = m_time * 1 + M_PI / 180 * i * 8;
151        float b = m_time * -2 + M_PI / 180 * i * 5;
152        int y = h / 2 + h / 4 * lol::cos(a) + h / 4 * lol::sin(b);
153        caca_set_color_argb(m_caca, hex_color(0.25f + 0.5f / w * i - 0.25f / h * y, 0.25f, 0.25f + 0.25f / w * i + 0.25f / h * y), bg_color);
154        caca_draw_line(m_caca, i, y - 1, i, y + 1,'%');
155    }
156
157/* __  _________ ______ ______ ______ ______
158  /  \/  /  __  >  __  >  __  >  ___//  ___/ \x0a9
159 /      /  ____/  ____/  __  <  <____\___  \
160/__/\__/\_______________/  \________________\ */
161
162    int logo_x = (w - 46) / 2;
163    int logo_y = h / 2 - 2;
164
165    caca_set_color_argb(m_caca, 0xbac, bg_color);
166    //caca_set_color_argb(m_caca, hex_color(0.5f + 0.5f * lol::cos(m_time * 3               ), 0.5f, 0.5f + 0.25f * lol::sin(m_time * 3               )), bg_color);
167    caca_put_str(m_caca, logo_x + 3, logo_y    ,"__  _________ ______ ______ ______ ______");
168    //caca_set_color_argb(m_caca, hex_color(0.5f + 0.5f * lol::cos(m_time * 3 + M_PI / 4 * 1), 0.5f, 0.5f + 0.25f * lol::sin(m_time * 3 + M_PI / 4 * 1)), bg_color);
169    caca_put_str(m_caca, logo_x + 2, logo_y + 1, "/  \\/  /  __  >  __  >  __  >  ___//  ___/ \x0a9");
170    //caca_set_color_argb(m_caca, hex_color(0.5f + 0.5f * lol::cos(m_time * 3 + M_PI / 4 * 2), 0.5f, 0.5f + 0.25f * lol::sin(m_time * 3 + M_PI / 4 * 2)), bg_color);
171    caca_put_str(m_caca, logo_x + 1, logo_y + 2, "/      /  ____/  ____/  __  <  <____\\___  \\");
172    //caca_set_color_argb(m_caca, hex_color(0.5f + 0.5f * lol::cos(m_time * 3 + M_PI / 4 * 3), 0.5f, 0.5f + 0.25f * lol::sin(m_time * 3 + M_PI / 4 * 3)), bg_color);
173    caca_put_str(m_caca, logo_x    , logo_y + 3, "/__/\\__/\\_______________/  \\________________\\");
174    caca_set_color_argb(m_caca, 0x269, bg_color);
175    caca_put_str(m_caca, logo_x + 5, logo_y + 5, "ALL YOUR TERMINALS ARE BELONG TO US");
176
177    caca_set_color_argb(m_caca, 0x777, bg_color);
178    caca_printf(m_caca, 2, h - 3, "W=%i", w);
179    caca_printf(m_caca, 2, h - 2, "H=%i", h);
180
181    caca_set_color_argb(m_caca, hex_color(0.6f + 0.4f * lol::cos(m_time * 2), 0.2f, 0.2f), bg_color);
182    caca_put_str(m_caca, w - 12, h - 2, "CACA RULEZ");
183
184/*_______
185 /      /|
186/______/ |
187|      | |
188|  :D  | /
189|______|/
190*/
191
192    int lolcube_x = w / 2 - 5 + (w - 10) * lol::cos(m_time / 2);
193    int lolcube_y = h - 5 - abs ((h - 5) * lol::sin(m_time * 4));
194
195    caca_set_color_argb(m_caca, hex_color(0.5f + 0.5f * lol::sin(m_time * 2), 0.5f + 0.5f * lol::cos(m_time * 3), 0.5f + 0.5f * lol::sin(m_time * 5)), bg_color);
196    caca_put_str(m_caca, lolcube_x + 2, lolcube_y    , "_______");
197    caca_put_str(m_caca, lolcube_x + 1, lolcube_y + 1, "/      /|");
198    caca_put_str(m_caca, lolcube_x    , lolcube_y + 2, "/______/ |");
199    caca_put_str(m_caca, lolcube_x    , lolcube_y + 3, "|      | |");
200    caca_put_str(m_caca, lolcube_x    , lolcube_y + 4, "|  :D  | /");
201    caca_put_str(m_caca, lolcube_x    , lolcube_y + 5, "|______|/");
202
203    caca_set_color_argb(m_caca, 0x777, bg_color);
204    caca_put_str(m_caca, 0, 0, "rez@lol:~/ sudo -s");
205    caca_put_str(m_caca, 0, 1, "[sudo] password for rez:");
206    caca_put_str(m_caca, 0, 2, "root@lol:~/ echo LOL");
207    caca_put_str(m_caca, 0, 3, "LOL");
208    caca_put_str(m_caca, 0, 4, "root@lol:~/");
209}
Note: See TracBrowser for help on using the repository browser.