Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / 2 Questions

Author
Message
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 18th Feb 2009 22:17
Hi! I have a two questions regarding key pressing and timers:

1. I've been using dbKeyState ( iScanCode ) to check if a key is pressed. But it's hard to press it once and only make it execute the code once. So is there an easier way of checking a key press once without having to use some kind of timer.

2. I want to know how to use the timer. I saw the dbTimer function but i just don't know how to use this. So any explanation would be great!

Thanks in advance!
Hayer
18
Years of Service
User Offline
Joined: 4th Nov 2005
Location: Norway
Posted: 18th Feb 2009 22:45
1. Dunno, timers are really easy so..

2. Well, dbTimer() returns the computer time in ms.
Which means

sec1 = dbTimer() + 1000

is 1s into the futher

Keep it simple.
Questions? Mail me
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 18th Feb 2009 22:50 Edited at: 18th Feb 2009 23:01
Well i see what it does but how would i check if the timer is up?
Also i have another question now,

1. Why are objects shown through other objects even if there Z depth is enabled?
Pharoseer
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: Right behind you
Posted: 19th Feb 2009 00:04 Edited at: 19th Feb 2009 00:04
Hey guys,

To manage single key presses I use something I learned about in a class on writing operating systems. It's a variation on a concept called a semaphore. Basically you "lock" the resource while it is in use and only "unlock" it when it isn't being used anymore.

Here's a quick code sample to handle single key presses of the spacebar. You could easily expand this to have an array of semaphores for each key so that any key pressed would only act once until it has been released and repressed.

I've included two samples with minor differences to illustrate the importance of the code structure. If you trace the code with pen and paper it starts to make sense why the first one works and the second doesn't.



Hope this helps,

-Frank
heyufool1
15
Years of Service
User Offline
Joined: 14th Feb 2009
Location: My quiet place
Posted: 19th Feb 2009 00:24
Ahhh thanks! It's so simple i'm almost mad for not thinking of it Thanks very much! Now for:

1. How to check if a timer is done (or just an example of a timer in use)

2. Why are objects shown through other objects even if there z value says they should be behind? (I have not called dbDisableZDepth)
prasoc
15
Years of Service
User Offline
Joined: 8th Oct 2008
Location:
Posted: 19th Feb 2009 00:30
Isn't that a bit long winded? I would add a variable called "int lastKey = 0;" and at the bottom of the main loop put "lastKey = dbScanCode();" and then do this:



it is much easier to manage
Pharoseer
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: Right behind you
Posted: 19th Feb 2009 01:14 Edited at: 19th Feb 2009 01:17
Hey Prasoc,

Your method works well in most cases, but it still allows for a single keypress to execute multiple times. Take the following example (not actual code, just an event walkthrough):



As you can see in the example, it is possible that an event gets triggered multiple times from a single keypress because of this. I admit that for most applications that isn't an issue and I use your method probably 95% of the time. However, when I absolutely need to maintain the 1 keypress = 1 action rule, I use the semaphore method.

-Frank
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 19th Feb 2009 02:01
Timer is rarely used just to get the computer time. Usually it's the difference between times. So just have a global called time, and call
time=dbTimer();
at the very end of each loop. so something like this at the beginning of the loop

int timedifference=dbTimer()-time;
dbtext(0,0,dbStr(timedifference));

would print out how many milliseconds per frame you were getting. If you have how many milliseconds per frame you're getting, then you can use timer based movement. Instead of figuring out how much movement you want per frame, you can type in how much movement you want per second, and then calculate how much movement there is per frame.

movement per frame=(movement per second*milleseconds per frame)/1,000

If you do timer based animations too, your game could be jumping between 30 and 300 fps, and the player would hardly notice.

Login to post a reply

Server time is: 2024-09-30 19:27:02
Your offset time is: 2024-09-30 19:27:02