Jump to content
  • Advertisement
Sign in to follow this  
Plasmarobo

Windows Console fails to write....

This topic is 4319 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

I have built a Console Output class. I try to use this class and it doesn't work. No errors, nothing. It just doesn't write. I had a similar problem recently so this is really starting to annoy me... anyway, these are the classes that I have problems with: Declarations and expations (they aren't very big I know)
//ScreenController
//This will do stuff to the screen

#include <stdlib.h>
#include <windows.h>
#define INPUT GetStdHandle(STD_INPUT_HANDLE)
#define OUTPUT GetStdHandle(STD_OUTPUT_HANDLE)



#ifndef IOCONTROL_H_
#define IOCONTROL_H_
#define BLUE 0x0009
#define RED 0x0012
#define GREEN 0x0010
#define DARKBLUE 0x0001
#define DARKRED 0x0004
#define DARKGREEN 0x0002

class ConsoleControl
{
    private:
    CHAR_INFO buffer[80*32];



    public:
    ConsoleControl();
    ~ConsoleControl();

    bool Init(char *name);
    bool Display();
    bool Fill(char a);
    bool Putline(char sbuffer[80], int line);

};




bool ConsoleControl::Init(char *name)
{
COORD charBufSize = {80,50};
    COORD characterPos = {0,0};
    SMALL_RECT writeArea = {0,0,79,49};
    SetConsoleTitle(name);
    SetConsoleWindowInfo(OUTPUT, true, &writeArea);
    SetConsoleScreenBufferSize(OUTPUT, charBufSize);
    return true;
};

bool ConsoleControl::Display()
{
    COORD charBufSize = {80,50};
    COORD characterPos = {0,0};
    SMALL_RECT writeArea = {0,0,79,49};
    WriteConsoleOutput(OUTPUT, buffer, charBufSize, characterPos, &writeArea);
    return true;
};

bool ConsoleControl::Fill(char a)
{
 int MAX = 80 * 32;

 for(int m = 0; m = MAX; m++)
{
    buffer[m].Char.AsciiChar = a;
}

return true;
}

bool ConsoleControl::Putline(char sbuffer[80], int line)
{
 int startline = (line-1) * 80;
 //Get the line index(h will be added to this)

 for(int h = 0; h = 80; h++)
 {
    //copy the line
    buffer[startline + h].Char.AsciiChar =  sbuffer[h];
 }

 return true;
}

Yeah, Here is the MAIN funtion:
#include <iostream>
#include <windows.h>
#include "IOController.h"
#include "item.h"
#include "creatures.h"
#include "InputMan.h"

int main()
{
    bool active = true;
    //Handle init

ConsoleControl Controler;
Controler.Fill('=');

    //Start loop
    while(active)
    {
Controler.Display();
    }



Share this post


Link to post
Share on other sites
Advertisement
Why are you calling Display() in a tight loop? Consoles do not need to be redrawn constantly; the OS will take care of that for you. A single call to Display() should be sufficient.

Also, it looks like you are never properly initializing the contents of buffer. This means that the Attributes member of each character is unspecified and likely random/bogus data. If you are running a debug build of your code, chances are good that the debugger has cleared the memory to 0 for you. (If Attributes is 0, you'll be writing black-on-black, which obviously won't do much in the way of visible results.) You should never rely on uninitialized data - always initialize it yourself.


Make sure you set Attributes for each character you write. See MSDN for details on what values represent.

Share this post


Link to post
Share on other sites
Wow, I cannot belive that I missed that.... thanks alot!

But I still get some problems here.

I call my init function, which has been updated:

bool ConsoleControl::Init(char *name)
{
COORD charBufSize = {80,32};
COORD characterPos = {0,0};
SMALL_RECT writeArea = {0,0,79,31};
SetConsoleTitle(name);
SetConsoleWindowInfo(OUTPUT, true, &writeArea);
SetConsoleScreenBufferSize(OUTPUT, charBufSize);
for(int l = 0; l = (80 * 32); l++)
{
buffer[l].Char.AsciiChar = '=';
buffer[l].Attributes = BLUE;

}
return true;
};



It now properly resizes and names the window, but!, but but but! It still fails to display... ( I am keeping it in a loop cause I am using a back-buffer type display mode. Inspired by directX. ) It doesn't print (I might be mixing the DWORD in my define statements wrong:

#define BLUE 0x0001 | 0x0008
#define RED 0x0004 | 0x0008
#define GREEN 0x0002 | 0x0008
#define DARKBLUE 0x0001
#define DARKRED 0x0004
#define DARKGREEN 0x0002
#define WHITE 0x0001 | 0x0002 | 0x0004 | 0x0008

Any pointers?


[Edited by - Plasmarobo on July 28, 2006 6:31:49 PM]

Share this post


Link to post
Share on other sites
Why are you using literal hex numbers directly? It is advisable to use the standard constants; they are far more descriptive, less error prone, and will be recognizable to anyone using the Windows console API. If you want a shorthand, no problem:

const unsigned BLUE = FOREGROUND_BLUE | FOREGROUND_INTENSITY;

(And you should favor using consts or enums instead of #define.)

While I'm nitpicking, a boolean function that always returns true is a waste. Either make it void, or actually return a proper boolean value. In fact, it would be a good idea to error-check your API calls, in case you're tripping an error there someplace.

Share this post


Link to post
Share on other sites
bool ConsoleControl::Fill(char a)
{
int MAX = 80 * 32;

for(int m = 0; m = MAX; m++)
{
buffer[m].Char.AsciiChar = a;
}



The for loop looks wrong to me. I think you would be in a infinite loop here with your code or it could be that you are going over the bounds of your array. When you access buffer[m] your are actually accessing buffer at position 80 * 32 since m is set to 0 then to MAX in your for loop.

Change it to m less than MAX i.e:
for(int m = 0; m < MAX; m++)

Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!