Hey so i’m making a simple text game using the pdCurses library and a few other minor things kind of like a vertical scroller in which you avoid the randomly generated walls….
Theres two walls on lef and right made out of ‘X’ characters and blank, black space in which you can move around and avoid the ‘X’s your character is an ‘8’ and your forced to continue forward or get touched by the X’s each time a new line of the randomly generated “map” is revealed( for performance tests i made a new line shown as fast as possible).
However i’m having some performance problems as the “map” (a vector of strings) gets bigger and bigger. I do not understand the problem however as i am not using all of it at any time, i’m only pulling out parts of it to display (56 lines usually).
I’ll show you what i’ve got and hopefully someone will help or suggest a better way to accomplish my game.
Here’s the condensed, important code:
Here’s the function that is taking like .25-.75 seconds (new_map is also a vector member of the “Screen” class):
void Insert(const Map& map, int y1, int y2) {for ( int mc = y1, nm= 0; mc< map.Contents().size() && mc< y2; mc++, nm++)
new_map[nm] = map.Contents(mc);};
Here’s the Map classes contents functions:
string Contents(int Y) {return contents[Y];};
char Contents(int Y, int X) {return contents[Y][X];};
vector <string> Save() {return save;};
and finally the main() which i have set so the screen updates as fast as possible… which isn’t turning out to be so fast oh and L1 is one of my “maps”…
generate adds on new lines to the map as so it never ends:
double refreshes= 0;
for (bool quit = false; quit != true;)
{ double newTime= myStopwatch.ElapsedTime()- refreshes;
theScreen.Insert(L1, 0+refreshes, nrows+refreshes);
refreshes++;
if(L1.Contents().size()<= nrows+refreshes+2)
L1.generate();}
Thanks for any help or tips!! I know it’s pretty terrible but i just started programming 2 months ago haha! =) ask if you need any more info.
The general issue seems to be that as your data set gets larger things like copying it around (which you seem to do all the time, e.g. in your snippet you are copying strings from one vector to the other) take longer.
Things to consider:
off the screen?
As an example for point #1 you may prefer to store your screen lines in a list and do something like:
It’s not perfect (there’s memory management and some copying behind the scenes) but it will outperform copying and accumulating all the screen lines.
As an example for point #2, consider this code:
This will “scroll” screen one character to the left. It will keep running at a constant and pretty fast time – on any modern CPU you can expect to be able to do over about a million of these scrolling operations per second.