1 /* CONSOLE INTERFACE FOR WINDOWS */
9 //#define RAW_MIN_CONSOLE
11 #define RAW_CONSOLE_DIRTY
12 /* allow max 6 character holes when printing blocks of dirty characters (RAW_CONSOLE_DIRTY defined) */
15 #ifdef RAW_CONSOLE_DIRTY
17 #error RAW_CONSOLE_DIRTY without RAW_CONSOLE
23 #define DEF_ATTR (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
24 char curattr
=DEF_ATTR
,
25 color
=(FOREGROUND_RED
| FOREGROUND_GREEN
| FOREGROUND_BLUE
),
30 #define generate_attributes() { curattr=translate(color)+bkcolor*0x10+hilight*FOREGROUND_INTENSITY+bkhilight*BACKGROUND_INTENSITY; \
31 SetConsoleTextAttribute(hMyConsole, curattr); }
33 #define generate_attributes() curattr=translate(color)+bkcolor*0x10+hilight*FOREGROUND_INTENSITY+bkhilight*BACKGROUND_INTENSITY
35 #ifdef RAW_MIN_CONSOLE
41 #ifdef RAW_CONSOLE_DIRTY
42 unsigned char *dirtyChars
=NULL
,
44 int *dirtyBlockStart
=NULL
;
51 void c_init(int w
, int h
) {
55 hMyConsole
=GetStdHandle(STD_OUTPUT_HANDLE
);
57 SetConsoleMode(hMyConsole
, 0);
59 #ifdef RAW_MIN_CONSOLE
68 CONSOLE_SCREEN_BUFFER_INFO sbi
;
69 GetConsoleScreenBufferInfo(hMyConsole
, &sbi
);
70 w
=sbi
.srWindow
.Right
-sbi
.srWindow
.Left
+1;
71 h
=sbi
.srWindow
.Bottom
-sbi
.srWindow
.Top
+1;
76 if ( !(screenC
=mem_alloc(w
*h
)) )
78 if ( !(screenA
=mem_alloc(sizeof(SHORT
)*w
*h
)) )
80 memset(screenC
, 0, w
*h
);
81 memset(screenA
, 0, w
*h
*sizeof(SHORT
));
82 #ifdef RAW_CONSOLE_DIRTY
83 if ( !(dirtyChars
=mem_alloc(w
*h
)) )
85 if ( !(dirtyLines
=mem_alloc(h
)) )
87 if ( !(dirtyBlockStart
=mem_alloc(h
*sizeof(int))) )
89 memset(dirtyChars
, 1, w
*h
);
90 memset(dirtyLines
, 1, h
);
91 memset(dirtyBlockStart
, 0, h
*sizeof(int));
98 SetConsoleScreenBufferSize(hMyConsole
, crd
);
99 crd
=GetLargestConsoleWindowSize(hMyConsole
);
102 sr
.Right
=min(crd
.X
-1, w
-1);
103 sr
.Bottom
=min(crd
.Y
-1, h
-1);
104 SetConsoleWindowInfo(hMyConsole
, TRUE
, &sr
);
109 void c_shutdown(void) {
120 #ifdef RAW_CONSOLE_DIRTY
122 mem_free(dirtyChars
);
125 /* dalsi chyba woken ... */
126 /*if ( dirtyLines ) {
127 mem_free(dirtyLines);
130 if ( dirtyBlockStart ) {
131 mem_free(dirtyBlockStart);
132 dirtyBlockStart=NULL;
140 // clears the screen:
148 FillConsoleOutputCharacter(hMyConsole
, 0, x
*y
, coord
, &written
);
149 FillConsoleOutputAttribute(hMyConsole
, 0, x
*y
, coord
, &written
);
151 memset(screenC
, 0, width
*height
);
152 memset(screenA
, 0, width
*height
);
153 #ifdef RAW_CONSOLE_DIRTY
154 memset(dirtyChars
, 1, width
*height
);
155 memset(dirtyLines
, 1, height
);
156 memset(dirtyBlockStart
, 0xFF, height
*sizeof(int));
161 void c_print(char *text
) {
163 int firstNonDirty
=-1;
165 if ( curpos
>width
*height
)
167 #ifdef RAW_CONSOLE_DIRTY
168 if ( dirtyChars
[curpos
]==0 ) {
169 if ( firstNonDirty
!=-1 )
170 firstNonDirty
=curpos
;
172 dirtyChars
[curpos
]=-1;
174 else if ( firstNonDirty
!=-1 ) {
175 dirtyChars
[firstNonDirty
]=curpos
-firstNonDirty
;
179 screenA
[curpos
]=curattr
;//curpos/2;
180 screenC
[curpos
++]=*(text
++);//'0'+((( curpos%2 )? curpos/2 : curpos/20)%10);
182 #ifdef RAW_CONSOLE_DIRTY
183 if ( firstNonDirty
!=-1 )
184 dirtyChars
[firstNonDirty
]=curpos
-firstNonDirty
;
185 dirtyBlockStart
[curpos
/width
]=-1;
186 dirtyLines
[curpos
/width
]=1;
191 WriteConsole(hMyConsole
, text
, strlen(text
), &written
, NULL
);
193 #ifdef RAW_MIN_CONSOLE
196 WriteConsoleOutputCharacter(hMyConsole
, text
, len
, curpos
, &wr
);
197 FillConsoleOutputAttribute(hMyConsole
, curattr
, len
, curpos
, &wr
);
198 curpos
.X
+=(SHORT
)len
;
202 void c_print_l(char * text
, int len
)
212 void c_putc(char c
) {
214 if ( curpos
>width
*height
)
216 #ifdef RAW_CONSOLE_DIRTY
217 if ( dirtyChars
[curpos
]==0 ) {
218 if ( dirtyBlockStart
[curpos
/width
]==-1 ) {
219 dirtyBlockStart
[curpos
/width
]=curpos
%width
;
220 dirtyChars
[curpos
]=1;
221 dirtyLines
[curpos
/width
]=1;
223 else if ( dirtyBlockStart
[curpos
/width
]+dirtyChars
[dirtyBlockStart
[curpos
/width
]]==curpos
%width
) {
224 dirtyChars
[curpos
]=-1;
225 if ( ++dirtyChars
[dirtyBlockStart
[curpos
/width
]]==254 )
226 dirtyBlockStart
[curpos
/width
]=-1;
229 dirtyBlockStart
[curpos
/width
]=curpos
%width
;
230 dirtyChars
[curpos
]=1;
234 screenA
[curpos
]=curattr
;//curpos/2;
235 screenC
[curpos
++]=c
;//'0'+((( curpos%2 )? curpos/2 : curpos/20)%10;
239 WriteConsole(hMyConsole
, &c
, sizeof(c
), &written
, NULL
);
241 #ifdef RAW_MIN_CONSOLE
243 WriteConsoleOutputCharacter(hMyConsole
, &c
, 1, curpos
, &wr
);
244 FillConsoleOutputAttribute(hMyConsole
, curattr
, 1, curpos
, &wr
);
249 void c_goto(int x
, int y
) {
254 SetConsoleCursorPosition(hMyConsole
, coord
);
256 #ifdef RAW_MIN_CONSOLE
265 void c_clear(int x1
,int y1
,int x2
, int y2
) {
274 for(y
=y1
; y
<=y2
; y
++) {
276 FillConsoleOutputCharacter(hMyConsole
, 0, w
, coord
, &written
);
277 FillConsoleOutputAttribute(hMyConsole
, 0, w
, coord
, &written
);
283 for(y
=y1
; y
<=y2
; y
++) {
284 memset(screenC
+x1
+y
*width
, 0, x2
-x1
);
285 memset(screenA
+x1
+y
*width
, DEF_ATTR
, x2
-x1
);
286 memset(dirtyChars
+x1
+y
*width
, 1, x2
-x1
);
288 #ifdef RAW_CONSOLE_DIRTY
289 memset(dirtyLines
+y1
, 1, y2
-y1
);
290 memset(dirtyBlockStart
+y1
, 0xFF, (y2
-y1
)*sizeof(int));
295 void c_cursor(int c
) {
296 CONSOLE_CURSOR_INFO ci
;
297 GetConsoleCursorInfo(hMyConsole
, &ci
);
298 ci
.bVisible
=( c
==C_NORMAL
);
299 SetConsoleCursorInfo(hMyConsole
, &ci
);
303 MessageBeep(0xFFFFFFFF);
306 int translate(int a
) {
319 ma=FOREGROUND_RED|FOREGROUND_GREEN;
325 ma=FOREGROUND_BLUE|FOREGROUND_RED;
328 ma=FOREGROUND_BLUE|FOREGROUND_GREEN;
331 ma=FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_GREEN;
334 ma
=(a
&0xFA)+0x4*(a
&0x1)+(( a
&0x4 )? 1 : 0);
338 /* set complete foreground color */
339 void c_setcolor(unsigned char a
) {
340 /*CONSOLE_SCREEN_BUFFER_INFO ci;
341 GetConsoleScreenBufferInfo(hMyConsole, &ci);
342 SetConsoleTextAttribute(hMyConsole, (a&0xF)+(ci.wAttributes&~0x000F));*/
345 hilight
=( a
>>3 ) ? 1 : 0;
346 generate_attributes();
349 /* set complete foreground color and background color */
350 void c_setcolor_bg(unsigned char fg
,unsigned char bg
) {
351 /*CONSOLE_SCREEN_BUFFER_INFO ci;
352 GetConsoleScreenBufferInfo(hMyConsole, &ci);
353 SetConsoleTextAttribute(hMyConsole, (fg&0xF)+((bg&0xF)<<8)+(ci.wAttributes&~0x00FF));*/
356 //curattr=(curattr&~0xFF)|(fg&0xF)|((bg&0xF)<<8);
360 hilight
=( fg
>>3 ) ? 1 : 0;
365 hilight
=( fg
>>3 ) ? 1 : 0;
366 //bkhilight=( bg>>3 ) ? 1 : 0;
368 generate_attributes();
371 void c_setcolor_3b(unsigned char a
) {
372 /*CONSOLE_SCREEN_BUFFER_INFO ci;
373 GetConsoleScreenBufferInfo(hMyConsole, &ci);
374 SetConsoleTextAttribute(hMyConsole, (a&0x7)+(ci.wAttributes&~0x0007));*/
377 generate_attributes();
380 void c_setcolor_3b_bg(unsigned char fg
,unsigned char bg
) {
381 /*CONSOLE_SCREEN_BUFFER_INFO ci;
382 GetConsoleScreenBufferInfo(hMyConsole, &ci);
383 SetConsoleTextAttribute(hMyConsole, (fg&0x7)+((bg&0x7)<<8)+(ci.wAttributes&~0x0077))*/;
388 generate_attributes();
391 /* set highlight color */
392 void c_sethlt(unsigned char a
) {
393 /*CONSOLE_SCREEN_BUFFER_INFO ci;
394 GetConsoleScreenBufferInfo(hMyConsole, &ci);
395 SetConsoleTextAttribute(hMyConsole, (a ? FOREGROUND_INTENSITY : 0)+(ci.wAttributes&~FOREGROUND_INTENSITY));*/
397 generate_attributes();
400 /* set highlight and background color */
401 void c_sethlt_bg(unsigned char hlt
,unsigned char bg
) {
402 /*CONSOLE_SCREEN_BUFFER_INFO ci;
403 GetConsoleScreenBufferInfo(hMyConsole, &ci);
404 SetConsoleTextAttribute(hMyConsole, (hlt ? BACKGROUND_INTENSITY : 0)+((bg&0xF)<<8)+(ci.wAttributes&~0x00F0));*/
408 generate_attributes();
411 /* set background color */
412 void c_setbgcolor(unsigned char a
) {
413 /*CONSOLE_SCREEN_BUFFER_INFO ci;
414 GetConsoleScreenBufferInfo(hMyConsole, &ci);
415 SetConsoleTextAttribute(hMyConsole, ((a&0xF)<<8)+(ci.wAttributes&~0x00F0));*/
417 //bkhilight=( a>>3 ) ? 1 : 0;
419 generate_attributes();
422 void c_refresh(void) {
424 FlushFileBuffers(hMyConsole
);
427 #ifndef RAW_CONSOLE_DIRTY
431 WriteConsoleOutputCharacter(hMyConsole
, screenC
, width
*height
, coord
, &written
);
432 WriteConsoleOutputAttribute(hMyConsole
, screenA
, width
*height
, coord
, &written
);
440 for(coord
.Y
=0; coord
.Y
<height
; coord
.Y
++) if ( dirtyLines
[coord
.Y
] ) {
441 base
=((DWORD
)coord
.Y
)*width
;
443 for(chr
=0; chr
<width
; chr
++) {
444 if ( dirtyChars
[base
+chr
] ) {
449 if ( dirtyChars
[base
+chr
]==-1 )
450 dirtyChars
[base
+chr
]=1;
451 cnt
+=dirtyChars
[base
+chr
];
452 chr
+=dirtyChars
[base
+chr
]-1; // -1 ... ++
456 coord
.X
=chr
-(cnt
+sur
-1);
457 WriteConsoleOutputCharacter(hMyConsole
, screenC
+base
+coord
.X
, cnt
, coord
, &written
);
458 WriteConsoleOutputAttribute(hMyConsole
, screenA
+base
+coord
.X
, cnt
, coord
, &written
);
465 coord
.X
=chr
-(cnt
+sur
);
466 WriteConsoleOutputCharacter(hMyConsole
, screenC
+base
+coord
.X
, cnt
, coord
, &written
);
467 WriteConsoleOutputAttribute(hMyConsole
, screenA
+base
+coord
.X
, cnt
, coord
, &written
);
471 memset(dirtyChars
, 0, width
*height
);
472 memset(dirtyLines
, 0, height
);
473 memset(dirtyBlockStart
, 0xFF, height
*sizeof(int));
478 void c_get_size(int *x
,int *y
) {
479 CONSOLE_SCREEN_BUFFER_INFO sbi
;
481 hMyConsole
=GetStdHandle(STD_OUTPUT_HANDLE
);
482 GetConsoleScreenBufferInfo(hMyConsole
, &sbi
);
483 *x
=sbi
.srWindow
.Right
-sbi
.srWindow
.Left
+1;
484 *y
=sbi
.srWindow
.Bottom
-sbi
.srWindow
.Top
+1;
487 int c_pressed(int k
) {
488 return kbd_is_pressed(k
);
491 int c_was_pressed(int k
) {
492 return kbd_was_pressed(k
);
495 void c_wait_for_key(void) {
499 void c_update_kbd(void) {