Yo wassup guys, I've just recently started learning C++ (I started 2 days ago, actually) and I was wondering if any of you would be willing to guide me towards the right direction.
I decided to try and make a little text game but ran into some inefficiency problems..
Relevant Variables ** excluding the map, as it is too long.
1 2 3 4 5
bool gameRunning = true;
int gameSpeed = 100;
int x = 1; // Start off at coordinates 1,1
int y = 1;
int main()
{
while (gameRunning == true)
{
system("cls");
// Constantly and unnecessarily keeps regenerating the map,
// I'm trying to make it so it only generates the map
// itself once, and updates it only when the character moves.
for (int yAxis = 0; yAxis<25; yAxis++)
{
cout << gameMap[yAxis] << endl;
}
system("pause>nul"); // stops constant flickering
handleMovement();
Sleep(gameSpeed);
}
system("cls");
cout << "GAME OVER";
return 0;
}
As the comment on the previous code points out, there is a lot of unnecessary map updating going on, which I tried to solve with the following code:
Well, is there any way to make it so it only update's the character's movement, instead of regenerating the entire map? , or at least get rid of that awful flickering effect..
int main()
{
clearScreen();
generateMap();
stopMapFlickering();
while (gameRunning == true)
{
bool mapNeedsUpdate = handleMovement();
if (mapNeedsUpdate)
{
clearScreen();
generateMap();
}
Sleep(gameSpeed);
}
return 0;
}
I am trying to make it so it only updates the player's location when a player moves, without the extra step of having to regenerate the entire map(only update the player's new location), or if that is impossible, is there at least some hacky way of getting around that constant flickering? (like generating the map fast enough that it is impossible to perceive).
First of all, usage of stream output for games is bad choice in general. Second is that system calls are terribly slow compared to other possibilities.
To get adequate results, you will need to eschew standard IO (which operates using stream ideology and is not suited for games) and manipulate console window directly using API calls.
For example use second snippet shown on next link to clear window: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682022%28v=vs.85%29.aspx
Also make yourself familiar with other console API and use it. You will probably have to write helper functions for things like drawing map on screen, etc.
Alternatively, use library which makes those things way easier to do. Like pdcurses.