summaryrefslogtreecommitdiff
path: root/src/video/ncurses.c
blob: 2e3e519d0bea5dd82a0be69dbbbc9a3e0cd8e1e6 (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
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
// ncurses.c
// Implements interface.h
// Provides an in-terminal interface to the emulator.

#include<ncurses.h>
#include"interface.h"
#include"signetics.c"
#include"../apple.h"


int TermX = 0;
int TermY = 0;

WINDOW *AppleWindow;


char UserInput()
{
	return getch();
}



void TerminalInit()
{
    initscr();
    cbreak();
    noecho();
    curs_set(0);
    keypad(stdscr, TRUE);

    AppleWindow = newwin(24, 40, 1, 1);

    // Draw the border.
    attron(A_REVERSE);
    move(0, 0);
    printw("                                          ");
    for (int i = 1; i <= 24; i++)
    {
        mvaddch(i,  0, ' ');
        mvaddch(i, 41, ' ');
    }
    mvprintw(25, 0, " ~                                        ");
    mvprintw(26, 0, "             Alekseis Apple I             ");
    mvprintw(27, 0, "                                          ");
    attroff(A_REVERSE);


    mvwaddch(AppleWindow, TermY, TermX, '@' | A_BLINK);

    TerminalShiftRegister = (byte*)malloc(960);
    TerminalShiftRegisterPosition = TerminalShiftRegister;
    TerminalShiftRegisterOffset = 0;

    refresh();
}



void TerminalClose()
{
    free(TerminalShiftRegister);
    curs_set(1);
    endwin();
}



// Takes an an Apple I ASCII character.
void TerminalInput(char n)
{
    mvwaddch(AppleWindow, TermY, TermX, ' ');
    mvwaddch(AppleWindow, TermY, TermX, n);

    *TerminalShiftRegisterPosition = n;
    TerminalShiftRegisterPosition++;
    if (TerminalShiftRegisterPosition >= (TerminalShiftRegister + 960))
        TerminalShiftRegisterPosition = TerminalShiftRegister;
    

    TermX++;

    //if (n == KEY_ENTER) {
    //    TermY++;
    //}

    if (TermX >= 40) {
        TermX = 0;
        TermY++;
    }

    if (TermY >= 24) {
        TerminalShiftRegisterOffset += 40; // Discard first line
        if (TerminalShiftRegisterOffset >= 960)
            TerminalShiftRegisterOffset = TerminalShiftRegister;

        byte *offset = TerminalShiftRegister + TerminalShiftRegisterOffset;

        // For every position, fill with contents of the terminal shift register
        for (int i = 0; i < 23; i++) {
        for (int j = 0; j < 40; j++) {

            if (offset >= (TerminalShiftRegister + 960))
                offset -= 960;
            
            mvwaddch(AppleWindow, i, j, ToAscii(*(offset)));
            offset++;
        }}

        TermY = 23;
        TermX = 0;

        // Clear bottom line.
        mvwprintw(AppleWindow, TermY, TermX, "                                        ");
    }

    mvwaddch(AppleWindow, TermY, TermX, '@' | A_BLINK);
    wrefresh(AppleWindow);
}