Skip to main content
GameDev.net gamedev.net
🔒 Locked

a slow 2D array

Started by kevin10 Nov 20, 2010 at 6:30 AM 9 replies 2k views
Original Post
kevin10
kevin10
Hi,

I've started to create a little multiplayer game in a console but I've got a little problem at the moment.

I'm at the point to show a simple map which is a 2d array of some characters which is the map. It looks like this:

* * * * * * *
* * * * * P *
P * * * * * *

* = a empty spot where the player can stand
p = A player.

So When a player logs in I draw this 2D array with 2 for loops
for(int i = 0; i < 16; i ++){	for(int j = 0; j < 30; j++)	{		printf("%c ",field[j]);	}}

After this one I'm waiting for input of the user. If the player needs to be moved i'm changing the P in the array and redraw the map but this makes my console flicker. Is this just a disadvantage of using a console? or is there a more efficient way to display the array?

I could make the map smaller so it doesn't flicker but that's more like hiding the problem.

Monkan
Monkan
Maybe you need to clear your console window before you redraw.

Try just printing out a few \n new lines before redrawing.
"To know the road ahead, ask those coming back."
kevin10
kevin10
Sorry I forgot to say that I clear the screen with:
System("cls");
So the screen is completely clear before I redraw.
SiCrane
SiCrane
This is a consequence of the way you're doing text output. If you want to do this without flicker you'll need to write directly to the coordinates of the sections to be updated. You can do this with platform dependent code (ex: WriteConsoleOutput() or related functions on Windows) or use a library with such functionality such as pdcurses.
incin
incin
At very least you could create the string representing the grid first, and then call printf only once to print the whole thing.
darkelf2k5
darkelf2k5
Redrawing the whole console is slow. Maybe try updating only the tiles that actually changed? For example, if p needs to go right 1 character, set the * next to p to a p, and p's old position to *.
Update only what's changed.
With a scrolling map you will still get the flicker thou.
Every time you implement a singleton, God kills a kitten. Please, think of the kittens!
incin
incin
I see no flicker doing this. Try printing all at once, and using newlines to clear the screen.
import timec='* 'def showgrid():    g='\n'*80    for i in range(40):        g+=c*20    print(g)showgrid()time.sleep(1)c='- 'showgrid()time.sleep(1)c='+ 'showgrid()time.sleep(1)
kevin10
kevin10
Thank you for all your posts it really helped:)

I went to search for some of your solutions and I found the problem it's the system("cls");

That one slow and make my screen flicker.

I'm now using this:

	HANDLE hOut;    COORD Position;    hOut = GetStdHandle(STD_OUTPUT_HANDLE);    Position.X = 0;    Position.Y = 0;    SetConsoleCursorPosition(hOut, Position);


The flicker is now gone.

thanks again:)
Zahlman
Zahlman
'system' means "ask the operating system to start up another program with the specified name, and wait until it finishes running" - in this case, the one named "cls", whose purpose is to clear the console window. For simple tasks, this takes much longer than just doing it yourself.
dpadam450
dpadam450
There is a function i think specific to windows.h "SetConsoleCursorPos()", or something like that, you can simply mark the old player position as a space and the new player position as 'P'
NBA2K, Madden, Maneater, Killing Floor, Sims 
kevin10
kevin10
Yeah I noticed that system("cls"); is a very bad way to clear the screen and slow. I tried some other ways to clear the screen but there was also a flicker. I made a little timer to see how much time it took to draw a new map and it was about 50 milliseconds. After that I tried that move mouse cursor pos function and that one works fine.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.