Skip to content
Browse files

More progress code.

  • Loading branch information...
1 parent 8fb4732 commit 445d6bac80aa88b377827af16645e398847b08f1 @davemichael committed
Showing with 56 additions and 33 deletions.
  1. +51 −27 quake.html
  2. +5 −6 quake_instance.cc
View
78 quake.html
@@ -11,39 +11,58 @@
<link href="quake.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
- var Quake = null;
- var progress_bar = null;
- var kBytesExpected = 19000000; //inexact
-
- var bytes_so_far = 0;
- function HandleMessage(message_event) {
- // Currently, the only messages we receive are integers telling us how
- // many bytes were just received. Add them to the total.
- bytes_so_far += message_event.data;
- progress_bar.fillStyle = "rgb(200, 200, 200)";
+ function updateProgressBar(percent, message, styleBackground,
+ styleForeground, styleText) {
+ var progress_bar =
+ document.getElementById('progress_bar').getContext('2d');
var percent = (bytes_so_far / kBytesExpected);
+ progress_bar.fillStyle = styleForeground ? styleForeground :
+ "rgb(200, 200, 200)";
progress_bar.fillRect(0, 0, percent*800, 20);
- progress_bar.fillStyle = "#A9A9A9"; //TODO: hack, copied from css
+ progress_bar.fillStyle = styleBackground ? styleBackground :
+ "#A9A9A9"; // From the css
progress_bar.fillRect(percent*800, 0, 800, 20);
- progress_bar.fillStyle = "black";
+ progress_bar.fillStyle = styleText ? styleText : "black";
progress_bar.textAlign = "center";
progress_bar.textBaseline = "middle";
progress_bar.font = 'sans-serif';
- var text = bytes_so_far.toString() + "/" + kBytesExpected.toString();
- //progress_bar.fillText(bytes_so_far.toString() + "/" +
- // kBytesExpected.toString(), 400, 10);
- progress_bar.fillText(text, 400, 10);
- if (bytes_so_far >= kBytesExpected) {
- progress_bar.fillStyle = "rgba(0,0,0)";
- progress_bar.fillRect(0, 0, 800, 20);
- console.log("Progress updated " + times_called + " times.");
+ progress_bar.fillText(message, 400, 10, 800);
+ }
+
+ function moduleLoadProgress(event) {
+ var loadPercent = 0.0;
+ var loadPercentString;
+ if (event.lengthComputable && event.total > 0) {
+ loadPercent = event.loaded / event.total;
+ } else {
+ // The total length is not yet known.
+ loadPercent = -1.0;
+ }
+ updateProgressBar(loadPercent, "Downloading Quake executable...");
+ }
+
+ function Fade(text, c) {
+ var style = "rgb(" + c + ", " + c + ", " + c + ")";
+ updateProgressBar(0, text, "black", style, "black");
+ if (c > 0) {
+ var new_c = c - 1;
+ setTimeout("Fade('" + text + "', " + new_c + ")", 15);
}
- }
+ }
- function moduleDidLoad() {
- Quake = document.getElementById('Quake');
- progress_bar = document.getElementById('progress_bar').getContext('2d');
- Quake.addEventListener('message', HandleMessage, false);
+ var kBytesExpected = 19181098;
+ var bytes_so_far = 0;
+ function HandleMessage(message_event) {
+ // Currently, the only messages we receive are integers telling us how
+ // many bytes were just received. Add them to the total.
+ bytes_so_far += message_event.data;
+ var percent = bytes_so_far / kBytesExpected;
+ if (percent < 1.0) {
+ updateProgressBar(percent,
+ "Downloading level data, graphics, and sound...");
+ } else {
+ Fade("Ready.", 255);
+ }
}
</script>
</head>
@@ -60,8 +79,13 @@
</ul>
<div id="listener">
<script type="text/javascript">
- document.getElementById('listener')
- .addEventListener('load', moduleDidLoad, true);
+ var listener = document.getElementById('listener');
+ //listener.addEventListener('loadstart', moduleDidStartLoad, true);
+ listener.addEventListener('progress', moduleLoadProgress, true);
+ //listener.addEventListener('error', moduleLoadError, true);
+ //listener.addEventListener('abort', moduleLoadAbort, true);
+ //listener.addEventListener('loadend', moduleDidEndLoad, true);
+ listener.addEventListener('message', HandleMessage, true);
</script>
<!-- Load the published .nexe. This includes the 'nacl' attribute which
shows how to load multi-architecture modules. Each entry in the "nexes"
View
11 quake_instance.cc
@@ -76,8 +76,6 @@ void QuakeInstance::DidChangeView(const pp::Rect& position,
}
void QuakeInstance::FilesFinished() {
- //nacl_file::FileManager::Dump("id1/gfx.wad");
- pthread_create(&quake_main_thread_, NULL, LaunchQuake, this);
if (bytes_since_last_progress_) {
PostMessage(bytes_since_last_progress_);
bytes_since_last_progress_ = 0;
@@ -96,8 +94,8 @@ bool QuakeInstance::Init(uint32_t argc, const char* argn[], const char* argv[])
PRINTF("Init called. Setting up files.\n");
using nacl_file::FileManager;
FileManager::set_pp_instance(this);
- //FileManager::set_ready_func(std::tr1::bind(&QuakeInstance::FilesFinished,
- // this));
+ FileManager::set_ready_func(std::tr1::bind(&QuakeInstance::FilesFinished,
+ this));
FileManager::set_progress_func(std::tr1::bind(&QuakeInstance::DownloadedBytes,
this, _1));
//#define USEPAK
@@ -111,8 +109,9 @@ bool QuakeInstance::Init(uint32_t argc, const char* argn[], const char* argv[])
FileManager::Fetch(file_list[i]);
++i;
}
-#endif
- FilesFinished();
+#endif
+ // Launch the quake 'main' thread.
+ pthread_create(&quake_main_thread_, NULL, LaunchQuake, this);
return true;
}

0 comments on commit 445d6ba

Please sign in to comment.
Something went wrong with that request. Please try again.