Develop games (6) Design using MIDP2.0

xiaoxiao2021-03-06  54

Clock is responsible for providing a real time and a virtual time, real time starts from 0 to increment, and hardware clock is synchronized; virtual time is also incremented by MS from 0, but does not necessarily synchronize with real time.

To get system.currenttimemillies (), the system hardware has a counter. When the computer starts, the counter starts every 1 ms 1 ms 1, System.currentTimeMillies () Returns from the boot to the current MS. We don't need to know time seconds, just need an incremental integer timing.

Clock changes C code in Marshall "Game Programming Gems 3", main member variables: thermistime: System.currentTimeMillies () SystemTime: The system time of the game, that is, the THISTIME is converted to the time VirtualTime incremented from 0: Virtual time, increment from 0, but does not sync with real time

code show as below:

Package game.Engine.core;

Class clock {

// Clock Whether it runs:

Private boolean running;

// Current HardWare Clock:

Private int thistime;

// Record The Last Hardware Clock When Calling Stop ():

Private Int lasttime;

// SystemTIME starts increment from 0, and hardware clock synchronization:

Private int systemtime;

// Systemoffset is the difference between the hardware clock and the SystemTime:

Private Int systemoffset;

// SYSTEMTIME to be stopped last time:

PRIVATE INT PAUSEAT;

// VirtualTime Starts from 0.

Private Int VirtualTime;

// VirtualOffset Records How long The clock paused:

Private Int VirtualOffset;

Private int framestart;

PRIVATE INT FRAMEEND;

PRIVATE INT FRAMECOUNT;

Public clock () {

RESET ();

}

// Reset the clock:

Public void reset () {

Running = false;

// Get The Hardware Clock:

THISTIME = (int) system.currenttimemillis ();

Lasttime = thistime;

// and systemtime starts from 0:

SystemTime = 0;

Systemoffset = thermistime;

Pauseat = 0;

// init Virtual Time:

VirtualTime = 0;

Virtualoffset = 0;

// init frame time:

FrameStart = 0;

Framed = 0;

Framecount = 0;

}

// Synchronize Hardware Clock:

Private void update () {

Lasttime = thistime;

THISTIME = (int) system.currenttimemillis ();

// increable the systemtime:

SystemTIME = (thisTime - lasttime);

// Start CLOCK:

Public void start () {

IF (! Running) {

Running = true;

Update ();

VirtualOffset = (SystemTime - Pauseat);

System.out.println ("[START]");

}

}

// Stop Clock:

Public void stop () {

IF (Running) {

Running = false;

Update ();

Pauseat = systemtime;

System.out.println ("[STOP] AT" Pauseat);

}

}

// Clock Whether it runs:

Public boolean isrunning () {

Return this.running;

}

// Start Frame:

Public void beginframe () {

IF (Running) {

Update ();

Framecount ;

Framestart = frameend;

Framed = systemtime - virtualoffset;

VirtualTime = framestart;

System.out.println ("[Beginframe] Virtual Time =" VirtualTime ", SystemTime =" SystemTime);

}

}

// End the Virtual Time to Frame:

Public void advancetoend () {

IF (Running) {

VirtualTime = frameend;

System.out.println ("[AdvanceToend] Virtual Time =" VirtualTime);

}

}

Public int getVirtualTime () {

Return VirtualTime;

}

// Get system time:

Public int getSystemTime () {

Update ();

Return SystemTime;

}

// Get Frame Start Time:

Public int getframestart () {

Return Framestart;

}

// Get the end time of the frame:

Public int getframeend () {

Return FrameEnd;

}

Public int getFramecount () {

Return Framecount;

}

}

Reference: Marshall: "Game Programming Gems 3"

to be continued :)

转载请注明原文地址:https://www.9cbs.com/read-115445.html

New Post(0)