SEXP getWindowsHandle(SEXP which) { void * handle; if(length(which) != 1) error(_("'%s' must be length 1"), "which"); if (isString(which)) handle = getConsoleHandle(CHAR(STRING_ELT(which,0))); else if (isInteger(which)) handle = getDeviceHandle(INTEGER(which)[0]); else handle = NULL; if (handle) return R_MakeExternalPtr(handle,R_NilValue,R_NilValue); else return R_NilValue; }
void drawCon() { //We want to minimize drawing to save CPU. So, we ask ourselves: //Do we need to draw the entire screen? if(conNeedTotalDraw) { //Draw the walls //Make the walls blue WORD attr = BACKGROUND_BLUE | FOREGROUND_BLUE; //Get the width/height of the screen CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo( getConsoleHandle(), &csbi ); DWORD width = csbi.dwSize.X; DWORD height = 25;//use a preset value //We need to fill the screen with spaces COORD cornerCoord = {0,0}; DWORD out; FillConsoleOutputCharacter( getConsoleHandle(), ' ', width*height, cornerCoord, &out); //Draw the walls for(int i = 0; i < height; i++) { COORD coord = {0, i}; //If we're at the top or bottom, we need to fill the entire row. if(i == 0 || i == height-1) { FillConsoleOutputAttribute( getConsoleHandle(), attr, width, coord, &out); } //Otherwise, only the two sides else { FillConsoleOutputAttribute( getConsoleHandle(), attr, 1, coord, &out); COORD coord2 = {width-1, i}; FillConsoleOutputAttribute( getConsoleHandle(), attr, 1, coord2, &out); //make sure the center has the correct properties COORD coord3 = {1, i}; WORD centAttr = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;//white text w/ black background FillConsoleOutputAttribute( getConsoleHandle(), centAttr, width-2, coord3, &out); } } //Draw the title and menu items DWORD numCharWritten; char* str = "CONSOLE SNAKE"; COORD strCoord = getCenteredStringCoord(str,width,3); SetConsoleCursorPosition( getConsoleHandle(), strCoord); WriteFile( getConsoleHandle(), str, strlen(str), &numCharWritten, NULL); WORD titleAttr = FOREGROUND_RED;//red text w/ black background FillConsoleOutputAttribute( getConsoleHandle(), titleAttr, strlen(str), strCoord, &numCharWritten); str = "Controls"; strCoord = getCenteredStringCoord(str,width,5); SetConsoleCursorPosition( getConsoleHandle(), strCoord); WriteFile( getConsoleHandle(), str, strlen(str), &numCharWritten, NULL); titleAttr = FOREGROUND_GREEN;//green text w/ black background FillConsoleOutputAttribute( getConsoleHandle(), titleAttr, strlen(str), strCoord, &numCharWritten); str = "Move Up: W, Up"; strCoord = getCenteredStringCoord(str,width,9); SetConsoleCursorPosition( getConsoleHandle(), strCoord); WriteFile( getConsoleHandle(), str, strlen(str), &numCharWritten, NULL); str = "Move Down: S, Down"; strCoord = getCenteredStringCoord(str,width,10); SetConsoleCursorPosition( getConsoleHandle(), strCoord); WriteFile( getConsoleHandle(), str, strlen(str), &numCharWritten, NULL); str = "Move Right: D, Right"; strCoord = getCenteredStringCoord(str,width,11); SetConsoleCursorPosition( getConsoleHandle(), strCoord); WriteFile( getConsoleHandle(), str, strlen(str), &numCharWritten, NULL); str = "Move Left: A, Left"; strCoord = getCenteredStringCoord(str,width,12); SetConsoleCursorPosition( getConsoleHandle(), strCoord); WriteFile( getConsoleHandle(), str, strlen(str), &numCharWritten, NULL); str = "Pause Game: P"; strCoord = getCenteredStringCoord(str,width,13); SetConsoleCursorPosition( getConsoleHandle(), strCoord); WriteFile( getConsoleHandle(), str, strlen(str), &numCharWritten, NULL); str = "Back to Main Menu"; strCoord = getCenteredStringCoord(str,width,16); SetConsoleCursorPosition( getConsoleHandle(), strCoord); WriteFile( getConsoleHandle(), str, strlen(str), &numCharWritten, NULL); strCoord.X -= 2; WORD selectedAttr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE;//white text w/ cyan background FillConsoleOutputAttribute( getConsoleHandle(), selectedAttr, strlen(str)+4, strCoord, &numCharWritten); conNeedTotalDraw = false; } }