Jump to content
  • Advertisement
Sign in to follow this  
skulldrudgery

Writing to the screenbuffer in Win32 Console

This topic is 4816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

*sigh* Some days I feel like I just started programming yesterday. Could someone help me out here. I want to pass an array of chars (or an array of CHAR_INFOs) to a function that will write it into the screen buffer. This is all text-based, Win32 Console stuff. When I draw the buffer to the screen, I get garbage. main.cpp
#include <iostream>
#include <windows.h>
#include <conio.h>
#include "screen.h"
    
using namespace std;
    
CScreen Console;
    
int main(int argc, char *argv[])
{
    char  Str[4] = {'|', '|', '|', '|'};
    COORD Pos[4]   = {{0, 0}, {0, 1}, {0,2}, {0,3}};
    
    Console.WriteToBuffer(Str, Pos);
    Console.DrawBuffer();
    
    while(!kbhit());
    return EXIT_SUCCESS;
}





screen.h
#ifndef SCREEN_H
#define SCREEN_H

#include <iostream>
#include <windows.h>

enum {
    CRed       = 1,
    CGreen     = 2,
    CBlue      = 4,
    CIntensity = 8
};
    
class CScreen {
public:
    CScreen(int width = 80, int height = 50);
    ~CScreen();
    
    void SetTitle(char *);
    void SetBackgroundColor(WORD);
    void SetForegroundColor(WORD);
    void SetPosition(COORD);
    void OutputText(char *);
    void WriteToBuffer(char *, COORD *);
    void WriteToBuffer(CHAR_INFO *, COORD *);
    void DrawBuffer();
    	
private:
    int        m_height;
    int        m_width;
    WORD       m_BGColor;
    WORD       m_FGColor;
    HANDLE     wHnd;
    CHAR_INFO *ScreenBuf;
    
    void ClearBuffer();
};

#endif






screen.cpp
#include "screen.h"
    
using namespace std;
    
CScreen::CScreen(int width, int height)
{
    m_width   = width;
    m_height  = height;
    wHnd      = GetStdHandle(STD_OUTPUT_HANDLE);
    ScreenBuf = new CHAR_INFO[width * height];
    
    SMALL_RECT WindowSize = {0, 0, m_width - 1, m_height - 1};
    COORD      BufferSize = {m_width, m_height};

    SetConsoleWindowInfo(wHnd, TRUE, &WindowSize);
    SetConsoleScreenBufferSize(wHnd, BufferSize);
    
    SetBackgroundColor(0);
    SetForegroundColor(CRed | CGreen | CBlue);
}
    
CScreen::~CScreen()
{
    // delete the screen buffer
}

void CScreen::SetTitle(char *Title)
{
    SetConsoleTitle(Title);
}

void CScreen::SetBackgroundColor(WORD Color)
{
    m_BGColor = 0;
    
    if(Color & CRed)
        m_BGColor |= BACKGROUND_RED;
    
    if(Color & CGreen)
        m_BGColor |= BACKGROUND_GREEN;
    
    if(Color & CBlue)
        m_BGColor |= BACKGROUND_BLUE;
    
    if(Color & CIntensity)
        m_BGColor |= BACKGROUND_INTENSITY;
    
    SetConsoleTextAttribute(wHnd, m_FGColor | m_BGColor);
}
    
void CScreen::SetForegroundColor(WORD Color)
{
    m_FGColor = 0;
    
    if(Color & CRed)
        m_FGColor |= FOREGROUND_RED;
    
    if(Color & CGreen)
        m_FGColor |= FOREGROUND_GREEN;
	    
    if(Color & CBlue)
        m_FGColor |= FOREGROUND_BLUE;
    
    if(Color & CIntensity)
        m_FGColor |= FOREGROUND_INTENSITY;

    SetConsoleTextAttribute(wHnd, m_FGColor | m_BGColor);
}
    
void CScreen::SetPosition(COORD Position)
{
    SetConsoleCursorPosition(wHnd, Position);
}

void CScreen::OutputText(char *String)
{
    DWORD Written;
    
    WriteConsole(wHnd, String, strlen(String), &Written, NULL);
}

void CScreen::WriteToBuffer(char *Array, COORD *Position)
{
    // Check size of char array verses COORD array. They should be the same length    
    int i = 0;
    int pos;
    
    while(Array && Position.X) {
        pos = Position.X + m_width * Position.Y;
        ScreenBuf[pos].Char.AsciiChar = Array;
        ScreenBuf[pos].Attributes = m_FGColor | m_BGColor;
        i++;
    }
}
    
void CScreen::WriteToBuffer(CHAR_INFO *Array, COORD *Position)
{
    // <-- Check sizes here -->
    
    int i = 0;
    int pos;
    
    while(Array.Char.AsciiChar && Position.X) {
        pos = Position.X + m_width * Position.Y;
        ScreenBuf[pos] = Array;
        i++;
    }
}

void CScreen::DrawBuffer()
{
    COORD BufSize   = {m_width, m_height};
    COORD Position  = {0, 0};
    SMALL_RECT Area = {0, 0, m_width - 1, m_height - 1};

    WriteConsoleOutput(wHnd, ScreenBuf, BufSize, Position, &Area);
    ClearBuffer();
}

void CScreen::ClearBuffer()
{
    int i;
    
    while(i < m_width * m_height) {
        ScreenBuf.Char.AsciiChar = ' ';
        ScreenBuf.Attributes = m_FGColor | m_BGColor;
        i++;
    }
}






Ok, lots of problems. It compiles fine, though. I know it's something that is going to make me smack my head, and I'm pretty sure that it has something to do with my screen buffer and maybe with my incomplete understanding of new. Or maybe some overlooked initialization. The function WriteToBuffer() should take an array of chars and their coordinates and write them into the screen buffer. DrawBuffer() should write the whole buffer to the screen. [Edited by - skulldrudgery on September 10, 2005 9:58:12 PM]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!