/** R_SetMode */
  protected boolean R_SetMode() {
    boolean fullscreen = (GlConfig.vid_fullscreen.value > 0.0f);

    GlConfig.vid_fullscreen.modified = false;
    GlConfig.gl_mode.modified = false;

    Dimension dim = new Dimension(GlState.vid.width, GlState.vid.height);

    int err; // enum rserr_t
    if ((err = GLimp_SetMode(dim, (int) GlConfig.gl_mode.value, fullscreen))
        == GlConstants.rserr_ok) {
      GlConfig.gl_state.prev_mode = (int) GlConfig.gl_mode.value;
    } else {
      if (err == GlConstants.rserr_invalid_fullscreen) {
        ConsoleVariables.SetValue("vid_fullscreen", 0);
        GlConfig.vid_fullscreen.modified = false;
        Window.Printf(
            Constants.PRINT_ALL, "ref_gl::R_SetMode() - fullscreen unavailable in this mode\n");
        if ((err = GLimp_SetMode(dim, (int) GlConfig.gl_mode.value, false)) == GlConstants.rserr_ok)
          return true;
      } else if (err == GlConstants.rserr_invalid_mode) {
        ConsoleVariables.SetValue("gl_mode", GlConfig.gl_state.prev_mode);
        GlConfig.gl_mode.modified = false;
        Window.Printf(Constants.PRINT_ALL, "ref_gl::R_SetMode() - invalid mode\n");
      }

      // try setting it back to something safe
      if ((err = GLimp_SetMode(dim, GlConfig.gl_state.prev_mode, false)) != GlConstants.rserr_ok) {
        Window.Printf(Constants.PRINT_ALL, "ref_gl::R_SetMode() - could not revert to safe mode\n");
        return false;
      }
    }
    return true;
  }
  /*
   * ============= Draw_Pic =============
   */
  public void DrawPic(int x, int y, String pic) {
    Image image;

    image = Images.findPicture(pic);
    if (image == null) {
      Window.Printf(Constants.PRINT_ALL, "Can't find pic: " + pic + '\n');
      return;
    }
    // if (scrap_dirty)
    // Scrap_Upload();

    // if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( (gl_config.renderer
    // & GL_RENDERER_RENDITION) != 0 ) ) && !image.has_alpha)
    // gl.glDisable (GLAdapter.GL_ALPHA_TEST);

    Images.GL_Bind(image.texnum);

    GlState.gl.glBegin(Gl1Context.SIMPLE_TEXUTRED_QUAD);
    GlState.gl.glVertex2f(x, y);
    GlState.gl.glVertex2f(x + image.width, y);
    GlState.gl.glVertex2f(x + image.width, y + image.height);
    GlState.gl.glVertex2f(x, y + image.height);
    GlState.gl.glEnd();

    // if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( (gl_config.renderer
    // & GL_RENDERER_RENDITION) != 0 ) ) && !image.has_alpha)
    // gl.glEnable (GLAdapter.GL_ALPHA_TEST);
  }
  /*
   * ============= Draw_TileClear
   *
   * This repeats a 64*64 tile graphic to fill the screen around a sized down
   * refresh window. =============
   */
  public final void DrawTileClear(int x, int y, int w, int h, String pic) {
    Image image;

    image = Images.findPicture(pic);
    if (image == null) {
      Window.Printf(Constants.PRINT_ALL, "Can't find pic: " + pic + '\n');
      return;
    }

    // if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( (gl_config.renderer
    // & GL_RENDERER_RENDITION) != 0 ) ) && !image.has_alpha)
    // gl.glDisable(GLAdapter.GL_ALPHA_TEST);

    Images.GL_Bind(image.texnum);
    GlState.gl.glBegin(Gl1Context._GL_QUADS);
    GlState.gl.glTexCoord2f(x / 64.0f, y / 64.0f);
    GlState.gl.glVertex2f(x, y);
    GlState.gl.glTexCoord2f((x + w) / 64.0f, y / 64.0f);
    GlState.gl.glVertex2f(x + w, y);
    GlState.gl.glTexCoord2f((x + w) / 64.0f, (y + h) / 64.0f);
    GlState.gl.glVertex2f(x + w, y + h);
    GlState.gl.glTexCoord2f(x / 64.0f, (y + h) / 64.0f);
    GlState.gl.glVertex2f(x, y + h);
    GlState.gl.glEnd();

    // if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( (gl_config.renderer
    // & GL_RENDERER_RENDITION) != 0 ) ) && !image.has_alpha)
    // gl.glEnable(GLAdapter.GL_ALPHA_TEST);
  }
  /**
   * @param dim
   * @param mode
   * @param fullscreen
   * @return enum rserr_t
   */
  public int GLimp_SetMode(Dimension dim, int mode, boolean fullscreen) {

    // TODO: jgw
    fullscreen = false;

    GlState.gl.log("GLimp_SetMode");

    Dimension newDim = new Dimension(dim.width, dim.height);

    /*
     * fullscreen handling
     */

    GlState.gl.log("determining old display mode");
    if (GlState.oldDisplayMode == null) {
      GlState.oldDisplayMode = getDisplayMode();
    }

    // destroy the existing window
    GlState.gl.shutdow();

    GlState.gl.log("searching new display mode");
    DisplayMode displayMode = DisplayModes.findDisplayMode(newDim);
    GlState.gl.log("copying w/h");
    newDim.width = displayMode.getWidth();
    newDim.height = displayMode.getHeight();

    GlState.gl.log("setting mode: " + displayMode);

    this.width = newDim.width;
    this.height = newDim.height;

    GlState.gl.log("storing mode");
    GlState.vid.width = newDim.width;
    GlState.vid.height = newDim.height;

    // let the sound and input subsystems know about the new window
    GlState.gl.log("newWindow notification");
    Window.NewWindow(GlState.vid.width, GlState.vid.height);
    return GlConstants.rserr_ok;
  }
  /** @see com.googlecode.gwtquake.shared.client.Renderer#Init() */
  public boolean Init(int vid_xpos, int vid_ypos) {
    // pre init
    assert (GlConstants.SIN.length == 256) : "warpsin table bug";

    Window.Printf(Constants.PRINT_ALL, "ref_gl version: " + GlConstants.REF_VERSION + '\n');
    Images.Draw_GetPalette();
    GlConfig.init();

    Commands.addCommand(
        "imagelist",
        new ExecutableCommand() {
          public void execute() {
            Images.GL_ImageList_f();
          }
        });

    Commands.addCommand(
        "screenshot",
        new ExecutableCommand() {
          public void execute() {
            Misc.GL_ScreenShot_f();
          }
        });
    Commands.addCommand(
        "modellist",
        new ExecutableCommand() {
          public void execute() {
            Models.Mod_Modellist_f();
          }
        });
    Commands.addCommand(
        "gl_strings",
        new ExecutableCommand() {
          public void execute() {
            Misc.GL_Strings_f();
          }
        });

    // set our "safe" modes
    GlState.prev_mode = 3;

    // create the window and set up the context
    if (!R_SetMode()) {
      Window.Printf(Constants.PRINT_ALL, "ref_gl::R_Init() - could not R_SetMode()\n");
      return false;
    }

    // post init
    GlState.qglPointParameterfEXT = true;

    Misc.GL_SetDefaultState();

    Images.GL_InitImages();
    Models.Mod_Init();
    Particles.R_InitParticleTexture();
    Drawing.Draw_InitLocal();

    int err = GlState.gl.glGetError();
    if (err != Gl1Context.GL_NO_ERROR) {
      Window.Printf(
          Constants.PRINT_ALL,
          "glGetError() = 0x%x\n\t%s\n",
          new Vargs(2).add(err).add("" + GlState.gl.glGetString(err)));
      //	return false;
    }
    return true;
  }