类LOGO语言的实现

作者在 2008-10-01 23:46:56 发布以下内容
  去年一家公司初面要求写一个类Logo语言的C实现。我写完了不过没被叫到。呵呵里面有有点OPENGL的资料。如果真要写好要写一个堆栈来解释语法。不管怎么样还是把这堆
东西发出来了。作为数据结构中(清华版)的参考吧。
 
程序为类LOGO语言的实现
编写:
主编写平台:vc6.0 without MFC
图形处理库:opengl
指令编辑器:delphi7.0
指令表:
fd n 前进n
bk n 后退n
lt n 左转n
rt n 右转n
pu 抬笔
pd 落笔
draw 清屏
home 回家
rep n[sub] 重复 n次[]中的子句
ushowg 不显示龟
hshowg 显示龟
指令与参数,子句与子句用空格分开。
语句例子:
画矩形:
pd rep 4[fd 3 rt 90]
画要求图形:
 pd rep 120[fd 0.1 lt 3] home pu rt 90 bk 0.1 lt 90 fd 0.6 rt 90 pd rep 5[rt 144 fd 3.65] bk 1.88 rep 36[pu fd 0.1 rt 9.9 pd fd 0.1 rt 9.9] ushowg

打开工程文件时确认含有opengl库文件
程序包:
 gl文件夹拷贝到Include文件下
 lib文件夹下三个文件拷贝到lib文件夹下
 opengl32.dll 拷贝到c:\windows\system32下
程序实现:
 在程序目录下的“order.txt”中问指令内容。运行“editorder.exe”对“order.txt”中的指令内容进行编写。
 运行"ks.exe"执行“order.txt”中的指令。
 
 
工程文件
HiResTimer.h文件
/****************************************************************************
 HiResTimer.h
 
 Wrapper for the high-resolution timer. Can't be used if the hi-res timer
 doesn't exist.
 
 Author   :   Dave Astle
 Date     :   2/1/2001
 Written for OpenGL Game Programming
*****************************************************************************/
#ifndef __TIMER_H_INCLUDED__
#define __TIMER_H_INCLUDED__
#include <windows.h>

class CHiResTimer
{
public:
  CHiResTimer() {}
  ~CHiResTimer() {}
/*****************************************************************************
 Init()
 If the hi-res timer is present, the tick rate is stored and the function
 returns true. Otherwise, the function returns false, and the timer should
 not be used.
*****************************************************************************/
bool Init()
{
  if (!QueryPerformanceFrequency(&m_ticksPerSecond))//取得频率
  {
    // system doesn't support hi-res timer
    return false;
  }
  else
  {
    QueryPerformanceCounter(&m_startTime);//取得开始时间
    return true;
  }
} // end Init()

float GetElapsedSeconds(unsigned long elapsedFrames = 1)
{
  static LARGE_INTEGER s_lastTime = m_startTime;
  LARGE_INTEGER currentTime;
  QueryPerformanceCounter(¤tTime);//取得当前时间
  float seconds =  ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart) / (float)m_ticksPerSecond.QuadPart;
  // reset the timer
  s_lastTime = currentTime;
  return seconds;
} // end GetElapsedSeconds()

/***************************************************************************
 GetFPS()
 Returns the average frames per second over elapsedFrames, which defaults to
 one. If this is not called every frame, the client should track the number
 of frames itself, and reset the value after this is called.
***************************************************************************/
float GetFPS(unsigned long elapsedFrames = 1)
{
  static LARGE_INTEGER s_lastTime = m_startTime;
  LARGE_INTEGER currentTime;
  QueryPerformanceCounter(¤tTime);
  float fps = (float)elapsedFrames * (float)m_ticksPerSecond.QuadPart / ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart);
  // reset the timer
  s_lastTime = currentTime;
  return fps;
} // end GetFPS

/***************************************************************************
 LockFPS()
 Used to lock the frame rate to a set amount. This will block until enough
 time has passed to ensure that the fps won't go over the requested amount.
 Note that this can only keep the fps from going above the specified level;
 it can still drop below it. It is assumed that if used, this function will
 be called every frame. The value returned is the instantaneous fps, which
 will be <= targetFPS.
***************************************************************************/
float LockFPS(unsigned char targetFPS)
{
  if (targetFPS == 0)
    targetFPS = 1;
  static LARGE_INTEGER s_lastTime = m_startTime;
  LARGE_INTEGER currentTime;
  float   fps;
  // 延时到设定的贞频
  do {
    QueryPerformanceCounter(¤tTime);//取得当前时间
    fps = (float)m_ticksPerSecond.QuadPart/((float)(currentTime.QuadPart - s_lastTime.QuadPart));
  } while (fps > (float)targetFPS);
  // 重设时间?
  //s_lastTime = m_startTime;
   s_lastTime=currentTime;
  return fps;
} // end LockFPS()

private:
  LARGE_INTEGER   m_startTime;//开始时间
  LARGE_INTEGER   m_ticksPerSecond;//频率
};
#endif // __TIMER_H_INCLUDED_
 
MyString.h文件
//------------------------------------
//编写:KLY
//E-MAIL:liuxin1984121@tom.com
//完成时间:2007-5-20
//模拟CString 类
//运算符加上重载会好用,那时没写
//------------------------------------
#include <stdio.h>
class CMyString
{
public:
  CMyString();
  unsigned char * GetBuf();
  bool SetBuf(unsigned char *ptrc);
  int Find(CMyString cc);//0为首位
  bool insert(CMyString cc,int n);//在第n位加入CC
  bool init();
  int GetLen();
  bool getnum(int num);
  bool getnum(float num);
  char getpos(int n);
  bool addchar(char c);
  bool repace(char c,int n);
  CMyString& operator=(const CMyString &a)
  {
   init();
   SetBuf(a.ptrbuf);
   buflen=a.buflen;
   belen=a.belen;
   return *this;
  };
private:
  unsigned char *ptrbuf;
  int buflen;
  int belen;
  bool addBuf(int len);
protected:
};
CMyString::CMyString()
{
 ptrbuf=NULL;
 buflen=0;
 belen=0;
}
bool CMyString::SetBuf(unsigned char *ptrc)
{
 bool re;
 int n=0;
 while(*(char *)(ptrc+n)!='\0')
  n++;
 if(!buflen)
   re=addBuf(n+1);
 else
   re=addBuf(n);
 memcpy(ptrbuf+belen,ptrc,n+1);
 return re;
}
bool CMyString::addBuf(int len)
{
 if(buflen==0)
 {
   buflen=len;
   belen=0;
   ptrbuf = (unsigned char *) malloc (sizeof(unsigned char) * buflen); 
   if(!ptrbuf)
    return false;
   else
    return true;
 }
   else
   {
    belen=buflen-1;//去掉前一个字符
    buflen+=len;
    ptrbuf = (unsigned char *) realloc (ptrbuf, sizeof(unsigned char) *buflen);
 if(!ptrbuf)
  return false;
 else
  return true;
   }
}
int CMyString::GetLen()
{
 return buflen;
}
bool CMyString::init()
{
 if(ptrbuf!=NULL)
 {
  free(ptrbuf);
  ptrbuf=NULL;
 }
 buflen=0;
 belen=0;
 return true;
}
unsigned char * CMyString::GetBuf()
{
 return ptrbuf;
}
int CMyString::Find(CMyString cc)
{
 int pos=-1;
 unsigned char *ptrhead;
 char cbuf,cbuf1;
 int len=0;
 int i=0,j=0;
 int samen=0;
 len=cc.GetLen()-1;
 ptrhead=cc.GetBuf();
 if(ptrhead)
 {
  for(i=0;i<buflen;i++)
  {
    for(j=0;j<len;j++)
    {
      cbuf=*(char *)(ptrbuf+i+j);
   cbuf1=*(char *)(ptrhead+j);
      if(cbuf==*(char *)(ptrhead+j))
   {
     if(++samen>=len)
  {
   pos=i;
      return pos;
  }
   }
   else
    goto end;
 }//for(j=0;j<len;j++)
 end:
    samen=0;
  }//for(i=0;i<buflen;i++)
 }//if(ptrhead)
 return pos;
}
bool CMyString::insert(CMyString cc,int n)
{
 int len=0;
 int i=0;
 unsigned char *ptrhead;
 if(n>=buflen)
  return false;
 else
 {
  len=cc.GetLen();
  ptrhead=cc.GetBuf();
  this->addBuf(len-1);
  for(i=0;i<=belen-n+1;i++)
    *(char *)(ptrbuf+belen+len-i)=*(char *)(ptrbuf+belen-i+1);
  for(i=0;i<len-1;i++)
 *(char *)(ptrbuf+n+i)=*(char *)(ptrhead+i);
  *(char *)(ptrbuf+buflen)='\0';//在最后补
  unsigned char *p;
  int cc;
  cc=buflen;
  p=ptrbuf;
  return true;
 }
}
bool CMyString::getnum(int num)
{
    char c[10];
 itoa(num,c,10);
 init();
 SetBuf((unsigned char *)c);
 return true;
}
bool CMyString::getnum(float num)
{
    char c[10];
 sprintf(c,"%.2f",num);
 init();
 SetBuf((unsigned char *)c);
 return true;
}
char CMyString::getpos(int n)
{
 if ((n>buflen) || (n<0))
  return '\0';
 else
  return *(char *)(ptrbuf+n);
}
bool CMyString::addchar(char c)
{
 
  if(buflen<=0)
  {
 if(!this->addBuf(2)) return false;
    *(char *)(ptrbuf)=c;
    *(char *)(ptrbuf+1)='\0';
  }
  else
  {
   if(!this->addBuf(1)) return false;
   *(char *)(ptrbuf+belen)=c;
   *(char *)(ptrbuf+buflen)='\0';
  }
 return true;
}
bool CMyString::repace(char c,int n)
{
 if(n>buflen)
  return false;
 else
 {
  *(char *)(ptrbuf+n)=c;
  return true;
 }
}
 
 Myorder.h 文件
