public GwtRenderingContext(Panel root, GwtAppConfiguration config)
      throws ParallaxRuntimeException {
    this.root = root;
    root.clear();

    Canvas canvasWidget = Canvas.createIfSupported();
    if (canvasWidget == null) throw new ParallaxRuntimeException("Canvas not supported");

    int width = root.getOffsetWidth();
    int height = root.getOffsetHeight();

    if (width == 0 || height == 0)
      new ParallaxRuntimeException("Width or Height of the Panel is 0");

    lastWidth = width;
    lastHeight = height;

    canvas = canvasWidget.getCanvasElement();
    root.add(canvasWidget);
    canvas.setWidth(width);
    canvas.setHeight(height);
    this.config = config;

    WebGLContextAttributes attributes = WebGLContextAttributes.create();
    attributes.setAntialias(config.antialiasing);
    attributes.setStencil(config.stencil);
    attributes.setAlpha(config.alpha);
    attributes.setPremultipliedAlpha(config.premultipliedAlpha);
    attributes.setPreserveDrawingBuffer(config.preserveDrawingBuffer);

    context = WebGLRenderingContext.getContext(canvas, attributes);
    context.viewport(0, 0, width, height);

    gl = new GwtGL20(context);

    renderer = new GLRenderer(gl, width, height);

    input = new GwtInput(canvas);

    addEventListeners();

    Window.addResizeHandler(this);
  }
Example #2
0
  public GwtGraphics(Panel root, GwtApplicationConfiguration config) {
    Canvas canvasWidget = Canvas.createIfSupported();
    if (canvasWidget == null) throw new GdxRuntimeException("Canvas not supported");
    canvas = canvasWidget.getCanvasElement();
    root.add(canvasWidget);
    canvas.setWidth(config.width);
    canvas.setHeight(config.height);
    this.config = config;

    WebGLContextAttributes attributes = WebGLContextAttributes.create();
    attributes.setAntialias(config.antialiasing);
    attributes.setStencil(config.stencil);
    attributes.setAlpha(false);
    attributes.setPremultipliedAlpha(false);
    attributes.setPreserveDrawingBuffer(config.preserveDrawingBuffer);

    context = WebGLRenderingContext.getContext(canvas, attributes);
    context.viewport(0, 0, config.width, config.height);
    this.gl = config.useDebugGL ? new GwtGL20Debug(context) : new GwtGL20(context);
  }
Example #3
0
  public GWTGraphics(final Panel panel, final LGame game, final GWTSetting cfg) {
    super(game, new GWTGL20(), new Scale(cfg.scaleFactor));

    this.config = cfg;
    Document doc = Document.get();
    this.dummyCanvas = doc.createCanvasElement();
    this.dummyCtx = dummyCanvas.getContext2d();

    Element root = panel.getElement();

    this.rootElement = root;

    measureElement = doc.createDivElement();
    measureElement.getStyle().setVisibility(Style.Visibility.HIDDEN);
    measureElement.getStyle().setPosition(Style.Position.ABSOLUTE);
    measureElement.getStyle().setTop(-500, Unit.PX);
    measureElement.getStyle().setOverflow(Style.Overflow.VISIBLE);
    measureElement.getStyle().setWhiteSpace(Style.WhiteSpace.NOWRAP);
    root.appendChild(measureElement);

    mouseScale = config.scaleFactor / Loon.devicePixelRatio();

    canvas = Document.get().createCanvasElement();
    root.appendChild(canvas);
    if (config.scaling()) {
      setSize(
          config.width_zoom > 0 ? config.width_zoom : root.getOffsetWidth(),
          config.height_zoom > 0 ? config.height_zoom : root.getOffsetHeight());
    } else {
      setSize(
          config.width > 0 ? config.width : root.getOffsetWidth(),
          config.height > 0 ? config.height : root.getOffsetHeight());
    }
    WebGLContextAttributes attrs = WebGLContextAttributes.create();
    attrs.setAntialias(config.antiAliasing);
    attrs.setStencil(config.stencil);
    attrs.setAlpha(config.transparentCanvas);
    attrs.setPremultipliedAlpha(config.premultipliedAlpha);
    attrs.setPreserveDrawingBuffer(config.preserveDrawingBuffer);

    WebGLRenderingContext glc = WebGLRenderingContext.getContext(canvas, attrs);
    if (glc == null) {
      throw new RuntimeException("Unable to create GL context");
    }

    ((GWTGL20) gl).init(glc);

    if (config.scaling()) {
      glc.viewport(0, 0, config.width_zoom, config.height_zoom);
    } else {
      glc.viewport(0, 0, config.width, config.height);
    }

    if (config.fullscreen) {
      Window.addResizeHandler(
          new ResizeHandler() {
            @Override
            public void onResize(ResizeEvent event) {
              if (getScreenWidthJSNI() == event.getWidth()
                  && getScreenHeightJSNI() == event.getHeight()) {
                float width = LSystem.viewSize.width(), height = LSystem.viewSize.height();
                experimentalScale =
                    Math.min(getScreenWidthJSNI() / width, getScreenHeightJSNI() / height);

                int yOfs = (int) ((getScreenHeightJSNI() - height * experimentalScale) / 3.f);
                int xOfs = (int) ((getScreenWidthJSNI() - width * experimentalScale) / 2.f);
                rootElement.setAttribute(
                    "style",
                    "width:"
                        + experimentalScale * width
                        + "px; "
                        + "height:"
                        + experimentalScale * height
                        + "px; "
                        + "position:absolute; left:"
                        + xOfs
                        + "px; top:"
                        + yOfs);
                Document.get().getBody().addClassName("fullscreen");
              } else {
                experimentalScale = 1;
                rootElement.removeAttribute("style");
                Document.get().getBody().removeClassName("fullscreen");
              }
            }
          });
    }

    Loon.self.addHandler(
        new OrientationChangedHandler() {
          @Override
          public void onChanged(Orientation newOrientation) {
            int width = Loon.self.getContainerWidth();
            int height = Loon.self.getContainerHeight();
            game.log().info("update screen size width :" + width + " height :" + height);
            setSize(width, height);
          }
        });
  }