Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sstream>
- #include <windows.h>
- #include <math.h>
- // For file loading
- #include<iostream>
- #include<fstream>
- #ifndef DISPLAY_H_INCLUDED
- #define DISPLAY_H_INCLUDED
- // Define namespace for blocks.
- namespace DisplayBlocks
- {
- // Define screen parameters
- int SCR_WIDTH = 80;
- int SCR_HEIGHT = 25;
- int SCR_POS_X = 0;
- int SCR_POS_Y = 0;
- short SCR_COLOR = 0xF0;
- const short textColors[20]
- {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
- 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
- 0x0C, 0x0D, 0x0E, 0x0F
- };
- const short backColors[20]
- {
- 0x00, 0x10, 0x20, 0x30, 0x40, 0x50,
- 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0,
- 0xC0, 0xD0, 0xE0, 0xF0
- };
- namespace colors {
- int black = 0;
- int blue = 1;
- int green = 2;
- int cyan = 3;
- int red = 4;
- int purple = 5;
- int brown = 6;
- int lightGray = 7;
- int gray = 8;
- int lightBlue = 9;
- int lime = 10;
- int neonBlue = 11;
- int neonRed = 12;
- int neonPurple = 13;
- int yellow = 14;
- int white = 15;
- };
- // Screen buffer events
- void setResolution(int w, int h)
- {
- SCR_WIDTH = w;
- SCR_HEIGHT = h;
- // C++ stupidly long and only way to convert an int to a string.
- std::ostringstream stream;
- stream << SCR_WIDTH;
- std::string STR_WIDTH = stream.str();
- stream.str("");
- stream.clear();
- stream << SCR_HEIGHT;
- std::string STR_HEIGHT = stream.str();
- //Resize CMD
- system( ("mode " + STR_WIDTH + "," + STR_HEIGHT).c_str());
- };
- void setScreenPos(int x, int y)
- {
- SCR_POS_X = x-1;
- SCR_POS_Y = y-1;
- COORD pos = {SCR_POS_X, SCR_POS_Y};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- }
- int getScreenX(){
- return SCR_POS_X;
- };
- int getScreenY(){
- return SCR_POS_Y;
- };
- // Pixel manipulation
- void setColors(int textcolor, int backcolor){
- // Usually used in unison with the Colors namespace.
- // Takes 2 bytes and adds them together, Back being MSB, and Text being LSB.
- SCR_COLOR = backColors[backcolor] + textColors[textcolor];
- // Set console color
- SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ), SCR_COLOR);
- };
- void drawPixel(int x, int y){
- COORD pos = {x-1, y-1};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- std::cout << " ";
- // Load old pos.
- COORD dpos = {SCR_POS_X, SCR_POS_Y};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- };
- void write(std::string text){
- std::cout << text;
- };
- /*
- std::string loadPixelStringFrom(std::string file)
- {
- std::ifstream input( (char*) &file[0] );
- std::string online;
- int next = 0;
- std::string pixels = "";
- while( std::getline( input, online ) ) {
- // Convert string into array of chars
- for(int c = 0; c < online.length(); c++)
- {
- pixels += online[c];
- }
- }
- input.close();
- return pixels;
- }
- */
- void drawPixelsFrom(std::string file)
- {
- std::ifstream input( (char*) &file[0] );
- std::string online;
- int dx = SCR_POS_X;
- int dy = SCR_POS_Y;
- while( std::getline( input, online ) ) {
- COORD pos = {dx, dy};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- std::cout << online;
- dy++;
- }
- input.close();
- COORD dpos = {SCR_POS_X, SCR_POS_Y};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), dpos);
- }
- std::string * getStringMapFrom(std::string file)
- {
- std::ifstream inputLookup( (char*) &file[0] );
- std::string online;
- int dx = SCR_POS_X;
- int dy = SCR_POS_Y;
- int next = 0;
- // Count size.
- while( std::getline( inputLookup, online ) ) {
- next++;
- }
- inputLookup.close();
- // Insert data into pixel map.
- std::ifstream inputRead( (char*) &file[0] );
- std::string* pixs = new std::string[next];
- next = 0;
- while( std::getline( inputRead, online ) ) {
- pixs[next] = online;
- next++;
- }
- inputRead.close();
- return pixs;
- }
- /*
- void drawPixelArray(char[] arr)
- {
- int dx = SCR_POS_X;
- int dy = SCR_POS_Y;
- for(int x = 0; x < sizeof(arr); x++)
- {
- if(x > SCR_WIDTH)
- {
- dy = ( x / SCR_WIDTH );
- dx = ( x - (SCR_WIDTH*dy) );
- }
- else
- {
- dx = x;
- dy = 1;
- }
- COORD pos = {dx, dy};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- std::cout << arr[x];
- }
- }
- */
- void drawPixelString(std::string arr)
- {
- std::string Line;
- int dx = SCR_POS_X;
- int dy = SCR_POS_Y;
- int lines = arr.length() / SCR_WIDTH;
- if(lines < 2)
- {
- for(int x = 0; x < sizeof(arr); x++)
- {
- Line += arr[x];
- }
- COORD pos = {dx, dy};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- std::cout << Line;
- }
- else
- {
- for(int y = 0; y < lines; y++)
- {
- Line = "";
- for(int x = 0; x < SCR_WIDTH; x++)
- {
- Line += arr[x];
- }
- COORD pos = {dx, y+dy};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- std::cout << Line;
- }
- }
- COORD dpos = {SCR_POS_X, SCR_POS_Y};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), dpos);
- }
- char readStringMap(std::string * cmap, int x, int y)
- {
- // Equal to string "text", where "text" is the value of the memory location of cmap plus y byte(s).
- // Then a simple get char from the string at x-1.
- return ((std::string&) *(cmap + y-1))[x-1];
- }
- void clearScreen() {
- std::cout << std::string((SCR_WIDTH*SCR_HEIGHT),'\n');
- };
- }
- #endif // DISPLAY_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement