cls

This is a discussion on cls within the C++ Programming forums, part of the General Programming Boards category; when i use system("cls") in my game it takes way too much time to re-draw the screen! is there anyway ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    cls

    when i use system("cls") in my game it takes way too much time to re-draw the screen! is there anyway to speed this up? in harryP's maze game this is not wrong!
    **infected by frenchfry164
    **
    I am a signature virus. Please add me to your signature so that I may multiply

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Clearing the screen is in the FAQ. In short, check your compiler documentation for a local cls function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    no can do.

    i dont have a documentation, is there one for Dev-C++ or MingW or whatever?
    my source is this
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <conio.h> 
    #include <windows.h> 
    
    using namespace std;
    
    void clrscr(void)
    {
        COORD                       coordScreen = { 0, 0 };
        DWORD                       cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        DWORD                       dwConSize;
        HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, TEXT(' '), 
                                   dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                                   dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    
    int main(void)
    {
      // configure the main console
      SetConsoleTitle("Engine Text v0.1");
      // start game loop
      int play = 1;
      int playerX = 10;
      int playerY = 10;
      while (play)
      {
      	// :: this is the inner-game code
      	// draw the board outline
      	cout<<"############################################"<<endl;
      	cout<<"############################################"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"####                                    ####"<<endl;
      	cout<<"############################################"<<endl;
      	cout<<"############################################"<<endl;
      	// redraw and clear
      	clrscr();
    
      }
      system("PAUSE");	
      return 0;
    }
    but that works as well as system("cls"); !
    so im lost.
    **infected by frenchfry164
    **
    I am a signature virus. Please add me to your signature so that I may multiply

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

    Re: no can do.

    Originally posted by CammoDude91
    i dont have a documentation, is there one for Dev-C++ or MingW or whatever?

    so im lost.
    go to www.mingw.org..

    there should be a link to the documentation there.

  5. #5
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    Posts
    124
    You could also make a function for it. This one probably wouldn't help any if system("cls") isn't working for you because it involves a loop and all, which just takes away more from the speed. But, you could try this.

    Code:
    #include <windows.>
    #include <stdio.h>
    
    #define MAX_WINDOW_X     79
    #define MAX_WINDOW_Y     24
    
    void gotoxy(int x, int y) // A function to place the cursor at x,y
    { 
    	HANDLE hConsoleOutput;
    	COORD dwCursorPosition;
    
    	dwCursorPosition.X = x;
    	dwCursorPosition.Y = y;
    	hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
    	SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
    }
    
    void ClearScreen()
    {
              for(int i=0; i < MAX_WINDOW_Y; i++)
              {
                    for(int j=0; j < MAX_WINDOW_X; j++)
                    {
                          gotoxy(j,i);
                          printf(" ");
                    }
                    printf("\n");
              }
              gotoxy(0,0); // This resets the cursor back to 0,0
    }
    Good luck with all this. I don't know if that'll help or not, but it's always worth a go.

    Brendan
    Draco dormiens nunquam titallandus.
    Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I wouldn't name my function clrscr() either, because that's already defined by a few compilers...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Originally posted by harryP
    You could also make a function for it. This one probably wouldn't help any if system("cls") isn't working for you because it involves a loop and all, which just takes away more from the speed. But, you could try this.
    //...
    Good luck with all this. I don't know if that'll help or not, but it's always worth a go.

    Brendan
    Why not just output 25 newlines, and let the screen scroll down?

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    im ssoososooso n00b.

    nothing here works. i asume it is just a diff function for mingW32 but i cant fund any FUNCTIONS in the doc at mingw.org.
    **infected by frenchfry164
    **
    I am a signature virus. Please add me to your signature so that I may multiply

  9. #9
    blank
    Guest
    Hey, this is harryP, but I'm on a different computer.

    Yeah, Salem brought up a good point. You don't need to clear the screen everytime the player moves (assuming I'm understanding this correctly). Just re-draw the player, that'll make things much smoother. For example, if the player was at 10,10 and the user decided to make him go up, just re-draw the player at 10,9 and erase the player at 10,10.
    Code:
    void UpdatePlayer(int direction, int &x, int &y)
    {
         // Move the player up
         switch(direction)
         case 1:
              gotoxy(x,y); // Go to the player's original position
              printf(" "); // Erase him (If this was being stored in an array, you'd want to update the array as well, like map[x][y] = 0 or something
              y++; // Update the coordinates
              gotoxy(x,y);
              printf("%"); // Or whatever your player looks like
              // Again, if you're using an array, you'd need to update that, too
              break;
           ...
    }
    That'll work much better for you. The only time I used a clear screen function in my maze thing was when a new maze was being loaded or I was showing the menu or something. You'll find that it moves much faster

    Brendan

  10. #10
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    thx man

    thx man - i think i got it from here.
    **infected by frenchfry164
    **
    I am a signature virus. Please add me to your signature so that I may multiply

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    You could do this in assembler if Windows will let you access the video memory directly. But you need the location of the video memory for the mode you are in - text modes are completely different beasties than graphics modes, but it can be done.

    I'm fairly sure that you can do this because Windows will allow you to access the video RAM for graphics modes. But absolutely no one has posted anything about this in all of the console mode clear screen questions so I might be wrong.

    Personally I feel that using printf() to clear the screen is archaic and slow, but that might be your only choice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  2. cls() vs system("cls")
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 10-26-2006, 03:13 PM
  3. Replies: 15
    Last Post: 05-13-2006, 09:28 PM
  4. cls
    By fkheng in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 06:45 AM
  5. cls in C
    By Shogun in forum C Programming
    Replies: 6
    Last Post: 04-04-2003, 06:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21