Public Group

# Moving Text

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

## Recommended Posts

Hello ppl, I was reading "Game Programming All in One" by Bruno Miguel Teixeira de Sousa, and was on the chapter where he builds the Monster's Game Library. Seeing some of the functions I decided to try my hands at a short program which has a string which moves depending on keypress. Initially it felt simple enuf, however, wen i tried my hand at it, NOTHING WORKED! :( I am pretty new at programming, so plz pardon any noobie mistakes. Here is the code
#include&lt;iostream.h&gt;
#include&lt;windows.h&gt;
#include&lt;conio.h&gt;

//global variables
WORD m_background;
HANDLE m_screen;

void cleaner()
{
COORD start;
DWORD written;

start.X=0;
start.Y=0;
m_background=0;

FillConsoleOutputAttribute( m_screen, FOREGROUND_BLUE|m_background,640*480,start,&written);
FillConsoleOutputCharacter(m_screen,' ',640*480,start,&written);

}

//this fn will draw my "stylized" name at the current cursor position
void draw(void)
{
DWORD written;

WriteConsole(m_screen,"~|bodhi|~",strlen("~|bodhi|~"),&written,NULL);
}

//like it says, this will evaluate the position of the cursor at each keypress
void calc_pos(COORD &current, char keypress)
{
if (keypress=='w')
current.Y+=1;

if (keypress=='s')
current.Y-=1;

if (keypress=='a')
current.X-=1;

if (keypress=='d')
current.X+=1;

}

//this fn will set the cursor to the position calc by the calc_pos fn above
void setpos(COORD pos)
{
SetConsoleCursorPosition(m_screen,pos);
}

int main(){

//starting point @ the center of the console window WEN IT IS MAXIMISED
COORD current;
current.X=320;
current.Y=240;

//initialize the current input device as the monitor
m_screen= GetStdHandle(STD_OUTPUT_HANDLE);

char keypress='a';

cout&lt;&lt;"This is a tiny diagnostic program which \"diagnoses\" whether i can do stuff!";
cout&lt;&lt;endl&lt;&lt;" Use the w,s,a,d keys to move the text around, and press 0 to quit";
cout&lt;&lt;endl&lt;&lt;"Press any key to start!!";

//the "game loop"
while(keypress!='0')
{
keypress=getch();
calc_pos(current,keypress);
setpos(current);
cleaner();
draw();
}

cout&lt;&lt;endl&lt;&lt;"thank you for chkin this out!!";
getch();
return 0;
}


Could you help me correct the errors and make it work? Thanks a lot... [Edited by - bodhisatva_b on July 9, 2006 2:57:10 AM]

##### Share on other sites
What's the problem? The program doesn't compile or the string doesn't move?

##### Share on other sites
It doesn't solve the problem but is something you missed. How does the draw function know where to draw the text if it doesn't have access to the COORD current?

Edit: Nevermind, you have a Goto Position function there..

Edit2:
0,0 is the top left of the console, not the bottem left.
Position x = 320 and y = 240 don't exist in the console.

Here is the fixed code (Escape is now quit):
#include<iostream>#include<windows.h>#include<conio.h>//global variablesWORD m_background;HANDLE m_screen;using namespace std;void cleaner(){    COORD start;    DWORD written;    start.X=0;    start.Y=0;    m_background=0;    FillConsoleOutputAttribute( m_screen, FOREGROUND_BLUE|m_background,640*480,start,&written);    FillConsoleOutputCharacter(m_screen,' ',640*480,start,&written);}//this fn will draw my "stylized" name at the current cursor positionvoid draw(void){    DWORD written;    WriteConsole(m_screen,"~|bodhi|~",strlen("~|bodhi|~"),&written,NULL);}//like it says, this will evaluate the position of the cursor at each keypressvoid calc_pos(COORD &current, char keypress){    if (keypress=='s')        current.Y+=1;    if (keypress=='w')        current.Y-=1;    if (keypress=='a')        current.X-=1;    if (keypress=='d')        current.X+=1;}//this fn will set the cursor to the position calc by the calc_pos fn abovevoid setpos(COORD pos){    SetConsoleCursorPosition(m_screen,pos);}int main(){//starting point @ the center of the console window WEN IT IS MAXIMISED    COORD current;    current.X=10;    current.Y=10;//initialize the current input device as the monitor    m_screen= GetStdHandle(STD_OUTPUT_HANDLE);    char keypress='a';    cout<<"This is a tiny diagnostic program which \"diagnoses\" whether i can do stuff!";    cout<<endl<<" Use the w,s,a,d keys to move the text around, and press 0 to quit";    cout<<endl<<"Press any key to start!!";//the "game loop"    while(keypress!=27)    {        keypress=getch();        calc_pos(current,keypress);        setpos(current);        cleaner();        draw();    }    cout<<endl<<"thank you for chkin this out!!";    getch();    return 0;}

##### Share on other sites
Thanks a lotttt!! :D
Tht fixed it. Well, mostly. Now, you said that the position 320x240 dont exist on the console and 0,0 is on the top left, i noticed that if i exceed the "bounds" of the console, crap hits the fan and all control is jacked. So, could you plz tell me wat is the max COORD of the X and Y axis using which i can do a "bounds-checking" when the key is pressed so that the txt doesnt exceed the console "bounds" and screws things up. Thanks a lot again for taking the effort to help me.

I added the bound checking for (0,0)
    if (keypress=='w')        {  if(current.Y>=0)              current.Y-=1;              }                  if (keypress=='a')        {  if (current.X>=0)              current.X-=1;              }

I get a peculiar problem, instead of "inaction" when i reach one of the X/Y axis, the text scrolls sideways. For instance, if i touch the top of the screen (meaning the X axis), and continue pressing 'w', then it starts scrolling to the right. Same is the case with the Y axis.

Edit: Oops. I'm quite the retard. I made the checking as "if(current.Y>=0)"
this causes it to get depreciated to -1 wen the location is 0. :D
(0,0) is resolved!! Whats the max value of X and Y?

PS: how do i get the code-box like thingy?

[Edited by - bodhisatva_b on July 7, 2006 9:55:53 PM]

##### Share on other sites
Something like 80 and 40 respectively. I can't actually remember.

Code is [ source] and [ /source] with out the spaces.

##### Share on other sites
The end-user may resize the console window, so use GetConsoleScreenBufferInfo to get the console's metrics.

##### Share on other sites
Thanks!

As for GetConsoleScreenBufferInfo,
Lemme look it up! Will see if i can find out how to use it. Thanks again!

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 17
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633362
• Total Posts
3011528
• ### Who's Online (See full list)

There are no registered users currently online

×