我是按照一个孤独的编码器视频渲染到控制台,我写了他们正在使用的代码。
我无法让控制台实际显示任何内容,但在尝试在Visual Studio上使用ctrl + f5时,我得到了一个调试窗口,显示“exit with code -1073741819”。
这是我目前拥有的代码:
#include <iostream>
#include <Windows.h>
#include <cmath>
#include<chrono>
using namespace std;
int nscreenwidth = 120;
int nscreenheight = 40;
float fPlayerX = 8.0f;
float fPlayerY = 8.0f;
float fPlayerA = 0.0f;
int nMapHeight = 16;
int nMapWidth = 16;
float fFOV = 3.1415 / 4.0;
int main() {
wchar_t* screen = new wchar_t[nscreenheight * nscreenwidth];
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"#..............#";
map += L"################";
auto tp1 = chrono::system_clock::now();
auto tp2 = chrono::system_clock::now();
while (1) {
tp2 = chrono::system_clock::now();
chrono::duration<float> elapsedtime = tp2 - tp1;
tp1 = tp2;
float fElapsedTime = elapsedtime.count();
if (GetAsyncKeyState((unsigned short)'A') & 0x8000)
fPlayerA -= (0.1f * fElapsedTime);
if (GetAsyncKeyState((unsigned short)'D') & 0x8000)
fPlayerA += (0.1f * fElapsedTime);
for (int x = 0; x < nscreenwidth; x++) {
float fDistanceWall = 0;
float fOffsetfov = (fPlayerA - fFOV / 2.0) + (((float)x / (float)nscreenwidth) * fFOV);
float fUnitVecX = sinf(fOffsetfov);
float fUnitVecy = cosf(fOffsetfov);
bool bHitWall = false;
while (!bHitWall && fDistanceWall < 16.0) {
fDistanceWall += 0.1;
int nTestX = (int)(fPlayerX + (fUnitVecX * fDistanceWall));
int nTestY = (int)(fPlayerY + (fUnitVecy * fDistanceWall));
if (nTestX < 0 || nTestX >= nMapWidth || nTestY >= nMapHeight) {
bHitWall = true;
fDistanceWall = 16.0f;
}
else if (map[nTestX * nMapWidth + nTestY] == '#') {
bHitWall = true;
}
}
int nCeiling = (float)(nscreenheight / 2.0) - nscreenheight / ((float)fDistanceWall);
int nFloor = nscreenheight - nCeiling;
for (int y = 0; y < nscreenheight; y++) {
if (y > nCeiling) {
screen[x * nscreenwidth + y] = ' ';
}
else if (y < nFloor) {
if(fDistanceWall <= 16.0/4.0)
screen[x * nscreenwidth + y] = '#';
else if (fDistanceWall < 16.0 / 2.0)
screen[x * nscreenwidth + y] = '.';
else {
screen[x * nscreenwidth + y] = ' ';
}
}
}
}
screen[nscreenheight * nscreenwidth - 1] = '\0';
WriteConsoleOutputCharacter(hconsole, screen, nscreenwidth * nscreenheight, { 0,0 }, &dwBytesWritten);
}
return 0;
}
我很想知道如何修复这个错误,因为我超级感兴趣的是什么原因。谢谢你这么多!
1条答案
按热度按时间mwg9r5ms1#
我试着运行你的代码却遇到了异常:写入访问冲突。屏幕为0x11102FC120B3152。
但是,for循环中的屏幕大小超出了定义的范围。
screen[x * nscreenwidth + y]
我可以通过交换nscreenwidth和nscreenheight的数据来解决这个问题,或者您可以考虑更改for循环的范围。