Thread: Need help in speeding up the output stream (C)

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    Need help in speeding up the output stream (C)

    Hi guys:

    I just started my text based game programming today i tried some of my basic skill on it, i programmed this very simple text program that simply consists of a character moving around the Command prompt screen with support of very Simple collision detection when it hits the walls.

    MY PROBLEM is that i programmed it so that when i hit either (a,w,s,d) keys the whole output buffer is cleared and a new updated one is created and displayed on the screen, you can feel the flashing screen during each operation, i wondered is there any solution for such glitching?? is there any function to overcome this problem ?? cause i just watched some videos on youtube some guys programmed their own text based games and it just works fine for them!!!

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    
    
    int main ()
    {
        void wall(int *, int *);
        
      char ch;  
      int x , y ;
        x = 5;
        y = 40;
        
               wall(&x , &y);
        ch = getch();
        while (1)
        {
              switch (ch)
        {
               case 'w':
               case 'W':
                    if (x > 1)
                    x--;
                    break;
                    
                case 'a':
                case 'A':
                     if (y > 1)
                     y--;
                     break;
                case 's':
                case 'S':
                     if (x < 10)
                     x++;
                     break;
                case 'd':
                case 'D':
                     if (y < 78)
                     y++;
                     break ;                                
               }
               
               system("cls");
               wall(&x ,&y);
              ch = getch();
             
               }
        
         
         
         
        getch();
        return 0;
        }
     
     
             
     void wall (int *x, int *y )
               {
                   
                     int i;
                     int j;
                     for (i = 1; i <= 80 ; i++)
                    printf("X");
                  
                     
                     for ( i = 1; i <= 10 ; i++)
                       {
                           printf("X");
                           for (j = 1; j <= 78; j++)
                            {
                               if (i == *x && j == *y)
                              {
                                printf("G");
                                
                                continue;
                                  }
                                  
                              printf(" ");
                           
                              }
                           printf ("X");
                           }
                            for (i = 1; i <= 80 ; i++)
                    printf("X");
                    
                     
                      }
    Note: Iam using Bloodshed DEV-C++ compiler

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Using the stdio library, the emphasis is on having a limited set of features with maximum portability.
    That said, using system() to do anything at all is exceedingly expensive.

    If you want performance, and control, then you usually have to resort to platform specific code.
    Here is an excellent win32 console tutorial written by one of the regulars here.
    Win32 Console Applications 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    The flashing you are seeing is from that system("cls") call that you are using. In order to work around that you are going to need to learn about back buffering, bitblting, and the whole basic concept of console game programming. Take a look at adrian's console tutorial. Also you may find some help with our game programming links.

    Edit: Too slow.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::string object is overwritten in output stream
    By eatwithaspork in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2009, 03:20 PM
  2. Output stream help
    By alibaba786 in forum C++ Programming
    Replies: 9
    Last Post: 03-02-2009, 09:33 AM
  3. positioning in output stream
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2004, 12:40 PM
  4. output stream with ****
    By matheo917 in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2002, 08:12 PM
  5. How to insert "tab" into a stream of output??
    By JCK in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2002, 10:23 PM