Console display base
A. First Edition
These classes are all base classes for console displays.
How to display in console without using iostream class?
These classes are for our comp354 project which is going to make a little game like cross word.
E.Further improvement
F.File listing
1. tile.h
2. tile.cpp
3. tiles.h
4. tiles.cpp
5. token.h
6. token.cpp
7. tokens.h
8. tokens.cpp
9. RoverType.h
10. main.cpp (main)
file name: tile.h
#include "RoverType.h" class CTile { private: char ch; int ftColor; int bkColor; public: void display(int x, int y); void setFontColor(int theColor){ftColor=theColor;} void setBKGroundColor(int theColor){ bkColor=theColor;} void setValue(char myCh) { ch=myCh;} CTile(char myCh=0); };
file name: tile.cpp
#include "Tile.h" HANDLE hOutPut=0; CTile::CTile(char myCh) { ftColor=DefaultFontColor; bkColor=DefaultBKGroundColor; setValue(myCh); if (hOutPut==0) { hOutPut=GetStdHandle(STD_OUTPUT_HANDLE); } } void CTile::display(int x, int y) { COORD coord; coord.X=x; coord.Y=y; FillConsoleOutputAttribute(hOutPut, ftColor|bkColor, 1, coord, NULL); FillConsoleOutputCharacter(hOutPut, ch, 1, coord, NULL); }
file name: tiles.h
#include "tile.h" class CTiles { private: CTile* tiles; int len; int ftColor; int bkColor; void initialize(); public: CTiles(const char* str); CTiles(); ~CTiles(); void setFontColor(int theColor){ftColor=theColor;} void setBKGroundColor(int theColor){bkColor=theColor;} int getLength(){return len;} void display(int x, int y); void setValue(const char* str); };
file name: tiles.cpp
#include "tiles.h" CTiles::CTiles() { initialize(); } void CTiles::initialize() { len=0; tiles=NULL; ftColor=DefaultFontColor; bkColor=DefaultBKGroundColor; } CTiles::~CTiles() { delete[]tiles; } void CTiles::setValue(const char* str) { len=strlen(str); tiles=new CTile[len] ; for (int i=0; i<len; i++) { tiles[i].setValue(str[i]); } } CTiles::CTiles(const char* str) { initialize(); setValue(str); } void CTiles::display(int x, int y) { for (int i=0; i<len; i++) { tiles[i].display(x+i, y); } }
file name: token.h
#include "RoverType.h" enum Suit {Club=5, Diamond=4, Heart=3, Spade=6}; class CToken { private: WORD colors[4][4]; WORD ftColor; WORD bkColor; WORD frColor; char chRow[3][3]; void initialize(); public: CToken(int num); CToken(char ch='A'); void setValue(char myCh); void setNumber(int num); void setFrameColor(WORD theColor); void setFontColor(WORD theColor); void setBKGroundColor(WORD theColor); void setSuit(Suit theSuit); void display(int x, int y); };
file name: token.cpp
#include "token.h" #include "tile.h" extern HANDLE hOutPut; void CToken::initialize() { ftColor=DefaultFontColor; bkColor=DefaultBKGroundColor; frColor=DefaultFrameColor; for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { if (i<3&&j<3) { chRow[i][j]=0; colors[i][j]=ftColor|bkColor; } else { colors[i][j]=frColor; } } } if (hOutPut==0) { hOutPut=GetStdHandle(STD_OUTPUT_HANDLE); } } void CToken::setBKGroundColor(WORD theColor) { bkColor=theColor; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { colors[i][j]=ftColor|theColor; } } } void CToken::setFontColor(WORD theColor) { ftColor=theColor; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { colors[i][j]=bkColor|theColor; } } } void CToken::setSuit(Suit theSuit) { chRow[0][0]=chRow[2][2]=theSuit; } CToken::CToken(char ch) { initialize(); chRow[1][1]=ch; } void CToken::setNumber(int num) { //in no condition, this would happen if (num>99) { return ; } if (num>9) { chRow[1][0]=num/10+'0'; chRow[1][1]=num%10+'0'; } else { chRow[1][1]=num+'0'; } } CToken::CToken(int num) { initialize(); setNumber(num); } void CToken::setFrameColor(WORD theColor) { for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { if (i==3||j==3) { colors[i][j]=theColor; } } } } void CToken::setValue(char myCh) { chRow[1][1]=myCh; } /* void CToken::setDegree(WORD num) { for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { colors[i][j]=bkColor|num; } } } */ void CToken::display(int x, int y) { COORD coord; coord.X=x; coord.Y =y; for (int i=0; i<4; i++) { coord.Y=y+i; coord.X=x; WriteConsoleOutputAttribute(hOutPut, colors[i], 4, coord, NULL); //WriteConsoleOutputAttribute(hOutPut, &temp, 3, coord, NULL); if (i<3) { for (int j=0; j<3; j++) { coord.X=x+j; FillConsoleOutputCharacter(hOutPut, chRow[i][j], 1, coord, NULL); } } } }
file name: tokens.h
#include "token.h" class CTokens { private: CToken* tokens; int len; WORD ftColor; WORD bkColor; WORD frColor; void initialize(); public: CTokens(); CTokens(const char* str); void setValue(const char* str); void setFontColor(WORD theColor); void setBKGroundColor(WORD theColor); void setFrameColor(WORD theColor); ~CTokens(); void display(int x, int y); };
file name: tokens.cpp
#include "tokens.h" CTokens::CTokens() { initialize(); } CTokens::CTokens(const char* str) { initialize(); setValue(str); } void CTokens::initialize() { len=0; tokens=NULL; ftColor=DefaultFontColor; bkColor=DefaultBKGroundColor; frColor=DefaultFrameColor; } void CTokens::setBKGroundColor(WORD theColor) { for (int i=0; i<len; i++) { tokens[i].setBKGroundColor(theColor); } } void CTokens::setFontColor(WORD theColor) { for (int i=0; i<len; i++) { tokens[i].setFontColor(theColor); } } void CTokens::setFrameColor(WORD theColor) { for (int i=0; i<len; i++) { tokens[i].setFrameColor(theColor); } } void CTokens::setValue(const char* str) { len=strlen(str); tokens=new CToken[len]; for (int i=0; i<len; i++) { tokens[i].setValue(str[i]); } } void CTokens::display(int x, int y) { for (int i=0; i<len; i++) { tokens[i].display(x+i*4, y); } } CTokens::~CTokens() { delete [] tokens; }
file name: RoverType.h
#include <windows.h> //the font color #define FTBLUE FOREGROUND_BLUE #define FTGREEN FOREGROUND_GREEN #define FTRED FOREGROUND_RED #define FTPURPLE FOREGROUND_BLUE|FOREGROUND_RED #define FTGREY FOREGROUND_BLUE|FOREGROUND_GREEN #define FTBROWN FOREGROUND_RED|FOREGROUND_GREEN, #define FTWHITE FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED #define FTINTENSITY FOREGROUND_INTENSITY //the background color #define BKBLUE BACKGROUND_BLUE #define BKGREEN BACKGROUND_GREEN #define BKRED BACKGROUND_RED #define BKPURPLE BACKGROUND_BLUE|BACKGROUND_RED #define BKGREY BACKGROUND_BLUE|BACKGROUND_GREEN #define BKBROWN BACKGROUND_RED|BACKGROUND_GREEN #define BKWHITE BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_RED #define BKINTENSITY BACKGROUND_INTENSITY #define DefaultFontColor FTRED #define DefaultBKGroundColor BKWHITE #define DefaultFrameColor BKGREY
file name: main.cpp(main)
#include "tokens.h" #include "tiles.h" int main() { CTokens T("CTokens with FRAME!"); T.display(5,5); CTiles R("This is CTiles without frame!"); R.display(4, 10); return 0; }
The input is something like following:
Here is the result: