instanceProto.draw won't work

For developers using the Construct 2 Javascript SDK

Post » Mon Mar 07, 2016 11:09 am

Message: shalmu can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
I am doing the simplest tutorial:
https://goo.gl/20ixH0
but it won't execute "draw" method, no matter what I do.
I even put "alert(111)" inside the body of the function, it won't fire.

Any ideas?
B
6
S
2
Posts: 30
Reputation: 540

Post » Mon Mar 07, 2016 11:57 am

draw() is only called in canvas2d mode, and drawGL() is called in WebGL mode. Almost everywhere runs WebGL by default.
Scirra Founder
B
397
S
236
G
88
Posts: 24,412
Reputation: 194,514

Post » Mon Mar 07, 2016 1:06 pm

Alright, so to draw anyway, I do this:
Code: Select all
instanceProto.drawGL = function (glw){
    this.draw(this.runtime.overlay_ctx);
}

but in this case .draw()'s parameter "ctx" is null...
How can I pass the real 2D canvas to .draw?
I tried "this.runtime.canvas" as well as "this.runtime.overlay_canvas" - nothing is working...
B
6
S
2
Posts: 30
Reputation: 540

Post » Mon Mar 07, 2016 2:00 pm

You might see rojo's canvas plugin.
plugin-canvas_t64239
B
108
S
26
G
271
Posts: 4,471
Reputation: 151,777

Post » Mon Mar 07, 2016 4:59 pm

That's all wrong, you definitely cannot forward drawGL() to draw(). drawGL uses the GLWrap class, which has a completely different API to canvas2d. So it is incorrect to call one from the other. Also overlay_ctx is only used to display the loading bar. It no longer exists once the game starts, from that point on you only have a WebGL context wrapped by GLWrap and there is no canvas2d surface available.
Scirra Founder
B
397
S
236
G
88
Posts: 24,412
Reputation: 194,514

Post » Tue Mar 08, 2016 7:34 am

Ashley, I merely saw it in a video tutorial about "How to create a Grid plugin".

Does it mean (I ask for the sake of good coding practice) that I have to write two different chunks of code that do the same thing in those two methods? Or should I only rely on webGL and skip the "draw"?

What I do right now is I write this:
Code: Select all
  var sorryShown=false;
   instanceProto.drawGL = function (glw){
    if(!sorryShown){
        alert('Sorry, but this plugin cannot be used with WebGL on.\n'
            +'To turn it off, go to Project->Enable WebGL and set it to "Off".');
      sorryShown=true;
    }
  };

So that people won't wonder why nothing happens if they are in GL mode.

Or maybe I could take advantage on a cross-context lib, like Two.js?
B
6
S
2
Posts: 30
Reputation: 540

Post » Tue Mar 08, 2016 1:18 pm

Yes, you need to write the rendering twice. All features in C2 do this. It is necessary to support both the canvas2d renderer and WebGL renderer, there isn't much we can do about that. Our long-term plan is to drop support for the canvas2d renderer, so it would be a poor choice to choose canvas2d only (especially when it can be up to 10x slower and does not support effects).
Scirra Founder
B
397
S
236
G
88
Posts: 24,412
Reputation: 194,514

Post » Tue Mar 08, 2016 1:53 pm

Message: shalmu can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
Ashley wrote:there isn't much we can do about that.


What about using an existing rendering lib, like http://www.pixijs.com? They claim it's a "2D webGL renderer with canvas fallback"

Or two.js: "It is renderer agnostic enabling the same api to draw in multiple contexts: svg, canvas, and webgl."
B
6
S
2
Posts: 30
Reputation: 540

Post » Tue Mar 08, 2016 2:32 pm

Using third party libraries, in general, means we tie the fate of our product to their library, bring in lots of code we don't understand ourselves, and inherit their bugs and limitations. There is more to lose than there is to gain, so we've always avoided that and used our own libraries. Besides, it's far too late now.
Scirra Founder
B
397
S
236
G
88
Posts: 24,412
Reputation: 194,514


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests