Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <windows.h>
- #define SETCOLOUR(a,b) a<<4|b
- /**
- * A programme demonstrating the use of colour nybbles for Windows
- * Tested using Code::Blocks and Windows 7 Professional SP1
- *
- * @Author: Shaun B
- * @Version: 1.0.0.3 - 2012-09-12
- * @Todo:
- * @Changes: Refactored with varibale names that might make sense
- * Found an odd bug with the release build, which is now
- * fixed.
- *
- **/
- int main();
- void gotoxy(int, int);
- void hideCursor();
- /// Sets variables for:
- /// Running boolean -
- static bool run = true;
- /// Reusable stuff -
- static unsigned char index[2] = "";
- static unsigned char count = 0;
- static unsigned char xAxis = 0;
- static unsigned char yAxis = 0;
- static unsigned char character [2] = "";
- int main()
- {
- // Sets default console size, colours and title:
- system("mode CON: COLS=80 LINES=25");
- system("cls");
- system("title Demonstrating the use of colour nybbles Donkeysoft MMXII");
- system("echo off");
- HANDLE hConsole;
- // Switches off blinking cursor:
- hConsole= GetStdHandle(STD_OUTPUT_HANDLE);
- hideCursor();
- // Sets cursor position for each scrolly text:
- gotoxy(0, 0);
- printf("For console applications, the colours are defined in a byte, so f0 would"
- "\ngive you white background and black text.\n");
- printf("Enter a byte in hex (00 - ff) to see what happens. Space to exit programme.\n");
- xAxis= 4;
- yAxis= 3;
- printf("C:\\>");
- // Main loop:
- while(run)
- {
- gotoxy(xAxis, yAxis);
- printf("²");
- // Scans keyboard into buffer:
- character[count] = _getch();
- if(character[count] == 32)
- {
- run=false;
- }
- else
- if(character[count] >= 48 && character[count] <= 57 && count < 2)
- {
- gotoxy(xAxis, yAxis);
- printf("%c", character[count]);
- index[count] = character[count]-48;
- xAxis++;
- count++;
- }
- else
- if(character[count] >= 97 && character[count] <= 102 && count < 2)
- {
- gotoxy(xAxis, yAxis);
- printf("%c", character[count]);
- index[count] = character[count]-87;
- xAxis++;
- count++;
- }
- else
- if(character[count] >= 65 && character[count] <= 70 && count < 2)
- {
- gotoxy(xAxis, yAxis);
- printf("%c", character[count]);
- index[count] = character[count]-55;
- xAxis++;
- count++;
- }
- else
- if(character[count] == 13 && count == 2)
- {
- gotoxy(xAxis-2, yAxis);
- printf(" ");
- unsigned char kCol = (unsigned char) SETCOLOUR(index[0], index[1]);
- SetConsoleTextAttribute(hConsole, kCol);
- printf("\nNew colour set to %3d!", kCol);
- xAxis = 4;
- count = 0;
- kCol = 0;
- index[2] = '\0';
- character[2] = '\0';
- gotoxy(0, yAxis);
- printf("C:\\>");
- if(!run)
- {
- run = true;
- }
- }
- else
- {
- character[count] = '\0';
- }
- }
- system("color f0");
- system("cls");
- printf("Cheers!");
- // Returns when loop is exited:
- return 0;
- }
- // Sets co-ordinates within the command line:
- void gotoxy(int x, int y)
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- // Turns off blinking cursor:
- void hideCursor()
- {
- CONSOLE_CURSOR_INFO lpCursor;
- lpCursor.bVisible = 0;
- lpCursor.dwSize = 20;
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&lpCursor);
- }
- /**
- * Old function:
- *unsigned char setColour(unsigned char a, unsigned char b)
- *{
- * if(i[0]!=a && i[1]!=b)
- * {
- * // Just in case there's an odd error...
- * return -1;
- * }
- * else
- * {
- * // Bitwise shift a four bits and then 'or' with b
- * return (a<<4)|b;
- * }
- *}
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement