Text Function

This is a discussion on Text Function within the C++ Programming forums, part of the General Programming Boards category; I've looked at other posts about this. Also, I searched the board. Usally I'll find what I'm looking for. But, ...

  1. #1
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269

    Text Function

    I've looked at other posts about this. Also, I searched the board. Usally I'll find what I'm looking for. But, almost always I won't understand it =/. Well, I want to know how to write a function so I can easily use text colors without have to write it all out over and over. Or at least tell me a thread that explains it really good. I just don't understand other threads.

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Should we assume you are outputting the text to a console window and your target OS is MS Windows? [maybe you should tell us]
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Windows ME
    Dev-C++ 4.9.8.0
    Console

    So, yeah

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    No. I mean a function (something I can call) that will make it easier to call colors. Instead of:

    Code:
    SetConsoleTextAttribute(hOut, FOREGROUND_RED);
    something like:

    Code:
    TextColor(hOut, F_Red);
    or something like that. I didn't find other posts of any help. I can't think of a way to do this without like a million if / else if statements.

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If your compiler doesn't provide one, just make one yourself to encapsulate the necessary code.

    Code:
    void myWriteColoredTextFunction(char *text, int FG, int BG)
    {
    
      // Code goes here
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    I wrote this class a while ago, it uses windows and colours and stuff. It may be useful to ya or not. Hope it is. It is not standard compliant neither.

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    class TWindow
    {
      int Left, Top, Right, Bottom, NumWindows, TxtColour, WinColour;
      char *Title;
    
      public:
        TWindow(int, int, int, int, int, int);
        void Draw(void);
        void SetWinSize(int, int, int, int);
        void SetTitle(char *TitlePtr, int TextCol)
             {strcpy(Title = new char[strlen(TitlePtr) + 1], TitlePtr);
              TxtColour = TextCol;}
        void SetWinColour(int Colour){WinColour = Colour;}
        ~TWindow(){delete []Title;}
    };
    
    TWindow::TWindow(int WinLeft = 1, int WinTop = 1, int WinRight = 80,
                     int WinBottom = 25 , int FontColour = WHITE,
                     int BackColour = BLUE)
    {
      strcpy(Title = new char[20], "Default Window");
    
      Left = WinLeft;
      Top = WinTop;
      Right = WinRight;
      Bottom = WinBottom;
      TxtColour = FontColour;
      WinColour = BackColour;
    }
    
    void TWindow::Draw(void)
    {
      int Temp;
    
      window(Left, Top, Right, Bottom);
    
      textbackground(WinColour);
      textcolor(TxtColour);
    
      for(int i=0; i < Bottom; i++)
        for(int j=0; j < Right; j++)
          cprintf(" ");
    
      Temp = (((Right - Left) - strlen(Title)) + 1)/ 2 ;
      gotoxy(++Temp, 1);
    
      cprintf("%s", Title);
    }
    
    void TWindow::SetWinSize(int WinLeft, int WinTop, int WinRight, int WinBottom)
    {
      Left = WinLeft;
      Top = WinTop;
      Right = WinRight;
      Bottom = WinBottom;
    }
    
    int main(void)
    {
      TWindow Win1;
    
      Win1.Draw();
    
      TWindow Win2(5, 5, 60, 18, YELLOW, GREEN);
      Win2.Draw();
    
      TWindow Win3(8, 8, 40, 14);
      Win3.Draw();
    
      Win1.SetWinColour(RED);
      Win1.SetWinSize(10, 9, 35, 9);
      Win1.SetTitle("Red Window", LIGHTGRAY);
      Win1.Draw();
    
      getchar();
    
      return 0;
    }
    Be a leader and not a follower.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Here's a nice header written by me.

    It allows code of the form:
    Code:
    using namespace Petter;
    cout << RED << "This is red." << BLUE << "This is blue.";
    And it's ANSI/ISO compatible. But you won't see any color unless compiling for Win32.
    Attached Files Attached Files
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21