2D array and writing to a console.

Hey everyone.

So I am attempting to program some form of basic ASCII game in C++ just to learn more of the basics. I have most of the fine details of how the game will work and most of my functions are designed. The only problem is that I cant get a 2D array to work for my game screen.

I have a 1D array version set up but it doesn't work how I would like. It uses the same bits of code as the one below but I can only get the character to move right and left properly. When you try and move up/down it you move all the way across (because it is 1D moving down means moving 50 across in my 80x50 screen which looks bizzare).

So my question lies specifically with the function:

WriteConsoleOutputA(wHnd, arryMap, charBufSize, characterPos, &writeArea)

As this is giving me the error. arryMap is the array I declared but is giving me an error because it does not convert to a const.

Is there an alternative function I can use? Is there a better way to go about generating screen and assigning the same tile to each? Like I said above I have a 1D version set up but it is a bit funny in terms of moving up and down.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include <iostream>

#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include <Tchar.h>

HANDLE wHnd;    // Handle to write to the console.
HANDLE rHnd;    // Handle to read from the console.

using namespace std;

class Map
{
public:

    int m_nPosX, m_nPosY, m_nItem;

    void SetInfo(int nItem, int nPosX, int nPosY)
    {

        m_nItem = nItem;
        m_nPosX = nPosX;
        m_nPosY = nPosY;

    }

    void Print()
    {

        cout << (char)m_nItem;
        cout << " at position (" << m_nPosX << "," << m_nPosY << ")" << endl;

    }


};

int main()
{

    int i, j;

    // Set up the handles for reading/writing:
    wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
    rHnd = GetStdHandle(STD_INPUT_HANDLE);

    // Change the window title:
    SetConsoleTitle(TEXT("2D Array Test"));

    // Set up the required window size:
    SMALL_RECT windowSize = {0, 0, 79, 49};

    // Change the console window size:
    SetConsoleWindowInfo(wHnd, TRUE, &windowSize);

    // Create a COORD to hold the buffer size:
    COORD bufferSize = {80, 50};

    // Change the internal buffer size:
    SetConsoleScreenBufferSize(wHnd, bufferSize);

    Map arryMap[80][50];

    for (i=0; i<80; i++){

        for (j=0; j<50; j++){

            Map arryMap[i][j].SetInfo(01, i, j);

        }

    }

    // Set up the positions:
    COORD charBufSize = {80,50};
    COORD characterPos = {0,0};
    SMALL_RECT writeArea = {0,0,79,49};

    // Copy to display:
    WriteConsoleOutputA(wHnd, arryMap, charBufSize, characterPos, &writeArea);

}


A lot of that code is taken from tutorials and I am tweaking it to get the result I want and learning how things work in the process.

If you need it I can include the 1D version which has everything about moving too, but my question is primarily about setting up the screen using a 2D array opposed to a 1D array. I can tweak everything else later, my functions shouldn't take too much to convert to a 2D array.

Thanks in advance,

Jebster.
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <Windows.h>
HANDLE wHnd;    // Handle to write to the console.
HANDLE rHnd;    // Handle to read from the console.
using namespace std;
int main()
{
    const static int sizeX=50,sizeY=50;
    // Set up the handles for reading/writing:
    wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
    rHnd = GetStdHandle(STD_INPUT_HANDLE);

    // Change the window title:
    SetConsoleTitle(TEXT("2D Array Test"));

    // Set up the required window size:
    SMALL_RECT windowSize = {0, 0, sizeX, sizeY};

    // Change the console window size:
    SetConsoleWindowInfo(wHnd, TRUE, &windowSize);

    // Create a COORD to hold the buffer size:
    COORD bufferSize = {sizeX, sizeY};

    // Change the internal buffer size:
    SetConsoleScreenBufferSize(wHnd, bufferSize);

    CHAR_INFO m_nItem[sizeY][sizeX];

    for (int i=0; i<sizeY; i++){
        for (int j=0; j<sizeX; j++){
            // An ANSI character is in the range 0-255,
            // so use % to keep it in this range.
            m_nItem[i][j].Char.AsciiChar = '1';//rand() % 256;

            // The colour is also in the range 0-255,
            // as it is a pair of 4-bit colours.
            m_nItem[i][j].Attributes = rand() % 256;
        }
    }

    // Set up the positions:
    COORD charBufSize = {sizeX,sizeY};
    COORD characterPos = {0,0};
    SMALL_RECT writeArea = {0,0,sizeX-1,sizeY-1};
    WriteConsoleOutputA(wHnd, *m_nItem, charBufSize, characterPos, &writeArea);

    return 0;
}

The above isn't coded to your design, but has demonstrated a 2d array with the write method.

Remember, it wants you to pass it CHAR_INFO for the second argument; not an object - also it is important that the data is linear.

You can encapsulate the multidimensional CHAR_INFO in an object, and pass its object to the write method.

You will be passing its origin to the command; again, be sure that it is linear. If it is not, you will receive unknown segmented data.

Example: dynamic multidimensional array:
-A new array, that has many new arrays, may not be linear (allocated side by side) and will not properly print using the write console method.

A work around may be: creating a large block of data that can hold both its data (y*x). And build a work around manipulation to process the data.
Last edited on
Ok, I think I understood what you were trying to tell me.

The main difference I see is that you declare the size of the array to be const using the variables sizeX and sizeY. The error message i was getting was that the array couldn't be assigned a const value, so that would seem to solve the problem.

In my original program I also had a class used to store multiple bits of information in the array, such as the character type stored there which would be useful to check for thing like walls. Is there any way to recreate this without using a class? If i create a class then i will have to create

classname m_nItem[sizeX][sizeY]

Which would not work I imagine due to the CHAR_INFO being missed?

Also if I was to use a linear array, lets say with 50*50 (2500 units) I could move left and right easily by using a variable to track the location of the character and then increment it with some thing like nPosChar++ or nPosChar--. However, if i want to move up and down i would have to increase or decrease the value of nPosChar but 50.

That seems to work in theory, but when I tried it the character icon moved sporadically, yes it moved up and down but also around 10-15 left or right.

If you could tell me how to get movement up and down in a 1D array that would be good too, but it seems as though a 2D array would be a more logical way of going about it.

Thanks,

Jebster.
Topic archived. No new replies allowed.