SOL9 Sample: OutlineFont

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



#include <sol/opengl/OpenGLMainView.h>
#include <sol/opengl/OpenGLView.h>
#include <sol/opengl/OpenGLOutlineFont.h>

#include <sol/opengl/OpenGLGC.h>

namespace SOL {

class MainView :public OpenGLMainView {

private:
  //Inner class starts.
  class SimpleView :public OpenGLView {
  private:
    SmartPtr<OpenGLGC>          gc;
    SmartPtr<Font>              arial;
    SmartPtr<OpenGLOutlineFont> arialFont;
    SmartPtr<Font>              courier;
    SmartPtr<OpenGLOutlineFont> courierFont;
    
    SmartPtr<Font>              times;
    SmartPtr<OpenGLOutlineFont> timesFont;
    
  public:
    virtual void display()
    {
      if (gc && arialFont && courierFont && timesFont) {
        ClientDC* cdc = getClientDC();
      
        GLfloat angle = 0.0;
        gc -> clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      
        gc -> scale(0.4, 0.4, 0.4);
        gc -> rotate(angle, 0, 0, 1.0);

        gc -> backgroundColor(0.0, 0.0, 0.0, 1.0);
        
        gc -> pushMatrix();
        gc -> rotate(angle+10, 0.0, 0.0, 1.0);
        gc -> translate(-10.0, 4.0, 0.0);
        gc -> foregroundColor(1.0, 0.0, 0.0);
        arialFont -> drawString(cdc, "Arial. Good Day.");
        gc -> popMatrix();
  
        gc -> pushMatrix();
        gc -> rotate(angle, 0.0, 0.0, 1.0);
        gc -> translate(-10.0, 0.0, 0.0);
        gc -> foregroundColor(0.0, 1.0, 0.0);
        courierFont -> drawString(cdc, "Courier New. Hello World.");
        gc -> popMatrix();

        gc -> pushMatrix();
        gc -> rotate(angle-10, 0.0, 0.0, 1.0);
        gc -> translate(-10.0, -4.0, 0.0);
        gc -> foregroundColor(0.0, 0.0, 1.0);
        timesFont -> drawString(cdc, "Times New Roman. Goodby World.");
        gc -> popMatrix();
      }
    }
 
    virtual void resize(int w, int h)
    {
      if (w == 0 || h == 0) {
        return;
      }
      if (gc) {
        gc -> matrixMode(GL_PROJECTION); 
        gc -> loadIdentity(); 
      
        GLfloat ww = (GLfloat)w;
        GLfloat hh = (GLfloat)h;

        if (w <= h) {
          gc ->ortho(-2.0, 2.0, -2.0 * hh / ww,  2.0 * hh / ww, -1.0, 1.0);
        } else {
          gc ->ortho(-2.0 * ww / hh, 2.0 * ww / hh, -2.0, 2.0, -1.0, 1.0);
        }
        gc -> matrixMode(GL_MODELVIEW);
        gc -> loadIdentity();
      }
    }

   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");
      //ar.set(XmNfaceName, "Courier New");

      arial = new Font(ar);
      arialFont = new OpenGLOutlineFont(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 New");
      courier = new Font(ar);
      courierFont = new OpenGLOutlineFont(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 OpenGLOutlineFont(cdc, times);
    }
    
    virtual void initialize()
    {
      ClientDC* cdc = getClientDC();
      
      createArialFont(cdc, 24);
      createCourierFont(cdc, 24);
      createTimesFont(cdc, 24);

      gc = new OpenGLGC();

      gc -> matrixMode(GL_PROJECTION);
      gc -> loadIdentity();
      gc -> ortho2D(0, 2000, 2000, 0);

      gc -> matrixMode(GL_MODELVIEW);
      gc -> enable(GL_LINE_SMOOTH);
      gc -> enable(GL_BLEND);
      gc -> blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      gc -> lineWidth(3.0);
 
      gc -> translate(0.0, 0.0, 0.0);
    }
    
  public:
    SimpleView(View* parent, const char* name, Args& args)
    :OpenGLView(parent, name, args)
    {
    }
 
    ~SimpleView()
    {
    }
  };
  //Inner class ends.

private:
  SmartPtr<SimpleView> view;

  long size(Event& event)
  {
    int w, h;
    event.getSize(w, h);
    if (view) {
      view -> reshape(10, 10, w-20, h-20);
      view -> postResizeRequest(w-20, h-20);
    }
    return 0L;
  }

public:
  MainView(Application& applet, const char* name, Args& args)
  :OpenGLMainView(applet, name, args)
  {
    Args ar;
    view = new SimpleView(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, 800);
    args.set(XmNheight, 400);
    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.