-
Notifications
You must be signed in to change notification settings - Fork 30
/
catacurse.cpp
830 lines (767 loc) · 26 KB
/
catacurse.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
#if (defined __MINGW32__ || defined _WIN32 || defined WINDOWS)
#include "catacurse.h"
#include <cstdlib>
#include <fstream>
//***********************************
//Globals *
//***********************************
WINDOW *mainwin;
const WCHAR *szWindowClass = (L"CataCurseWindow"); //Class name :D
HINSTANCE WindowINST; //the instance of the window
HWND WindowHandle; //the handle of the window
HDC WindowDC; //Device Context of the window, used for backbuffer
int WindowX; //X pos of the actual window, not the curses window
int WindowY; //Y pos of the actual window, not the curses window
int WindowWidth; //Width of the actual window, not the curses window
int WindowHeight; //Height of the actual window, not the curses window
int lastchar; //the last character that was pressed, resets in getch
int inputdelay; //How long getch will wait for a character to be typed
//WINDOW *_windows; //Probably need to change this to dynamic at some point
//int WindowCount; //The number of curses windows currently in use
HDC backbuffer; //an off-screen DC to prevent flickering, lower cpu
HBITMAP backbit; //the bitmap that is used in conjunction wth the above
int fontwidth; //the width of the font, background is always this size
int fontheight; //the height of the font, background is always this size
int halfwidth; //half of the font width, used for centering lines
int halfheight; //half of the font height, used for centering lines
HFONT font; //Handle to the font created by CreateFont
RGBQUAD *windowsPalette; //The coor palette, 16 colors emulates a terminal
pairs *colorpairs; //storage for pair'ed colored, should be dynamic, meh
unsigned char *dcbits; //the bits of the screen image, for direct access
char szDirectory[MAX_PATH] = "";
//***********************************
//Non-curses, Window functions *
//***********************************
//Registers, creates, and shows the Window!!
bool WinCreate()
{
int success;
WNDCLASSEXW WindowClassType;
int WinBorderHeight;
int WinBorderWidth;
int WinTitleSize;
unsigned int WindowStyle;
const WCHAR *szTitle= (L"Cataclysm");
WinTitleSize = GetSystemMetrics(SM_CYCAPTION); //These lines ensure
WinBorderWidth = GetSystemMetrics(SM_CXDLGFRAME) * 2; //that our window will
WinBorderHeight = GetSystemMetrics(SM_CYDLGFRAME) * 2; // be a perfect size
WindowClassType.cbSize = sizeof(WNDCLASSEXW);
WindowClassType.style = 0;//No point in having a custom style, no mouse, etc
WindowClassType.lpfnWndProc = ProcessMessages;//the procedure that gets msgs
WindowClassType.cbClsExtra = 0;
WindowClassType.cbWndExtra = 0;
WindowClassType.hInstance = WindowINST;// hInstance
WindowClassType.hIcon = LoadIcon(WindowINST, IDI_APPLICATION);//Default Icon
WindowClassType.hIconSm = LoadIcon(WindowINST, IDI_APPLICATION);//Default Icon
WindowClassType.hCursor = LoadCursor(NULL, IDC_ARROW);//Default Pointer
WindowClassType.lpszMenuName = NULL;
WindowClassType.hbrBackground = 0;//Thanks jday! Remove background brush
WindowClassType.lpszClassName = szWindowClass;
success = RegisterClassExW(&WindowClassType);
if ( success== 0){
return false;
}
WindowStyle = WS_OVERLAPPEDWINDOW & ~(WS_THICKFRAME) & ~(WS_MAXIMIZEBOX);
WindowHandle = CreateWindowExW(WS_EX_APPWINDOW || WS_EX_TOPMOST * 0,
szWindowClass , szTitle,WindowStyle, WindowX,
WindowY, WindowWidth + (WinBorderWidth) * 1,
WindowHeight + (WinBorderHeight +
WinTitleSize) * 1, 0, 0, WindowINST, NULL);
if (WindowHandle == 0){
return false;
}
ShowWindow(WindowHandle,5);
return true;
};
//Unregisters, releases the DC if needed, and destroys the window.
void WinDestroy()
{
if ((WindowDC > 0) && (ReleaseDC(WindowHandle, WindowDC) == 0)){
WindowDC = 0;
}
if ((!WindowHandle == 0) && (!(DestroyWindow(WindowHandle)))){
WindowHandle = 0;
}
if (!(UnregisterClassW(szWindowClass, WindowINST))){
WindowINST = 0;
}
};
//This function processes any Windows messages we get. Keyboard, OnClose, etc
LRESULT CALLBACK ProcessMessages(HWND__ *hWnd,unsigned int Msg,
WPARAM wParam, LPARAM lParam)
{
switch (Msg){
case WM_CHAR: //This handles most key presses
lastchar=(int)wParam;
switch (lastchar){
case 13: //Reroute ENTER key for compatilbity purposes
lastchar=10;
break;
case 8: //Reroute BACKSPACE key for compatilbity purposes
lastchar=127;
break;
};
break;
case WM_KEYDOWN: //Here we handle non-character input
switch (wParam){
case VK_LEFT:
lastchar = KEY_LEFT;
break;
case VK_RIGHT:
lastchar = KEY_RIGHT;
break;
case VK_UP:
lastchar = KEY_UP;
break;
case VK_DOWN:
lastchar = KEY_DOWN;
break;
default:
break;
};
case WM_ERASEBKGND:
return 1; //We don't want to erase our background
case WM_PAINT: //Pull from our backbuffer, onto the screen
BitBlt(WindowDC, 0, 0, WindowWidth, WindowHeight, backbuffer, 0, 0,SRCCOPY);
ValidateRect(WindowHandle,NULL);
break;
case WM_DESTROY:
exit(0);//A messy exit, but easy way to escape game loop
default://If we didnt process a message, return the default value for it
return DefWindowProcW(hWnd, Msg, wParam, lParam);
};
return 0;
};
//The following 3 methods use mem functions for fast drawing
inline void VertLineDIB(int x, int y, int y2,int thickness, unsigned char color)
{
int j;
for (j=y; j<y2; j++)
memset(&dcbits[x+j*WindowWidth],color,thickness);
};
inline void HorzLineDIB(int x, int y, int x2,int thickness, unsigned char color)
{
int j;
for (j=y; j<y+thickness; j++)
memset(&dcbits[x+j*WindowWidth],color,x2-x);
};
inline void FillRectDIB(int x, int y, int width, int height, unsigned char color)
{
int j;
for (j=y; j<y+height; j++)
memset(&dcbits[x+j*WindowWidth],color,width);
};
void DrawWindow(WINDOW *win)
{
int i,j,drawx,drawy;
char tmp;
for (j=0; j<win->height; j++){
if (win->line[j].touched)
for (i=0; i<win->width; i++){
win->line[j].touched=false;
drawx=((win->x+i)*fontwidth);
drawy=((win->y+j)*fontheight);//-j;
if (((drawx+fontwidth)<=WindowWidth) && ((drawy+fontheight)<=WindowHeight)){
tmp = win->line[j].chars[i];
int FG = win->line[j].FG[i];
int BG = win->line[j].BG[i];
FillRectDIB(drawx,drawy,fontwidth,fontheight,BG);
if ( tmp > 0){
//if (tmp==95){//If your font doesnt draw underscores..uncomment
// HorzLineDIB(drawx,drawy+fontheight-2,drawx+fontwidth,1,FG);
// } else { // all the wa to here
int color = RGB(windowsPalette[FG].rgbRed,windowsPalette[FG].rgbGreen,windowsPalette[FG].rgbBlue);
SetTextColor(backbuffer,color);
ExtTextOut(backbuffer,drawx,drawy,0,NULL,&tmp,1,NULL);
// } //and this line too.
} else if ( tmp < 0 ) {
switch (tmp) {
case -60://box bottom/top side (horizontal line)
HorzLineDIB(drawx,drawy+halfheight,drawx+fontwidth,1,FG);
break;
case -77://box left/right side (vertical line)
VertLineDIB(drawx+halfwidth,drawy,drawy+fontheight,2,FG);
break;
case -38://box top left
HorzLineDIB(drawx+halfwidth,drawy+halfheight,drawx+fontwidth,1,FG);
VertLineDIB(drawx+halfwidth,drawy+halfheight,drawy+fontheight,2,FG);
break;
case -65://box top right
HorzLineDIB(drawx,drawy+halfheight,drawx+halfwidth,1,FG);
VertLineDIB(drawx+halfwidth,drawy+halfheight,drawy+fontheight,2,FG);
break;
case -39://box bottom right
HorzLineDIB(drawx,drawy+halfheight,drawx+halfwidth,1,FG);
VertLineDIB(drawx+halfwidth,drawy,drawy+halfheight+1,2,FG);
break;
case -64://box bottom left
HorzLineDIB(drawx+halfwidth,drawy+halfheight,drawx+fontwidth,1,FG);
VertLineDIB(drawx+halfwidth,drawy,drawy+halfheight+1,2,FG);
break;
case -63://box bottom north T (left, right, up)
HorzLineDIB(drawx,drawy+halfheight,drawx+fontwidth,1,FG);
VertLineDIB(drawx+halfwidth,drawy,drawy+halfheight,2,FG);
break;
case -61://box bottom east T (up, right, down)
VertLineDIB(drawx+halfwidth,drawy,drawy+fontheight,2,FG);
HorzLineDIB(drawx+halfwidth,drawy+halfheight,drawx+fontwidth,1,FG);
break;
case -62://box bottom south T (left, right, down)
HorzLineDIB(drawx,drawy+halfheight,drawx+fontwidth,1,FG);
VertLineDIB(drawx+halfwidth,drawy+halfheight,drawy+fontheight,2,FG);
break;
case -59://box X (left down up right)
HorzLineDIB(drawx,drawy+halfheight,drawx+fontwidth,1,FG);
VertLineDIB(drawx+halfwidth,drawy,drawy+fontheight,2,FG);
break;
case -76://box bottom east T (left, down, up)
VertLineDIB(drawx+halfwidth,drawy,drawy+fontheight,2,FG);
HorzLineDIB(drawx,drawy+halfheight,drawx+halfwidth,1,FG);
break;
default:
// SetTextColor(DC,_windows[w].line[j].chars[i].color.FG);
// TextOut(DC,drawx,drawy,&tmp,1);
break;
}
};//switch (tmp)
}//(tmp < 0)
};//for (i=0;i<_windows[w].width;i++)
};// for (j=0;j<_windows[w].height;j++)
win->draw=false; //We drew the window, mark it as so
};
//Check for any window messages (keypress, paint, mousemove, etc)
void CheckMessages()
{
MSG msg;
while (PeekMessage(&msg, 0 , 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
};
//***********************************
//Psuedo-Curses Functions *
//***********************************
//Basic Init, create the font, backbuffer, etc
WINDOW *initscr(void)
{
// _windows = new WINDOW[20]; //initialize all of our variables
BITMAPINFO bmi;
lastchar=-1;
inputdelay=-1;
std::string typeface;
char * typeface_c;
std::ifstream fin;
fin.open("data\\FONTDATA");
if (!fin.is_open()){
MessageBox(WindowHandle, "Failed to open FONTDATA, loading defaults.",
NULL, NULL);
fontheight=16;
fontwidth=8;
} else {
getline(fin, typeface);
typeface_c= new char [typeface.size()+1];
strcpy (typeface_c, typeface.c_str());
fin >> fontwidth;
fin >> fontheight;
if ((fontwidth <= 4) || (fontheight <=4)){
MessageBox(WindowHandle, "Invalid font size specified!",
NULL, NULL);
fontheight=16;
fontwidth=8;
}
}
halfwidth=fontwidth / 2;
halfheight=fontheight / 2;
WindowWidth=80*fontwidth;
WindowHeight=25*fontheight;
WindowX=(GetSystemMetrics(SM_CXSCREEN) / 2)-WindowWidth/2; //center this
WindowY=(GetSystemMetrics(SM_CYSCREEN) / 2)-WindowHeight/2; //sucker
WinCreate(); //Create the actual window, register it, etc
CheckMessages(); //Let the message queue handle setting up the window
WindowDC = GetDC(WindowHandle);
backbuffer = CreateCompatibleDC(WindowDC);
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = WindowWidth;
bmi.bmiHeader.biHeight = -WindowHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount=8;
bmi.bmiHeader.biCompression = BI_RGB; //store it in uncompressed bytes
bmi.bmiHeader.biSizeImage = WindowWidth * WindowHeight * 1;
bmi.bmiHeader.biClrUsed=16; //the number of colors in our palette
bmi.bmiHeader.biClrImportant=16; //the number of colors in our palette
backbit = CreateDIBSection(0, &bmi, DIB_RGB_COLORS, (void**)&dcbits, NULL, 0);
DeleteObject(SelectObject(backbuffer, backbit));//load the buffer into DC
int nResults = AddFontResourceExA("data\\termfont",FR_PRIVATE,NULL);
if (nResults>0){
font = CreateFont(fontheight, fontwidth, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
PROOF_QUALITY, FF_MODERN, typeface_c); //Create our font
} else {
MessageBox(WindowHandle, "Failed to load default font, using FixedSys.",
NULL, NULL);
font = CreateFont(fontheight, fontwidth, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
PROOF_QUALITY, FF_MODERN, "FixedSys"); //Create our font
}
//FixedSys will be user-changable at some point in time??
SetBkMode(backbuffer, TRANSPARENT);//Transparent font backgrounds
SelectObject(backbuffer, font);//Load our font into the DC
// WindowCount=0;
delete typeface_c;
mainwin = newwin(25,80,0,0);
return mainwin; //create the 'stdscr' window and return its ref
};
WINDOW *newwin(int nlines, int ncols, int begin_y, int begin_x)
{
int i,j;
WINDOW *newwindow = new WINDOW;
//newwindow=&_windows[WindowCount];
newwindow->x=begin_x;
newwindow->y=begin_y;
newwindow->width=ncols;
newwindow->height=nlines;
newwindow->inuse=true;
newwindow->draw=false;
newwindow->BG=0;
newwindow->FG=8;
newwindow->cursorx=0;
newwindow->cursory=0;
newwindow->line = new curseline[nlines];
for (j=0; j<nlines; j++)
{
newwindow->line[j].chars= new char[ncols];
newwindow->line[j].FG= new char[ncols];
newwindow->line[j].BG= new char[ncols];
newwindow->line[j].touched=true;//Touch them all !?
for (i=0; i<ncols; i++)
{
newwindow->line[j].chars[i]=0;
newwindow->line[j].FG[i]=0;
newwindow->line[j].BG[i]=0;
}
}
//WindowCount++;
return newwindow;
};
//Deletes the window and marks it as free. Clears it just in case.
int delwin(WINDOW *win)
{
int j;
win->inuse=false;
win->draw=false;
for (j=0; j<win->height; j++){
delete win->line[j].chars;
delete win->line[j].FG;
delete win->line[j].BG;
}
delete win->line;
delete win;
return 1;
};
inline int newline(WINDOW *win){
if (win->cursory < win->height - 1){
win->cursory++;
win->cursorx=0;
return 1;
}
return 0;
};
inline void addedchar(WINDOW *win){
win->cursorx++;
win->line[win->cursory].touched=true;
if (win->cursorx > win->width)
newline(win);
};
//Borders the window with fancy lines!
int wborder(WINDOW *win, chtype ls, chtype rs, chtype ts, chtype bs, chtype tl, chtype tr, chtype bl, chtype br)
{
int i, j;
int oldx=win->cursorx;//methods below move the cursor, save the value!
int oldy=win->cursory;//methods below move the cursor, save the value!
if (ls>0)
for (j=1; j<win->height-1; j++)
mvwaddch(win, j, 0, 179);
if (rs>0)
for (j=1; j<win->height-1; j++)
mvwaddch(win, j, win->width-1, 179);
if (ts>0)
for (i=1; i<win->width-1; i++)
mvwaddch(win, 0, i, 196);
if (bs>0)
for (i=1; i<win->width-1; i++)
mvwaddch(win, win->height-1, i, 196);
if (tl>0)
mvwaddch(win,0, 0, 218);
if (tr>0)
mvwaddch(win,0, win->width-1, 191);
if (bl>0)
mvwaddch(win,win->height-1, 0, 192);
if (br>0)
mvwaddch(win,win->height-1, win->width-1, 217);
//_windows[w].cursorx=oldx;//methods above move the cursor, put it back
//_windows[w].cursory=oldy;//methods above move the cursor, put it back
wmove(win,oldy,oldx);
return 1;
};
//Refreshes a window, causing it to redraw on top.
int wrefresh(WINDOW *win)
{
if (win==0) win=mainwin;
if (win->draw)
DrawWindow(win);
return 1;
};
//Refreshes window 0 (stdscr), causing it to redraw on top.
int refresh(void)
{
return wrefresh(mainwin);
};
//Not terribly sure how this function is suppose to work,
//but jday helped to figure most of it out
int getch(void)
{
refresh();
InvalidateRect(WindowHandle,NULL,true);
lastchar=ERR;//ERR=-1
if (inputdelay < 0)
{
do
{
CheckMessages();
if (lastchar!=ERR) break;
MsgWaitForMultipleObjects(0, NULL, FALSE, 50, QS_ALLEVENTS);//low cpu wait!
}
while (lastchar==ERR);
}
else if (inputdelay > 0)
{
unsigned long starttime=GetTickCount();
unsigned long endtime;
do
{
CheckMessages(); //MsgWaitForMultipleObjects won't work very good here
endtime=GetTickCount(); //it responds to mouse movement, and WM_PAINT, not good
if (lastchar!=ERR) break;
Sleep(2);
}
while (endtime<(starttime+inputdelay));
}
else
{
CheckMessages();
};
Sleep(25);
return lastchar;
};
//The core printing function, prints characters to the array, and sets colors
inline int printstring(WINDOW *win, char *fmt)
{
int size = strlen(fmt);
int j;
for (j=0; j<size; j++){
if (!(fmt[j]==10)){//check that this isnt a newline char
if (win->cursorx <= win->width - 1 && win->cursory <= win->height - 1) {
win->line[win->cursory].chars[win->cursorx]=fmt[j];
win->line[win->cursory].FG[win->cursorx]=win->FG;
win->line[win->cursory].BG[win->cursorx]=win->BG;
win->line[win->cursory].touched=true;
addedchar(win);
} else
return 0; //if we try and write anything outside the window, abort completely
} else // if the character is a newline, make sure to move down a line
if (newline(win)==0){
return 0;
}
}
win->draw=true;
return 1;
}
//Prints a formatted string to a window at the current cursor, base function
int wprintw(WINDOW *win, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
char printbuf[2048];
vsnprintf(printbuf, 2047, fmt, args);
va_end(args);
return printstring(win,printbuf);
};
//Prints a formatted string to a window, moves the cursor
int mvwprintw(WINDOW *win, int y, int x, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
char printbuf[2048];
vsnprintf(printbuf, 2047, fmt, args);
va_end(args);
if (wmove(win,y,x)==0) return 0;
return printstring(win,printbuf);
};
//Prints a formatted string to window 0 (stdscr), moves the cursor
int mvprintw(int y, int x, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
char printbuf[2048];
vsnprintf(printbuf, 2047, fmt, args);
va_end(args);
if (move(y,x)==0) return 0;
return printstring(mainwin,printbuf);
};
//Prints a formatted string to window 0 (stdscr) at the current cursor
int printw(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
char printbuf[2078];
vsnprintf(printbuf, 2047, fmt, args);
va_end(args);
return printstring(mainwin,printbuf);
};
//erases a window of all text and attributes
int werase(WINDOW *win)
{
int j,i;
for (j=0; j<win->height; j++)
{
for (i=0; i<win->width; i++) {
win->line[j].chars[i]=0;
win->line[j].FG[i]=0;
win->line[j].BG[i]=0;
}
win->line[j].touched=true;
}
win->draw=true;
wmove(win,0,0);
wrefresh(win);
return 1;
};
//erases window 0 (stdscr) of all text and attributes
int erase(void)
{
return werase(mainwin);
};
//pairs up a foreground and background color and puts it into the array of pairs
int init_pair(short pair, short f, short b)
{
colorpairs[pair].FG=f;
colorpairs[pair].BG=b;
return 1;
};
//moves the cursor in a window
int wmove(WINDOW *win, int y, int x)
{
if (x>=win->width)
{return 0;}//FIXES MAP CRASH -> >= vs > only
if (y>=win->height)
{return 0;}// > crashes?
if (y<0)
{return 0;}
if (x<0)
{return 0;}
win->cursorx=x;
win->cursory=y;
return 1;
};
// returns the glyph at [y,x]
long mvwinch(WINDOW *win, int y, int x)
{
if (x < 0 || x >= win->width || y < 0 || y >= win->height) {
return 0;
}
long ret;
ret = win->line[y].chars[x] |
win->line[y].FG[x] |
win->line[y].BG[x];
return ret;
}
//Clears windows 0 (stdscr) I'm not sure if its suppose to do this?
int clear(void)
{
return wclear(mainwin);
};
//Ends the terminal, destroy everything
int endwin(void)
{
DeleteObject(font);
WinDestroy();
RemoveFontResourceExA("data\\termfont",FR_PRIVATE,NULL);//Unload it
return 1;
};
// Resizes win. TODO: This is crappy and does not work! FIXME
int wresize(WINDOW *win, int ysize, int xsize)
{
delete win;
win = newwin(ysize, xsize, 0, 0);
}
//adds a character to the window
int mvwaddch(WINDOW *win, int y, int x, const chtype ch)
{
if (wmove(win,y,x)==0) return 0;
return waddch(win, ch);
};
//clears a window
int wclear(WINDOW *win)
{
werase(win);
wrefresh(win);
return 1;
};
//gets the max x of a window (the width)
int getmaxx(WINDOW *win)
{
if (win==0) return mainwin->width; //StdScr
return win->width;
};
//gets the max y of a window (the height)
int getmaxy(WINDOW *win)
{
if (win==0) return mainwin->height; //StdScr
return win->height;
};
inline RGBQUAD BGR(int b, int g, int r)
{
RGBQUAD result;
result.rgbBlue=b; //Blue
result.rgbGreen=g; //Green
result.rgbRed=r; //Red
result.rgbReserved=0;//The Alpha, isnt used, so just set it to 0
return result;
};
int start_color(void)
{
colorpairs=new pairs[50];
windowsPalette=new RGBQUAD[16]; //Colors in the struct are BGR!! not RGB!!
windowsPalette[0]= BGR(0,0,0);
windowsPalette[1]= BGR(0, 0, 196);
windowsPalette[2]= BGR(0,196,0);
windowsPalette[3]= BGR(30,180,196);
windowsPalette[4]= BGR(196, 0, 0);
windowsPalette[5]= BGR(180, 0, 196);
windowsPalette[6]= BGR(200, 170, 0);
windowsPalette[7]= BGR(196, 196, 196);
windowsPalette[8]= BGR(96, 96, 96);
windowsPalette[9]= BGR(64, 64, 255);
windowsPalette[10]= BGR(0, 240, 0);
windowsPalette[11]= BGR(0, 255, 255);
windowsPalette[12]= BGR(255, 20, 20);
windowsPalette[13]= BGR(240, 0, 255);
windowsPalette[14]= BGR(255, 240, 0);
windowsPalette[15]= BGR(255, 255, 255);
return SetDIBColorTable(backbuffer, 0, 16, windowsPalette);
};
int keypad(WINDOW *faux, bool bf)
{
return 1;
};
int noecho(void)
{
return 1;
};
int cbreak(void)
{
return 1;
};
int keypad(int faux, bool bf)
{
return 1;
};
int curs_set(int visibility)
{
return 1;
};
int mvaddch(int y, int x, const chtype ch)
{
return mvwaddch(mainwin,y,x,ch);
};
int wattron(WINDOW *win, int attrs)
{
bool isBold = !!(attrs & A_BOLD);
bool isBlink = !!(attrs & A_BLINK);
int pairNumber = (attrs & A_COLOR) >> 17;
win->FG=colorpairs[pairNumber].FG;
win->BG=colorpairs[pairNumber].BG;
if (isBold) win->FG += 8;
if (isBlink) win->BG += 8;
return 1;
};
int wattroff(WINDOW *win, int attrs)
{
win->FG=8; //reset to white
win->BG=0; //reset to black
return 1;
};
int attron(int attrs)
{
return wattron(mainwin, attrs);
};
int attroff(int attrs)
{
return wattroff(mainwin,attrs);
};
int waddch(WINDOW *win, const chtype ch)
{
char charcode;
charcode=ch;
switch (ch){ //LINE_NESW - X for on, O for off
case 4194424: //#define LINE_XOXO 4194424
charcode=179;
break;
case 4194417: //#define LINE_OXOX 4194417
charcode=196;
break;
case 4194413: //#define LINE_XXOO 4194413
charcode=192;
break;
case 4194412: //#define LINE_OXXO 4194412
charcode=218;
break;
case 4194411: //#define LINE_OOXX 4194411
charcode=191;
break;
case 4194410: //#define LINE_XOOX 4194410
charcode=217;
break;
case 4194422: //#define LINE_XXOX 4194422
charcode=193;
break;
case 4194420: //#define LINE_XXXO 4194420
charcode=195;
break;
case 4194421: //#define LINE_XOXX 4194421
charcode=180;
break;
case 4194423: //#define LINE_OXXX 4194423
charcode=194;
break;
case 4194414: //#define LINE_XXXX 4194414
charcode=197;
break;
default:
charcode = (char)ch;
break;
}
int curx=win->cursorx;
int cury=win->cursory;
//if (win2 > -1){
win->line[cury].chars[curx]=charcode;
win->line[cury].FG[curx]=win->FG;
win->line[cury].BG[curx]=win->BG;
win->draw=true;
addedchar(win);
return 1;
// else{
// win2=win2+1;
};
//Move the cursor of windows 0 (stdscr)
int move(int y, int x)
{
return wmove(mainwin,y,x);
};
//Set the amount of time getch waits for input
void timeout(int delay)
{
inputdelay=delay;
};
#endif