C语言 为什么我的游程编码算法不能正确地将内容绘制到控制台?

hivapdat  于 2023-02-15  发布在  其他
关注(0)|答案(2)|浏览(96)

所以我做了一个RLE实现来从矩阵中将字符绘制到控制台,但是由于某种原因,print_chars();函数似乎覆盖了应该显示的字符数量,我只在矩阵中设置了8个大于0的值,但是它打印出来的像是30。我相信这可能是由于分配了太多的内存,但是我不知道。任何帮助都是感激的。
代码:

#include <stdio.h>
#include <time.h>
#include <windows.h>

#define sizeX 150
#define sizeY 40

int grid[sizeY][sizeX];

void colorChar(int r, int g, int b){
    printf("\x1b[48;2;%d;%d;%dm",r,g,b);
}

void draw_screen(){
 int y, x;
 for(y=0;y<sizeY;y++){
  for(x=0;x<sizeX;x++){
    if(grid[y][x] == 0){
        colorChar(0,0,0);
    printf(" ");
   }
   else{
    colorChar(255,255,255);
    printf(" ");
   }
  }
  printf("\n");
 }
}

int main(void) {
 int x, y;
 float frameTime;
 grid[19][19] = 1;
 grid[2][8] = 1;
 grid[15][12] = 1;
 grid[6][3] = 1;
 grid[13][16] = 99;
 grid[17][3] = 1;
 grid[6][6] = 1;
 grid[9][3] = 1;

 clock_t start = clock();
 draw_screen();
 clock_t stop = clock();
 frameTime = (float)(stop - start) / CLOCKS_PER_SEC;
 printf("Frame: %f\n", frameTime);
}
myss37ts

myss37ts1#

我将对print_chars的调用替换为调试printf

void draw_screen(){
 int y, x;
 for(y=0;y<=sizeY;y++){
  for(x=0;x<=sizeX;x++){
   if(grid[y][x] == 0){
    counter0++;
    if(grid[y][x+1] != 0 || x+1 >= sizeX){
     printf("grid[%d][%d] == 0; grid[%d][%d+1] != 0 || %d+1 >= %d; ", y, x, y, x, x, sizeX);
     printf("x=%d; y=%d; counter0=%d; counter1=%d\n", x, y, counter0, counter1);
     //print_chars(counter0, ' ', "BLACK");
     counter0 = 0;
    }
   }
   else{
    counter1++;
    if(grid[y][x+1] != 1 || x+1 >= sizeX){
     printf("!(grid[%d][%d] == 0); grid[%d][%d+1] != 1 || %d+1 >= %d; ", y, x, y, x, x, sizeX);
     printf("x=%d; y=%d; counter0=%d; counter1=%d\n", x, y, counter0, counter1);
     //print_chars(counter1, ' ', "WHITE");
     counter1 = 0;
    }
   }
  }
  //printf("\n");
 }
}

这将输出:

