Quell
January 31st, 2006, 03:18 PM
Hello.
I have been coding for a while but i have pathetic conventions and style.
Here is a sample piece of code that i ahve and i was wondering if anyone got any ideas on how i could improve.Any critisims+ideas are welcome.
#ifndef sys_console
#define sys_console
#include <windows.h>
#include <iostream>
#include <conio.h>
static const WORD fgBlack= 0;
static const WORD fgLoRed= FOREGROUND_RED;
static const WORD fgLoGreen= FOREGROUND_GREEN;
static const WORD fgLoBlue= FOREGROUND_BLUE;
static const WORD fgLoCyan= fgLoGreen|fgLoBlue;
static const WORD fgLoMagenta= fgLoRed|fgLoBlue;
static const WORD fgLoYellow= fgLoRed|fgLoGreen;
static const WORD fgLoWhite= fgLoRed|fgLoGreen|fgLoBlue;
static const WORD fgGray= fgBlack|FOREGROUND_INTENSITY;
static const WORD fgHiWhite= fgLoWhite|FOREGROUND_INTENSITY;
static const WORD fgHiBlue= fgLoBlue|FOREGROUND_INTENSITY;
static const WORD fgHiGreen= fgLoGreen| FOREGROUND_INTENSITY;
static const WORD fgHiRed= fgLoRed|FOREGROUND_INTENSITY;
static const WORD fgHiCyan= fgLoCyan|FOREGROUND_INTENSITY;
static const WORD fgHiMagenta= fgLoMagenta|FOREGROUND_INTENSITY;
static const WORD fgHiYellow= fgLoYellow|FOREGROUND_INTENSITY;
static const WORD bgBlack= 0;
static const WORD bgLoRed= BACKGROUND_RED;
static const WORD bgLoGreen= BACKGROUND_GREEN;
static const WORD bgLoBlue= BACKGROUND_BLUE;
static const WORD bgLoCyan= bgLoGreen|bgLoBlue;
static const WORD bgLoMagenta= bgLoRed|bgLoBlue;
static const WORD bgLoYellow= bgLoRed|bgLoGreen;
static const WORD bgLoWhite= bgLoRed|bgLoGreen|bgLoBlue;
static const WORD bgGray= bgBlack|BACKGROUND_INTENSITY;
static const WORD bgHiWhite= bgLoWhite|BACKGROUND_INTENSITY;
static const WORD bgHiBlue= bgLoBlue|BACKGROUND_INTENSITY;
static const WORD bgHiGreen= bgLoGreen|BACKGROUND_INTENSITY;
static const WORD bgHiRed= bgLoRed|BACKGROUND_INTENSITY;
static const WORD bgHiCyan= bgLoCyan|BACKGROUND_INTENSITY;
static const WORD bgHiMagenta= bgLoMagenta|BACKGROUND_INTENSITY;
static const WORD bgHiYellow= bgLoYellow|BACKGROUND_INTENSITY;
class csys_console{
public:
csys_console();
~csys_console();
void cls();
void color(WORD wColor);
void pause();
void cursor(DWORD dwSize,bool bVisible);
};
#endif
#include "sys_console.h"
csys_console::csys_console(){
}
csys_console::~csys_console(){
}
void csys_console::cls(){
COORD coordScreen = {0,0};
DWORD cCharsWritten=0;
CONSOLE_SCREEN_BUFFER_INFO csbiWindow;
DWORD dwConSize=0;
if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiWindow))std::cout<<"Error getting screen buffer info."<<std::endl;
dwConSize = csbiWindow.dwSize.X * csbiWindow.dwSize.Y;
if(!FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ))std::cout<<"Error clearing the console."<<std::endl;
if(!FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbiWindow.wAttributes,dwConSize, coordScreen, &cCharsWritten ))std::cout<<"Error clearing the console."<<std::endl;
if(!SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen ))std::cout<<"Error reseting the cursor position."<<std::endl;
}
void csys_console::color(WORD wColor){
if(!SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor))std::cout<<"Error setting new color."<<std::endl;
}
void csys_console::pause(){
std::cout<<"Press any key to continue."<<std::endl;
while(!_kbhit()){
Sleep(100);
}
_getch();
}
void csys_console::cursor(DWORD dwSize,bool bVisible){
_CONSOLE_CURSOR_INFO cCursor;
cCursor.dwSize=dwSize;
cCursor.bVisible=bVisible;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cCursor);
}
Any ideas how i could improve the code?
Note that the current code works.
Thx alot.
I have been coding for a while but i have pathetic conventions and style.
Here is a sample piece of code that i ahve and i was wondering if anyone got any ideas on how i could improve.Any critisims+ideas are welcome.
#ifndef sys_console
#define sys_console
#include <windows.h>
#include <iostream>
#include <conio.h>
static const WORD fgBlack= 0;
static const WORD fgLoRed= FOREGROUND_RED;
static const WORD fgLoGreen= FOREGROUND_GREEN;
static const WORD fgLoBlue= FOREGROUND_BLUE;
static const WORD fgLoCyan= fgLoGreen|fgLoBlue;
static const WORD fgLoMagenta= fgLoRed|fgLoBlue;
static const WORD fgLoYellow= fgLoRed|fgLoGreen;
static const WORD fgLoWhite= fgLoRed|fgLoGreen|fgLoBlue;
static const WORD fgGray= fgBlack|FOREGROUND_INTENSITY;
static const WORD fgHiWhite= fgLoWhite|FOREGROUND_INTENSITY;
static const WORD fgHiBlue= fgLoBlue|FOREGROUND_INTENSITY;
static const WORD fgHiGreen= fgLoGreen| FOREGROUND_INTENSITY;
static const WORD fgHiRed= fgLoRed|FOREGROUND_INTENSITY;
static const WORD fgHiCyan= fgLoCyan|FOREGROUND_INTENSITY;
static const WORD fgHiMagenta= fgLoMagenta|FOREGROUND_INTENSITY;
static const WORD fgHiYellow= fgLoYellow|FOREGROUND_INTENSITY;
static const WORD bgBlack= 0;
static const WORD bgLoRed= BACKGROUND_RED;
static const WORD bgLoGreen= BACKGROUND_GREEN;
static const WORD bgLoBlue= BACKGROUND_BLUE;
static const WORD bgLoCyan= bgLoGreen|bgLoBlue;
static const WORD bgLoMagenta= bgLoRed|bgLoBlue;
static const WORD bgLoYellow= bgLoRed|bgLoGreen;
static const WORD bgLoWhite= bgLoRed|bgLoGreen|bgLoBlue;
static const WORD bgGray= bgBlack|BACKGROUND_INTENSITY;
static const WORD bgHiWhite= bgLoWhite|BACKGROUND_INTENSITY;
static const WORD bgHiBlue= bgLoBlue|BACKGROUND_INTENSITY;
static const WORD bgHiGreen= bgLoGreen|BACKGROUND_INTENSITY;
static const WORD bgHiRed= bgLoRed|BACKGROUND_INTENSITY;
static const WORD bgHiCyan= bgLoCyan|BACKGROUND_INTENSITY;
static const WORD bgHiMagenta= bgLoMagenta|BACKGROUND_INTENSITY;
static const WORD bgHiYellow= bgLoYellow|BACKGROUND_INTENSITY;
class csys_console{
public:
csys_console();
~csys_console();
void cls();
void color(WORD wColor);
void pause();
void cursor(DWORD dwSize,bool bVisible);
};
#endif
#include "sys_console.h"
csys_console::csys_console(){
}
csys_console::~csys_console(){
}
void csys_console::cls(){
COORD coordScreen = {0,0};
DWORD cCharsWritten=0;
CONSOLE_SCREEN_BUFFER_INFO csbiWindow;
DWORD dwConSize=0;
if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiWindow))std::cout<<"Error getting screen buffer info."<<std::endl;
dwConSize = csbiWindow.dwSize.X * csbiWindow.dwSize.Y;
if(!FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ))std::cout<<"Error clearing the console."<<std::endl;
if(!FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbiWindow.wAttributes,dwConSize, coordScreen, &cCharsWritten ))std::cout<<"Error clearing the console."<<std::endl;
if(!SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen ))std::cout<<"Error reseting the cursor position."<<std::endl;
}
void csys_console::color(WORD wColor){
if(!SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor))std::cout<<"Error setting new color."<<std::endl;
}
void csys_console::pause(){
std::cout<<"Press any key to continue."<<std::endl;
while(!_kbhit()){
Sleep(100);
}
_getch();
}
void csys_console::cursor(DWORD dwSize,bool bVisible){
_CONSOLE_CURSOR_INFO cCursor;
cCursor.dwSize=dwSize;
cCursor.bVisible=bVisible;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cCursor);
}
Any ideas how i could improve the code?
Note that the current code works.
Thx alot.