制作控制台应用程序时屏幕上没有显示任何内容

问题描述 投票:0回答:1

我一直在尝试练习一些c++项目。我试图从 FPS 项目运行这么多,但屏幕上没有显示任何内容

#include <iostream>

using namespace std;

#include <Windows.h>
int nScreenWidth = 120;
int nScreenHeight = 40;
float fPlayerX = 0.0f;
float fPlayerY = 0.0f;
float fPlayerA = 0.0f;
int nMapHeight = 16;
int nMapWidth = 16;
float fFOV = 3.14159 / 4.0;
float fDepth = 16.0f;

int main() {
    // creat screen buffer
    wchar_t *screen = new wchar_t[nScreenWidth * nScreenHeight];

    HANDLE hConsole = CreateConsoleScreenBuffer(
        GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);

    SetConsoleActiveScreenBuffer(hConsole);

    DWORD dwBytesWritten = 0;

    wstring map;
    map += L"################";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"#..............#";
    map += L"################";

    // the game loop
    while (1) {
        for (int x = 0; x < nScreenWidth; x++) {
            // for each column, calculate the projected ray angle into world
            // space

            float fRayAngle = (fPlayerA - fFOV / 2.0f) +
                              ((float)x / (float)nScreenWidth) * fFOV;

            float fDistanceToWall = 0;

            bool bHitWall = false;

            float fEyeX =
                sinf(fRayAngle);  // unit vector for ray in player space

            float fEyeY = cosf(fRayAngle);

            while (!bHitWall && fDistanceToWall < fDepth) {
                fDistanceToWall += 0.1f;
                int nTestX = (int)(fPlayerX + fEyeX * fDistanceToWall);
                int nTestY = (int)(fPlayerY + fEyeY * fDistanceToWall);

                // test if ray is out of bounds
                if (nTestX < 0 || nTestX >= nMapWidth || nTestY < 0 ||
                    nTestY >= nMapHeight) {
                    bHitWall = true;  // just set distance to maaximum depth
                    fDistanceToWall = fDepth;
                } else {
                    // ray is inbounds so test to see if the ray cell is awall
                    // block
                    if (map[nTestY * nMapWidth + nTestX] == '#') {
                        bHitWall = true;
                    }
                }
            }

            // clculate distance to ceiling and floor
            int nCeiling = (float)(nScreenHeight / 2.0) -
                           nScreenHeight / ((float)fDistanceToWall);
            int nFloor = nScreenHeight - nCeiling;

            for (int y = 0; y < nScreenHeight; y++) {
                if (y < nCeiling)
                    screen[y * nScreenWidth + x] = ' ';
                else if (y > nCeiling && y <= nFloor)
                    screen[y * nScreenWidth + x] = '#';
                else
                    screen[y * nScreenWidth + x] = ' ';
            }
        }

        screen[nScreenWidth * nScreenHeight - 1] = '\0';

        ReadConsoleOutputCharacter(hConsole, screen,
                                   nScreenWidth * nScreenHeight, {0, 0},
                                   &dwBytesWritten);
    }

    return 0;
}

我尝试更改文本的颜色和大小以查看是否打印出来,但没有任何变化。w

c++ console
1个回答
0
投票

您在控制台上看不到任何内容,因为您一开始就没有向控制台“写入”任何内容。您需要使用 WriteConsoleOutputCharacter() 而不是

ReadConsoleOutputCharacter()
将数据写入活动屏幕缓冲区。
    

© www.soinside.com 2019 - 2024. All rights reserved.