Page 1 of 1

usin color stuff in c++

Posted: Thu Aug 01, 2002 1:07 pm
by Tenchi_Masaki6
Hello People! Can somone help me... I want to use color in c++... I want to make like little windows and stuff like that. Can somone pleeeeeeeeeeze help me. Thanks

Re:usin color stuff in c++

Posted: Thu Aug 01, 2002 1:29 pm
by matt aka kernel
if you want to make windows, get visual c++ (cheap on ebay). you MIGHT be able to find a free (but really old) version of vc++ on the internet.

Re:usin color stuff in c++

Posted: Thu Aug 01, 2002 1:30 pm
by crazybuddha
You are going to have to learn to do research and present specific code that you are having trouble with. Most of the topics we deal with in here are complicated and if you ask general questions, you will get meaningless answers, if any at all.

C++ doesn't directly support "color". You either have to program the hardware yourself, or use existing system services, such as Win32 API, BIOS, DOS, etc.

Making "little windows" is harder than you are probably imagining. But if you can program a square in a video graphics mode, then you have a fighting chance. If not, you need to back up a few steps.

Re:usin color stuff in c++

Posted: Thu Aug 01, 2002 2:03 pm
by Tenchi_Masaki6
I did some searching and i found this code. And i have Visual Studio... Visual C++ 6 and Visual Basic 6.








   /************************************************
   *The declaration for WORD; COORD; DWORD; HANDLE**
   ************************************************/
#include <windows.h>
   
   /***************************************************
   *The declaration for cout; FOREGROUND_; BACKGROUND_*
   ***************************************************/
#include <iostream.h>
   /************************************************
   *The declaration of an objet HANDLE*
   ************************************************/
HANDLE cxHandle;
   /******************************************************************
   *It's the function to Set posX, posY, the color And Print the Text*
   ******************************************************************/
void SetPosXYAndColor(int x, int y, LPSTR Text, WORD Color);
   /************************************************
   * Macro colors of Text*
   ************************************************/
#define BLUE FOREGROUND_BLUE
#define LIGHTBLUE FOREGROUND_BLUE | FOREGROUND_INTENSITY
#define RED FOREGROUND_RED
#define LIGHTRED FOREGROUND_RED | FOREGROUND_INTENSITY
#define GREEN FOREGROUND_GREEN
#define LIGHTGREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define PURPLE FOREGROUND_BLUE | FOREGROUND_RED
#define LIGHTPURPLE PURPLE | FOREGROUND_INTENSITY
#define BLUEGREEN FOREGROUND_BLUE | FOREGROUND_GREEN
#define LIGHTBLUEGREEN BLUEGREEN | FOREGROUND_INTENSITY
#define ORANGE FOREGROUND_GREEN | FOREGROUND_RED
#define YELLOW ORANGE | FOREGROUND_INTENSITY
#define INTENSITY FOREGROUND_INTENSITY
   /************************************************
   *Macro colors for background *
   ************************************************/
#define BBLUE BACKGROUND_BLUE
#define BLIGHTBLUE BACKGROUND_BLUE | BACKGROUND_INTENSITY
#define BRED BACKGROUND_RED
#define BLIGHTRED BACKGROUND_RED | BACKGROUND_INTENSITY
#define BGREEN BACKGROUND_GREEN
#define BLIGHTGREEN BACKGROUND_GREEN | BACKGROUND_INTENSITY
#define BPURPLE BACKGROUND_BLUE | BACKGROUND_RED
#define BLIGHTPURPLE BPURPLE | BACKGROUND_INTENSITY
#define BBLUEGREEN BACKGROUND_BLUE | BACKGROUND_GREEN
#define BLIGHTBLUEGREEN BBLUEGREEN | BACKGROUND_INTENSITY
#define BORANGE BACKGROUND_GREEN | BACKGROUND_RED
#define BYELLOW ORANGE | BACKGROUND_INTENSITY
#define BINTENSITY BACKGROUND_INTENSITY
void main()
{
   HANDLE handle = GetStdHandle ( STD_OUTPUT_HANDLE );
   SetConsoleTextAttribute ( handle, YELLOW | BACKGROUND_INTENSITY );
   cout << "lalala\n";
}




So it is proven there is color in C++

Re:usin color stuff in c++

Posted: Thu Aug 01, 2002 2:27 pm
by Schol-R-LEA
I hope y don't take this the wrong way, but it appears that you're confusing a specific library (to wit, the Windows API) with the language itself. It is, of course, possible to use color in C++ with an appropriate library reference; but C++ - the core language - has no standard support for color, nor does the standard function libraries or the STL.

The declarations you see in this example are all specific to Windows - they define the values that Windows uses for those colors, so that a Visual C++ program can pass the values as named constants rather than 'magic numbers'. It is not a part of C++ itself.

If you are looking to write your own video library for a new operating system, then you have a lot of work ahead of you. While you can write at least part of it C++, you will still have to code the video card driver and the library routines from scratch.

Writing code to access standard 640x 480 VGA video is tedious and time consuming, but not overly difficult; the standard is well supported by most cards, and reasonably well documented if you can find the right books or web sites.

OTOH, writing drivers to access the more powerful SVGA and 2D/3D accelerator features of modern video cards (S3, GForce, RIVA, etc.) without using the VESA BIOS is likely to drive you insane - each card works in a different, proprietary way, and the details of how to access the best toys (z-buffering, fast polygon mapping) are usually poorly documented, if at all. The BIOS is much easier, but in all but a handful of cards, it requires you to drop into real mode or v86 mode to call use them - which is probably more trouble than it's worth, unless you simply use unreal mode for everything.

