Howdy, y'all!

I'm trying to make a simple little game, but redrawing the game board is a lot stower than I'd like. Any suggestions to optimize my code?
I'm using a Console program because I'm still a novice and am not ready to tackle a real GUI yet.

Here is my source code:

#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <sstream>
#include "Game.h"
using namespace std;

void startGame()
{
    while(1)
    {
        int ch = ' ';
        Gameboard GB;
        vector pos(1, 1);
        GB.setPos(pos, '*');
        GB.printGameboard();

        while(ch != 27)
        {
            while(!_kbhit());
            ch = _getch();
            if(ch == 0)
                _getch();
            else if(ch == 224)
            {
                ch = _getch();
                vector newPos = pos;
                if(ch == 72)
                {
                    newPos.y--;
                    if(GB.BoardChar(newPos) == ' ')
                    {
                        GB.setPos(pos, ' ');
                        pos = newPos;
                        GB.setPos(pos, '*');
                        GB.printGameboard();
                    }
                }
                if(ch == 75)
                {
                    newPos.x--;
                    if(GB.BoardChar(newPos) == ' ')
                    {
                        GB.setPos(pos, ' ');
                        pos = newPos;
                        GB.setPos(pos, '*');
                        GB.printGameboard();
                    }
                }
                if(ch == 77)
                {
                    newPos.x++;
                    if(GB.BoardChar(newPos) == ' ')
                    {
                        GB.setPos(pos, ' ');
                        pos = newPos;
                        GB.setPos(pos, '*');
                        GB.printGameboard();
                    }
                }
                if(ch == 80)
                {
                    newPos.y++;
                    if(GB.BoardChar(newPos) == ' ')
                    {
                        GB.setPos(pos, ' ');
                        pos = newPos;
                        GB.setPos(pos, '*');
                        GB.printGameboard();
                    }
                }
            }
        }
        if(ch == 27)
            exit(0);
    }
}

void vector::operator=(vector& prevVector)
{
    x = prevVector.x;
    y = prevVector.y;
}
vector::vector(int newX, int newY)
{
    x = newX;
    y = newY;
}
void Gameboard::printGameboard()
{
    stringstream sout;
    system("clear");
    for(int ROW = 0; ROW < 61; ROW++)
    {
        for(int COLUMN = 0; COLUMN < 124; COLUMN++)
            sout << Board[ROW][COLUMN];
    }
    string board = sout.str();
    cout << board;
}
void Gameboard::setBoard()
{
    //First Row
    Board[0][0] = ' ';
    for(int COLUMN = 1; COLUMN < 123; COLUMN++)
        Board[0][COLUMN] = '-';
    Board[0][123] = ' ';
    //Middle Rows
    for(int ROW = 1; ROW < 60; ROW++)
    {
        Board[ROW][0] = '|';
        for(int COLUMN = 1; COLUMN < 123; COLUMN++)
        {
            Board[ROW][COLUMN] = ' ';
        }
        Board[ROW][123] = '|';
    }
    //Last Row
    Board[60][0] = ' ';
    for(int COLUMN = 1; COLUMN < 123; COLUMN++)
        Board[60][COLUMN] = '-';
    Board[60][123] = ' ';
}
void Gameboard::setPos(vector& V, char C)
{
    Board[V.y][V.x] = C;
}
char Gameboard::BoardChar(vector& V)
{
    return Board[V.y][V.x];
}

Gameboard::Gameboard()
{
    setBoard();
}

printGameboarh() is the function I'm most concerned with.
I used sout.str() to pass a single string to cout rather than make repetitive calls to cout. This didn't appear to change the speed at all.

I've been told <conio> is deprecated. Is their a better replacement?

How slow is it? If it's slow, it's slow because either (1) computing Board[i][j] is slow or because (2) system("clear") is slow or because (3) appending chars to a stringstream is slow.

1 would be the case if you used a bad datastructure for whatever Board is.

2 could be the case.

3 is probably not the problem -- stringstream implementations are generally slow on a lot of systems. They'll acquire a mutex before doing their operation, which means that appending chars is pretty expensive. However, the board is so small that you wouldn't think this to be a problem. There are only about 7500 characters there, so even if each append took 100 ns it would take under a millisecond to build the stringstream.

Gabebord is a char array w/ 124 x 61 elements. When printBoard() is called, the screen begins redrawing almost instaneously, so I know none of the computations beforehand are slowing it down. It's the process of printing each subsequent line to the screen which appears slow.

For anyone else who comes across this thread looking for an answer, I've solved my problem. I used C-style printf() instead of C++ style cout. Despite rumors to the contrary, it really is a lot faster. And here's the reason: cout takes multiple types as parameters and has to convert them; conversly, printf() requires a C-string and type conversions are nonexistent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.