grid[0][87] == 0; grid[0][87+1] != 0 || 87+1 >= 150; x=87; y=0; counter0=88; counter1=0
!(grid[0][88] == 0); grid[0][88+1] != 1 || 88+1 >= 150; x=88; y=0; counter0=0; counter1=1
grid[0][149] == 0; grid[0][149+1] != 0 || 149+1 >= 150; x=149; y=0; counter0=61; counter1=0
grid[0][150] == 0; grid[0][150+1] != 0 || 150+1 >= 150; x=150; y=0; counter0=1; counter1=0
grid[1][47] == 0; grid[1][47+1] != 0 || 47+1 >= 150; x=47; y=1; counter0=48; counter1=0
!(grid[1][48] == 0); grid[1][48+1] != 1 || 48+1 >= 150; x=48; y=1; counter0=0; counter1=1
grid[1][149] == 0; grid[1][149+1] != 0 || 149+1 >= 150; x=149; y=1; counter0=101; counter1=0
grid[1][150] == 0; grid[1][150+1] != 0 || 150+1 >= 150; x=150; y=1; counter0=1; counter1=0
grid[2][7] == 0; grid[2][7+1] != 0 || 7+1 >= 150; x=7; y=2; counter0=8; counter1=0
!(grid[2][8] == 0); grid[2][8+1] != 1 || 8+1 >= 150; x=8; y=2; counter0=0; counter1=1
grid[2][149] == 0; grid[2][149+1] != 0 || 149+1 >= 150; x=149; y=2; counter0=141; counter1=0
grid[2][150] == 0; grid[2][150+1] != 0 || 150+1 >= 150; x=150; y=2; counter0=1; counter1=0
grid[3][122] == 0; grid[3][122+1] != 0 || 122+1 >= 150; x=122; y=3; counter0=123; counter1=0
!(grid[3][123] == 0); grid[3][123+1] != 1 || 123+1 >= 150; x=123; y=3; counter0=0; counter1=1
grid[3][125] == 0; grid[3][125+1] != 0 || 125+1 >= 150; x=125; y=3; counter0=2; counter1=0
!(grid[3][126] == 0); grid[3][126+1] != 1 || 126+1 >= 150; x=126; y=3; counter0=0; counter1=1
grid[3][149] == 0; grid[3][149+1] != 0 || 149+1 >= 150; x=149; y=3; counter0=23; counter1=0
grid[3][150] == 0; grid[3][150+1] != 0 || 150+1 >= 150; x=150; y=3; counter0=1; counter1=0
grid[4][82] == 0; grid[4][82+1] != 0 || 82+1 >= 150; x=82; y=4; counter0=83; counter1=0
!(grid[4][83] == 0); grid[4][83+1] != 1 || 83+1 >= 150; x=83; y=4; counter0=0; counter1=1
grid[4][85] == 0; grid[4][85+1] != 0 || 85+1 >= 150; x=85; y=4; counter0=2; counter1=0
!(grid[4][86] == 0); grid[4][86+1] != 1 || 86+1 >= 150; x=86; y=4; counter0=0; counter1=1
grid[4][149] == 0; grid[4][149+1] != 0 || 149+1 >= 150; x=149; y=4; counter0=63; counter1=0
grid[4][150] == 0; grid[4][150+1] != 0 || 150+1 >= 150; x=150; y=4; counter0=1; counter1=0
grid[5][42] == 0; grid[5][42+1] != 0 || 42+1 >= 150; x=42; y=5; counter0=43; counter1=0
!(grid[5][43] == 0); grid[5][43+1] != 1 || 43+1 >= 150; x=43; y=5; counter0=0; counter1=1
grid[5][45] == 0; grid[5][45+1] != 0 || 45+1 >= 150; x=45; y=5; counter0=2; counter1=0
!(grid[5][46] == 0); grid[5][46+1] != 1 || 46+1 >= 150; x=46; y=5; counter0=0; counter1=1
grid[5][149] == 0; grid[5][149+1] != 0 || 149+1 >= 150; x=149; y=5; counter0=103; counter1=0
grid[5][150] == 0; grid[5][150+1] != 0 || 150+1 >= 150; x=150; y=5; counter0=1; counter1=0
grid[6][2] == 0; grid[6][2+1] != 0 || 2+1 >= 150; x=2; y=6; counter0=3; counter1=0
!(grid[6][3] == 0); grid[6][3+1] != 1 || 3+1 >= 150; x=3; y=6; counter0=0; counter1=1
grid[6][5] == 0; grid[6][5+1] != 0 || 5+1 >= 150; x=5; y=6; counter0=2; counter1=0
!(grid[6][6] == 0); grid[6][6+1] != 1 || 6+1 >= 150; x=6; y=6; counter0=0; counter1=1
grid[6][122] == 0; grid[6][122+1] != 0 || 122+1 >= 150; x=122; y=6; counter0=116; counter1=0
!(grid[6][123] == 0); grid[6][123+1] != 1 || 123+1 >= 150; x=123; y=6; counter0=0; counter1=1
grid[6][149] == 0; grid[6][149+1] != 0 || 149+1 >= 150; x=149; y=6; counter0=26; counter1=0
grid[6][150] == 0; grid[6][150+1] != 0 || 150+1 >= 150; x=150; y=6; counter0=1; counter1=0
grid[7][82] == 0; grid[7][82+1] != 0 || 82+1 >= 150; x=82; y=7; counter0=83; counter1=0
!(grid[7][83] == 0); grid[7][83+1] != 1 || 83+1 >= 150; x=83; y=7; counter0=0; counter1=1
grid[7][149] == 0; grid[7][149+1] != 0 || 149+1 >= 150; x=149; y=7; counter0=66; counter1=0
grid[7][150] == 0; grid[7][150+1] != 0 || 150+1 >= 150; x=150; y=7; counter0=1; counter1=0
grid[8][42] == 0; grid[8][42+1] != 0 || 42+1 >= 150; x=42; y=8; counter0=43; counter1=0
!(grid[8][43] == 0); grid[8][43+1] != 1 || 43+1 >= 150; x=43; y=8; counter0=0; counter1=1
grid[8][149] == 0; grid[8][149+1] != 0 || 149+1 >= 150; x=149; y=8; counter0=106; counter1=0
grid[8][150] == 0; grid[8][150+1] != 0 || 150+1 >= 150; x=150; y=8; counter0=1; counter1=0
grid[9][2] == 0; grid[9][2+1] != 0 || 2+1 >= 150; x=2; y=9; counter0=3; counter1=0
!(grid[9][3] == 0); grid[9][3+1] != 1 || 3+1 >= 150; x=3; y=9; counter0=0; counter1=1
grid[9][149] == 0; grid[9][149+1] != 0 || 149+1 >= 150; x=149; y=9; counter0=146; counter1=0
grid[9][150] == 0; grid[9][150+1] != 0 || 150+1 >= 150; x=150; y=9; counter0=1; counter1=0
grid[10][135] == 0; grid[10][135+1] != 0 || 135+1 >= 150; x=135; y=10; counter0=136; counter1=0
!(grid[10][136] == 0); grid[10][136+1] != 1 || 136+1 >= 150; x=136; y=10; counter0=0; counter1=1
grid[10][149] == 0; grid[10][149+1] != 0 || 149+1 >= 150; x=149; y=10; counter0=13; counter1=0
grid[10][150] == 0; grid[10][150+1] != 0 || 150+1 >= 150; x=150; y=10; counter0=1; counter1=0
grid[11][95] == 0; grid[11][95+1] != 0 || 95+1 >= 150; x=95; y=11; counter0=96; counter1=0
!(grid[11][96] == 0); grid[11][96+1] != 1 || 96+1 >= 150; x=96; y=11; counter0=0; counter1=1
grid[11][149] == 0; grid[11][149+1] != 0 || 149+1 >= 150; x=149; y=11; counter0=53; counter1=0
grid[11][150] == 0; grid[11][150+1] != 0 || 150+1 >= 150; x=150; y=11; counter0=1; counter1=0
grid[12][55] == 0; grid[12][55+1] != 0 || 55+1 >= 150; x=55; y=12; counter0=56; counter1=0
!(grid[12][56] == 0); grid[12][56+1] != 1 || 56+1 >= 150; x=56; y=12; counter0=0; counter1=1
grid[12][131] == 0; grid[12][131+1] != 0 || 131+1 >= 150; x=131; y=12; counter0=75; counter1=0
!(grid[12][132] == 0); grid[12][132+1] != 1 || 132+1 >= 150; x=132; y=12; counter0=0; counter1=1
grid[12][149] == 0; grid[12][149+1] != 0 || 149+1 >= 150; x=149; y=12; counter0=17; counter1=0
grid[12][150] == 0; grid[12][150+1] != 0 || 150+1 >= 150; x=150; y=12; counter0=1; counter1=0
grid[13][15] == 0; grid[13][15+1] != 0 || 15+1 >= 150; x=15; y=13; counter0=16; counter1=0
!(grid[13][16] == 0); grid[13][16+1] != 1 || 16+1 >= 150; x=16; y=13; counter0=0; counter1=1
grid[13][91] == 0; grid[13][91+1] != 0 || 91+1 >= 150; x=91; y=13; counter0=75; counter1=0
!(grid[13][92] == 0); grid[13][92+1] != 1 || 92+1 >= 150; x=92; y=13; counter0=0; counter1=1
grid[13][149] == 0; grid[13][149+1] != 0 || 149+1 >= 150; x=149; y=13; counter0=57; counter1=0
grid[13][150] == 0; grid[13][150+1] != 0 || 150+1 >= 150; x=150; y=13; counter0=1; counter1=0
grid[14][51] == 0; grid[14][51+1] != 0 || 51+1 >= 150; x=51; y=14; counter0=52; counter1=0
!(grid[14][52] == 0); grid[14][52+1] != 1 || 52+1 >= 150; x=52; y=14; counter0=0; counter1=1
grid[14][122] == 0; grid[14][122+1] != 0 || 122+1 >= 150; x=122; y=14; counter0=70; counter1=0
!(grid[14][123] == 0); grid[14][123+1] != 1 || 123+1 >= 150; x=123; y=14; counter0=0; counter1=1
grid[14][149] == 0; grid[14][149+1] != 0 || 149+1 >= 150; x=149; y=14; counter0=26; counter1=0
grid[14][150] == 0; grid[14][150+1] != 0 || 150+1 >= 150; x=150; y=14; counter0=1; counter1=0
grid[15][11] == 0; grid[15][11+1] != 0 || 11+1 >= 150; x=11; y=15; counter0=12; counter1=0
!(grid[15][12] == 0); grid[15][12+1] != 1 || 12+1 >= 150; x=12; y=15; counter0=0; counter1=1
grid[15][82] == 0; grid[15][82+1] != 0 || 82+1 >= 150; x=82; y=15; counter0=70; counter1=0
!(grid[15][83] == 0); grid[15][83+1] != 1 || 83+1 >= 150; x=83; y=15; counter0=0; counter1=1
grid[15][149] == 0; grid[15][149+1] != 0 || 149+1 >= 150; x=149; y=15; counter0=66; counter1=0
grid[15][150] == 0; grid[15][150+1] != 0 || 150+1 >= 150; x=150; y=15; counter0=1; counter1=0
grid[16][42] == 0; grid[16][42+1] != 0 || 42+1 >= 150; x=42; y=16; counter0=43; counter1=0
!(grid[16][43] == 0); grid[16][43+1] != 1 || 43+1 >= 150; x=43; y=16; counter0=0; counter1=1
grid[16][138] == 0; grid[16][138+1] != 0 || 138+1 >= 150; x=138; y=16; counter0=95; counter1=0
!(grid[16][139] == 0); grid[16][139+1] != 1 || 139+1 >= 150; x=139; y=16; counter0=0; counter1=1
grid[16][149] == 0; grid[16][149+1] != 0 || 149+1 >= 150; x=149; y=16; counter0=10; counter1=0
grid[16][150] == 0; grid[16][150+1] != 0 || 150+1 >= 150; x=150; y=16; counter0=1; counter1=0
grid[17][2] == 0; grid[17][2+1] != 0 || 2+1 >= 150; x=2; y=17; counter0=3; counter1=0
!(grid[17][3] == 0); grid[17][3+1] != 1 || 3+1 >= 150; x=3; y=17; counter0=0; counter1=1
grid[17][98] == 0; grid[17][98+1] != 0 || 98+1 >= 150; x=98; y=17; counter0=95; counter1=0
!(grid[17][99] == 0); grid[17][99+1] != 1 || 99+1 >= 150; x=99; y=17; counter0=0; counter1=1
grid[17][149] == 0; grid[17][149+1] != 0 || 149+1 >= 150; x=149; y=17; counter0=50; counter1=0
grid[17][150] == 0; grid[17][150+1] != 0 || 150+1 >= 150; x=150; y=17; counter0=1; counter1=0
grid[18][58] == 0; grid[18][58+1] != 0 || 58+1 >= 150; x=58; y=18; counter0=59; counter1=0
!(grid[18][59] == 0); grid[18][59+1] != 1 || 59+1 >= 150; x=59; y=18; counter0=0; counter1=1
grid[18][149] == 0; grid[18][149+1] != 0 || 149+1 >= 150; x=149; y=18; counter0=90; counter1=0
grid[18][150] == 0; grid[18][150+1] != 0 || 150+1 >= 150; x=150; y=18; counter0=1; counter1=0
grid[19][18] == 0; grid[19][18+1] != 0 || 18+1 >= 150; x=18; y=19; counter0=19; counter1=0
!(grid[19][19] == 0); grid[19][19+1] != 1 || 19+1 >= 150; x=19; y=19; counter0=0; counter1=1
grid[19][149] == 0; grid[19][149+1] != 0 || 149+1 >= 150; x=149; y=19; counter0=130; counter1=0
grid[19][150] == 0; grid[19][150+1] != 0 || 150+1 >= 150; x=150; y=19; counter0=1; counter1=0
grid[20][149] == 0; grid[20][149+1] != 0 || 149+1 >= 150; x=149; y=20; counter0=150; counter1=0
grid[20][150] == 0; grid[20][150+1] != 0 || 150+1 >= 150; x=150; y=20; counter0=1; counter1=0
grid[21][149] == 0; grid[21][149+1] != 0 || 149+1 >= 150; x=149; y=21; counter0=150; counter1=0
grid[21][150] == 0; grid[21][150+1] != 0 || 150+1 >= 150; x=150; y=21; counter0=1; counter1=0
grid[22][149] == 0; grid[22][149+1] != 0 || 149+1 >= 150; x=149; y=22; counter0=150; counter1=0
grid[22][150] == 0; grid[22][150+1] != 0 || 150+1 >= 150; x=150; y=22; counter0=1; counter1=0
grid[23][149] == 0; grid[23][149+1] != 0 || 149+1 >= 150; x=149; y=23; counter0=150; counter1=0
grid[23][150] == 0; grid[23][150+1] != 0 || 150+1 >= 150; x=150; y=23; counter0=1; counter1=0
grid[24][149] == 0; grid[24][149+1] != 0 || 149+1 >= 150; x=149; y=24; counter0=150; counter1=0
grid[24][150] == 0; grid[24][150+1] != 0 || 150+1 >= 150; x=150; y=24; counter0=1; counter1=0
grid[25][149] == 0; grid[25][149+1] != 0 || 149+1 >= 150; x=149; y=25; counter0=150; counter1=0
grid[25][150] == 0; grid[25][150+1] != 0 || 150+1 >= 150; x=150; y=25; counter0=1; counter1=0
grid[26][149] == 0; grid[26][149+1] != 0 || 149+1 >= 150; x=149; y=26; counter0=150; counter1=0
grid[26][150] == 0; grid[26][150+1] != 0 || 150+1 >= 150; x=150; y=26; counter0=1; counter1=0
grid[27][149] == 0; grid[27][149+1] != 0 || 149+1 >= 150; x=149; y=27; counter0=150; counter1=0
grid[27][150] == 0; grid[27][150+1] != 0 || 150+1 >= 150; x=150; y=27; counter0=1; counter1=0
grid[28][149] == 0; grid[28][149+1] != 0 || 149+1 >= 150; x=149; y=28; counter0=150; counter1=0
grid[28][150] == 0; grid[28][150+1] != 0 || 150+1 >= 150; x=150; y=28; counter0=1; counter1=0
grid[29][149] == 0; grid[29][149+1] != 0 || 149+1 >= 150; x=149; y=29; counter0=150; counter1=0
grid[29][150] == 0; grid[29][150+1] != 0 || 150+1 >= 150; x=150; y=29; counter0=1; counter1=0
grid[30][149] == 0; grid[30][149+1] != 0 || 149+1 >= 150; x=149; y=30; counter0=150; counter1=0
grid[30][150] == 0; grid[30][150+1] != 0 || 150+1 >= 150; x=150; y=30; counter0=1; counter1=0
grid[31][149] == 0; grid[31][149+1] != 0 || 149+1 >= 150; x=149; y=31; counter0=150; counter1=0
grid[31][150] == 0; grid[31][150+1] != 0 || 150+1 >= 150; x=150; y=31; counter0=1; counter1=0
grid[32][149] == 0; grid[32][149+1] != 0 || 149+1 >= 150; x=149; y=32; counter0=150; counter1=0
grid[32][150] == 0; grid[32][150+1] != 0 || 150+1 >= 150; x=150; y=32; counter0=1; counter1=0
grid[33][149] == 0; grid[33][149+1] != 0 || 149+1 >= 150; x=149; y=33; counter0=150; counter1=0
grid[33][150] == 0; grid[33][150+1] != 0 || 150+1 >= 150; x=150; y=33; counter0=1; counter1=0
grid[34][149] == 0; grid[34][149+1] != 0 || 149+1 >= 150; x=149; y=34; counter0=150; counter1=0
grid[34][150] == 0; grid[34][150+1] != 0 || 150+1 >= 150; x=150; y=34; counter0=1; counter1=0
grid[35][149] == 0; grid[35][149+1] != 0 || 149+1 >= 150; x=149; y=35; counter0=150; counter1=0
grid[35][150] == 0; grid[35][150+1] != 0 || 150+1 >= 150; x=150; y=35; counter0=1; counter1=0
grid[36][149] == 0; grid[36][149+1] != 0 || 149+1 >= 150; x=149; y=36; counter0=150; counter1=0
grid[36][150] == 0; grid[36][150+1] != 0 || 150+1 >= 150; x=150; y=36; counter0=1; counter1=0
grid[37][149] == 0; grid[37][149+1] != 0 || 149+1 >= 150; x=149; y=37; counter0=150; counter1=0
grid[37][150] == 0; grid[37][150+1] != 0 || 150+1 >= 150; x=150; y=37; counter0=1; counter1=0
grid[38][149] == 0; grid[38][149+1] != 0 || 149+1 >= 150; x=149; y=38; counter0=150; counter1=0
grid[38][150] == 0; grid[38][150+1] != 0 || 150+1 >= 150; x=150; y=38; counter0=1; counter1=0
grid[39][149] == 0; grid[39][149+1] != 0 || 149+1 >= 150; x=149; y=39; counter0=150; counter1=0
grid[39][150] == 0; grid[39][150+1] != 0 || 150+1 >= 150; x=150; y=39; counter0=1; counter1=0
grid[40][149] == 0; grid[40][149+1] != 0 || 149+1 >= 150; x=149; y=40; counter0=150; counter1=0
grid[40][150] == 0; grid[40][150+1] != 0 || 150+1 >= 150; x=150; y=40; counter0=1; counter1=0
Frame: 0.004000

