0

Im trying to get a square to move across the terminal with code when I compile and run the code. ive managed to draw squares on the screen but I have no idea how to get key presses. When I figure out how to get key presses I will implement it. Here is my code and dont mind the suggestions those are to help me understand the code :

#include <stdio.h>
#include <stdlib.h>

#define ROWS 16
#define COLS 24

#define dot ->
#define or ||
#define and &&

#define pointer *

typedef struct Square {
    // where?
    float x, y;
    // how big?
    float w, h;

    // what char?
    char c;
} Square;

// draw_squares takes a pointer to a pointer to a square
// assuming you know how many pointers to squares there are in a row,
// you can use this as an array of sorts
void draw_squares(Square pointer pointer, int);

// this creates a square, so you don't have to manually set everything
Square pointer create_square(float, float, float, float, char);

// main entry point
int main(void) {
    // how many?
    int square_count = 3;

    // allocate memory to store three pointers to squares back-to-back
    // basically an array
    Square pointer pointer squares = malloc(sizeof(Square pointer) * square_count);

    // create squares
    squares[0] = create_square(14, 11, 4, 4, '#');
    squares[1] = create_square(8, 6, 4, 3, '@');
    squares[2] = create_square(1, 1, 3, 7, '&');

    draw_squares(squares, square_count);

    return 0;
}

void draw_squares(Square pointer pointer squares, int len) {
    for(int y = 0; y < ROWS; ++ y) {
        for(int x = 0; x < COLS; ++ x) {
            char ch = ' ';

            for(int ix = 0; ix < len; ++ ix) {
                Square pointer s = squares[ix];

                if(
                    // `x` is within bounds horizontally
                    (s dot x) <= x and (s dot x + s dot w) > x and
                    // `y` is within bounds vertically
                    (s dot y) <= y and (s dot y + s dot h) > y
                ) {
                    ch = s dot c;
                    break ;
                }
            }

            putchar(ch);
        }

        // go to next row with a newline
        putchar('\n');
    }
}



Square pointer create_square(float x, float y, float w, float h, char c) {
    // memory allocation for the size of a square
    Square pointer sq = malloc(sizeof(Square));

    // property setting
    sq dot x = x;
    sq dot y = y;
    sq dot w = w;
    sq dot h = h;

    sq dot c = c;

    return sq;
}
Muffinz
  • 15
  • 3
  • https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed – Abel Jul 04 '21 at 00:04
  • 1
    There is no standard-defined mechanism for detecting key press events in standard C. Platform-specific libraries/toolkits will be required if you need that functionality. There are several, and they vary depending on your target platform. A popular multi-platform kit is [ncurses](https://en.wikipedia.org/wiki/Ncurses) (which I would surmise to be like driving a thumbtack with a sledgehammer if that's the route you take). – WhozCraig Jul 04 '21 at 00:06
  • Try searching web or reading the standard library. – TomServo Jul 04 '21 at 01:00
  • One other tip: using the preprocessor to do things like `#define or ||` and `#define and &&` is generally considered to be more trouble than it's worth. And `#define dot ->` is downright misleading. – Steve Summit Jul 04 '21 at 01:34
  • @SteveSummit in fact there's the [standard `iso646.h` header](https://en.cppreference.com/w/c/language/operator_alternative) that already defined `or` as `||` and `and` as `&&` so no need to define that yourself. But using that isn't recommended anyway – phuclv Jul 18 '21 at 00:58

1 Answers1

0

There isn't a way of doing this in the C standard. Each operating system (and terminal for that matter) has a different way of doing it, so your best bet for a cross-platform solution would be an external library which consolidates these. For instance, SFML is a library with C bindings that allows you to treat key presses as events.

eejakobowski
  • 105
  • 5