//------------------------------------
//编写:KLY
//E-MAIL:liuxin1984121@tom.com
//完成时间:2007-5-20
//指令处理类
//------------------------------------
#include "MyString.h"
class CMyOrder:public CMyString
{
public:
 CMyOrder(CMyString c,char t);
 void SetC(char c);
 char GetC();
 bool init();
 CMyString getneedbuf(int n0,int n1);
 int gettzn();
private:
 CMyString buf;
    char tz;
 int tzn;
 int tznp[1000];
protected:
};
CMyOrder::CMyOrder(CMyString c,char t):CMyString(),buf()
{
 buf=c;
 tz=t;
 int i;
 char cbuf;
 tzn=0;
 for(i=0;i<buf.GetLen();i++)
 {
  cbuf=buf.getpos(i);
  if(cbuf==t)
  {
    tznp[tzn]=i;
    tzn++;
  }
 }
}
void CMyOrder::SetC(char c)
{
  tz=c;
}
char CMyOrder::GetC()
{
 return tz;
}
bool CMyOrder::init()
{
 buf.init();
 tzn=0;
 return true;
}
int CMyOrder::gettzn()
{
 return tzn;
}
CMyString CMyOrder::getneedbuf(int n0,int n1)
{
 CMyString needbuf;
 char tempbuf[1000];
 int pos1,pos2;
 int i,j;
 pos1=0;
 pos2=0;
 if(n0<=0)
   pos1=0;
 else
   pos1=tznp[n0-1]+1;
 if(n1>tzn)
   pos2=buf.GetLen();
 else
   pos2=tznp[n1-1]-1;
 for(i=pos1,j=0;i<=pos2;i++)
   tempbuf[j++]=buf.getpos(i);
 tempbuf[j]='\0';
 needbuf.SetBuf((unsigned char *)tempbuf);
 return needbuf;
}
theg.h文件
//------------------------------------
//编写:kly
//E-MAIL:liuxin1984121@tom.com
//完成时间:2007-5-20
//theg类为海龟的实现
//------------------------------------
#include <math.h>
#include <gl/gl.h> //以下是三个GL的头文件
#include <gl/glu.h>
#include <gl/glaux.h>
#define PI 3.1415926
typedef struct spoint
{
 float x;
 float y;
}thepoint;
typedef struct sj
{
 thepoint p1;
 thepoint p2;
 thepoint p3;
}thesj;
typedef struct recp
{
 thepoint p;
 bool show;
}therecp;
class theg
{
public:
  thepoint p1;//1点位置
  thepoint p2;//2点位置
  thepoint p3;//3点位置
  theg(bool pen,bool record,float size,float t1,float t2,thepoint point);
  theg();
  void setpost(float therad,thepoint npoint);//在当前位置画出图形
  void setpost();
  void moveup(float n);//前进
  void movedown(float n);//后退
  void tright(float n);//右转
  void tleft(float n);//左转
  void draw();//清屏
  void moveback();//回到原点
  void getpoint(thepoint &sp1,thepoint &sp2,thepoint &sp3);//取得当前位置
  void setscan(thepoint p1,thepoint p2);//设置屏幕范围
  void getscan(thepoint &sp1,thepoint &sp2);//取得屏幕范围
  void setrecord(bool flag);
  void hshowg();
  void ushowg();
  void showg();
  void showp();
protected:
  bool isshow;//是否显示轨迹
  bool isshowg;//是否显示乌龟
  int pn;
  bool havepen;
  therecp tpoints[80000];
  float thesize;//大小
  float jd1;//图形角度
  float jd2;//图形角度
  float rad;//图形旋转的角度
  thepoint bpoint;//初始化图形中心位置
  thepoint nowpoint;//中心点当前位置
private:
   thepoint top;
   thepoint bottom;
};
theg::theg(bool pen,bool record,float size,float t1,float t2,thepoint point)
{
 thesize=size;
 bpoint.x=point.x;
 bpoint.y=point.y;
 nowpoint.x=point.x;
 nowpoint.y=point.y;
 rad=180.0;
 pn=0;
 havepen=pen;
 isshow=record;
 isshowg=true;
 jd1=t1;
 jd2=t2;
 top.x=-5;
 top.y=5;
 bottom.x=5;
 bottom.y=-5;
 setpost();
}
theg::theg()
{
 rad=180.0;
 thesize=0.4;
 bpoint.x=0.0;
 bpoint.y=0.0;
 nowpoint.x=0.0;
 nowpoint.y=0.0;
 pn=0;
 havepen=false;
 isshow=false;
 isshowg=true;
 jd1=60;
 jd2=30;
 top.x=-5;
 top.y=5;
 bottom.x=5;
 bottom.y=-5;
 setpost();
}
void theg::setpost(float therad,thepoint npoint)
{
 //
  nowpoint.x=npoint.x;
  nowpoint.y=npoint.y;
  rad=therad;
//
 float length;
 float length1;
 float length2;
 float length3;
 float x1;
 float y1;
 float x2;
 float y2;
 float x3;
 float y3;
 float x0;
 float y0;
 //计算第一点
 x1=thesize;
 y1=thesize*(sin(jd2*PI/180)/cos(jd2*PI/180));
 x0=x1;
 y0=y1;
 x1=x0*cos(rad*PI/180)+y0*sin(rad*PI/180)+nowpoint.x;
 y1=-x0*sin(rad*PI/180)+y0*cos(rad*PI/180)+nowpoint.y;
 p1.x=x1;
 p1.y=y1;
 //计算第二点
 x2=-thesize;
 y2=thesize*(sin(jd2*PI/180)/cos(jd2*PI/180));
 x0=x2;
 y0=y2;
 x2=x0*cos(rad*PI/180)+y0*sin(rad*PI/180)+nowpoint.x;
 y2=-x0*sin(rad*PI/180)+y0*cos(rad*PI/180)+nowpoint.y;
 p2.x=x2;
 p2.y=y2;
 //计算第三点
 length2=(sin(jd2*PI/180)/cos(jd2*PI/180))*thesize;
 length3=(sin(jd1*PI/180)/cos(jd1*PI/180))*thesize;
 x3=0;
 y3=-length3-length2;
 x0=x3;
 y0=y3;
 x3=x0*cos(rad*PI/180)+y0*sin(rad*PI/180)+nowpoint.x;
 y3=-x0*sin(rad*PI/180)+y0*cos(rad*PI/180)+nowpoint.y;
 p3.x=x3;
 p3.y=y3;
 //记录点
 if(pn>=80000)
   pn=0;
  tpoints[pn].p.x=nowpoint.x;
  tpoints[pn].p.y=nowpoint.y;
  tpoints[pn].show=isshow;
  pn++;
}
void theg::setpost()
{
 float length;
 float length1;
 float length2;
 float length3;
 float x1;
 float y1;
 float x2;
 float y2;
 float x3;
 float y3;
 float x0;
 float y0;
 //计算第一点
 x1=thesize;
 y1=thesize*(sin(jd2*PI/180)/cos(jd2*PI/180));
 x0=x1;
 y0=y1;
 x1=x0*cos(rad*PI/180)+y0*sin(rad*PI/180)+nowpoint.x;
 y1=-x0*sin(rad*PI/180)+y0*cos(rad*PI/180)+nowpoint.y;
 p1.x=x1;
 p1.y=y1;
 //计算第二点
 x2=-thesize;
 y2=thesize*(sin(jd2*PI/180)/cos(jd2*PI/180));
 x0=x2;
 y0=y2;
 x2=x0*cos(rad*PI/180)+y0*sin(rad*PI/180)+nowpoint.x;
 y2=-x0*sin(rad*PI/180)+y0*cos(rad*PI/180)+nowpoint.y;
 p2.x=x2;
 p2.y=y2;
 //计算第三点
 length2=(sin(jd2*PI/180)/cos(jd2*PI/180))*thesize;
 length3=(sin(jd1*PI/180)/cos(jd1*PI/180))*thesize;
 x3=0;
 y3=-length3-length2;
 x0=x3;
 y0=y3;
 x3=x0*cos(rad*PI/180)+y0*sin(rad*PI/180)+nowpoint.x;
 y3=-x0*sin(rad*PI/180)+y0*cos(rad*PI/180)+nowpoint.y;
 p3.x=x3;
 p3.y=y3;
 //---------------
  if(pn>=80000)
   pn=0;
  tpoints[pn].p.x=nowpoint.x;
  tpoints[pn].p.y=nowpoint.y;
  tpoints[pn].show=isshow;
  pn++;
}
void theg::movedown(float n)
{
  nowpoint.x=nowpoint.x+n*sin(rad*PI/180.0);
  nowpoint.y=nowpoint.y+n*cos(rad*PI/180.0);
 if(nowpoint.y<bottom.y)
  nowpoint.y=top.y;
 if(nowpoint.y>top.y)
  nowpoint.y=bottom.y;
 if(nowpoint.x<top.x)
  nowpoint.x=bottom.x;
 if(nowpoint.x>bottom.x)
  nowpoint.x=top.x;
 setpost();
}
void theg::moveup(float n)
{
  nowpoint.x=nowpoint.x-n*sin(rad*PI/180.0);
  nowpoint.y=nowpoint.y-n*cos(rad*PI/180.0);
 if(nowpoint.y<bottom.y)
  nowpoint.y=top.y;
 if(nowpoint.y>top.y)
  nowpoint.y=bottom.y;
 if(nowpoint.x<top.x)
  nowpoint.x=bottom.x;
 if(nowpoint.x>bottom.x)
  nowpoint.x=top.x;
 setpost();
}

void theg::tright(float n)
{
 rad+=n;
 setpost();
}
void theg::tleft(float n)
{
 rad-=n;
 setpost();
}
void theg::getpoint(thepoint &sp1,thepoint &sp2,thepoint &sp3)
{
 sp1.x=this->p1.x;
 sp1.y=this->p1.y;
 sp2.x=this->p2.x;
 sp2.y=this->p2.y;
 sp3.x=this->p3.x;
 sp3.y=this->p3.y;
}
void theg::setscan(thepoint p1,thepoint p2)
{
 top.x=p1.x;
 top.y=p1.y;
 bottom.x=p2.x;
 bottom.y=p2.y;
}
void theg::getscan(thepoint &sp1,thepoint &sp2)
{
 sp1.x=this->top.x;
 sp1.y=this->top.y;
 sp2.x=this->bottom.x;
 sp2.y=this->bottom.y;
}
 
void theg::draw()
{
 rad=180.0;
 nowpoint.x=bpoint.x;
 nowpoint.y=bpoint.y;
 pn=0;
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 setpost();
}
void theg::setrecord(bool flag)
{
 isshow=flag;
}
void theg::showg()
{
 glPushMatrix();
  glBegin(GL_LINES);
   glVertex3f(p1.x, p1.y, -10.0); 
   glVertex3f(p2.x,p2.y, -10.0);
   glVertex3f(p2.x, p2.y, -10.0);
   glVertex3f(p3.x, p3.y, -10.0);
            glVertex3f(p1.x, p1.y, -10.0);
   glVertex3f(p3.x, p3.y, -10.0);
  glEnd();
 glPopMatrix();
}
void theg::showp()
{
 if(isshowg)
  showg();
 int i;
 i=0;
 if(pn>0)
 {
  for(i=0;i<pn-1;i++)
    if(tpoints[i+1].show)
 {
   glBegin(GL_LINES);
    glVertex3f(tpoints[i].p.x,tpoints[i].p.y, -10.0); 
    glVertex3f(tpoints[i+1].p.x,tpoints[i+1].p.y, -10.0);
     glEnd();
 }
 }
}
void theg::moveback()
{
 rad=180.0;
 nowpoint.x=bpoint.x;
 nowpoint.y=bpoint.y;
 setpost();
}
void theg::hshowg()
{
 isshowg=true;
}
void theg::ushowg()
{
 isshowg=false;
}
 
