Sign in to follow this  

Console formatter design issue.

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

For anyone that read the other topic I posted, this is it. Not able to delete it as too much time has elapsed since I posted it. Anyway, I've removed everything else as it seems there are 2 methods you can use to directly do what my code accomplished anyway. Those 2 methods also reproduce the same problem I was facing, so at least I know that my code was fine. However, now that I've removed my own custom formatter, it's mean that I'm not so sure of my design structure anymore. Here's what I previously had:
Component - ITypewriterOutDevice
|
+-- virtual void Play(const std::string& input, int interval) = 0;

Leaf - TypewriterOutConsoleDevice derives from ITypewriterOutDevice
|
+-- void Play(const std::string& input, int interval);


Component - ITypewriterFormatter
|
+-- virtual std::string Format(std::string input) = 0;

Leaf - TypewriterConsoleFormatter derives from ITypewriterFormatter
|
+-- std::string Format(std::string input);


Composite - Typewriter
|
+-- ITypewriterOutDevice* mOutDevice
+-- ITypewriterFormatter* mFormatter
|
+-- void Format();
+-- void Play();
Sorry for the crappy, long-winded notation but the basic idea is for the Typewriter class to playback text in the console with a typewriter sound. I thought that would be cool for a simple text-based RPG and then I realised that potentially, I could use the same effect again, just outputting it and formatting it in a different way, such as in DirectX with DrawText or whatever the method is called. The intention was to have something like:
#include <string>

#include "Typewriter.h"
#include "TypewriterOutConsoleDevice.h"

int main()
{
	std::string input("Shortened string to stop forum stretching.");

	TypewriterOutConsoleDevice typewriterDevice;
	TypewriterConsoleFormatter typewriterFormatter;
	Typewriter typewriter(input, 40, &typewriterDevice, &typewriterFormatter);

	typewriter.Format();
	typewriter.Play();

	return 0;
}







The problem is, the method I've changed to in order to playback the text in the console, now uses ReadConsoleOutput and WriteConsoleOutput which took care of the formatting I was doing in the first place, so it's seemingly made TypewriterConsoleFormatter redundant. However, I'm not convinced that what I'm left with is the right way of progessing. Here's the Play method from TypewriterOutConsoleDevice:
void TypewriterOutConsoleDevice::Play(const std::string& input, int interval)
{
	// Console size.
	const int consoleWidth = 80;
	const int consoleHeight = 25;

	// Grab a handle to the console.
	HANDLE hOutput = (HANDLE) GetStdHandle(STD_OUTPUT_HANDLE);

	// Setup a drawing rect.
	COORD dwBufferSize = { consoleWidth, consoleHeight };
	COORD dwBufferCoord = { 0, 0 };
	SMALL_RECT rcRegion = { 0, 0, consoleWidth - 1, consoleHeight - 1 };

	// Define a buffer the size of the console.
	CHAR_INFO buffer[consoleHeight][consoleWidth];

	// Grab the output from the console and initialise the buffer.
	ReadConsoleOutput(hOutput, (CHAR_INFO *) buffer, dwBufferSize, dwBufferCoord, &rcRegion);

	DWORD startTime = GetTickCount();
	int row = 0, column = 0;

	for (std::string::size_type size = 0; size < input.size(); )
	{
		// Only process a character if the elapsed time is larger
		// than the specified interval.
		if (GetTickCount() - startTime > (DWORD)interval)
		{
			buffer[row][column].Char.AsciiChar = input[size];
			buffer[row][column].Attributes = 0x0E;

			// Write the current output to the console and play the typing sound.
			WriteConsoleOutput(hOutput, (CHAR_INFO *) buffer, dwBufferSize, dwBufferCoord, &rcRegion);
			PlaySound(L"typewriter.wav", NULL, SND_ASYNC | SND_FILENAME);

			// Update timer.
			startTime = GetTickCount();

			// Got to the end of the row, so reset the column
			// and move to the next row, else move to next column.
			if (column == consoleWidth - 1)
			{
				row++;
				column = 0;
			}
			else
				column++;

			// Make the invariant true again.
			++size;
		}
	}
}







You can see that I've basically got no real way of formatting the output besides hardcoding it here. Could anyone offer me a shove in the right direction as to how I could get the formatting flexibility back please? Sorry it's so long. Cheers. :) [Edited by - SeymourClearly on February 10, 2008 7:01:53 AM]

Share this post


Link to post
Share on other sites
OK sorry, I'm not being clear enough. Right now all of the output to the console comes out in yellow text. If I wanted maybe to add a border around the text or change the colour of the text, right now, I have no way of doing that except by hardcoding it into the TypewriterOutConsoleDevice::Play method.

For example, it'd be nice if I could customise the way each room in my text RPG looked, so I could have different coloured writing in different rooms. I wouldn't really be able to do that now and this is where I've lost the thread a bit.

Share this post


Link to post
Share on other sites

This topic is 3598 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this