Archived

This topic is now archived and is closed to further replies.

A simple console question

This topic is 6027 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am sure this question has been asked many times in different ways, so me being the "advanced newbie" that i am, i decided to repost according to the way i need it answered and i''m posting here cuz this seems the most suitble forum. I need to be able to clear the console output of all text and replace the cursor at the beginning (top left). If you are familiar with the old turboc++ from Borland, they had a function named "clrscr()"....I am looking for a way of doing this in MS Vis C++ 6 I have tried several different solutions given to me by ppl, but none seem to work....they run, just don''t clear the screen.... I''ve tried the "fillconsoleoutputcharacter" api, and the "system()" function among others, but neither seem to do what i want... is there a way other than creating a loop of "\n"s to do this??? Any help would be appreciated.... commando_337

Share this post


Link to post
Share on other sites
Well, if it isn''t the ''ol "clear the console" question ...

I understand what you mean by a loop of "\n"s being slow. It is. The next best thing I would do would be to code it in assembly and access the console memory directly to clear it (using a lighting fast assembly loop). I don''t know how stable that would be. The third and probably easiest method would be to use interrupts in assembly to clear the screen...but interrupts aren''t ALLOWED under Windows. WTF??!?!!! Yeah it sucks...

OR, if you design your entire console around Win32''s console "API", clearing it should be a breeze.

Just in case you''re interested, heres method 3 in assembly. OF course the interrupt will crash your system if you do it:
  
XOR AL,AL
XOR CX,CX
MOV DH,24
MOV DL,79
MOV BH,7
MOV AH,6
INT 10h

Share this post


Link to post
Share on other sites
That command doesn''t work on my computer. It actually depends on what system files are loaded in your config.sys file and in other places. ANSI.SYS for example allows the use of ANSI escape sequences for things like changing text color, cursor position, etc. However if you try to do a special escape sequence without ANSI.SYS loaded you get a lot of garble. I don''t know what file "cls" or any other DOS commands are contained in (i think "restart" is another good command ), but you can try it, and if it works great, otherwise, you either need to find the file you need or find another method.

Share this post


Link to post
Share on other sites
The fillconsoleoutputcharacter-function would normally work great, check the following example

#include //''<''windows.h''>''; if i put things between < and >, these aren''t shown
char *buffer = new char[25*80];
//Now you can fill the buffer with whatever you want, e.g. fill it with spaces to have a blank screen
#include ''<''memory.h''>''
memset( buffer, '' '', 80*25 );

//then flip the buffer to the console screen:
COORD co;
co.X = 0; co.Y = 0; //starting coordinates, 0, 0
DWORD cWritten; //don''t actually need this
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

int fSuccess = WriteConsoleOutputCharacter( hOutput, buffer, strlen( buffer ), co, &cWritten );
//now the content of the buffer should be flipped to the console screen
let me know if it works, I just wrote this fast

I created a snake-looking game in console mode in about 3-4 days with 2 players and you can schoot...
I created my own class called BackBuffer, where you have that buffer and stuff... You can also use colors with my backbuffer, it''s not that difficult. If you are interested, give me a sign, I''ll give you my backbuffer class cuz it''s really handy

cheerz

Katana

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the asm code isn''t win32-it''s dos(i think). you''re running 16 bit code in a 32 bit environment.

Share this post


Link to post
Share on other sites
The above asm code is valid both in a 32 bit and a 16 environment. It just depends on if you put it in a 16 bit or a 32 bit code segment.

The reason BIOS interrupts dont work is because they are setup during boot, while the processor is still in real mode (backwards compatibility mode). Then when windows is started it enters the processor into protected mode and since real mode and and protected mode have different strategies when it comes to dealing with interrupts, all previously initialized interrupts are invalidated.

Share this post


Link to post
Share on other sites
Ok, well i''m going away for the weekend, but thanks for the posts and i''ll try the suggestions when i get back...

It''s very depressing when i was so good at programming using turboc then i come to ms vis studio and i can''t even clear the output...but hopefully this will remedy the situation...

thanks again

commando_337

Share this post


Link to post
Share on other sites
Me Again...=)

I''m not sure how many ppl are still watching this thread but i''ll ask this question as it pertains to my last...

I finally figured out why the previous attempts to clear the screen did not work...as i am just a beginner at c++ have pity if i seem stupid...
It seems that if i use the "printf()" command. everythign works properly...I''m now using the system("cls") command cuz it''s so simple...

But if i use the "cout" command....no attempts to clear the screen work....this was my problem....

Is this because the cout command waits till everything has been run the flushes some sort of buffer to the screen, and so any attempts to clear the screen between outputs will not affect it???

Am i making any sense?

commando_337

Share this post


Link to post
Share on other sites
http://qsoft.ragestorm.com/tutors/windows/index.html#snippets
follow the link
you'll see it on the list of the code snippets "Clear Con Buffer"

Edited by - arkon on June 12, 2001 4:29:43 AM

Share this post


Link to post
Share on other sites