Giter VIP home page Giter VIP logo

rlutil's Introduction

rlutil

Build Status

RLUTIL is simple utility collection to aid the creation of cross-platform console-mode roguelike games with C++ and C. At least that was the original idea. In reality, it mostly just contains functions to position and colorize text as well as reading keyboard input.

QUICK START

Just #include "rlutil.h" in your source code. It automagically detects if you are using C++ or C and adjusts the code accordingly (e.g. std::string vs. char*).

DOCUMENTATION

See the API reference in docs/HTML/ or the source code comments for documentation.

DOWNLOAD

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/tapio/rlutil

LICENSE

See docs/License.txt or API Documentation.

rlutil's People

Contributors

cxong avatar huntsmouse avatar nabijaczleweli avatar sandrohc avatar tapio avatar zorgatone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rlutil's Issues

keyboard artefacts ^[[B ^[[D ^[[C

Hello!
I wrote very simple app and use keyboard input.
`
if (kbhit()) {
ch = rlutil::getkey();

	if (ch == 'q')
		break;
	switch (ch) {
		case 'w':
		case rlutil::KEY_UP:
		case rlutil::KEY_SPACE:
			if (board.allowRotate(*figure)) {
				painter.drawFigure(*figure, false);
				figure->setNextPoints();
			}
			break;
		case 'a':
		case rlutil::KEY_LEFT:
			if (board.allowMove(Direction::Left, *figure)) {
				painter.drawFigure(*figure, false);
				figure->move(Direction::Left);
			}
			break;
		case 'd':
		case rlutil::KEY_RIGHT:
			if (board.allowMove(Direction::Right, *figure)) {
				painter.drawFigure(*figure, false);
				figure->move(Direction::Right);
			}
			break;
		case 's':
		case rlutil::KEY_DOWN:
			if (board.allowMove(Direction::Down, *figure)) {
				painter.drawFigure(*figure, false);
				figure->move(Direction::Down);
			}
			break;
		default:
			break;
	}
}

But in terminal I see such keyboard artefacts ^[[B ^[[D ^[[C. When I used ncurses library I could switch off them by noecho() method. But here I tried to clean screen by rlutil::cls(), tried to play with such flags. newt.c_lflag - is always is 0. newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);`
Also I tried to use std::cout.flush() after each key-pressing.
Nothing helps.
Maybe you can help me, where I did mistake.
Please.

Set console title

Something of the sort would be helpful.

void setConsoleTitle(std::string title) {

ifdef _WIN32

SetConsoleTitleA(title.c_str());

else

std::printf("%c]0;%s%c", '\033', title.c_str(), '\007');

endif

}

[C] Use define instead of constants

const int KEY_ESCAPE  = 0;
const int KEY_ENTER   = 1;
const int KEY_SPACE   = 32;
// [...]

It should be:

#define KEY_ESCAPE   0;
#define KEY_ENTER    1;
#define KEY_SPACE   32;
// [...]

That's because if I try to use the value in a switch case, the C compiler cannot evaluate it on compile-time.
I must use a lot of if/else-if/............/else and that sucks.

// [...]
int key;
bool playing = true;

while(playing) {
    key = getkey();

    switch(key) {
    case KEY_ESCAPE:
        playing = false;
        break;
    }
}
// [...]

Here is the GCC error:

19:03:54 **** Incremental Build of configuration Default for project crook ****
make all 
gcc -std=c11 src/crook.c -o bin/crook
src/crook.c: In function 'main':
src/crook.c:40:3: error: case label does not reduce to an integer constant
   case KEY_ESCAPE:
   ^
make: *** [all] Error 1
makefile:2: recipe for target 'all' failed

19:03:56 Build Finished (took 1s.115ms)

Error in rlutil.h

I'm getting an error at line 584 "WriteConsoleOutputCharacter(hConsoleOutput, str, len, csbi.dwCursorPosition, &numberOfCharsWritten);". Error shown in photo attached.
image

Intensity handling with resetColor()

Regarding #5 and #15, the color can now be reset, but the intensity (that some terminals display as bold font) is not handled, at least with ANSI. This can be seen at least on Gnome Terminal with the test.cpp program as after reseting the color after the first test, all following text appears bold and not regular weight.

I think there should be some discussion whether the resetColor() function should reset other text styling attributes too, or if we should support that with separate functions. Then again Windows CMD probably offers rather limited support for styling and I think the current behaviour of resetColor() there is to reset all attributes.

Background color

Is there any support for background colors?
If not, could we have it added? ๐Ÿ˜„

Reset color to default

Hey.

Is there a way to reset the current color? Like when I use setColor(2) and want to later return to what it was before i launched my application, how could I possibly do that?

Kind regards,
Ingwie.

Mac OS X clear screen

In OS X I'm able to see all the example's screens (which should have been cleared) when I scroll my terminal upwards.

Can we portably reset the screen without keeping the old buffer?

[osx catalina] kbhit count broken cannot get arrow keys

Tested on iTerm2.app and Terminal.app on OSX Catalina.

I couldn't get they arrow keys (Clang++ std=c++2a) using rlutil::getKey().
Multiple ANSI character sequences don't get handled properly because cnt variable is 0 and the ^[[A sequence in my game loop is split in multiple calls of rlutil::getKey()

As a result I never get rlutil::getKey() to equal rlutil::KEY_UP

example.c not compiling on mac, test.cpp does.

I am getting this error:

Ingwie@localhost ~/Work/tui/rlutil $ make
gcc -I. -Wall -Wextra -o example-c example.c
In file included from example.c:1:
./rlutil.h:93:2: warning: non-constant static local variable in inline function may be different in different files [-Wstatic-local-in-inline]
        static struct termios oldt, newt;
        ^
./rlutil.h:91:1: note: use 'static' to give inline function 'kbhit' internal linkage
RLUTIL_INLINE int kbhit(void) {
^
static 
./rlutil.h:50:25: note: expanded from macro 'RLUTIL_INLINE'
                #define RLUTIL_INLINE __inline__
                                      ^
./rlutil.h:93:2: warning: non-constant static local variable in inline function may be different in different files [-Wstatic-local-in-inline]
        static struct termios oldt, newt;
        ^
./rlutil.h:91:1: note: use 'static' to give inline function 'kbhit' internal linkage
RLUTIL_INLINE int kbhit(void) {
^
static 
./rlutil.h:50:25: note: expanded from macro 'RLUTIL_INLINE'
                #define RLUTIL_INLINE __inline__
                                      ^
2 warnings generated.
Undefined symbols for architecture x86_64:
  "_anykey", referenced from:
      _main in example-8c1b05.o
  "_cls", referenced from:
      _draw in example-8c1b05.o
  "_getch", referenced from:
      _main in example-8c1b05.o
  "_hidecursor", referenced from:
      _main in example-8c1b05.o
  "_kbhit", referenced from:
      _main in example-8c1b05.o
  "_setColor", referenced from:
      _draw in example-8c1b05.o
      _main in example-8c1b05.o
  "_showcursor", referenced from:
      _main in example-8c1b05.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [example-c] Error 1

I dont quite get it; the functions should be defined, shouldn't they? -D'ing RLUTIL_INLINE only causes a LOT more errors.

Compiling on OS X 10.9.2

[WIN32] Weird behavior lowest-rightmost CMD character

I have this sourcecode: Crook - Roguelike (bug-wincmd branch on file src/crook.c, commit 3e2e1ac)

I'm having a very weird issue on the windows' CMD prompt.
I am saving the coordinates correctly (AFAICT), and I'm debugging the player position and the arrow pressed.

If you can test this, go to the lower-rightmost position in the terminal. Everytime x == maxx && y == maxy both the debug info and the player are printed one line above

`setConsoleTitle` strings

There is a problem with the setConsoleTitle function. It uses const char * as string parameter for the print function when compiling with C++. This seems to be a problem because when compiling with clang++ and with the flag -fsanitizer=memory, I just get a warning about uninitialized value at the line:
RLUTIL_PRINT(true_title);

So, I'd advise to change the code of that function to:

RLUTIL_INLINE void setConsoleTitle(RLUTIL_STRING_T title) {
#if defined(_WIN32) && !defined(RLUTIL_USE_ANSI)
#ifdef __cplusplus
    SetConsoleTitleA(title.c_str());
#else
    SetConsoleTitleA(title);
#endif
#else
    RLUTIL_PRINT(ANSI_CONSOLE_TITLE_PRE);
    RLUTIL_PRINT(title);
    RLUTIL_PRINT(ANSI_CONSOLE_TITLE_POST);
#endif // defined(_WIN32) && !defined(RLUTIL_USE_ANSI)
}

[feature] Suggestion: screen buffering and update...?

I was thinking it would be cool, and would also be a workaround for #14.

We could implement an optional screen buffering and an update function, maybe like this:

// Global buffer
char **buffer = NULL;

char **rlutil_init(void) {
    int rows = trows(), cols = tcols(), i;

    buffer = (char **) malloc(rows * sizeof(char **)); // Matrix of chars

    for (i = 0; i < rows; i++)
        buffer[i] = (char *) malloc(tcols * sizeof(char)); // sizeof(char) is always 1

    return buffer; // Optional
}

void rlutil_free(void) {
    int i, rows = trows();

    for (i = 0; i < rows; i++)
        free(buffer[i]);

    free(buffer);
}

void tupdate(void) {
    int i, j, rows = trows(), cols = tcols();

    cls();

    // Print the contents of the buffer to the terminal's screen
    for(i = 0; i < trows; i++) {
        for(int j = 0; j < tcols; j++) {
          // call whatever i/o function that outputs the chars and doesn't move the position
        }
    }
}

Imagine this, you have a game loop in which you update a lot of game logic, monster positions... and you use the normal rlutil's i/o functions.
The functions would know if rlutil_init() was called, and in this case would use the buffer rather than outputting on the screen directly.
Then you explicitly call update once per game loop to display everything to the player.

If you don't use rlutil_init and rlutil_free the i/o functions will work as usual.

The only downside to this would be that we need to handle the terminal resize event, and eventually add rows and/or realloc each column of the buffer. But I suppose if the user won't resize the terminal often we could have some performance boosts if done correctly.

Another way to do this would be to initialize a bigger matrix[MAXROWS][MAXCOLS] without huge numbers, but that can handle most of the terminal screens and resolutions.

I could even try to implement it myself (in C), if I can find some free time (job and university...)

Multiple definition of const RLUTIL_STRING_T ANSI_CLS[...]

Hello,
I tried to use rlutil.h in C, but when I include it in multiple file, "const RLUTIL_STRING_T ANSI_[..]" have multiple declaration, so I had to make a rlutil.c with the declaration of the variables and making them extern on the header.

for the .h extern const RLUTIL_STRING_T ANSI_CLS;
for the .c const RLUTIL_STRING_T ANSI_CLS = "\033[2J\033[3J";

It is better explained on a answerd on my stack question.

https://stackoverflow.com/questions/46591356/c-guard-not-work-as-intended-with-rlutil-h/46592048#46592048

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.