Archived

This topic is now archived and is closed to further replies.

killer333

where can i find these functions???????????????

Recommended Posts

i need help(big surprise) what is the name of the header file that has the function that comes up with a random number? and one more thing i there a header that has a function that changes the color of the words the file i got dont work thanx

Share this post


Link to post
Share on other sites
> what is the name of the header file that has the function that comes up with a random number?

srand() and rand() are in stdlib.h

> i there a header that has a function that changes the color of the words

Which words?

Bri

Share this post


Link to post
Share on other sites
SetTextColor is a Windows function - not for the console. I don''t understand what you''re wanting exactly, and I don''t know of any text-color functions off-hand for the console - you could look through the manuals just as easily as me.

Bri

Share this post


Link to post
Share on other sites
srand seeds the random number generator, give it the current time or something (only do it ONCE - in main for example).

if you seed the generator with a constant, you will get identical ''random'' numbers generated each run (i think).


eg:
srand(GetTickCount());

int rndNum = rand() % 10; //generate a number in range 0<=x<10

the old borland text color stuff was in conio.h, but if you''re using MSVC, its not there.

Share this post


Link to post
Share on other sites
Maybe this function interests you too: timeGetTime()
You want to write a win console progi right?

If you want to know which header you need for which function then you should search for the "function-name" in the MSDN Library.
Here is a link to the online version.

Edited by - Jonus on January 30, 2001 6:53:23 PM

Share this post


Link to post
Share on other sites
Here''s some code to change MSVC''s console mode application''s text colors, et cetera. It wasn''t written by me, but by someone at Programmer''s Heaven''s C/C++ Message Board. If you email me, I can give you the source code outside of gamedev''s source tag.

  
//**************************** BEGIN CONSOLE.H ********************************
#ifndef ECONSOLE_H
#define ECONSOLE_H

#include <windows.h>

//==============================================================================
// class Console - Eric Tetz 10/5/99
//
// Encapsulates the Windows console API (some of it).
//
// Each process can have one and only one console. For this reason, all Console
// members are static. If your program is already a console app, you can freely
// call any method of this class. If your app is NOT a console app, you must
// first call Alloc() to create a console for your process and redirect IO to
// it. You may call Free() to detach your process from it''s console.
//
//==============================================================================

/* bool SetTitle (LPCSTR sTitle)
bool SetSize (int columns, int lines)
bool GetSize (int* pcolumns, int* plines)
bool SetCursorPos (int x, int y)
bool GetCursorPos (int* px, int* py)
bool SetCursorSize (DWORD dwPercentShown, bool bVisible = false)
bool SetTextColor (Color FgColor, Color BgColor = Black)
bool Clear()
*/

class Console
{
public:
enum Color
{
Black = 0,
Grey = FOREGROUND_INTENSITY,
LightGrey = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
White = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
Blue = FOREGROUND_BLUE,
Green = FOREGROUND_GREEN,
Cyan = FOREGROUND_GREEN | FOREGROUND_BLUE,
Red = FOREGROUND_RED,
Purple = FOREGROUND_RED | FOREGROUND_BLUE,
LightBlue = FOREGROUND_BLUE | FOREGROUND_INTENSITY,
LightGreen = FOREGROUND_GREEN | FOREGROUND_INTENSITY,
LightCyan = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
LightRed = FOREGROUND_RED | FOREGROUND_INTENSITY,
LightPurple = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY,
Orange = FOREGROUND_RED | FOREGROUND_GREEN,
Yellow = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
};

public:
static bool Alloc();
static bool Free();
static bool SetTitle (LPCSTR sTitle);
static bool SetSize (int columns, int lines);
static bool GetSize (int * pcolumns, int * plines);
static bool SetCursorPos (int x, int y);
static bool GetCursorPos (int * px, int * py);
static bool SetCursorSize (DWORD dwPercentShown, bool bVisible = false);
static bool SetTextColor (Color FgColor, Color BgColor = Black);
static bool Clear();

protected:
static bool RedirectIoToConsole ();
};

#endif //ECONSOLE_H
//**************************** BEGIN CONSOLE.H ********************************







//*************************** BEGIN CONSOLE.CPP *******************************
#include "stdafx.h" // precompiled header directive
#include <iostream>
#include <wincon.h>
#include <fcntl.h>
#include <io.h>
#include "eConsole.h"

using std::ios;

extern "C" void ConsoleAlloc()
{
Console::Alloc();
}

bool Console::Alloc()
{
// If this is already a console app we don''t need to call
// RedirectToConsole(). AllocConsole() fails if this process
// already has a console.
return (AllocConsole() && RedirectIoToConsole());
}

bool Console::Free()
{
return FreeConsole();
}

bool Console::SetTitle (LPCSTR sTitle)
{
return SetConsoleTitle (sTitle);
}

bool Console::RedirectIoToConsole ()
{
HANDLE hStdHandle;
int nConHandle;

// redirect unbuffered STDOUT to the console
hStdHandle = GetStdHandle (STD_OUTPUT_HANDLE);
nConHandle = _open_osfhandle ((long)hStdHandle, _O_TEXT);
*stdout = *_fdopen (nConHandle, "w");
setvbuf (stdout, NULL, _IONBF, 0);

// redirect unbuffered STDIN to the console
hStdHandle = GetStdHandle (STD_INPUT_HANDLE);
nConHandle = _open_osfhandle ((long)hStdHandle, _O_TEXT);
*stdin = *_fdopen (nConHandle, "r");
setvbuf (stdin, NULL, _IONBF, 0);

// redirect unbuffered STDERR to the console
hStdHandle = GetStdHandle (STD_ERROR_HANDLE);
nConHandle = _open_osfhandle ((long)hStdHandle, _O_TEXT);
*stderr = *_fdopen (nConHandle, "w");
setvbuf (stderr, NULL, _IONBF, 0);

// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
// point to console as well
ios::sync_with_stdio();

return true;
}

bool Console::GetSize (int * pcolumns, int * plines)
{
HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);

CONSOLE_SCREEN_BUFFER_INFO coninfo;

if (GetConsoleScreenBufferInfo (hconsole, &coninfo))
{
*pcolumns = coninfo.dwSize.X;
*plines = coninfo.dwSize.Y;

return true;
}
else
{
return false;
}
}

bool Console::SetSize (int columns, int lines)
{
HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);

COORD size = { columns, lines };

return SetConsoleScreenBufferSize (hconsole, size);
}

bool Console::SetTextColor (Color FgColor, Color BgColor /*= Black*/)
{
HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);

return (SetConsoleTextAttribute (hconsole, FgColor | BgColor << 4) == TRUE);
}

bool Console::Clear ()
{
HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);

// get the number of character cells in the current buffer
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo (hconsole, &csbi))
return false;

COORD coordScreen = { 0, 0 }; // here''s where we''ll home the cursor
DWORD cCharsWritten; // number of chars written by console output routines
DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

// fill the entire screen with blanks
return (FillConsoleOutputCharacter (hconsole, '' '', dwConSize, coordScreen, &cCharsWritten) &&
FillConsoleOutputAttribute (hconsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten) &&
SetConsoleCursorPosition (hconsole, coordScreen));
}

bool Console::SetCursorPos (int x, int y)
{
HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);

COORD dwCursorPosition = { x, y };

return (SetConsoleCursorPosition (hconsole, dwCursorPosition) == TRUE);
}


bool Console::GetCursorPos (int * px, int * py)
{
HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);

CONSOLE_SCREEN_BUFFER_INFO coninfo;

if (GetConsoleScreenBufferInfo (hconsole, &coninfo))
{
*px = coninfo.dwCursorPosition.X;
*py = coninfo.dwCursorPosition.Y;

return true;
}
else
{
return false;
}
}

bool Console::SetCursorSize (DWORD dwPercentShown, bool bVisible /*=false*/
)
{
HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo = { dwPercentShown, bVisible };
return (SetConsoleCursorInfo (hconsole, &CursorInfo) == TRUE);
}
//*************************** END CONSOLE.CPP **********************************





http://www.gdarchive.net/druidgames/

Share this post


Link to post
Share on other sites
There''s another way to change text color in DOS without all that handler stuff (way too confusing). You get to use ASCII escape codes!!!!

First of all, you NEED to add:
DEVICE=C:\WINDOWS\COMMAND\ANSI.SYS

to config.sys and restart the computer. Do this by running "msconfig" from the run box in windows (might be "msconfig32" if the first one doesn''t work). You can add the mentioned line from there. IF YOU DONT add the line then the escape codes for color WONT WORK.

Let''s get started .

to set the color:
  
printf("\x1B[%d;%dm", fore, back);


where FORE is the foregournd color and BACK is the background color. Here are the colors:

FORGROUND COLORS:
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37

BACKGROUND COLORS:
BLACK = 40
RED = 41
GREEN = 42
YELLOW = 43
BLUE = 44
MAGENTA = 45
CYAN = 46
WHITE = 47

You dont need to use printf. You can use cout or WHATEVER function you use to print in the console. You can then use"

printf("\x1B[2J");

to clear the screen, since clrscr() seems to have disappeared...

You can do a WHOLE BUNCH with ASCII escape codes, most notably being able to reposition the cursor (!!!) Just ask, ill tell .

Share this post


Link to post
Share on other sites
hmmm
interestin

i forgot to ask one more thing
where can i find the function that clears the screen?
do u knoe of any other functions that would be useful for my text only rpg?

thanx

Share this post


Link to post
Share on other sites