On the gripping hand, you are looking to write MS Windows code, then the best place to start is with Microsoft's documentation. I'm not sure if that's going to be any easier than writing your own video library, however ;)

Re:usin color stuff in c++

Posted: Thu Aug 01, 2002 2:32 pm
by Tenchi_Masaki6
Hmmm... u know... i think i will stick with just programming instead of writing my own os for a while ;D i wanna get good at programming so i can make programs for your people's os's some day. heh. U never know, i might want to start making an os. One day

Re:usin color stuff in c++

Posted: Thu Aug 01, 2002 3:55 pm
by srg
Don't give up on writing an os, I havent yet and I'm not fully clued up on C or asm yet! (getting there though).

Rather than writing a GUI based os like windows, maybe you should start writing a text mode based one, that is what I'm planning on doing. I personally will write my os (when I finally get it started, I'm currently writing small programs that show me how to program the hardware directly) in soely text mode, then I will add graphics mode support into my VGA driver and rest of os.

Basically, like the others, I am just saying don't try to walk before you can crawl, you'll get there eventually!

But NEVER give up! I'm not, and I want to write my stuff in asm!

Hope This helps
Steven Graham

Re:usin color stuff in c++

Posted: Thu Aug 01, 2002 3:58 pm
by Tenchi_Masaki6
Hmmm, mabey i shouldent give up. Not just yet. I would like some help though. Whare should i start? Should i learn asm? What is asm...ive seen stuff about it...Nasm i think it was. Any way. Please help

Re:usin color stuff in c++

Posted: Wed Aug 07, 2002 8:16 am
by frank
Asm is a programming language which can access the hardwar e directly.
I suggest you download nasm first (my favorite assembler) from nasm.sourceforge.net

Then write this little code:
--
org 7c00h
jmp main
main : jmp main

section .data
times 502-($-$$) db 0
sign dw 0xAA55
--
compile it
unix/linux: nasm boot.asm -f bin
win : nasmw boot.asm -f bin

then write it to a floppydisk
unix/linux : dd if=boot of=/dev/fd0
windows: partcopy (don't know exactly how t hat works, i run freebsd, see a topic about partcopy)

After this, try to do something more... like outputing text, jumping, calling etc...
After you managed that try to jump to your kernel.
(read it in using int 13h, - then do a jump to the addr...)
(see my topic for example)

If you need more help, just tell us :)

Re:usin color stuff in c++

Posted: Wed Aug 07, 2002 2:18 pm
by Schol-R-LEA
Assembly language, put simply, is a human-readable analog of the actual machine operation codes (opcodes) which the CPU actually performs. Assembly language instructions have nearly a one-to-one relationship to the opcodes they represent, though some instructions represent more than one opcode, and some opcodes have more than one instruction name. For example,

MOV AX, FF01h ; copy the number FF01 hex (65281 decimal) into register AX

would be translated into (in hexidecimal):

B8 01 FF

"B8" being the opcode for "Move the next two bytes into register AX". Since the x86 is a little-endian processor (it moves to least significant byte first, then the next higher, and so forth), the assembler automagically reverses order of the bytes in the data word. (if none of this is making sense yet, don't worry; it will become clearer in time).

Just about any assembler (the program which translates the instructions into the binary opcodes) will also allow you to define symbolic labels (names for memory locations) and set aside memory for variables, as well. They also take what are called 'directives', which indicate how it should assemble the code (similar to pragmas, but more integral to the assembly process). Finally, most have a macro preprocessor of some kind.

Netwide Assembler (NASM) is the most popular assembler for x86 systems right now. Others include gas (Gnu Assembler), [http://fasm.sourceforge.net/]Flat Assembler[/url] (FASM), MS Macro Assembler (MASM), and Turbo Assembler (TASM). The first three are probably the ones you'll want to choose from. Most of these use some variant of the syntax Intel originally defined for 8086 family, but gas used the more general AT&T syntax, which is used by Unix and related systems on just about every other platform but is rarely seen in x86 circles. Which one you use is up to you, but NASM is what most people on this board use and is the lingua franca of x86 programming these days.

Assembly is at once both easier and harder than other forms of programming; while assembly instructions are all quite simple, you have to know the details of the computer and the operating system you are using, and even medium-sized programs can be very long and difficult to debug. Learning assembly will give you considerable insight into the behavior of high-level programming languages, as it is, in a sense, what the compiler has to turn a program in (for example) C++ into.

If you intend to write an OS, you must use a certain amount of assembly language, as there are a handful of necessary operations which can only be written in assembly. If you use a high-level language, you'll also need to learn certain details of how the compiler you are using generates code, particularly how it passes function areguments and return values.

Some of the folks here will tell you that all you have to do is learn the instruction mnemonics, and you'll have assembly programming down. If only it were that simple; the instructions are actually the easy part. On the x86, the really difficult stuff comes from understanding the memory segmentation system, which is rather odd and hard to get a grasp on. It is not impossible, however, and Duntemann's book (see below) explains it quite well.

I usually advise newcomers to the art to buy a copy of Jeff Duntemann's Assembly Language Step by Step, second ed., as it is the best beginner's book on the subject I've found so far (the web site also has some useful links, as well). There are a number of good sites on the web about assembly programming, as well. A few to try are:

x86 Assembly Language FAQ for comp.lang.asm.x86
Gavin's Guide to 80x86 Assembly
Assembly Language Tutorial
PC Assembly Language Page
Paul Hsieh's Assembly Language Page (Paul is one of the Gurus of x86 assembly, and the page has oodles of good links)
Assembly Language (x86) Resources
and finally,
The Jargon File - ok, so it's not directly relevant, but I always recommend it, so why stop now?