I am making a simple game in the console using some of the tricks from the youtuber JavidX9. Everything displays correctly when the window and buffer size are correct, FYI for anyone that tries to run it. My issue comes about when I try to close the application when debugging it. Sometimes if I mess with it enough by moving the character around the screen randomly it will break. The error I get is the one listed in the title. But after that, I get a linker error that tells me that the process is still running ( which I can not find ). So it is a nightmare to debug. I have been searching for the answer to this issue for several days now, hopefully, one of you C++ gods can help me out. The code is pretty short.
#include <iostream>
#include <Windows.h>
#include <chrono>
using namespace std;
int screenWidth = 120; //Console Screen Size. X = Cols & Y = Rows
int screenHeight = 40;
int mapWidth = 48; //World Dimensions
int mapHeight = 48;
int playerX = mapWidth / 2;
int playerY = mapHeight / 2;
int main()
{
wchar_t *screen = new wchar_t[screenWidth * screenHeight];
HANDLE consoleHandle = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(consoleHandle);
DWORD bytesWritten = 0;
// 16 x 3
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"#..............................................#";
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"#..............................................#";
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"################################################";
const int TICKS_PER_SECOND = 20;
const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
const int MAX_FRAMESKIP = 10;
DWORD next_game_tick = GetTickCount();
int loops;
bool game_is_running = true;
while (game_is_running)
{
loops = 0;
while (GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP)
{
// Update
for (int i = 0; i < screenWidth * screenHeight; i++)
{
screen[i] = ' ';
}
if (GetAsyncKeyState((unsigned short)'A'))
{
if (map[(playerX - 1) + mapWidth * playerY] == '#')
{
break;
}
else
{
playerX--;
}
}
if (GetAsyncKeyState((unsigned short)'D'))
{
if (map[(playerX + 1) + mapWidth * playerY] == '#')
{
break;
}
else
{
playerX++;
}
}
if (GetAsyncKeyState((unsigned short)'S'))
{
if (map[playerX + mapWidth * (playerY + 1)] == '#')
{
break;
}
else
{
playerY++;
}
}
if (GetAsyncKeyState((unsigned short)'W'))
{
if (map[playerX + mapWidth * (playerY - 1)] == '#')
{
break;
}
else
{
playerY--;
}
}
next_game_tick += SKIP_TICKS;
loops++;
}
// Draw the map to the buffer
for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth; x++)
{
screen[x + screenWidth * y] = map[x + mapWidth * y];
}
screen[playerX + screenWidth * playerY] = '@'; // Draw the player
}
// Display the Buffer
screen[screenWidth * screenHeight - 1] = '\0';
WriteConsoleOutputCharacter(consoleHandle, screen, screenWidth * screenHeight, { 0,0 }, &bytesWritten);
}
}
Aucun commentaire:
Enregistrer un commentaire