Allegro.cc - Online Community
Post Reply

Allegro.cc Forums » Programming Questions » Rest Vs a better form

rss feed Print
Rest Vs a better form
AceBlkwell
Member #13,038
July 2011
avatar

All,

I have a program where I use a 2 sec rest. What wasn't noticed in the game until now is the 2 sec rest stalls the screen updating. Is there a better form for stalling without stopping screen updates? I was thinking something like setting and clearing flags based on clock status. I also thought I had scanned something about ticks passed since last check. I didn't know if this would be a C/C++ command or a A5 command?

Thanks.

DanielH
Member #934
January 2001
avatar

You could simply use a variable. If your clock updates 30 times a second then multiply that by how many seconds you want it to pause logic.

#SelectExpand
1const double clock_speed = 30.0; 2double pause = 0.0; 3 4void set_pause(double seconds) 5{ 6 pause = seconds * clock_speed; 7} 8 9void mylogic() 10{ 11 if (pause > 0.0) 12 { 13 pause -= 1.0; 14 if (pause < std::numeric_limits<double>::epsilon()) 15 { 16 pause = 0.0; 17 } 18 } 19 else 20 { 21 // do actual logic that is affected by pause 22 } 23 24 // do any other logic not affected by pause 25}

AceBlkwell
Member #13,038
July 2011
avatar

Yeah Daniel I was thinking down the same path. However I don't know what my clock speed is. Plus wouldn't that vary machine to machine?

To pseudocode sort of
Currently I have cards the player flips over like concentration game. If the cards match they stay over and if not there are flipped back. The issue is they flipped back so fast you couldn't see what the color was on the last card. So I added a 2 second wait. The fact the game was stopping wasn't obvious.

I have since added a continually moving background. I can't have the display stop for 2 seconds

My proposal sort of follows your train of thought. On a wrong match, I was going to trigger a bool variable to true and continue. As long as it was true the bad match would stay visible. After a certain amount of time (or ticks?) I would set the bool to false. The false status would be a condition of clearing the bad match set.

Where my problem lies in the best approach for triggering the bool variable.
Do I use <time.h> tv.tv_usec type approach or is there a uniform "ticks since last check" type approach and would that stay consistent machine to machine?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

As you've figured out, al_rest blocks. That won't work. al_wait_for_event also blocks, but can respond to other events in the meantime.

Try using a 2.0 second timer, or just count elapsed time. If the timer is still running, keep showing the old state so it doesn't disappear.

DanielH
Member #934
January 2001
avatar

I'm assuming you have a timer?

ALLEGRO_TIMER *al_create_timer(double speed_secs);

// If you want the logic rate at 30 times per second then:
ALLEGRO_TIMER *al_create_timer(1.0/30.0);

I would have 2 timers. The first for ordinary logic.
The 2nd for special things like one your asking about. The 2nd timer would be set at a very small rate. It would be used in occasions like this.

Since you don't flip the cards are the same time, I would also add the pause variable to the card itself and a state variable.

#SelectExpand
1// in timer event for 2nd timer 2for (i : all the cards) 3{ 4 if (card[i].state == card_flipped && card[i].timer > 0.0) 5 { 6 card[i].timer = card[i].timer - (some value depending on timer rate); 7 } 8} 9 10// in main logic area 11for (i : all the cards) 12{ 13 if (card[i].state == card_flipped && card[i].timer <= 0.0) 14 { 15 card[i].state = card_unflipped; 16 card[i].timer = 0.0; 17 } 18}

AceBlkwell
Member #13,038
July 2011
avatar

Thanks Edgar and Daniel,

So timers it is then. I appreciate the insight.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

If you want to do it the slick way, you only need one timer, your regular display timer.

Here is a simple example. You need two variables, dt and old dt to check when dt crosses zero. That means the delay is over.

/// timer setup whatever, 1.0/60 is good and usu. matches the display hertz

double delay = 2.0;
double dt = -1.0;

if (player_flipped_card) {
   dt = delay;
}
else {
   double olddt = dt;
   dt -= REFRESH_RATE_OF_TIMER;
   if (olddt > 0.0 && dt <= 0.0) {event....}
}

Don't forget to rtfm. Look at the TIMER event here :

https://liballeg.org/a5docs/trunk/events.html#allegro_event_timer

You can see the TIMER event holds a count variable that you can use to monitor tick count if you want to do it the simplest way.

#SelectExpand
1const double TPS = 60; 2const double SPT = 1.0/TPS; 3 4ALLEGRO_TIMER* gtimer = al_create_timer(SPT); 5 6 7// main 8al_start_timer(gtimer); 9 10// in event loop 11 12double delay = 2.0*TPS; 13int dt = -1; 14int olddt = dt; 15 16// player flipped a card 17if (card_flip) { 18 dt = delay; 19 /// start the delay 20} 21else { 22 olddt = dt; 23} 24 25 26if (event.type == ALLEGRO_EVENT_TIMER) 27 --dt; 28 if (olddt > 0 && dt <= 0) { 29 /// set the card to flipped on this frame 30 card[selected_card].FlipFaceDown();/// or whatever you do 31 } 32}

;D

DanielH
Member #934
January 2001
avatar

RTFM??? Read the FRIENDLY Manual. ;D;D;D

AceBlkwell
Member #13,038
July 2011
avatar

DanielH - "RTFM??? Read the FRIENDLY Manual."

That's exactly how I would have taken it, if I didn't know the manuals are user hostile :D

Seriously, thanks for the help and code example guys.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

AceBlkwell
Member #13,038
July 2011
avatar

Now if I always read the manuals (not that they are easily understood) and didn't ask silly a$# questions on here , what would become of this site?

I'm starting to think my ignorance is the only think keeping us afloat. 8-)

gillius
Member #119
April 2000

Somehow, Allegro.cc still lives on my bookmarks bar.

But, I have been concerned there is only 1 live thread at a time.

Every once in awhile I poke a thread with a post :).

I can't believe I started using Allegro in what, I think 1998? 1999? I remember starting with using DJGPP compiling for DOS, so definitely was before Win2k days.

Gillius
Gillius's Programming -- https://gillius.org/

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Dizzy Egg
Member #10,824
March 2009
avatar

This forum is hanging on by a thread

Boom! ;D

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

gillius
Member #119
April 2000

Edgar, I've been here the whole time ;D. For some reason I've kept recent threads on my bookmarks bar since the dawn of time (Jan 1, 1970), so I visit every one in awhile. Maybe once every week or two but probably at least monthly. Just I normally don't have much to say. My low number comes from being in the first batch of people who came from an old site I think it was called allegro games depot. Then I think maybe it was shutting down or something so Matthew created this site and invited everyone there to come here, so I did.

What's up with the site taking 30 seconds to load sometimes though? Other times it's fast. I very rarely notice Matthew posting anymore though.

Gillius
Gillius's Programming -- https://gillius.org/

DanielH
Member #934
January 2001
avatar

There was a discussion with Matthew. This site needs a new home (new server location) and a major overhaul. It's written in an older version of PHP and has some outdated dependencies. He has no desire to update it and has relinquished that task to someone else who is willing to do it.

That said, quite a few of us have moved to Discord and/or are still on the IRCnet channel (#allegro).

Not as convenient as Allegro.cc, but it's something.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Oh well, stop lurking so damn much!! We need your support gillius! Like DanielH said, join us on Discord or irc.libera.chat #allegro

What DanielH said is true about Matthew. He pretty much said he's willing to pass allegro.cc on to someone who has the time and energy to update and migrate it.

gillius
Member #119
April 2000

Well, I lurk because I haven't done C/C++ development or Allegro in almost 20 years, so I normally don't have much to say. That said, I installed MSVC community the other day so I could compile a custom version of PasswordSafe to try to debug a tool.

I have a question for another thread. But, maybe in the interest of keeping A.cc alive a little bit more (RIP "thread closes too soon") I'll start a thread :).

As for helping maintain anything, my life with 3 kids is keeping me pretty busy, and I've not had time for side projects in a long time. I think the bigger question is money, not time. If you moved wherever this lives to the cloud it will cost a lot. Probably cheaper is to try to host it on a home connection with a dedicated server, considering how few users there are, in which case you have a one time expense of the server only. These days though with working from home I get afraid to host things even of my own on such a connection due to concerns about floods of traffic. And of course, it would be a lot less reliable as home users would shut down their router a lot. As this is a PHP site you could use a cpanel provider but those even are $10 a month or more to start I think.

Gillius
Gillius's Programming -- https://gillius.org/

Post Reply
Go to: