[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Flushing af stdin



On Tue, 30 May 2000, Peter Seidler wrote:

> Ok, måske har jeg lavet lort i den og spildt jeres tid. Her kommer en
> forklaring:
> 
> Jeg vil gøre noget ala følgende...
> 
> START
> 	* Læs første tegn fra stdin.
> 	* Tøm stdin, så der ikke er nogen tegn tilbage.
> 	* Behandl det læste tegn.
> 	* Vent _lidt_ tid og gå til START

OK, det er meget nemmere at forstå, men hvad med at lave:

START

* Læs alle tegn fra stdin
* Brug det sidst læste tegn til behandling
* Vent lidt tid og gå til START

> 
> På den måde vil det tegn, der behandles være indtastet i det sidste tidsrum.
> På den måde risikerer man ikke, at programmet reagerer på noget personen
> gjorde for "lang" tid siden (dvs. indtastede måske tre tegn i første
> tidsrum).
> 

Som jeg forstod det var du igang med at lave et spil. Hvad med at lave din
tegn behandling asynkron? Altså at du sådan set læser hver gang du kører
rundt i dit "main loop". Noget ala det her (total pseudo):

while (spillet kører) {
  char sidst_læst = 0;
  while(sidst_læst = noget_på_stdin()); /* noget_på_stdin() skal være 
                                           non-blocking */	
  if (0 != sidst_læst) {
    opdater_spil_tilstand(sidst_læst);
  }
  animer(); /* Whatever */
}


> Det var der mange, der ikke forstod, men folk hjalp alligevel så godt de
> kunne.
> 
> Så begyndte vi at diskutere. Og nu kommer det frygtelige. Jeg har baseret
> programmet på ncurses. Jeg troede dog, at ncurses' getch() og getchar()
> svarede til hinanden. midlertid returnerer getch() ERR vorimod getchar
> giver 0, når der ikke er noget på stdin.
> Og så gik der lidt kage i den (for mit vedkommende i hvert fald)
> 
> Om ikke andet, kunne man så ikke tømme (jeg tør ikke sige flushe :-) stdin
> som nedenunder via std. getchar()?
> 

Hvis du bruger ncurses er der formentlig metoder til at håndtere
tastaturet, som du skulle overveje at bruge, frem for stdlib.

prøv f.eks. man curs_getch :

       The getch, wgetch, mvgetch and mvwgetch, routines  read  a
       character  from the window.  In no-delay mode, if no input
       is waiting, the value ERR is returned.  In delay mode, the
       program  waits until the system passes text through to the
       program.  Depending on the  setting  of  cbreak,  this  is
       after one character (cbreak mode), or after the first new­
       line (nocbreak mode).  In  half-delay  mode,  the  program
       waits  until a character is typed or the specified timeout
       has been reached.


Iøvrigt vil jeg tro at ncurses sætter din stdin/terminal i unbuffered mode
når du beder om no-delay og cbreak mode - hvilket er et held og gør det
noget nemmere. (Har aldrig brugt ncurses).

Mads

-- 
Mads Bondo Dydensborg.                               sslug@sslug
In a stinging rebuke, a federal judge Monday ruled Microsoft Corp. violated
the nation's antitrust laws by using its monopoly power in personal computer
operating systems to stifle competition.
                        - CNN Financial News April 03, 2000: 7:53 p.m. ET



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *