|
10 Jul 2003, 21:35
|
#1
|
b0rked!
Join Date: Apr 2001
Posts: 44
|
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"
|
|
|
11 Jul 2003, 08:09
|
#2
|
Spelling is for pussies
Join Date: Mar 2003
Location: Actually, where the feck am I........?
Posts: 446
|
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
|
|
|
12 Jul 2003, 14:07
|
#3
|
mmm.. pills
Join Date: Apr 2000
Location: Australia
Posts: 2,152
|
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.
|
|
|
12 Jul 2003, 17:42
|
#4
|
b0rked!
Join Date: Apr 2001
Posts: 44
|
\o/ thnx
one question though: why is that last 'ReadConsoleOutput(hOutput, oldbuffer, size, start, &rectangle);' needed?
|
|
|
12 Jul 2003, 19:33
|
#5
|
mmm.. pills
Join Date: Apr 2000
Location: Australia
Posts: 2,152
|
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.
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
|
All times are GMT +1. The time now is 14:13.
| |