User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 10 Jul 2003, 21:35   #1
Reck
b0rked!
 
Join Date: Apr 2001
Posts: 44
Reck is an unknown quantity at this point
C++ console/screenbuffer problem

Code:
#include <stdlib.h>
#include <windows.h>

int main()
{
 short left = 20;
 short top = 10;
 #define WIDTH  9
 #define HEIGHT  5
 
 // tekenen
 
 CHAR_INFO screenbuffer[WIDTH*HEIGHT];
 SMALL_RECT rectangle = {left, top, left+(WIDTH-1), top+(HEIGHT-1)}; //left, top, right, bottom
 
 int m = 1;
 
 for(int i = 0; i < WIDTH * HEIGHT; i++)
 {
   screenbuffer[i].Char.AsciiChar=' ';
   screenbuffer[i].Attributes = 0;
 }
 
 for(int i = 0; i < HEIGHT; i++)
 {
   for(int j = 0; j < m; j++)
   {
     screenbuffer[( 4 + (9*i) ) + j].Char.AsciiChar = 1;
     screenbuffer[( 4 + (9*i) ) + j].Attributes = FOREGROUND_GREEN;
     screenbuffer[( 4 + (9*i) ) - j].Char.AsciiChar = 1;
     screenbuffer[( 4 + (9*i) ) - j].Attributes = FOREGROUND_GREEN;
   }
   m++; 
 }
   
 HANDLE hOutput;
 hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
 COORD size = {WIDTH, HEIGHT};
 COORD start = {0, 0};
 
 WriteConsoleOutput(hOutput, screenbuffer, size, start, &rectangle);
 
 INPUT_RECORD InputRecord;
 HANDLE hInput;
 hInput = GetStdHandle(STD_INPUT_HANDLE);
 DWORD Events;
   
 while(1)
 {
   ReadConsoleInput(hInput, &InputRecord, 1, &Events);
   
   if(InputRecord.EventType == KEY_EVENT)
   {
     if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
     {
       rectangle.Left = rectangle.Left + 1;
       rectangle.Right = rectangle.Right + 1;
     } 
     
     if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
     {
       rectangle.Left = rectangle.Left - 1;
       rectangle.Right = rectangle.Right - 1;
     } 
    
     if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP)
     {
       rectangle.Top = rectangle.Top - 1;
       rectangle.Bottom = rectangle.Bottom - 1;
     } 
    
     if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
     {
       rectangle.Top = rectangle.Top + 1;
       rectangle.Bottom = rectangle.Bottom + 1;
     }
               
     WriteConsoleOutput(hOutput, screenbuffer, size, start, &rectangle);
     FlushConsoleInputBuffer(hInput); 
   
   } 
 }
  
 system("pause");
 
 return 0;
}
the idea was to move a triangle of smilies in a console screen by using the arrow keys. However the top and bottom rows (of the triangle) tend to leave a "trail"
Reck is offline   Reply With Quote
Unread 11 Jul 2003, 08:09   #2
SbOlly
Spelling is for pussies
 
SbOlly's Avatar
 
Join Date: Mar 2003
Location: Actually, where the feck am I........?
Posts: 446
SbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant future
Don't you need some sort of clear screen command every time the user presses a button?
__________________
If God made me in his image, he's one fat ugly biatch.

I always get the soggy biscuit

Veni Vidi Codi
SbOlly is offline   Reply With Quote
Unread 12 Jul 2003, 14:07   #3
djbass
mmm.. pills
 
djbass's Avatar
 
Join Date: Apr 2000
Location: Australia
Posts: 2,152
djbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond repute
Following code will do the job:

Code:
#include <stdlib.h>
#include <windows.h>

int main()
{
 short left = 20;
 short top = 10;
 #define WIDTH  9
 #define HEIGHT  5
 
 // tekenen
 
 CHAR_INFO screenbuffer[WIDTH*HEIGHT];
 CHAR_INFO oldbuffer[WIDTH*HEIGHT];
 SMALL_RECT rectangle = {left, top, left+(WIDTH-1), top+(HEIGHT-1)}; //left, top, right, bottom
 
 int m = 1;
 
 for(int i = 0; i < WIDTH * HEIGHT; i++)
 {
   screenbuffer[i].Char.AsciiChar=' ';
   screenbuffer[i].Attributes = 0;
 }
 
 for(i = 0; i < HEIGHT; i++)
 {
   for(int j = 0; j < m; j++)
   {
     screenbuffer[( 4 + (9*i) ) + j].Char.AsciiChar = 1;
     screenbuffer[( 4 + (9*i) ) + j].Attributes = FOREGROUND_GREEN;
     screenbuffer[( 4 + (9*i) ) - j].Char.AsciiChar = 1;
     screenbuffer[( 4 + (9*i) ) - j].Attributes = FOREGROUND_GREEN;
   }
   m++; 
 }
   
 HANDLE hOutput;
 hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
 COORD size = {WIDTH, HEIGHT};
 COORD start = {0, 0};
 
 ReadConsoleOutput(hOutput, oldbuffer, size, start, &rectangle);
 WriteConsoleOutput(hOutput, screenbuffer, size, start, &rectangle);
 
 INPUT_RECORD InputRecord;
 HANDLE hInput;
 hInput = GetStdHandle(STD_INPUT_HANDLE);
 DWORD Events;
   
 while(1)
 {
   ReadConsoleInput(hInput, &InputRecord, 1, &Events);
   
   if(InputRecord.EventType == KEY_EVENT)
   {
	 WriteConsoleOutput(hOutput, oldbuffer, size, start, &rectangle);
     if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
     {
       rectangle.Left = rectangle.Left + 1;
       rectangle.Right = rectangle.Right + 1;
     } 
     
     if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
     {
       rectangle.Left = rectangle.Left - 1;
       rectangle.Right = rectangle.Right - 1;
     } 
    
     if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP)
     {
       rectangle.Top = rectangle.Top - 1;
       rectangle.Bottom = rectangle.Bottom - 1;
     } 
    
     if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
     {
       rectangle.Top = rectangle.Top + 1;
       rectangle.Bottom = rectangle.Bottom + 1;
     }
  
	 ReadConsoleOutput(hOutput, oldbuffer, size, start, &rectangle);
	 WriteConsoleOutput(hOutput, screenbuffer, size, start, &rectangle);
     FlushConsoleInputBuffer(hInput); 
   
   } 
 }
  
 system("pause");
 
 return 0;
}
=[DJ Bass]=
__________________
CSS : the result of letting artists design something only an engineer should touch.
djbass is offline   Reply With Quote
Unread 12 Jul 2003, 17:42   #4
Reck
b0rked!
 
Join Date: Apr 2001
Posts: 44
Reck is an unknown quantity at this point
\o/ thnx

one question though: why is that last 'ReadConsoleOutput(hOutput, oldbuffer, size, start, &rectangle);' needed?
Reck is offline   Reply With Quote
Unread 12 Jul 2003, 19:33   #5
djbass
mmm.. pills
 
djbass's Avatar
 
Join Date: Apr 2000
Location: Australia
Posts: 2,152
djbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond repute
scalability I suppose, the technique used in the code is simular to that used for drawing sprites where you need to store a copy of the background behind the sprite so that you can restore the surface once sprite has moved to a different location.

For your purposes though you can probably get by without it.

=[DJ Bass]=
__________________
CSS : the result of letting artists design something only an engineer should touch.
djbass is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 14:13.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018