main.cpp文件
//------------------------------------
//编写:kly
//E-MAIL:liuxin1984121@tom.com
//完成时间:2007-5-20
//类LOGO语言的实现
//------------------------------------
#define win32_lean_and_mean
#include <windows.h>
#include <stdio.h>
#include <gl/gl.h> //以下是三个GL的头文件
#include <gl/glu.h>
#include <gl/glaux.h>
#include "theg.h"
#include "HiResTimer.h"
#include "MyOrder.h"
#define KEY_DOWN(vk_code)  ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)    ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
//order--------------------------
CMyString strdraw;
CMyString strnull;
CMyString strfd;
CMyString strbk;
CMyString strlt;
CMyString strrt;
CMyString strpu;
CMyString strpd;
CMyString strhome;
CMyString strrep;
CMyString strshowg;
CMyString strushowg;
char cnull[20]={'#','\0'};
char cdraw[20]={'d','r','a','w','\0'};
char cfd[20]={'f','d','\0'};
char cbk[20]={'b','k','\0'};
char clt[20]={'l','t','\0'};
char crt[20]={'r','t','\0'};
char cpu[20]={'p','u','\0'};
char cpd[20]={'p','d','\0'};
char chome[20]={'h','o','m','e','\0'};
char crep[20]={'r','e','p','\0'};
char cshowg[20]={'s','h','o','w','g','\0'};
char cushowg[20]={'u','s','h','o','w','g','\0'};
//-----------------------------
thepoint initp;
theg thegg(false,false,0.2,30.0,30.0,initp);
//-------------------------------
bool g_keys[256];
//
bool runorder(CMyString orderbuf);
bool myloop(CMyString order);
//
HDC g_HDC;//用与WINDOWS窗口交换的设备上下文句柄
// 初始化OPENGL
void Initialize()
{
 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  // clear to black
 glShadeModel(GL_SMOOTH);     // 平滑处理
    glEnable(GL_NORMALIZE);
}
void SetupPixelFormat(HDC hDC) //设置像素格式
{
 int nPixelFormat;
 static PIXELFORMATDESCRIPTOR pfd={
  sizeof(PIXELFORMATDESCRIPTOR),
   1,
   PFD_DRAW_TO_WINDOW |
   PFD_SUPPORT_OPENGL |
   PFD_DOUBLEBUFFER,
   PFD_TYPE_RGBA,
   32,
   0,0,0,0,0,0,
   0,
   0,
   0,
   0,0,0,0,
   16,
   0,
   0,
   PFD_MAIN_PLANE,
   0,
   0,0,0};
  nPixelFormat=ChoosePixelFormat(hDC,&pfd);
  SetPixelFormat(hDC,nPixelFormat,&pfd);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM IParam)
{
 static HGLRC hRC;
 static HDC hDC;
 int width,height;
 switch(message)
 {
 case WM_CREATE:
  hDC=GetDC(hwnd);
  g_HDC=hDC;
  SetupPixelFormat(hDC);
  hRC=wglCreateContext(hDC);
  wglMakeCurrent(hDC,hRC);
  return 0;
  break;
 case WM_CLOSE:
  wglMakeCurrent(hDC,NULL);
  wglDeleteContext(hRC);
  PostQuitMessage(0);
  return 0;
  break;
 case WM_SIZE:
  height=HIWORD(IParam);
  ;
  if(height==0)
  {
   height=1;
  }
  glViewport(0,0,width,height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  return 0;
  break;
 case WM_KEYDOWN:
      g_keys[wParam] = GL_TRUE;
      return 0;
  case WM_KEYUP:
       g_keys[wParam] = GL_FALSE;
       return 0;
 default:
  break;
 }
 return (DefWindowProc(hwnd,message,wParam,IParam));
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR IpCmdLine,int nShowCmd)
{
 WNDCLASSEX windowClass;
 HWND hwnd;
 MSG msg;
 bool done;
 windowClass.cbSize=sizeof(WNDCLASSEX);
 windowClass.style=CS_HREDRAW | CS_VREDRAW;
 windowClass.lpfnWndProc=WndProc;
 windowClass.cbClsExtra=0;
 windowClass.cbWndExtra=0;
 windowClass.hInstance=hInstance;
 windowClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 windowClass.hCursor=LoadCursor(NULL,IDC_ARROW);
 windowClass.hbrBackground=NULL;
 windowClass.lpszMenuName=NULL;
 windowClass.lpszClassName="MyClass";
 windowClass.hIconSm=LoadIcon(NULL,IDI_WINLOGO);
 if(!RegisterClassEx(&windowClass))
  return 0;
 hwnd=CreateWindowEx(NULL,
  "MyClass",
  "LOGO 语言实现",
  WS_OVERLAPPEDWINDOW | WS_VISIBLE |
  WS_SYSMENU | WS_CLIPCHILDREN |
  WS_CLIPSIBLINGS,
  100,100,
  400,400,
  NULL,
  NULL,
  hInstance,
  NULL);
 if(!hwnd)
  return 0;
 ShowWindow(hwnd,SW_SHOW);
 UpdateWindow(hwnd);
 done=false;
 //指令初始化------------------------------------
  strdraw.SetBuf((unsigned char *)cdraw);
  strnull.SetBuf((unsigned char *)cnull);
  strfd.SetBuf((unsigned char *)cfd);
  strbk.SetBuf((unsigned char *)cbk);
  strlt.SetBuf((unsigned char *)clt);
  strrt.SetBuf((unsigned char *)crt);
  strpu.SetBuf((unsigned char *)cpu);
  strpd.SetBuf((unsigned char *)cpd);
  strhome.SetBuf((unsigned char *)chome);
  strrep.SetBuf((unsigned char *)crep);
  strshowg.SetBuf((unsigned char *)cshowg);
  strushowg.SetBuf((unsigned char *)cushowg);
  //从文件中读取指令
  CMyString myorder;
  char cmyfile[100];
  char corderfile[100];
  ::GetCurrentDirectory(100,cmyfile);
  sprintf(corderfile,"%s\\order.txt",cmyfile);
  FILE *filePtr;
  char ch;
  filePtr=fopen(corderfile,"rb");
  ch=fgetc(filePtr);
  while(ch!=EOF)
  {
   myorder.addchar(ch);
   ch=fgetc(filePtr);
  }
  myorder.repace('\0',myorder.GetLen()-3);
  fclose(filePtr);
 runorder(myorder);
 //贞频----------------------
 CHiResTimer timer;
 timer.Init();
 int nFrames = 0;
//--------------------------
 while(!done)
 {
  PeekMessage(&msg,NULL,0,0,PM_REMOVE);
  if(msg.message==WM_QUIT)
  {
   done=true;
  }
  else
  {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   Initialize();
   thegg.showp();
   timer.LockFPS(70);
   glFlush();
   SwapBuffers(g_HDC);
   nFrames++;
   if(nFrames>100)//执行一百次后计算祯频(1秒钟能执行绘制的次数)
   {
        char cOutBuffer[32];
        sprintf(cOutBuffer,"%0.1f",timer.GetFPS(100));//1秒钟能执行的次数.
        SetWindowText(hwnd, cOutBuffer);
        nFrames = 0;
   }//if(nFrames>100)
   if(KEY_DOWN(VK_ESCAPE))
   done=true;
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }
 return msg.wParam;
}
bool runorder(CMyString orderbuf)
{
 CMyString opbuf[1000];
 CMyString nbuf[1000];
 int i,j,head,haven,haven1;
 char charbuf;
 CMyString tmepbuf;
 int buflen;
 CMyString thebuf,thebuf1;
 CMyString rebuf,rebuf1;
 bool opflag;
 CMyString sbuf;
 int all;
 i=0;j=0;head=0;haven=0;all=0;
 thebuf=orderbuf;
 if(thebuf.GetLen()<=0) return false;
 //去多的空格-----------
 buflen=thebuf.GetLen();
 for(i=0;i<=buflen;i++)
 {
  charbuf=thebuf.getpos(i);
  if(charbuf==' ')
  {
   j++;
   if(j<=1)
    rebuf.addchar(' ');
  }
  else
  {
   rebuf.addchar(charbuf);
   j=0;
  }//if(charbuf==' ')
 }//for(i=0;i<buflen;i++)
//处理循环中的空格为','-------------------------
 thebuf1=rebuf;
 buflen=thebuf1.GetLen();
 j=0;
 for(i=0;i<buflen;i++)
 {
  charbuf=thebuf1.getpos(i);
  if(charbuf=='[') j=1;
  if(charbuf==']') j=0;
  if(charbuf==' ')
   if(j==1)
    rebuf1.addchar(',');
   else
    rebuf1.addchar(' ');
  else
    rebuf1.addchar(charbuf);
 }
//处理指令集------------------------------------
  CMyOrder myorder(rebuf1,' ');
  opflag=false;
  haven=0;
  haven1=0;
  all=myorder.gettzn();
  for(i=0;i<=all;i++)
  {
   opflag=!opflag;
   sbuf=myorder.getneedbuf(i,i+1);
   if(opflag==true)
   {
     if(sbuf.Find(strdraw)>=0)
  {
   opflag=false;
   nbuf[haven1++]=strnull;
  }
     if(sbuf.Find(strpu)>=0)
  {
   opflag=false;
   nbuf[haven1++]=strnull;
  }
     if(sbuf.Find(strpd)>=0)
  {
   opflag=false;
   nbuf[haven1++]=strnull;
  }
     if(sbuf.Find(strhome)>=0)
  {
   opflag=false;
   nbuf[haven1++]=strnull;
  }
     if(sbuf.Find(strshowg)>=0)
  {
   opflag=false;
   nbuf[haven1++]=strnull;
  }
     if(sbuf.Find(strushowg)>=0)
  {
   opflag=false;
   nbuf[haven1++]=strnull;
  }
     opbuf[haven++]=sbuf;
   }//if(opflag==true)
   else
    nbuf[haven1++]=sbuf;
  }//for(i=0;i<=all;i++)
//指令分析----------------------------
  CMyString suborder;
  float fcs;
  for(i=0;i<=haven-1;i++)
  {
   suborder=opbuf[i];
   if(suborder.Find(strdraw)>=0)
    thegg.draw();
   if(suborder.Find(strfd)>=0)
   {
    fcs=atof((char *)nbuf[i].GetBuf());
 thegg.moveup(fcs);
   }
   if(suborder.Find(strbk)>=0)
   {
    fcs=atof((char *)nbuf[i].GetBuf());
 thegg.movedown(fcs);
   }
   if(suborder.Find(strlt)>=0)
   {
    fcs=atof((char *)nbuf[i].GetBuf());
 thegg.tleft(fcs);
   }
   if(suborder.Find(strrt)>=0)
   {
    fcs=atof((char *)nbuf[i].GetBuf());
 thegg.tright(fcs);
   }
   if(suborder.Find(strpu)>=0)
    thegg.setrecord(false);
   if(suborder.Find(strpd)>=0)
    thegg.setrecord(true);
   if(suborder.Find(strhome)>=0)
    thegg.moveback();
   if(suborder.Find(strshowg)>=0)
    thegg.hshowg();
   if(suborder.Find(strushowg)>=0)
  thegg.ushowg();
   if(suborder.Find(strrep)>=0)
    myloop(nbuf[i]);
  }
//---------------------------------
 return true;
}
bool myloop(CMyString order)
{
 int i,j,n,loopn,haven,haven1,all;
 CMyString rebuf,thebuf,strn,sbuf;
 char charbuf;
 CMyString opbuf[1000];
 CMyString nbuf[1000];
 bool opflag;
 thebuf=order;
 i=0;j=0;
 //取得循环的次数----------------------
 for(i=0;i<thebuf.GetLen()-1;i++)
 {
   charbuf=thebuf.getpos(i);
   if(charbuf=='[') j=1;
   if(j==0)
    strn.addchar(charbuf);
   else
    if(charbuf!='[') rebuf.addchar(charbuf);
 }
 loopn=atoi((char *)strn.GetBuf());
//-处理指令集------------
 CMyOrder orderbuf(rebuf,',');
 opflag=false;
 haven=0;haven1=0;
 all=orderbuf.gettzn();
 for(i=0;i<=all;i++)
 {
  opflag=! opflag;
  sbuf=orderbuf.getneedbuf(i,i+1);
  if(opflag==true)
  {
   if(sbuf.Find(strdraw)>=0)
   {
    opflag=false;
 nbuf[haven1++]=strnull;
   }
   if(sbuf.Find(strpu)>=0)
   {
    opflag=false;
 nbuf[haven1++]=strnull;
   }
   if(sbuf.Find(strpd)>=0)
   {
    opflag=false;
 nbuf[haven1++]=strnull;
   }
   if(sbuf.Find(strhome)>=0)
   {
    opflag=false;
 nbuf[haven1++]=strnull;
   }
   if(sbuf.Find(strshowg)>=0)
   {
    opflag=false;
 nbuf[haven1++]=strnull;
   }
   if(sbuf.Find(strushowg)>=0)
   {
    opflag=false;
 nbuf[haven1++]=strnull;
   }
   opbuf[haven++]=sbuf;
  }//if(opflag==true)
  else
  nbuf[haven1++]=sbuf;
 }// for(i=0;i<=all;i++)
 //------------------------------
 //处理循环中的指令
 CMyString suborder;
 float fcs;
 for(n=0;n<loopn;n++)
 {
  for(i=0;i<haven;i++)
  {
   suborder=opbuf[i];
   if(suborder.Find(strdraw)>=0) thegg.draw();
   if(suborder.Find(strfd)>=0)
   {
    fcs=atof((char *)nbuf[i].GetBuf());
 thegg.moveup(fcs);
   }
   if(suborder.Find(strbk)>=0)
   {
    fcs=atof((char *)nbuf[i].GetBuf());
 thegg.movedown(fcs);
   }
   if(suborder.Find(strlt)>=0)
   {
    fcs=atof((char *)nbuf[i].GetBuf());
 thegg.tleft(fcs);
   }
   if(suborder.Find(strrt)>=0)
   {
    fcs=atof((char *)nbuf[i].GetBuf());
 thegg.tright(fcs);
   }
   if(suborder.Find(strpu)>=0)
    thegg.setrecord(false);
   if(suborder.Find(strpd)>=0)
    thegg.setrecord(true);
   if(suborder.Find(strhome)>=0)
    thegg.moveback();
   if(suborder.Find(strshowg)>=0)
    thegg.hshowg();
   if(suborder.Find(strushowg)>=0)
    thegg.ushowg();
  }
 }
 return true;
}
 

效果

技术 | 阅读 5731 次
文章评论,共0条
游客请输入验证码
浏览60391次
文章分类