c++ - Trying to make a void function edit a 2D array! -


OK, this is a newbie question, and I understand that the function parameters I have given for the function are probably bad By the way, but what I'm trying to do here:

I got a CHAR_INFO (buffer) of an 80x25 2D array that will be recursive of the infinite loop on the console screen. Because the buffer will be constantly changing (like a ninja game), I want to change the array to each loop.

I can easily integrate the loop in the main function, but for learning and organization, I wanted to divide it into my own function WriteBuffer (); I'm looking everywhere how I can pass the 2D CHAR_INFO array in the context in context so that I can edit it, but nothing is working. Please do not tell me to use vectors or anything, I I want to work with whatever I have found here.

  #defineWIN32_LEAN_AND_MEAN #include & lt; Windows.h & gt; # Include & lt; Stdlib.h & gt; #define SCREEN_WIDTH 80 # sparse_heightet25 std using the namespace; Blank WriteBuffer (CHAR_INFO ** buffer [] [SCREEN_WIDTH]); Int main () {// Start screen buffer and cursor handle hConsoleOutput (GetStdHandle (STD_OUTPUT_HANDLE)); COORD dwBufferSize = {SCREEN_WIDTH, SCREEN_HEIGHT}; Coward sink buffer code = {0, 0}; SMALL_RECT rcRegion = {0, 0, SCREEN_WIDTH-1, SCREEN_HEIGHT-1}; CHAR_INFO buffer [SCREEN_HEIGHT] [SCREEN_WIDTH]; CONSOLE_CURSOR_INFO lpConsoleCursorInfo = {1, false}; SetConsoleCursorInfo (hConsoleOutput, & amp; lpConsoleCursorInfo); // characters character characters [3]; Characters [0] = Static_cast & lt; Char & gt; (177); Characters [1] = Fixed_cast & lt; Char & gt; (177); Characters [2] = Static_cast & lt; Char & gt; (177); Characters [3] = Static_cast & lt; Char & gt; (177); // Colors anchor color [3]; Color [0] = 0x0a; Color [1] = 0x0b; Color [2] = 0x0c; Color [3] = 0x0d; // When starting the infinite loop (1) {ReadConsoleOutput (hConsoleOutput, (CHAR_INFO *) buffer, dwBufferSize, dwBufferCoord, and rcRegion); WriteConsoleOutput (hConsoleOutput, (CHAR_INFO *) buffer, dwBufferSize, dwBufferCoord, and rcRegion); Sleep (100); }} Blank WriteBuffer (CHAR_INFO ** buffer [] [SCREEN_WIDTH]) {int x, y, i = 0; While (y & lt; SCREEN_HEIGHT) {while (x & lt; SCREEN_WIDTH) {Buffer [or] [x]. HARSCIRHR = Characters; Buffer [or] [x]. Attribute = color [i]; X ++; } X = 0; Y ++; }}  

You can use any of the following declarations: < / P> Blank WriteBuffer (CHAR_INFO buffer [SCREEN_HEIGHT] [SCREEN_WIDTH]); Blank WriteBuffer (CHAR_INFO buffer [] [SCREEN_WIDTH]); Blank WriteBuffer (CHAR_INFO (* buffer) [SCREEN_WIDTH]);

is allowed from the second because you can eliminate the dimension of the array of multi-dimensional array. The third is allowed through pointer decay - a pointer in the list indicates a parameter "indicator" and the equivalent. Note that this can only be done once you not use the following announcement:

wrong!

  Blank WriteBuffer (CHAR_INFO ** bufferp);   

This is because because CHAR_INFO * bufferp [SCREEN_WIDTH] in decays CHAR ** buffer , but as you see above we have < Code> CHAR_INFO (* buffer) [SCREEN_WIDTH] which is a neatly different animal.

* The reason that you can skip the first dimension is because the compiler does not need to calculate offset if you have array [w] [h] in the array Access element is array [x] [y] , then the compiler changes it into * (& array [0] [0] + y * H + x) . Note that W is not required in that calculation, so it can be left in the parameter list of the function.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -