SOL9 Sample: CircleThread

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL++2000
// 2000.02.18
// 2009/10/10

#include <sol\Semaphore.h>
#include <sol\Exception.h>
#include <sol\Stdio.h>
#include <sol\ApplicationView.h>
#include <sol\Thread.h>
#include <sol\ClientDC.h>
#include <sol\Brush.h>
#include <sol\Pen.h>
#include <sol\PaintDC.h>
#include "resource.h"

namespace SOL {

class CircleThread :public Thread {
private:
  BOOL  doing;
  int    count;
  int    radius;
  View*  view;
  int    x, y;
  int    px, py;
  int    vx, vy;
  // regin
  int    rx1, rx2;
  int    ry1, ry2;

public:

  /**
   * Constructor
   */
  CircleThread(View* view, int radius, int count)
  :Thread(),
  doing(TRUE)
  {

    this -> view   = view;
    this -> radius = radius;
    this -> count  = count;
    vx = 3;//11;
    vy = radius;//4; //8;
    x  = -10;
    y  = -9;
    px = -10;
    py = -9;
    rx1 = radius;//4;
    rx2 = 100;// + radius;//24;
    ry1 = radius;//4;
    ry2 = 100;//+ radius;//24;
  }


public:
  void run()
  {
    int i = 0;

    while(doing) {
      sleep(100);
      ClientDC dc(view);
      int r = radius;

      px = x;
      py = y;
      x += vx;
      y += vy;
      // right
      if(vx > 0 && x +r > rx2) {
        x = 2* rx2 - x;
        vx = -vx;
      }

      //left
      else if(vx < 0 && x -r < rx1) {
        x = 2*rx1 - x;
        vx = -vx;
      }
      // 
      if(vy > 0 && y +r >ry2) {
        y = 2* ry2 - y;
        vy = -vy;
      }
      else if( vy < 0 && y -r < ry1){
        y = 2*ry1 - y;
        vy = -vy;
      }
       
      Pen pen(PS_INSIDEFRAME, 10, RGB(255,100, 255));
    
      HGDIOBJ prev = dc.select(&pen);
        
      dc.setROP2(R2_XORPEN);

      dc.ellipse(px + r, py + r, px- r, py -r);

      dc.ellipse(x + r, y + r, x- r, y -r);
      dc.select(prev);
      i++;
    }
  }

};


class HelloView :public ApplicationView {
private:
  CircleThread* thread;
  
public:
  /**
   */
  HelloView(Application& applet, const TCHAR* name, Args& args)
    :ApplicationView(applet, name, args)
  {
    thread = new CircleThread(this, 4, 100);
    thread->start();
    addEventHandler(WM_PAINT, this,
      (Handler)&HelloView::paint, NULL);
    addCallback(XmNmenuCallback, IDM_EXIT, this, 
      (Callback)&HelloView::exit, NULL);
  }

public:
  ~HelloView() 
  {
    thread -> terminate(0);
    thread -> wait(INFINITE);
    delete thread;
  }

public:
  long paint(Event& event) {
    PaintDC dc(this);
    dc.rectangle(0, 0, 100+4, 100+4);
    return 0L;
  }
};

}

//

void  Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();
  try {
    Application applet(name, argc, argv);

    Args args;
    args.set(XmNwidth, 200);
    args.set(XmNheight, 200);

    HelloView hellov(applet, name, args);
    hellov.realize();
    applet.run();
    
  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
}

Last modified: 1 Feb 2017

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