简而言之,if语句中的条件被满足了 * this * 多次,所以函数被调用了 * that * 多次,打印了 * that * 许多字符串。
我的建议是重新思考并简化你的逻辑。我真的不能给出任何额外的建议,因为我真的不明白你想要达到什么目的。我不知道你期望print_chars被调用多少次。如果你希望它只在grid[y][x] > 0时被调用,那么为什么不把它用作if条件呢?变量counter0counter1counter2对我来说真的很混乱。
补充意见:

  • 如果您从未通过SetStdHandle()修改过标准输出句柄,那么您可以"缓存" GetStdHandle(STD_OUTPUT_HANDLE)的返回值,而不是反复调用. HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);或类似函数。
  • 你真的不应该像这样比较字符串。像"RED"这样的原始字符串在双等于比较中会"衰减"成一个指向该字符串的指针。正如**@Some Programmer Dude**所说,这个函数起作用的唯一原因是你调用这个函数时使用的字符串是byte-wise与要与之比较的字符串常量相同,并且编译器足够聪明,能够将两个示例解析为相同的(很可能是只读的)内存地址。在我看来,你要处理这个问题的方式,最好是使用enum color { GREEN, RED, BLUE, WHITE, BLACK, YELLOW };或类似的东西。
  • 当你使用像if(color == "GREEN") { ... } else if(color == "RED") { ...这样的if-else-if链时,你可能会找到一种方法来使用一个开关大小写的结构来代替,但是不要感到有压力,这主要是为了风格。
  • 当你malloc你的缓冲区,你应该做malloc(sizeof(char)*(size + 1))。这不是严格必要的,因为sizeof(char)将永远是1,除非在 * 非常奇怪 * 的情况下,但我提到它,因为你指定sizeof(char)
  • 如果不介意升级到C99,可以将声明int y, x;内联到for循环中,如for(int y=0; ...for(int x=0; ...
  • 符号sizeXsizeY是"宏",在编译时被扩展。您应该考虑将grid声明为int grid[sizeX][sizeY];,以便更好地使用这些宏。还可以考虑将它们重命名为SIZE_XSIZE_Y-似乎大多数C/C ++程序员都将宏设置为CAPSLOCKED_WITH_UNDERSCORES,以便更好地将它们与标识符区分开来。
  • 如果在cmd下执行color /?,它会显示对应于cmd调色板中颜色的十六进制代码。您可以插入一个两位数的十六进制代码(就像color命令一样!)转换为SetConsoleTextAttribute,就像SetConsoleTextAttribute(conout_handle, 0xA0);一样,可以在黑色背景上获得明亮的绿色前景。还要注意的是,更现代的cmd版本并不 * 真的 * 受16-您也可以使用虚拟终端转义序列来指定颜色,并且\e[38;2;r;g;bm\e[48;2;r;g;bm转义支持任意的r,g,b三元组。
  • 提醒你一句:对输出格式化文本的问题越深入,混合C标准库函数和WinApi函数的问题就越大。特别是当Unicode支持进入画面时。我不希望你遇到任何可怕的问题,只是像这样弄乱颜色,但要注意如果你开始做 * 一堆 *非标准的东西,那么你的C标准库函数可能会不高兴。
qacovj5a

qacovj5a2#

我相信我已经解决了这个问题,你可以分层打印屏幕,而不是打印所有的东西,我们使用goTo()函数去任何需要打印值的地方,然后在那里运行算法,这样就避免了打印所有的0。
代码:

#include <stdio.h>
#include <time.h>
#include <windows.h>

#define sizeX 100
#define sizeY 20

int counter;

int grid[sizeY][sizeX];

void goTo(int x,int y){printf("%c[%d;%df",0x1B,y,x);}

void colorChar(int r, int g, int b){
    printf("\x1b[48;2;%d;%d;%dm",r,g,b);
}

void print_chars(int size, char character, int r,int g, int b){ //Prints x number of spaces
  unsigned char* spaces = malloc(sizeof(char)*size + 1);
  memset (spaces,character,size);
  spaces[size] = '\0';
  //Colour options
  colorChar(r,g,b);
  fputs(spaces, stdout);
  free(spaces);
}

void draw_screen(int num, char character, int r, int g, int b){
 int y, x;
 for(y=0;y<sizeY;y++){
  for(x=0;x<sizeX;x++){
   if(grid[y][x] == num){
    goTo(x,y); //Goes to the coordenates with the selected number
    colorChar(r,g,b); //Changes colour
    //RLE alorithm begins
    if(grid[y][x+1] == num){
        counter++; //Adds to the number of vals to be printed
    }
    else{
     print_chars(counter, character,r,g,b); //Draws the character
     counter = 1; //There is a 1 instead of 0, this ensures that single characters are always printed
                }
   //RLE algoritm ends
   }
  }
  printf("\n");
 }
}

int main(void) {
 int x, y;
 float frameTime;
 
 grid[19][19] = 1;
 grid[19][20] = 1;
 grid[2][8] = 1;
 grid[15][12] = 1;
 grid[6][3] = 1;
 grid[13][16] = 99;
 grid[17][3] = 1;
 grid[6][6] = 1;
 grid[9][3] = 1;
 grid[9][89] = 1;

 clock_t start = clock();
 draw_screen(1,'@', 0,0,255); //Calls the function for all the 1's
 draw_screen(99,'@', 255,255,255); //Calls the function for the 99
 clock_t stop = clock();
 frameTime = (float)(stop - start) / CLOCKS_PER_SEC;
 printf("Frame: %f\n", frameTime);
}

相关问题