Search code examples
c++visual-studioterminalrenderinggame-engine

Not able to write to console


I was following the one lone coder video on rendering to the console and I wrote up the code they were using.

I am unable to get the console to actually show anything but instead upon trying to ctrl + f5 on visual studio, I get the debug window that says "exited with code -1073741819."

this is the code I have so far:

#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;
} 

I would love to know how to fix this error as I'm super interested as to what's causing it. thank you so much!


Solution

  • I tried to run your code and got an exception: write access violation.screen was 0x11102FC120B3152.

    enter image description here

    int nscreenwidth = 120;
    int nscreenheight = 40;
    wchar_t* screen = new wchar_t[nscreenheight * nscreenwidth];
    

    However, the size of the screen in the for loop exceeds the defined range.

    screen[x * nscreenwidth + y]

    I could solve this problem by swapping the data of nscreenwidth and nscreenheight, or you could consider changing the range of the for loop.

    for (int x = 0; x < nscreenwidth; x++)