VIZ++ Sample: BitmapFont

VIZ++ Samples

1 Screenshot


2 Source code

/*
 * BitmapFont.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


//2017/02/10

#include <viz++/opengl/OpenGLMainView.h>
#include <viz++/opengl/OpenGLGC.h>
#include <viz++/opengl/OpenGLBitmapFont.h>

namespace VIZ {

class MainView :public OpenGLMainView {

private:
  SmartPtr<OpenGLGC> gc;
  int width;
  int height;

public:
    virtual void display()
    {   
      if (gc) {
       gc -> matrixMode(GL_PROJECTION);

       gc -> loadIdentity();
       gc -> ortho2D(0, width, height, 0);
       gc -> matrixMode(GL_MODELVIEW);
       gc -> loadIdentity();

       gc -> clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

       gc -> backgroundColor(0.0, 0.0, 0.0, 1.0);

       gc -> pushMatrix();
       gc -> foregroundColor(1.0, 0.0, 0.0);
       OpenGLBitmapFont font(GLUT_BITMAP_9_BY_15);
       font.drawString(10, 40, "2015/06/20 Hello world");
       gc -> popMatrix();

       gc -> pushMatrix();
       gc -> foregroundColor(0.0, 1.0, 0.0);
       OpenGLBitmapFont timesRoman(GLUT_BITMAP_TIMES_ROMAN_24);
       timesRoman.drawString(20, 100, "2015/06/20 Goodbye world");
       gc -> popMatrix();

       gc -> pushMatrix();
       gc -> foregroundColor(0.0, 0.0, 1.0);
       OpenGLBitmapFont helvetica(GLUT_BITMAP_HELVETICA_18);
       helvetica.drawString(40, 160, "2015/06/20 OpenGLBitmapFont");
       gc -> popMatrix();
     }
    }
 
    virtual void resize(int w, int h)
    {
      if (w == 0 || h == 0) {
          return;
      }
      if (gc) {
        gc -> matrixMode(GL_PROJECTION);
        gc -> loadIdentity();

        gc -> matrixMode(GL_MODELVIEW);
      }
    }

    virtual void initialize()
    {
      gc = new OpenGLGC();

      gc -> matrixMode(GL_PROJECTION);
      gc -> loadIdentity();
      //gc -> ortho2D(0, width, height, 0);

      gc -> matrixMode(GL_MODELVIEW);
      gc -> enable(GL_LINE_SMOOTH);
      gc -> enable(GL_BLEND);
      gc -> blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    }

 
public:
  MainView(OpenGLApplet& applet, int width, int height, const char* name)
  :OpenGLMainView(applet, width, height, name)
  {
    this -> width = width;
    this -> height = height;
  }
  
  ~MainView()
  {
  }
};

}

//
int main(int argc, char** argv) 
{
  try {
    //Intialize glut library
    glutInit(&argc, argv);
    ModuleFileName module;
    const char*  name = module.getAppName();
    OpenGLApplet applet(argc, argv);

    MainView view(applet, 400, 300, name);
    view.realize();
    applet.run();
    
  } catch (Exception& ex) {
    caught(ex);
  }
  return 0;
}


Last modified: 10 Feb 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.