Jump to content
  • Advertisement
Sign in to follow this  

Output to console not fast enough

This topic is 2592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been trying to get my game to work recently. As of now Im just trying to get it to print the map. I made it so everytime you click a button to move, it reprints the whole map 45 x 80 characters, and then place the character. Apparently this is way too slow to do, as everytime I move, my character blinks and then moves. So if I keep pressing the button, he slowly moves and keeps moving after im done pressing the keys. This is the code:

<BR>#include "header.h"<BR>#include "mapdata.h"<BR>#include "playerdatabase.h"<BR>#include "commandcentral.h"</DIV><DIV class="post entry-content" itxtNodeId="386" itxtHarvested="1">using namespace std;</DIV><DIV class="post entry-content" itxtNodeId="386" itxtHarvested="1">void printmap()<BR>{<BR> HANDLE hPrint;<BR> COORD pos;<BR> pos.X = 0;<BR> pos.Y = 0;<BR> hPrint = GetStdHandle(STD_OUTPUT_HANDLE);<BR> SetConsoleCursorPosition(hPrint, pos);<BR> for(int Y = 0; Y < 45; Y++)<BR> {<BR> for(int X = 0; X < 80; X++)<BR> {<BR> SetConsoleTextAttribute(hPrint, cGameMap[0][Y][X]);<BR> cout << tGameMap[0][Y][X];<BR> }<BR> }<BR> pos.X = mapx;<BR> pos.Y = mapy;<BR> SetConsoleCursorPosition(hPrint, pos);<BR> SetConsoleTextAttribute(hPrint, clr);<BR> cout << symbl;<BR> pos.X = 2;<BR> pos.Y = 46;<BR> SetConsoleCursorPosition(hPrint, pos);<BR> SetConsoleTextAttribute(hPrint, 7);<BR> cout << gamemsg << " ";<BR> gamemsg = "";<BR> cmdcent();<BR>}<BR>

And this is the slow part:

<BR>for(int Y = 0; Y < 45; Y++)<BR>{<BR> for(int X = 0; X < 80; X++)<BR> {<BR> SetConsoleTextAttribute(hPrint, cGameMap[0][Y][X]);<BR> cout << tGameMap[0][Y][X];<BR> }<BR>}

Is there another container I can use for this?<BR itxtNodeId="381">

Share this post

Link to post
Share on other sites
I'm having trouble reading your code, but I think the problem isn't your container, but your use of std::cout to do output. I'd recommend that instead of writing each character individually with std::cout, create a CHAR_INFO buffer and write the whole thing out with WriteConsoleOutput() in a single go.

Share this post

Link to post
Share on other sites
Sorry, for some reason it messed up my code posts. Anyways, I tried using WriteConsoleOutput(), but cant figure out how to completely use it. Here is what I have so far:

#include "header.h"
#include "mapdata.h"
#include "playerdatabase.h"
#include "commandcentral.h"

using namespace std;

void printmap()
HANDLE hPrint, hBuffer;
SMALL_RECT srctReadRect;
SMALL_RECT srctWriteRect;
COORD coordBufSize;
COORD coordBufCoord;
BOOL fSuccess;
hBuffer = CreateConsoleScreenBuffer(
srctReadRect.Top = 0;
srctReadRect.Left = 0;
srctReadRect.Bottom = 44;
srctReadRect.Right = 79;
coordBufSize.Y = 45;
coordBufSize.X = 80;
coordBufCoord.X = 0;
coordBufCoord.Y = 0;

COORD pos;

pos.X = 0;
pos.Y = 0;
hPrint = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hPrint, pos);
fSuccess = WriteConsoleOutput(

pos.X = mapx;
pos.Y = mapy;
SetConsoleCursorPosition(hPrint, pos);
SetConsoleTextAttribute(hPrint, clr);
cout << symbl;
pos.X = 2;
pos.Y = 46;
SetConsoleCursorPosition(hPrint, pos);
SetConsoleTextAttribute(hPrint, 7);
cout << gamemsg << " ";
gamemsg = "";

tGameMap is a 3d array, and I cant figure out what to do with it. The link is kind of confusing me. The example it gives doesnt help much either.

Share this post

Link to post
Share on other sites
In order to use WriteConsoleOutput() you need to create a CHAR_INFO array the size of the block you want to write. Multiply the number of columns of the area by the number of rows of the area. For a number of columns C, the first C CHAR_INFO entries represent the first row. For the first element set the Char member to whatever character you want to display in the upper-left of the block. Then set the Attributes member to represent the character's foreground and background color. Repeat until the entire array is filled. Does that help?

Share this post

Link to post
Share on other sites
Alright, I got it all fixed. It runs smoothly now. However, now I have another problem. I have a loop that keeps running that asks you for a keypress, and then it sends to movechar function, and back to printmap which prints the map. For some reason though, After exactly 140 moves of any type, the game pauses and I get a windows error. Then I tried debugging it, and it says:

"An action violation (Segmentation Fault) has been raised."

It fails on this line:

WriteConsoleOutput( GetStdHandle( STD_OUTPUT_HANDLE ), buffer, size, null, &rect );

But there doesnt seem to be anything special happening until then, and it all runs fine. It even prints the it out the final time, then fails. What could be the problem? If you need to, I can post the codes it runs through.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!