View Full Version : Another C++ Question regarding Rectangles...
cameronlanni
12-01-2006, 11:08 PM
I simply need to implement a few rectangles into my C++ console program. Preferably 4, 1 in each corner. As I understand, it should be a simple task, but alas - I can't seem to get it right. I have found some web pages on creating rectangles, basically copy & paste code, but it turns out that it isn't C++. Anyone who could point in the right direction either by refering me to a site, telling me how to do it, or simply sending or posting the code (if its simple) is greatly appreciated.
Thank you so much!
Cameron
cameronlanni
12-02-2006, 01:12 AM
And it's worth mentioning that this is not for school, or work or anything. I'm simply playing around in my free time with a new text based game. Also, I should note that I will end up displaying text (and variables, etc.) in these four boxes, so I'm not sure if it has to be done differently, that being said.
Thanks again,
Cameron
Since it's a console application, all graphics will have to be faked using ASCII characters. So a rectangle might look like...
-----------
| |
-----------
I don't know of any other way to do it. Here's two rectangles.
1 ----------- -----------
2 | | | |
3 ----------- -----------
The thing about printing to the console, is you can't "go back" up lines in the console and print more stuff. To be more clear, to make the 2 rectangles above you'd have to print everything on line 1, the top line for both rectangles, followed by everything on line 2, and then 3.
If you want to print variables in the rectangles you must also do that when printing the rectangles themselves. i.e.
string name = "Gox";
cout << "| " << name << " |" << endl;
This would print something that looks like
| Gox |
i.e the middle portion of the rectangle with information in it.
To make life simpler you may want to make a method or Rectangle class that will print a rectangle in part or in full on the screen. You may also want the method or class to take a parameter specifying how many to print.
Here's a little more code
//I'm not gonna write the Rectangle class, but here's how I invision that it might be used
Rectangle r = new Rectangle();
r.printTop(2); //Means print 2 rectangle tops on the screen. You could add another parameter to tell the method how much space to put between the two tops
r.printMiddle(2); //Could add another parameter allowing a variable to be printed in the middle of the rectangle. i.e. r.printMiddle(1, name);
r.printBottom(2);
//This pseudo code would attempt to print the two rectangles in my previous example.
oracleguy
12-03-2006, 08:01 AM
The thing about printing to the console, is you can't "go back" up lines in the console and print more stuff. To be more clear, to make the 2 rectangles above you'd have to print everything on line 1, the top line for both rectangles, followed by everything on line 2, and then 3.[/code]
Not quite true. You can use the SetConsoleCursorPosition method to move the cursor to a different point in the console window and then thats where cout will write its data. And it will overwrite whatever is already there (if anything). There are a couple caveats to using it, mainly the coordinate system is relative to the window, so if the data moves, its coordinates change.
Here is some example code that was used in a for loop to update the percent complete of a function:
//Update the percentage
COORD point;
point.X = 0;
point.Y = 5;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
int percent = int(double(x) / num * 100);
cout << percent << "% generated.";
Though depending on what you are doing and how you implement it this could get messy. But it works well if you wanted to say, fill the background all with a color then go back and write your data on top of it.
Oh yeah, and you'll need to include windows.h to get that function.
oracleguy
12-03-2006, 08:20 AM
And thinking about it more, designing some classes to handle drawing some of this stuff might work. Or at least a draw class with some different methods, for drawing different shapes. Oh actually, what you could do is have a base class (lets call it like shape) that has some of the basic methods you'll need for drawing stuff and a virtual draw function. And then make your rectangle class inherit shape and provide a draw function. Then you could have methods for setting the contents of the box. Then you can also make any other sort of shape classes you might need for drawing your graphics.
The advantage then would be when you go to draw, you can retain the current cursor position, then go and draw the shape, then reset the cursor position to what it was before. And since the object would know where it drew it, you could add functionality to set the text inside of it easier. So like the functionality to save and restore the cursor position would go in the base class.
It would at least abstract out that functionality from the game. Also look at the ASCII table there are characters in there that let you draw more real boxes than just using: | and -
Hopefully that at least makes some sense.
Not quite true. You can use the SetConsoleCursorPosition method to move the cursor to a different point in the console window and then thats where cout will write its data. And it will overwrite whatever is already there (if anything). There are a couple caveats to using it, mainly the coordinate system is relative to the window, so if the data moves, its coordinates change.
Huh, I wasn't aware of that. Thanks for the info!
Gox
oracleguy
12-03-2006, 10:26 AM
Huh, I wasn't aware of that. Thanks for the info!
Gox
No problem.
In most console applications it doesn't really have much of use so its one of those things you typically only know if you've had to use it.
cameronlanni
12-04-2006, 07:20 PM
Thank you all so much for your help. I am beggining to see that adding boxes in my console application is going to be slightly more difficult than creating iframes in HTML or JavaScript. What language would be a better way to go for what I need to accomplish. I'd hate to learn yet another language, but it sounds like it would be worth it in the long run. I am thinking that Basic may be an easy way to go. I don't know Basic currently, but I just got Microsoft Visual Studio 2005, so I'm ready to learn it if someone says I can do what I need to do in Basic. Again, I appreciate all of your help.
Cameron
Aradon
12-04-2006, 07:32 PM
Well does it have to be a console application? Why not create a GUI? It would be a good learning experience on GUI making through C++ using the Windows API, etc.
You could always write a GUI in Java as well, but if you aren't interested in learning new languages, then I suppose you don't want to do that. :(
rpgfan3233
12-04-2006, 08:14 PM
To be honest, Java would be easier for a GUI via Swing. I haven't messed with any GUI stuff for Windows in C++ after seeing the default "skeleton code" that Dev-C++ had for a Windows API project. On the other hand, I have used Windows API for console applications. For example, text colors ( SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), <color>); where <color> is a value 0-255 ).
I could get into how text colors work as far as the binary part of it (it is all binary), but let's just say all you need to know is this:
background color -
128 = brightness
64 = red
32 = green
16 = blue
foreground color -
8 = brightness
4 = red
2 = green
1 = blue
(4 | 2 (bitwise OR operator) would give you yellow text with a black background; 32 | 16 would give you black text (foreground is omitted, remember?) with a cyan background; 32 | 16 | 4 | 2 would give you yellow text with a cyan background)
I think I might have just confused you though...
Anyway, if you need more help, I or someone else might be able to explain it better.
cameronlanni
12-04-2006, 08:15 PM
I know Java, as well as C++ (console), JavaScript, CSS, PHP, and of course HTML. I've been advised to use the GUI in C++, but I was also told that is essentially would be like learning a new language. I have no idea where to get started to do this, but if I get word from any of you guys that the GUI would be less time consuming than learning Basic, then, by all means - I'd prefer to use C++. I just finished installing my Visual Studio and have began messing around with Basic, so far, it seems to be working, but I haven't tried anything advanced yet. But, assuming I decide to use C++, how would I make the jump out of the console? Where should I turn to, to learn C++ GUI? Thanks again.
Cameron
cameronlanni
12-04-2006, 08:18 PM
I know how to change text colors in C++ console, I read a tutorial about it a long time ago, so that's never been a problem. Also, I found a header file that someone had written for the C++ console that essentially allows you to type "color=red" to change the text to red. So I know how to do that. But I need to create dynamic, interactive frames. It sounds like I really can't do that with the console, so I need to figure out which direction to go. I'd prefer not to use Java, only because I haven't used it in so long, but if need be - I will.
oracleguy
12-04-2006, 08:47 PM
You can do what you want in the console for sure. Its just going to be a bit of work getting everything setup initially before you can start writing your game.
If you wish to try a GUI and still use C++, you should maybe look into using something like wxWidgets (http://www.wxwidgets.org/) which I personally find a little easier to use and has some pretty decent documentation. Not to mention its cross platform compatible.
cameronlanni
12-04-2006, 09:42 PM
I've looked into Widgets before, and couldn't quite grasp it, but I will take a look at that link and see if it helps.
You've all been awesome. Thanks so much.
Cameron
oracleguy
12-04-2006, 10:08 PM
I've looked into Widgets before, and couldn't quite grasp it, but I will take a look at that link and see if it helps.
You've all been awesome. Thanks so much.
Cameron
If you download their stuff, they do include a lot of example programs which is how I learned it since I couldn't really find any good tutorials.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.