Sign in to follow this  
MAGIgullorks

Output to console not fast enough

Recommended Posts

MAGIgullorks    100
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:


[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>[/code]

And this is the slow part:


[code]<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>}[/code]

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

Share this post


Link to post
Share on other sites
SiCrane    11839
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 [url=http://msdn.microsoft.com/en-us/library/ms687404(v=vs.85).aspx]WriteConsoleOutput()[/url] in a single go.

Share this post


Link to post
Share on other sites
MAGIgullorks    100
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:

[code]
#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(
GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ |
FILE_SHARE_WRITE,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL);
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(
hBuffer,
tGameMap[0],
coordBufSize,
coordBufCoord,
&srctWriteRect);

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 = "";
cmdcent();
}

[/code]

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
SiCrane    11839
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
jbadams    25712
You could have a look at Ben Ryves' [url="http://benryves.com/tutorials/winconsole/all"]Windows Console tutorials[/url]. He provides a pretty good explanation along with sample code and covers what you're trying to do.

Hope that helps! [img]http://public.gamedev.net/public/style_emoticons/default/cool.gif[/img]

Share this post


Link to post
Share on other sites
MAGIgullorks    100
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this