c - 用正方形填充帧缓冲区

标签 c arrays framebuffer

我正在尝试用 ASCII 艺术方 block 填充无符号字符数组,然后将其打印在屏幕上。目前这是我的代码:

#include <stdio.h>
#define W 80
#define H 23

static int clear(unsigned char *p, int lim)
{
        int i;

        for (i = 0; i < lim; i++)
                p[i] = ' ';
        return i;
}

static int square(unsigned char *p, int x, int y, int wh)
{
        int i, j;

        for (i = y; i < wh; i++) {
                for (j = x; j < wh; j++)
                        p[j] = '@';
                p[i] = '\n';
        }

        return i - j;
}

int main(void)
{
        unsigned char buf[W * H];
        clear(buf, sizeof buf);
        square(buf, 1, 2, 3);
        fwrite(buf, 1, sizeof buf, stdout);

        return 0;
}

此代码在编译时将导致不打印任何内容、无法正确打印等。

这是我想要的代码的结果:

  @@@
  @@@
  @@@

感谢任何帮助,因为我不明白如何填充帧缓冲区然后将其打印到屏幕上。谢谢。

最佳答案

试试这个

static void point(unsigned char *p, int x, int y, unsigned char ch){
    if(x < W && y < H)
        p[W*y + x] = ch;
}

static void square(unsigned char *p, int x, int y, int wh){
    int i, j;

    for (i = y; i < y + wh; i++) {
        for (j = x; j < x + wh; j++)
            point(p, j, i, '@');
    }
}

关于c - 用正方形填充帧缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28680837/

相关文章:

c - 奇怪的 while 循环。困惑

c - 中间舍入

c - 在 C 中移动多维数组指针 - 不兼容的类型

python - 无序比较两个 NumPy 数组

c - 如何将此 C 日历代码转换为 Objective-C 语法并使其与矩阵一起使用

javascript - 从索引数组中重新排序 immutable.js 列表

javascript - WEBGL-FBO : Why is the DEPTH_COMPONENT texture blank after rendercall

c++ - 使用帧缓冲区缩放纹理

linux - 在 Linux 上使用帧缓冲设备作为普通显示器(USB 转 HDMI 适配器)

java - 识别数组中未使用的字母