/[anoncvs]/projects/roguelike/curses/main-curses.cpp


UCC Code Repository

Contents of /projects/roguelike/curses/main-curses.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download)
Sat Apr 15 16:42:13 2006 UTC (15 years, 2 months ago) by banana
Branch: MAIN
CVS Tags: HEAD
Ghetto

1 #include "../core.h"
2 #include "../port.h"
3 #include <string>
4 #include <map>
5 #include <curses.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 using namespace std;
9
10 // Globals
11 int quit;
12
13 map<unsigned int, int> colour_table;
14 int next_colour = 1;
15
16 // Port hooks
17 void port_quit() {
18 quit = 1;
19 }
20
21 void port_beep() {
22 beep();
23 }
24
25 wchar_t *port_open_data(const char *path) {
26 //Variables lol
27 unsigned short *contents_raw;
28 wchar_t *contents_formatted;
29 int i, flen = 0;
30 bool reverse_endian;
31
32 //UNIX-specific code
33 std::string fpath("data/");
34 fpath += path;
35 const char *filepath = fpath.c_str();
36
37 //Open file and get length
38 FILE *file = fopen(filepath, "r");
39 struct stat sb;
40 stat(filepath, &sb);
41 flen = sb.st_size;
42
43 //Allocate memory
44 contents_raw = (unsigned short*)malloc(flen);
45 contents_formatted = (wchar_t*)malloc(flen/2 * sizeof(wchar_t)); //we replace the BOM with a \0
46
47 //Read data in
48 rewind(file);
49 fread(contents_raw, 1, flen, file);
50
51 fclose(file);
52
53 //Turn the UTF-16 file into a wide-char string
54 if (contents_raw[0] == 0xfeff) {
55 reverse_endian = false;
56 } else if (contents_raw[0] == 0xfffe) {
57 reverse_endian = true;
58 } else {
59 return NULL; //This is not a valid file
60 }
61 for (i = 1; i < flen/2; i++) {
62 if (reverse_endian) { //Endian swap
63 char left, right;
64 unsigned short combine;
65
66 combine = contents_raw[i];
67 right = combine;
68 left = combine >> 8;
69 combine = right;
70 combine <<= 8;
71 combine += left;
72
73 contents_formatted[i-1] = combine;
74 } else {
75 contents_formatted[i-1] = contents_raw[i];
76 }
77 }
78 contents_formatted[i-1] = L'\0';
79
80 return(contents_formatted);
81 }
82
83 wchar_t *port_open_user(const char *path) {
84 //Variables lol
85 unsigned short *contents_raw;
86 wchar_t *contents_formatted;
87 int i, flen = 0;
88 bool reverse_endian;
89
90 //UNIX-specific code
91 std::string fpath("user/");
92 fpath += path;
93 const char *filepath = fpath.c_str();
94
95 //Open file and get length
96 FILE *file = fopen(filepath, "r");
97 struct stat sb;
98 stat(filepath, &sb);
99 flen = sb.st_size;
100
101 //Allocate memory
102 contents_raw = (unsigned short*)malloc(flen);
103 contents_formatted = (wchar_t*)malloc(flen/2 * sizeof(wchar_t)); //we replace the BOM with a \0
104
105 //Read data in
106 rewind(file);
107 fread(contents_raw, 1, flen, file);
108
109 fclose(file);
110
111 //Turn the UTF-16 file into a wide-char string
112 if (contents_raw[0] == 0xfeff) {
113 reverse_endian = false;
114 } else if (contents_raw[0] == 0xfffe) {
115 reverse_endian = true;
116 } else {
117 return NULL; //This is not a valid file
118 }
119 for (i = 1; i < flen/2; i++) {
120 if (reverse_endian) { //Endian swap
121 char left, right;
122 unsigned short combine;
123
124 combine = contents_raw[i];
125 right = combine;
126 left = combine >> 8;
127 combine = right;
128 combine <<= 8;
129 combine += left;
130
131 contents_formatted[i-1] = combine;
132 } else {
133 contents_formatted[i-1] = contents_raw[i];
134 }
135 }
136 contents_formatted[i-1] = L'\0';
137
138 return(contents_formatted);
139 }
140
141 void port_save_user(const char *path, wchar_t *data) {
142 int i;
143 unsigned short character;
144
145 //UNIX-specific code
146 std::string fpath("user/");
147 fpath += path;
148 const char *filepath = fpath.c_str();
149
150 //Open file and get length
151 FILE *file = fopen(filepath, "w");
152
153 //Write BOM
154 character = 0xfeff;
155 fwrite(&character, sizeof(unsigned short), 1, file);
156
157 //Write data out
158 for (i = 0; i < wcslen(data); i++) { //-1 because we don't want to write the \0
159 character = data[i];
160 fwrite(&character, sizeof(unsigned short), 1, file);
161 }
162
163 //Cleanup
164 fclose(file);
165 }
166
167 //Initialisation
168 void init_curses() {
169 initscr();
170 start_color();
171 keypad(stdscr, TRUE); /* enable keyboard mapping */
172 nonl(); /* tell curses not to do NL->CR/NL on output */
173 cbreak(); /* take input chars one at a time, no wait for \n */
174 noecho(); /* don't echo input */
175 }
176
177 void port_display(raw_screen scr) {
178 int y, x;
179
180 for (y = 0; y < 24; y++)
181 for (x = 0; x < 80; x++) {
182 attributed_char c = *(scr + 80*y + x);
183
184 if(can_change_color() || 1) {
185 //Look up the colour
186 int attr = colour_table[c.colour];
187 if (attr == 0) { //Generate an entry
188 attr = colour_table[c.colour] = next_colour;
189
190 unsigned char r, g, b;
191 r = ((c.colour & 0xff0000) >> 16);
192 g = ((c.colour & 0x00ff00) >> 8);
193 b = ((c.colour & 0x0000ff));
194 init_color(next_colour, r*3.921569, g*3.921569, b*3.921569);
195 init_pair(next_colour, next_colour, COLOR_BLACK);
196 next_colour++;
197 }
198
199 mvaddch(y, x, wctob(c.character) | COLOR_PAIR(colour_table[c.colour]));
200 } else {
201 mvaddch(y, x, wctob(c.character));
202 }
203 }
204 refresh();
205 }
206
207 //Main
208 int main() {
209 init_curses();
210
211 core_init(80, 24);
212
213 //Event-processing loop
214 for (quit = 0; !quit; core_event(btowc(getch())));
215
216 endwin();
217 }

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26