-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTEXTREAD.CPP
167 lines (140 loc) · 3.99 KB
/
TEXTREAD.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
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
#include <dos.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "..\mylibs\stdtext.h"
static char text[390][82];
FILE *handle;
int num_lines;
const int SINGLE = 1;
const int PAGE = 22;
void open_file(char *filename);
void view_text(int starty, int endy);
void colour_line(int y, char *startup_text);
void main()
{
char *top_bar = " [MegaBreak Manual] - KnutReader v 1.0 Copyright ¸ 1998 Knut A. Ruud \0";
char *bottom_bar = " Keys: [] [] [Page up] [Page down] [Home] [End] Press [Esc] to quit \0";
int starty, endy;
signed int tinc = 0;
char key;
open_file("manual.txt");
// init
starty = 1;
endy = 23;
system("mode co80");
set_cursor(0,25);
view_text(0, 23);
cls();
colour_line(0, top_bar);
colour_line(24, bottom_bar);
view_text(starty, endy);
// Mainloop
while (key != 27) {
if (kbhit()) {
key = getch();
switch (key) {
case 72: // up
if (starty > 0) {
starty -= 1;
endy -= 1;
}
break;
case 80: // down
if (endy < num_lines) {
starty += 1;
endy += 1;
}
break;
case 73: // page up
if ((starty - PAGE) > 0) {
starty -= PAGE;
endy -= PAGE;
}
else {
starty = 0;
endy = 23;
}
break;
case 81: // page down
if ((endy + PAGE) < num_lines) {
starty += PAGE;
endy += PAGE;
}
else {
endy = num_lines;
starty = endy - 23;
}
break;
case 71: // home
starty = 0;
endy = 23;
break;
case 79: // end
endy = num_lines;
starty = endy - 23;
} // switch
view_text(starty, endy);
} // if kbhit
}
system("cls");
}
void open_file(char *filename)
{
handle = fopen(filename, "r");
if (!handle) {
cout << "Error opening file" << endl;
exit(0);
}
int x = 0, y = 0, still_read;
char letter;
for (y = 0; y < 500; y++) {
x=0;
still_read = 1;
while (still_read) {
letter = getc(handle);
if (letter == 10) {
still_read = 0;
text[y][x] = 0;
}
else if (letter == 255) {
num_lines = --y;
y = 10000;
still_read = 0;
}
else {
text[y][x] = letter;
++x;
}
} // while
} // for
fclose(handle);
} // function
void view_text(int starty, int endy)
{
cls();
int yy = 1;
for (int y = starty; y < endy; y++) {
print_line(yy, text[y], 1, 15);
yy++;
}
}
void colour_line(int y, char *startup_text)
{
int i = 0, x = 0;
byte bg_color, fg_color;
x = 0;
i = 0;
while ( *(startup_text + i) != '\0') {
if ( *(startup_text + i) < 20 || *(startup_text + i) == 143) {
fg_color = *(startup_text + i);
i++;
bg_color = *(startup_text + i);
i++;
}
print_character(x, y, *(startup_text + i), bg_color, fg_color);
x++;
i++;
}
}