View Full Version : going from >>cin to real time
smartalco
2006.12.28, 11:36 PM
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
unknown
2006.12.28, 11:57 PM
what API are you using to get a window etc?
smartalco
2006.12.29, 12:03 AM
uh... its in the console window (terminal), so right now, nothing (actual graphics will be my next adventure)
akb825
2006.12.29, 12:14 AM
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)
OneSadCookie
2006.12.29, 12:27 AM
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).
smartalco
2006.12.29, 12:32 AM
ok, threads are way above my head, apparently im not quite as decent as i thought/you are assuming
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
unknown
2006.12.29, 12:50 AM
you will be using the curses API for your game
smartalco
2007.01.01, 09:48 PM
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
#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
Undefined symbols:
formatSpace(char (*)[41], int*)
Enemy::enemy_total
*filepath to code*
collect2: Id returned 1 exit stats
akb825
2007.01.01, 11:26 PM
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;
smartalco
2007.01.02, 04:09 PM
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
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?
Steven
2007.01.02, 04:35 PM
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 :)
smartalco
2007.01.02, 04:38 PM
oops, ya, where i actually had the code in xcode i have ;'s
Steven
2007.01.02, 04:46 PM
man ncurses ->
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 :)
edit: This works for me
#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;
}
smartalco
2007.01.02, 07:04 PM
i get an error when running that
Error opening terminal: unknown.
:\
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.