• W32 Console Library
    6 replies, posted
I'm dynamically creating a 2 dimensional array of CHAR_INFO structs using the following code: [cpp] CHAR_INFO **m_chiBuffer; .. m_chiBuffer = new CHAR_INFO *[iY]; for(int i = 0; i < iY; i++) m_chiBuffer[i] = new CHAR_INFO[iX];[/cpp] and writing it to the screen using WriteConsoleOutput. However, using this method causes a buffer of black characters to become: [img]http://i44.tinypic.com/105sc3b.png[/img] Copying the contents of the buffer to an array of constant width and height (CHAR_INFO chiBuffer[iY][iX]) as per the following code, works: [cpp] CHAR_INFO chiBuffer[25][80]; for(int y = 0; y < 25; y++) { for(int x = 0; x < 80; x++) chiBuffer[y][x] = m_chiBuffer[y][x]; } fSuccess = WriteConsoleOutput( m_hStdout, // screen buffer to write to &chiBuffer[0][0], // buffer to copy from --> (&m_chiBuffer[0][0] doesn't work) <-- coordBufSize, // col-row size of chiBuffer coordBufCoord, // top left src cell in chiBuffer &srctReadRect); // dest. screen buffer rectangle[/cpp] Any ideas why?
CHAR_INFO chiBuffer[iY][iX] is one continuous block of memory which is what WriteConsoleOutput expects. new CHAR_INFO *[iY] is simply an array of pointers; the actual data you wanted to portray is stored elsewhere (indicated by the pointer address). You can dynamically allocate the equivalent of CHAR_INFO chiBuffer[iY][iX] doing: [cpp] CHAR_INFO* chiBuffer = new CHAR_INFO[iY * iX]; [/cpp]
Then I end up with this funky pattern instead: [img]http://i42.tinypic.com/2lbkuwm.png[/img] Using: [cpp]chiBuffer = new CHAR_INFO [iY * iX]; CHAR_INFO BLK; BLK.Char.UnicodeChar = ' '; BLK.Attributes = 0; for(int y = 0; y < iY; y++) { for(int x = 0; x < iX; x++) chiBuffer[y * x] = BLK; } fSuccess = WriteConsoleOutput( m_hStdout, // screen buffer to write to chiBuffer, // buffer to copy from coordBufSize, // col-row size of chiBuffer coordBufCoord, // top left src cell in chiBuffer &srctReadRect); // dest. screen buffer rectangle [/cpp]
Try chiBuffer[iY * y + x] = BLK;
Changed it to: [cpp]for(int i = 0; i < iY * iX; i++) chiBuffer[i] = BLK;[/cpp] and it's working, thanks!
[QUOTE=Shootfast;20663517]Changed it to: [cpp]for(int i = 0; i < iY * iX; i++) chiBuffer[i] = BLK;[/cpp] and it's working, thanks![/QUOTE] If that's all you're going to do in that loop, you should just use memcpy.
[QUOTE=jA_cOp;20663755]If that's all you're going to do in that loop, you should just use memcpy.[/QUOTE] memset, no?
Sorry, you need to Log In to post a reply to this thread.