Win32: Storing Multi-Line Text in a Buffer - c++

How would I go about displaying multi-line text in Win32? This code is within my GamePaint() function, and I want to write the top 5 High Scores (stored in an attribute of a struct) out to the screen. I can get it to successfully output a single line using this method...how do I make the TCHAR buffer, szText, store multiple lines? Here's what I've attempted so far:
Original Code:
//draw rect for normal scores
ChangeTextFormat(hDC, hWnd, 1);
TCHAR szText[64];
RECT rcNormalScores = { 268, 122, 436, 330};
RECT rcHardScores = { 37, 122, 198, 330};
//stringstream ssTemp;
for(int i = 0; i < 5; i++)
{
//ssTemp << i;
//display nth Normal score
wsprintf(szText, "%d \n", g_scoreTop[i].num);
DrawText(hDC, szText, -1, &rcNormalScores, DT_LEFT | DT_WORDBREAK);
}
EDIT: Thanks for the info, but I'm still having some difficulty converting between data types. Here's the error I'm getting:
cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::basic_string<_Elem,_Traits,_Ax>'
EDIT2: Thanks for the help, queen3. I've posted the working code below:
Working Code:
ChangeTextFormat(hDC, hWnd, 1);
RECT rcNormalScores = { 37, 122, 198, 330};
RECT rcHardScores = { 268, 122, 436, 330};
stringstream ssTemp;
for(int i = 0; i < 5; i++)
{
ssTemp << g_scoreTop[i].num << " \n";
}
string sTemp = ssTemp.str();
LPCSTR LPTemp = (LPCSTR)sTemp.c_str();
DrawText(hDC, LPTemp, -1, &rcNormalScores, DT_LEFT | DT_WORDBREAK);
DrawText(hDC, LPTemp, -1, &rcHardScores, DT_LEFT | DT_WORDBREAK);

Either of
Make one string with all lines and newlines and do single DrawText
Adjust rcNormalScores .top each time by adding height of the string (for this you can use DT_CALCRECT flag)
The first one might work better if you later decide to change DT_LEFT to DT_CENTER.

Related

WriteConsoleOutputCharacter Drawing extra pixel thick of blank space

Ive been designing a roguelike video game in the console.
My problem is i initially draw the entire map with writeconsoleoutput at once then use writeconsoleoutputcharacter to redraw whats neccessary. But i have found that it draws an extra pixel thick on the right side.
This essentially erases one pixel off the tile on the right side.
Does anyone know a way to make it stop drawing a character and adding a 1 pixel blank space on the right side of the character.
EDIT: ONE WORKAROUND I HAVE FOUND IS TO REDRAW THE LINES THE PLAYER STEPS ON BUT IT ISNT PERFECT ONCE IN A WHILE I CAN NOTICE A FLICKER. Would be preferable to just redraw characters on an individual basis without any pixels attached on the end. or anywhere else.
The Code used to print the character
void printchar(char character, COORD location,short color)
{
const char *pointer = &character;
//short LENGTH = 1;
DWORD dwWritten = 0;
std::vector<WORD> attributes;attributes.clear();;
setAttributesChar(color,attributes);//Pushes back the color
if(attributes.size() == 1)
{ WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),&attributes[0], attributes.size(), location, &dwWritten);
WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), pointer, attributes.size(), location, &dwWritten);
}
}
void setAttributesChar(short color,std::vector<WORD> &attributes)
{
switch(color)
{
case DEFAULTCOLOR:
attributes.push_back(FOREGROUND_INTENSE_WHITE);return;
case HOVEREDCOLOR:
attributes.push_back(FOREGROUND_GREEN);return;
case RED:
attributes.push_back(FOREGROUND_RED);return;
case GOLD:
attributes.push_back(FOREGROUND_INTENSE_YELLOW);return;
case REDWHITE:
attributes.push_back(COMBINED_REDWHITE);return;
case YELLOWPURPLE:
attributes.push_back(COMBINED_YELLOWPURPLE);return;
case NPCCOLOR:
attributes.push_back(FOREGROUND_INTENSE_CYAN);return;
case PURPLE:
attributes.push_back(FOREGROUND_MAGENTA);return;
case REDPURPLE:
attributes.push_back(COMBINED_REDPURPLE);return;
case BLUE:
attributes.push_back(FOREGROUND_BLUE);return;
case BLUEPURPLE:
attributes.push_back(COMBINED_BLUEPURPLE);return;
case INTENSEGREEN:
attributes.push_back(FOREGROUND_INTENSE_GREEN);return;
case REDGREEN:
attributes.push_back(COMBINED_REDGREEN);return;
case BWHITE:
attributes.push_back(BACKGROUND_WHITE);return;
}
}
EXAMPLE:
BEFORE CHARACTER WALKS OVER ROAD: BEFORE IMAGE
AFTER CHARACTER WALKS OVER ROAD: AFTER IMAGE
EXECUTABLE LINK: EXECUTABLE
I have created a working example that you guys can compile(Assuming u use windows) to see what is occuring. In it i create a console set the size fill the screen with the asci symbol 223. then i use Writeconsoleoutchar to print a green 223 symbol. You can see the extra drawn black pixel line.
#include <windows.h>
#include <cwchar>
#include <vector>
enum COLORS{HOVEREDCOLOR};
void setAttributesChar(short color,std::vector<WORD> &attributes)
{
switch(color)
{
case HOVEREDCOLOR:
attributes.push_back(FOREGROUND_GREEN);return;
}
}
void printchar(char character, COORD location,short color)
{
const char *pointer = &character;
//short LENGTH = 1;
DWORD dwWritten = 0;
std::vector<WORD> attributes;attributes.clear();;
setAttributesChar(color,attributes);//Pushes back the color
if(attributes.size() ==1)
{ WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),&attributes[0], attributes.size(), location, &dwWritten);
WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), pointer, attributes.size(), location, &dwWritten);
}
}
#define SCREENWIDTH 1550
#define SCREENHEIGHT 850
void SETWINDOWUP(DWORD &SIZE)
{
CONSOLE_SCREEN_BUFFER_INFO info;
MoveWindow(GetConsoleWindow(),0,0,SCREENWIDTH,SCREENHEIGHT,true); //MOVE WINDOW TO 0,0 AND MAKE 1600 BY 880
::SetWindowPos(GetConsoleWindow(), HWND_TOPMOST, 0, 0, 0,0, SWP_DRAWFRAME | SWP_NOREPOSITION | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); //SET WNDOWS POSITION COMPARED TO OTHER WINDOWS IN DRAW ORDER
::SetWindowPos(GetConsoleWindow(),HWND_NOTOPMOST, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
HANDLE hout = GetStdHandle( STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hout,&info);
COORD newBuffersize =
{
info.srWindow.Right - info.srWindow.Left + 1,
info.srWindow.Bottom - info.srWindow.Top + 1
};
SetConsoleScreenBufferSize(hout,newBuffersize);
SIZE = info.dwSize.X * info.dwSize.Y;
//EDIT REGULATED FONT SIZE
CONSOLE_FONT_INFOEX cfi;
std::wcscpy(cfi.FaceName, L"Lucida Console"); // Choose your font
cfi.FontFamily = FF_DONTCARE;
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 25; // Height
cfi.FontWeight = FW_BOLD;
cfi.cbSize = sizeof(cfi);
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), false, &cfi);
}
int main()
{
Sleep(500);DWORD SIZE = 0;SETWINDOWUP(SIZE);
DWORD charswritten;COORD start = {0,0};TCHAR CHARACTER = 223;
FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),CHARACTER,SIZE,start,&charswritten);
Sleep(500);
printchar(223,{6,5},HOVEREDCOLOR);
Sleep(500);
return 0;
};

C++ console application using double buffer size limitation

# include "DISPLAY.h"
DoubleBuffer::DoubleBuffer()
{
COORD size = { WINDOW_X_SIZE , WINDOW_Y_SIZE };
SMALL_RECT rect;
rect.Left = 0;
rect.Right = WINDOW_X_SIZE - 1;
rect.Top = 0;
rect.Bottom = WINDOW_Y_SIZE - 1;
hBuffer[0] = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleScreenBufferSize(hBuffer[0], size);
SetConsoleWindowInfo(hBuffer[0], TRUE, &rect);
hBuffer[1] = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleScreenBufferSize(hBuffer[1], size);
SetConsoleWindowInfo(hBuffer[1], TRUE, &rect);
CONSOLE_CURSOR_INFO cursorinfo;
cursorinfo.dwSize = 1;
cursorinfo.bVisible = FALSE;
SetConsoleCursorInfo(hBuffer[0], &cursorinfo);
SetConsoleCursorInfo(hBuffer[1], &cursorinfo);
nBufferIndex = 0;
}
void DoubleBuffer::WriteBuffer(int x, int y, char *string)
{
DWORD dw;
COORD startposition = { x,y };
SetConsoleCursorPosition(hBuffer[nBufferIndex], startposition);
WriteFile(hBuffer[nBufferIndex], string, strlen(string), &dw, NULL);
}
void DoubleBuffer::FlippBuffer()
{
Sleep(1000);
SetConsoleActiveScreenBuffer(hBuffer[nBufferIndex]);
nBufferIndex = !nBufferIndex;
}
void DoubleBuffer::ClearBuffer()
{
COORD coord = { 0,0 };
DWORD dw;
FillConsoleOutputCharacter(hBuffer[nBufferIndex], ' ', WINDOW_X_SIZE*WINDOW_Y_SIZE, coord, &dw);
}
void DoubleBuffer::ReleaseBuffer()
{
CloseHandle(hBuffer[0]);
CloseHandle(hBuffer[1]);
}
I used this code to construct a double buffer.
The entire game map which would be printed at the function 'WriteBuffer' was designed as a 1D char array.
But a big problem is that if the length of string goes over 80, then the console just shows 80 character in one line regardless of the console window size.
I mean, for example the input string is
char 1D_map[90] = {'D'};
and the console window size is 5*20.
Then no matter what the window size is, it shows like behind.
DDDDD (.. the left 70 characters are hidden..)
DDDDD (.. the left 5 character are hidden..)
As I pull the side bar of window to its maximum, it shows like behind.
DDDDDDDDDDDDDDDD ... 80 characters ... DDDDDDDDDDDDDDDDD
DDDDDDDDDD
So when I use these codes, then I cannot adjust the game map size.
(concretely horizontal size.)
Because whatever I do, it just shows 80 characters in a line!!
Can I fix the problem? or Should I apply other double buffering method?

How to use font without ligatures via WinAPI?

I'm trying to create simple application with custom output. I'm using CreateFont function in order to load monospaced font with specific options. As a result, the text has been drawn (DrawText) with ligatures such as fi.
How to disable this?
I am very sorry for my bad English
Creating font
normal = CreateFont(char_height, 0, 0, 0, FW_NORMAL, 0, 0, 0,
RUSSIAN_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, 5,
FIXED_PITCH | FF_DONTCARE, "Menlo");
bold = CreateFont(char_height, 0, 0, 0, FW_BOLD, 0, 0, 0,
RUSSIAN_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, 5,
FIXED_PITCH | FF_DONTCARE, "Menlo");
Printing
void Console::Print(string text, int color, int weight) {
RECT r;
r.top = padding + temp_y * (char_height + space_y);
r.bottom = r.top + char_height;
r.left = padding + temp_x * char_width;
r.right = padding + length();
HDC hdc = GetDC(hwnd);
SetTextColor(hdc, color);
SetBkColor(hdc, RGB(43, 48, 59));
SelectObject(GetDC(hwnd), weight == WEIGHT_NORMAL ? normal : bold);
DrawText(GetDC(hwnd), text.c_str(), text.length(), &r, DT_LEFT);
temp_x += text.length();
}
Current output (note the word "offline"):
Desired output:
The root problem seems to be that your font is broken. I'm not sure why a monospace (fixed-pitch) font would have ligature information.
Solution 1
Use ExtTextOutW with ETO_IGNORELANGUAGE. You may lose other functionality (like bi-di, digit substitution, shaping for complex scripts), but it does seem to prevent kerning and ligation, so it may be suitable for your purpose.
::ExtTextOutW(hdc, x, y, ETO_IGNORELANGUAGE, &rc, msg.c_str(), msg.size(), nullptr);
I tested with the font Gabriola, since I don't have Menlo or Meslo. Gabriola is a variable pitch font, but it has distinctive ligatures that make it easy to spot, especially fi.
Solution 2
A second approach, which has the same drawbacks, is to draw the strings character by character with TextOut in a loop. This is a little trickier because you have to worry about Unicode surrogate pairs, clipping, and updating the current position.
const auto old_alignment = ::SetTextAlign(hdc, TA_UPDATECP);
const auto old_mode = ::SetBkMode(hdc, TRANSPARENT);
::MoveToEx(hdc, x, y, nullptr);
// Loop simplified for demo. This doesn't handle Unicode surrogate pairs.
for (auto ch : msg) {
::TextOutW(hdc, 0, 0, &ch, 1);
}
::SetBkMode(hdc, old_mode);
::SetTextAlign(hdc, old_alignment);
This produced identical results to the first solution.
Non-Solution
Note that my earlier idea to use GetCharacterPlacement without the GCP_LIGATE flag followed by ExtTextOut with ETO_GLYPH_INDEX does not work. The glyphs returned from GetCharacterPlacement still included ligatures even without the GCP_LIGATE flag.
// DOES NOT WORK
GCP_RESULTSW results = {sizeof(results)};
WCHAR modified[64] = L""; // FIXED BUFFER LENGTHS JUST FOR TESTING
results.lpOutString = modified;
int deltas[64] = {0};
results.lpDx = deltas;
WCHAR glyphs[64] = L"";
glyphs[0] = 1;
results.lpGlyphs = glyphs;
results.nGlyphs = ARRAYSIZE(glyphs);
const DWORD flags = GCP_REORDER; // but not GCP_LIGATE or GCP_USEKERNING
::GetCharacterPlacementW(hdc, msg.c_str(), msg.size(), 0, &results, flags);
::ExtTextOutW(hdc, x, y, ETO_GLYPH_INDEX, &rc, glyphs, results.nGlyphs, deltas);

C++ WriteConsoleOutput Display Issues

I've started making a simple game in c++.
At this stage I have all the characters and attributes (tiles) stored in a file that is then loaded in to CHAR_INFO. Then using the WriteConsoleOutputA I can basically paint the map/world.
That all appears to work correctly and there is no display issues. However when I start to move my character around he is mucking up the tiles. It a bit hard to explain so here is a little video. http://www.youtube.com/watch?v=v5Be5qz30r8.
As you can see there is not problem with the green tiles (foreground and background are same colour). But when I go near the grey tiles it isn't working as intended.
main.cpp snippet...
int main()
{
while (0 < 1)
{
if (GetAsyncKeyState(VK_UP) || GetAsyncKeyState(0x57))
player.Move('u');
if (GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState(0x53))
player.Move('d');
if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState(0x41))
player.Move('l');
if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState(0x44))
player.Move('r');
Sleep(100);
}
}
Character class snippet
void Character::Move(char d)
{
COORD oldLoc = loc;
char walkableTiles[12] = {
4, 179, 191, 192, 193, 194, 195, 196, 197, 217, 218, 255
};
//set character
switch (d)
{
case 'u' :
loc.Y--;
break;
case 'd':
loc.Y++;
break;
case 'l':
loc.X--;
break;
case 'r':
loc.X++;
break;
}
//setup the buffer and location of tiles
CHAR_INFO newTile[1];
COORD buffSize = {1,1};
COORD buffPos = {0,0};
SMALL_RECT newBuffArea = {loc.X, loc.Y, loc.X, loc.Y};
SMALL_RECT oldBuffArea = {oldLoc.X, oldLoc.Y, oldLoc.X, oldLoc.Y};
//store newTile info
ReadConsoleOutputA(outHnd, newTile, buffSize, buffPos, &newBuffArea);
//check if newTile is valid
bool valid = 0; // assume not valid
for (int i=0; i < sizeof(walkableTiles); i++)
{
if (newTile[0].Char.AsciiChar == walkableTiles[i])
valid = 1;
}
if (!valid)
{
loc = oldLoc;
return;
}
//restore oldTile information
WriteConsoleOutputA(outHnd, oldTile, buffSize, buffPos, &oldBuffArea);
//newTile becomes oldTile
oldTile[0] = newTile[0];
//get bg of newTile and set character/attributes
int bg = (newTile[0].Attributes & Blue << BG ) | (newTile[0].Attributes & Red << BG) | (newTile[0].Attributes & Green << BG) | (newTile[0].Attributes & BACKGROUND_INTENSITY);
newTile[0].Char.AsciiChar = 1;
newTile[0].Attributes = White | bg;
//place character on newTile
WriteConsoleOutputA(outHnd, newTile, buffSize, buffPos, &newBuffArea);
Apologies for the code, this is my first game.

Problem in Next Line in Edit Box in Win32C++

I'm having a problem where in displaying my output. Whenever I display the output instead of having multiple lines of output i only got single line output.
Here is my Current code:
int TextLength = GetWindowTextLength(GetEditControl) + 1;
TCHAR Text[100000];
GetWindowText(GetEditControl, Text, TextLength);
if(TextLength > 1) {
vector <string> filelist;
string path;
path = Text;
path = stripPath(path);
filelist = GetPath(path);
stringstream buffer;
copy(filelist.begin()+1, filelist.end(),ostream_iterator<string>(buffer, "\n"));
SetWindowText(EditShow,buffer.str().c_str());
for(unsigned i=0;i<=99999;i++)
{
Text[i]='\0';
}
} else
{
MessageBox(NULL, "No Text", "Error", MB_OK | MB_ICONERROR);
}
this is the property of the Edit box
EditShow = CreateWindowEx(0, TEXT("Edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL ,
60, 70, 400, 150,
GetWindow, (HMENU)ID_EDITSHOW, NULL, 0);
Try specifying "\r\n" to separate the strings instead of just "\n".