going from >>cin to real time

Apprentice
Posts: 18
Joined: 2006.11
Post: #1
I have a decent understanding of C++, and have just started working on a console version of galaga (cheesy, yes, but relatively fun and easy) but having to hit enter for anything to happen or even just hitting a key would make it horrible, so i need to do it in real time.

I know (or at least it seems most logical to me) that a game would run in the following manner

initiates game loop
checks for current user input (pressed key/mouse)
responds to user input (if any)
finishes then repeats game loop (except for certain conditions resulting in player death/whatever)

so, how?

thanks for any help
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
what API are you using to get a window etc?

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2006.11
Post: #3
uh... its in the console window (terminal), so right now, nothing (actual graphics will be my next adventure)
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
You could have a separate thread for running the game, and gather input etc. from your main thread. As long as you synchronize it so you aren't changing anything when one thread expects it to be steady, you should be fine. (for threading, look at pthreads)
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
telling someone who is trying to program their first game in text mode to use threads is *very* unhelpful.

If you want good control over the console, and instantaneous input, curses is your friend.

Graphics really aren't that scary though. I'd definitely recommend moving to graphics soon (probably before you write that text-mode game).
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2006.11
Post: #6
ok, threads are way above my head, apparently im not quite as decent as i thought/you are assuming

Code:
void Player1Move(char array[75][31]){
    int input;

    if(kbhit()){
        input=getch();

there is a snippet of coding for one of my friends console version of pong, in order for it to work in real time he used kbhit() and getch() together, unfortunately i have not found an equivalent of that combo for mac (nor can i even get getch() to work decently...)

i am looking for something like that that is simple, however if you think that this is a stupid solution and/or leading me to horrible coding habits or something of that like please suggest something more appropriate
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #7
you will be using the curses API for your game

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2006.11
Post: #8
this is a little bit off from why i made the thread, but now im getting some compiler error that i cant figure out, i was hoping someone here could tell me what is going on

Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
//#include <curses.h>

using namespace std;

void formatSpace(char space[80][41], int* const pLevel);
void ranNumber(int HIGH, int LOW, int* const random);

class Enemy {
    public:
        static int enemy_total;
        Enemy(int hp=1, char symbol='v');
        void Attack(int random, char space[80][41]);
    protected:
        int m_hp;
        int m_attackNumber;
        int m_xpos;
        int m_ypos;
        char m_symbol;
};

Enemy::Enemy(int hp, char symbol) {
    m_hp = hp;
    ++enemy_total;
    m_attackNumber = enemy_total;
    m_symbol = symbol;
}

void Enemy::Attack(int random, char space[80][41]) {
    bool attack = true;

    if (m_attackNumber = random) {
        for (int x=1; x<12; x++) {
            if (space[m_xpos][m_ypos+x] != ' ') {
                attack = false;
            }
        }
    }
    
    if (attack == true) {
        space[m_xpos][m_ypos + 1] = '|';
    }
        
}

int main () {
//    Enemy::enemy_total = 0;

    int R_LOW = 1;
    int R_HIGH = 20;
    
    int random;
    
    ranNumber(R_HIGH, R_LOW, &random);
    
    char space[80][41];
    int level = 1;

    formatSpace(space, &level);
    
    for(int x=1; x<=80; x++){
        for(int y=1; y<=41; y++){
            cout<<space[x][y];
        }
    }
}


void formatSpace(char space[80][41], int const *pLevel){
    for(int x=0; x<=80; x++){
        space[x][0]='-';
        space[x][30]='-';
    }
    
    for(int y=0; y<=40; y++){
        space[0][y]='|';
        space[80][y]='|';
    }
    
    for(int x=1; x<=79; x++){
        for(int y=1; y<=29; y++){
            space[x][y]=' ';
        }
    }
    
//    if (*pLevel = 1){
}

void ranNumber(int HIGH, int LOW, int* const random) {
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    
    *random = rand() % (HIGH - LOW + 1) + LOW;
}

i get an error saying

Quote: Undefined symbols:
formatSpace(char (*)[41], int*)
Enemy::enemy_total
*filepath to code*
collect2: Id returned 1 exit stats
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #9
Static class members must be defined outside of the class. So in your .cpp file, you'd want to put the line
int Enemy::enemey_total;
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2006.11
Post: #10
ok, i looked at the man pages for the functions within curses.h, it looks like to me that i will have to use half-delay mode (or it at least seems logical at this point), so if i call halfdelay with halfdelay(1); it should be set to 1/10 of a second, then every time getch is called it will wait 1/10th of a second before moving on

Code:
int main() {
    halfdelay(1);
    int input;

    for (int x=1; x<10; x++) {
        input = getch();
        if (input != ERR) {
            cout<<input;
        }
    }
}

that should loop for one second displaying what, if anything, you pressed correct?

edit: that didn't seem to work...where am i off track?
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #11
Keep in mind that the for construct uses semicolons, not commas. The comma does something entiely different and is almost useless if you're writing proper code Smile

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2006.11
Post: #12
oops, ya, where i actually had the code in xcode i have ;'s
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #13
man ncurses ->

Quote: To initialize the routines, the routine initscr or newterm must be
called before any of the other routines that deal with windows and
screens are used. The routine endwin must be called before exiting.
To get character-at-a-time input without echoing ...

Looks like you need a few more function calls Smile

edit: This works for me
Code:
#include <stdio.h>
#include <curses.h>

int main() {
    int input,x;
initscr();
    halfdelay(1);

    for (x=1; x<10; x++) {
        input = getch();
        if (input != ERR) {
            putchar(input);
        }
    }
endwin();
return 0;
}

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2006.11
Post: #14
i get an error when running that
Quote:Error opening terminal: unknown.
Annoyed
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How would you do *real* collisions? leRiCl 3 3,011 Mar 19, 2007 05:29 PM
Last Post: Aressera