SOL9 Sample: BitmapFont

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


#include <sol/opengl/OpenGLMainView.h>
#include <sol/opengl/OpenGLView.h>
#include <sol/opengl/OpenGLGC.h>
#include <sol/opengl/OpenGLBitmapFont.h>
#include <sol/Panel.h>
#include <sol/PushButton.h>

namespace SOL {

class MainView :public OpenGLMainView {

private:
  //Inner class starts
  class SimpleView: public OpenGLView {
  private:
    SmartPtr<OpenGLGC>          gc;
    SmartPtr<Font>              arial;
    SmartPtr<OpenGLBitmapFont>  arialFont;

    SmartPtr<Font>              courier;
    SmartPtr<OpenGLBitmapFont>  courierFont;

    SmartPtr<Font>              times;
    SmartPtr<OpenGLBitmapFont>  timesFont;

    int width;
    int height;

  public:
    virtual void display()
    {    
      ClientDC* cdc = getClientDC();
      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);
        timesFont -> drawString(cdc, 10, 40, "Times New Roman. Hello world");
        gc -> popMatrix();

        gc -> pushMatrix();
        gc -> foregroundColor(0.0, 1.0, 0.0);
        courierFont -> drawString(cdc, 20, 100, "Courier. Goodbye world");
        gc -> popMatrix();

        gc -> pushMatrix();
        gc -> foregroundColor(0.0, 0.0, 1.0);
        arialFont -> drawString(cdc, 40, 160, "Arial. OpenGLBitmapFont");
        gc -> popMatrix();
      }
    }
 
    virtual void resize(int w, int h)
    {
      if (w == 0 || h == 0) {
          return;
      }
      width  = w;
      height = h;
      if (gc) {
       gc -> matrixMode(GL_PROJECTION);
       gc -> loadIdentity();
       
       gc -> matrixMode(GL_MODELVIEW);
      }
    }

    void createArialFont(ClientDC* cdc, int pointSize)
    {
      int logPixel = cdc -> getDeviceCaps(LOGPIXELSY);

      int nHeight = -MulDiv(pointSize, logPixel, 72); 
      Args ar;
      ar.set(XmNheight, nHeight);
      ar.set(XmNweight,  FW_NORMAL);
      ar.set(XmNcharSet, ANSI_CHARSET);
      ar.set(XmNfaceName, "Arial");
      arial = new Font(ar);
      arialFont = new OpenGLBitmapFont(cdc, arial);
    }

    void createCourierFont(ClientDC* cdc, int pointSize)
    {
      int logPixel = cdc -> getDeviceCaps(LOGPIXELSY);

      int nHeight = -MulDiv(pointSize, logPixel, 72); 
      Args ar;
      ar.set(XmNheight, nHeight);
      ar.set(XmNweight,  FW_NORMAL);
      ar.set(XmNcharSet, ANSI_CHARSET);
      ar.set(XmNfaceName, "Courier");
      courier = new Font(ar);
      courierFont = new OpenGLBitmapFont(cdc, courier);
    }

    void createTimesFont(ClientDC* cdc, int pointSize)
    {
      int logPixel = cdc -> getDeviceCaps(LOGPIXELSY);

      int nHeight = -MulDiv(pointSize, logPixel, 72); 
      Args ar;
      ar.set(XmNheight, nHeight);
      ar.set(XmNweight,  FW_NORMAL);
      ar.set(XmNcharSet, ANSI_CHARSET);
      ar.set(XmNfaceName, "Times New Roman");
      times = new Font(ar);
      timesFont = new OpenGLBitmapFont(cdc, times);
    }

    virtual void initialize()
    {
      ClientDC* cdc = getClientDC();

      createArialFont(cdc, 24);
      createCourierFont(cdc, 32); 
      createTimesFont(cdc, 16);
      
      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:
    SimpleView(View* parent, const char* name, Args& args)
    :OpenGLView(parent, name, args)
    {
    } 

    ~SimpleView()
    {
    }
  };

  //WorkArea sample panel
  class WorkArea :public Panel {
  private:
    SmartPtr<PushButton> quit;
  private:
    
  void bye(Action& action)
  {
    ::exit(0);
  }
    
  public:
    WorkArea(View* parent, const char* name, Args& args)
    :Panel(parent, name, args)
    {
      Args ar;
      ar.set(XmNx, 10);
      ar.set(XmNy, 10);
      quit = new PushButton(this, "Quit", ar);
      quit -> addCallback(XmNactivateCallback, this, (Callback)&WorkArea::bye, NULL);
    }
    
    ~WorkArea()
    {
    }
  };
  //Inner class ends
  
private:
  SmartPtr<SimpleView>  view;
  SmartPtr<WorkArea>    warea;
  
  long size(Event& event)
  {
    int w, h;
    event.getSize(w, h);
    if (view.notNull() && warea.notNull()) {
      view -> reshape(0, 0, w*2/3, h);
    
      view -> postResizeRequest(w*2/3, h);
      warea -> reshape(w*2/3, 0, w/3, h);
    }
    return 0L;
  }
  
public:
  MainView(Application& applet, const char* name, Args& args)
  :OpenGLMainView(applet, name, args)
  {
    Args ar;
    view = new SimpleView(this, "", ar);

    ar.reset();
    warea = new WorkArea(this, "", ar);
  }
  
  ~MainView()
  {
  }
};

}

//
void Main(int argc, char** argv) 
{
  try {
    const char*  name = appName(argv[0]);
    Application applet(name, argc, argv);

    Args args;
    args.set(XmNwidth, 700);
    args.set(XmNheight, 300);
    MainView view(applet, name, args);
    view.realize();

    applet.run();
    
  } catch (Exception& ex) {
    ex.show();
  }
}


Last modified: 25 Feb 2017

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