Welcome! |
|
|
|
|
Site Forum |
|
|
|
|
|
| Multi Threading |
| << Prev Topic | Next Topic >> |
| RjunArtealio |
| Posted: 05-April-2006 at 7:54pm | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 01-April-2006 Location: United States Posts: 11
|
Hi everyone,
I've written a basic nibbles game (your snake grows as you guide it and cause it to eat food) for the console, and I'm having a really tough time figuring out how to have the game constantly render whilst awaiting user at the same time. At the moment I am using "_getch" to read the keyboard buffer, but the problem is that it pauses the program.
I was told that threading is the way to get around this, and if anyone has, or knows of some good examples of multi threading it would be greatly appreciated if you chould share it.
Thanks,
--Ryan "RjunArtealio Stemen
|
Online Status: Offline
|
|
| |
| Games4TheFuture |
| Posted: 17-April-2006 at 5:14pm | IP Logged
|
|
|
Super Dedicated Groupie

Group: Super Dedicated Groupie
Joined: 03-April-2006 Location: United States Posts: 91
|
Try this. It's not multi threading, but it should work.
char input;
if(getch()) // If a key is pressed
{
input = getch();
// process input
}
|
Online Status: Offline
|
|
| |
| rsf01 |
| Posted: 17-April-2006 at 7:43pm | IP Logged
|
|
|
Groupie

Group: Groupie
Joined: 08-March-2006 Location: Philippines Posts: 28
|
create a loop with a timer, when the timer reaches a certain number of milliseconds you render your screen
|
Online Status: Offline
|
|
| |
| RjunArtealio |
| Posted: 19-April-2006 at 7:56pm | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 01-April-2006 Location: United States Posts: 11
|
Thanks for the tips, but unless I'm misunderstanding what you mean I don't think that either of them work. What I need is a way to render the screen at the same time as the console awaits input.
Thanks,
--Ryan Stemen
|
Online Status: Offline
|
|
| |
| Games4TheFuture |
| Posted: 19-April-2006 at 9:15pm | IP Logged
|
|
|
Super Dedicated Groupie

Group: Super Dedicated Groupie
Joined: 03-April-2006 Location: United States Posts: 91
|
If you put the code I wrote above in a game loop it will process input if there is any and will constantly render the screen every cycle. If this dosn't work for you I'd try Windows and DirectX.
|
Online Status: Offline
|
|
| |
| RjunArtealio |
| Posted: 19-April-2006 at 9:56pm | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 01-April-2006 Location: United States Posts: 11
|
I still can't get it to work, Games4TheFuture. If you could post a rudimentery program that gets what you told me to do to work it would be greatly appreciated.
As it stands now, the program still pauses until it receives input everytime it encounters getch(). It waits on input even when I tried putting the getch() inside of the "if" statement. grr.
Thanks in advanced,
Ryan Stemen
|
Online Status: Offline
|
|
| |
| Games4TheFuture |
| Posted: 22-April-2006 at 11:48am | IP Logged
|
|
|
Super Dedicated Groupie

Group: Super Dedicated Groupie
Joined: 03-April-2006 Location: United States Posts: 91
|
I have an example program some where in one of the books I read. I'll look for it and post it as soon as I find it.
PS-Thanks for joining the game project. I can tell you'll be a great help.
|
Online Status: Offline
|
|
| |
| RjunArtealio |
| Posted: 23-April-2006 at 9:49am | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 01-April-2006 Location: United States Posts: 11
|
Thanks, sounds great!
|
Online Status: Offline
|
|
| |
| Games4TheFuture |
| Posted: 01-May-2006 at 10:41am | IP Logged
|
|
|
Super Dedicated Groupie

Group: Super Dedicated Groupie
Joined: 03-April-2006 Location: United States Posts: 91
|
I'm having a little trouble locating the code, but I should have it up sometime within the week.
|
Online Status: Offline
|
|
| |
| m4ster_r0shi |
| Posted: 26-March-2010 at 8:52pm | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 26-March-2010 Posts: 4
|
check this out, move with w,a,s,d, quit with esc. I tried to make it platform independent so I m not using any windows API. I don't know how to fix the flickering :( Anyway, I think it might be useful. I m working on a version using windows API right now, I ll be posting it soon...
--------------------------------------------------------- ---------------------------------------------------------
#include <iostream> #include <cstdlib> #include <process.h> #include <conio.h>
using namespace std;
const int UP=0; const int LEFT=1; const int DOWN=2; const int RIGHT=3;
void user_input_handler(void*p); struct UserInput { int direction[4]; //up, left, down, right bool quit; };
const int ascii_player=2; const int ascii_wall=219; const int ascii_floor=32;
void wait(int ms);
int main() { int map[5][5]={1,1,1,1,1, 1,0,0,0,1, 1,0,2,0,1, 1,0,0,0,1, 1,1,1,1,1}; int px=2,py=2; int newpx=2,newpy=2; int i,j; UserInput input; input.quit=false; memset(input.direction,0,4*sizeof(int)); _beginthread(user_input_handler,0,&input); while (true) { newpx=px; newpy=py; newpx-=input.direction[LEFT]; newpx+=input.direction[RIGHT]; newpy-=input.direction[UP]; newpy+=input.direction[DOWN]; memset(input.direction,0,4*sizeof(int)); cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << flush; if (map[newpy][newpx]==0) { map[py][px]=0; map[newpy][newpx]=2; px=newpx; py=newpy; } for (i=0; i<5; i++) { for (j=0; j<5; j++) { switch (map[i][j]) { case 0: cout.put(ascii_floor); break; case 1: cout.put(ascii_wall); break; case 2: cout.put(ascii_player); } } if (i<4) cout << '\n'; } if (input.quit) {cout << "\nquiting now..." << endl;wait(100);break;} wait(50); } cout << "hit any key to continue..." << flush; getch(); return 0; }
void user_input_handler(void*p) { UserInput * input=(UserInput*)p;
char ch; input->quit=false; while (true) { ch=getch(); switch (ch) { case 'w': case 'W': input->direction[UP]=1; break; case 'a': case 'A': input->direction[LEFT]=1; break; case 's': case 'S': input->direction[DOWN]=1; break; case 'd': case 'D': input->direction[RIGHT]=1; break; case 27: input->quit=true; } if (input->quit) break; wait(25); } }
void wait(int ms) { int start=(clock()*1000)/CLOCKS_PER_SEC; while (( ((clock()*1000)/CLOCKS_PER_SEC) -start)<=ms); }
|
Online Status: Offline
|
|
| |
| m4ster_r0shi |
| Posted: 26-March-2010 at 10:31pm | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 26-March-2010 Posts: 4
|
"I tried to make it platform independent so I m not using any windows
API." forget about that... there's no conio.h in linux... Here's an improved version using windows API. (still having problem with flickering though...)
---------------------------------------- ----------------------------------------
#include <iostream> #include <cstdlib> #include <process.h> #include <windows.h>
using namespace std;
inline bool is_pressed(int key); void clear_screen(HANDLE & console);
const int UP=0; const int LEFT=1; const int DOWN=2; const int RIGHT=3;
void user_input_handler(void*p); struct UserInput { int direction[4]; //up, left, down, right bool quit; };
const CHAR ascii_player=2; const CHAR ascii_wall=219; const CHAR ascii_floor=32;
void wait(int ms);
int main() { int map[5][5]={1,1,1,1,1, 1,0,0,0,1, 1,0,2,0,1, 1,0,0,0,1, 1,1,1,1,1}; int px=2,py=2; int newpx=2,newpy=2; int i,j; UserInput input; input.quit=false; memset(input.direction,0,4*sizeof(int)); _beginthread(user_input_handler,0,&input); HANDLE console; console=GetStdHandle(STD_OUTPUT_HANDLE); SMALL_RECT rect={0,0,10,10}; COORD size={10,10}; COORD start={0,0}; CHAR_INFO buffer[10*10]; memset(buffer,0,100*sizeof(CHAR_INFO)); for (i=0; i<100; i++) buffer[i].Attributes= FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE; while (true) { newpx=px; newpy=py; newpx-=input.direction[LEFT]; newpx+=input.direction[RIGHT]; newpy-=input.direction[UP]; newpy+=input.direction[DOWN]; memset(input.direction,0,4*sizeof(int)); clear_screen(console); if (map[newpy][newpx]==0) { map[py][px]=0; map[newpy][newpx]=2; px=newpx; py=newpy; } for (i=0; i<5; i++) { for (j=0; j<5; j++) { switch (map[i][j]) { case 0: {buffer[i*10+j].Char.AsciiChar=ascii_floor; break;} case 1: {buffer[i*10+j].Char.AsciiChar=ascii_wall; break;} case 2: {buffer[i*10+j].Char.AsciiChar=ascii_player;} } } } WriteConsoleOutput(console,buffer,size,start,&rect); if (input.quit) {cout << "\n\n\n\n\nquiting now..." << endl;wait(100);break;} wait(50); } cout << "hit enter to continue..." << flush; while (!is_pressed(VK_RETURN)) wait(25); return 0; }
void user_input_handler(void*p) { UserInput * input=(UserInput*)p;
input->quit=false; while (true) { if (is_pressed(VK_UP)) input->direction[UP]=1; if (is_pressed(VK_LEFT)) input->direction[LEFT]=1; if (is_pressed(VK_DOWN)) input->direction[DOWN]=1; if (is_pressed(VK_RIGHT)) input->direction[RIGHT]=1; if (is_pressed(VK_ESCAPE)) input->quit=true; if (input->quit) break; wait(25); } }
void wait(int ms) { int start=(clock()*1000)/CLOCKS_PER_SEC; while (( ((clock()*1000)/CLOCKS_PER_SEC) -start)<=ms); }
inline bool is_pressed(int key) { return ((GetAsyncKeyState(key)>>15)&1); }
void clear_screen(HANDLE & console) { COORD start={0,0}; DWORD n; FillConsoleOutputCharacter(console,' ',80*25,start,&n); SetConsoleCursorPosition(console,start); }
|
Online Status: Offline
|
|
| |
| 1 User(s) are browsing this topic, 1 Guest(s) and 0 Member(s) |
| 0 Members: |
|
|
|
|
|
If you wish to post a reply to this topic you must first login If you are not already registered you must first register
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum
|
This page was generated in 0.2188 seconds. |