Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
chip-ahoyto
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
João Magalhães
chip-ahoyto
Commits
80cce195
Verified
Commit
80cce195
authored
2 years ago
by
João Magalhães
Browse files
Options
Downloads
Patches
Plain Diff
feat: better calculus of next tick time
parent
4f91958e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!5
Support for the new web layout
Pipeline
#664
passed
2 years ago
Stage: build
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/web/index.js
+29
-9
29 additions, 9 deletions
examples/web/index.js
with
29 additions
and
9 deletions
examples/web/index.js
+
29
−
9
View file @
80cce195
...
...
@@ -62,6 +62,7 @@ const state = {
background_index
:
0
,
nextTickTime
:
0
,
fps
:
VISUAL_HZ
,
frameStart
:
new
Date
().
getTime
(),
frameCount
:
0
};
...
...
@@ -85,6 +86,7 @@ const state = {
// updates the ROM information on display
setRom
(
ROM_NAME
,
data
.
length
);
setLogicFrequency
(
state
.
logicFrequency
);
setFps
(
state
.
fps
);
// creates the CHIP-8 instance and resets it
state
.
chip8
=
new
Chip8Neo
();
...
...
@@ -101,17 +103,17 @@ const state = {
continue
;
}
cons
t
currentTime
=
new
Date
().
getTime
();
le
t
currentTime
=
new
Date
().
getTime
();
// in case the time to draw the next frame has been
// reached the flush of the logic and visuals is done
if
(
currentTime
>=
state
.
nextTickTime
)
{
const
ratioLogic
=
state
.
logicFrequency
/
VISUAL_HZ
;
const
ratioLogic
=
state
.
logicFrequency
/
state
.
visualFrequency
;
for
(
let
i
=
0
;
i
<
ratioLogic
;
i
++
)
{
state
.
chip8
.
clock_ws
();
}
const
ratioTimer
=
TIMER_HZ
/
VISUAL_HZ
;
const
ratioTimer
=
state
.
timerFrequency
/
state
.
visualFrequency
;
for
(
let
i
=
0
;
i
<
ratioTimer
;
i
++
)
{
state
.
chip8
.
clock_dt_ws
();
state
.
chip8
.
clock_st_ws
();
...
...
@@ -121,18 +123,36 @@ const state = {
// visual information coming in
updateCanvas
(
state
.
chip8
.
vram_ws
());
state
.
frameCount
+=
1
;
if
(
state
.
frameCount
===
state
.
visualFrequency
*
3
)
{
const
currentTime
=
new
Date
().
getTime
();
const
deltaTime
=
(
currentTime
-
state
.
frameStart
)
/
1000
;
const
fps
=
parseInt
(
Math
.
round
(
state
.
frameCount
/
deltaTime
));
setFps
(
fps
);
state
.
frameCount
=
0
;
state
.
frameStart
=
new
Date
().
getTime
();
}
// increments the number of frames rendered in the current
// section, this value is going to be used to calculate FPS
state
.
frameCount
+=
1
;
// updates the next update time reference to the current
// time so that it can be used from game loop control
state
.
nextTickTime
=
currentTime
+
1000
/
state
.
visualFrequency
;
// updates the next update time reference to the, so that it
// can be used to control the game loop
state
.
nextTickTime
=
Math
.
max
(
state
.
nextTickTime
+
1000
/
state
.
visualFrequency
,
currentTime
);
}
// calculates the amount of time until the next draw operation
// this is the amount of time that is going to be pending
currentTime
=
new
Date
().
getTime
();
const
pendingTime
=
Math
.
max
(
state
.
nextTickTime
-
currentTime
,
0
);
// waits a little bit for the next frame to be draw,
// this should control
// this should control
the flow of render
await
new
Promise
((
resolve
)
=>
{
window
.
requestAnimationFrame
(
resolv
e
);
setTimeout
(
resolve
,
pendingTim
e
);
});
}
})();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment