Recommended Posts

I'm trying to make a text based window like this in the dos console with C++ :

* * * Titel * * *
* *
* *
* * * * * * * * *

but for the moment all I can come up with is

* * * * * * * * *
* *
* *
* * * * * * * * *

I can't seem to find a good algoritme to create that first line with a text centered in it. The main problem for now is that I create the width with "* ".
Does any1 have any tips, or maybe links?
thanx in advance...

Share this post

Link to post

Share on other sites

I dont know how you are doing your drawing, but Consider what you know.-The width of the window-The number of characters in the title, strlen()

The distance between the title and the edge is: ((float)width-strlen("title"))/2Since your corners are always '*', subtract one:(((float)width-strlen("title"))/2f)-1This is the length between the corner and the title, exclusive.Divide by two again to see how many "* " pairs you need on each side of your title:((((float)width-strlen("title"))/2f)-1)/2(Should be right, but I didnt have a chance to test it)I'm sure you'll have to play around with it, but it should get you off on the right start.

It really depends on how you draw your windows. Are all the windows drawn seperately or do you just store the whole screen in an array?

I think the best way for you do it is to write a line drawing algorithm.Something like drawline(int x1, int y1, int x2, int z2, char* pattern)Ex: drawline(30, 20, 40, 50, "* ")

This way you can draw anyline you wish with any pattern you wish.Look into line drawing algorithms, Bresenham seems to be a popular one. You could easily adapt it to display any pattern you'ld like.

Share this post

Link to post

Share on other sites

[EDIT] I swear no one had posted when i began typing this! [embarrass][/EDIT]

Let's say you have 1 space after each *, as you said

On the first line you will have text that is supposed to be -cenetered-, but you need to figure out how many *'s you wanted on each side.

Lets say you wanted only 3 per side, as the example has it

Now for any text, lets say we had "Gamedev.net".

If you were to make a string such that:"* * * GameDev.net * * *" was stored,"* * * " + "GameDev.net" + "* * * ", you could the copy the string and write a for loop, so it would replace all even based indexes (since it starts at 0) with an *, so you would be left with:"* * * *a*e*e*.*e* * * *"

Now you need to loop through each odd based index and make it a " ", so you would be left with:"* * * * * * * * * * * *"

Now that you had the top and bottom, you would just need to draw in the sides (before the bottom tohugh), which you just use 0 and the sizeof the string: "* * * * * * * * * * * *"

Tell me how it works for you, I designed it in my head and gave it a quick test, and it worked fine. It is very inefficient and bad use of C++ standards, I mixed C/C++ together like crazy [lol], but since you are just making a dos console program, it sohuld be ok. Good luck!

Share this post

Link to post

Share on other sites

The '&1' is a simple trick to make a toggling value out of a counter.So the value of 'current' is ANDed with 1, which results in either 0 or 1. This is used as an index in chars[] to get the next character to draw.Hope you get it, I'm not very good at explaining. :)