ViewBox.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:4k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // ViewBox.cpp: implementation of the CViewBox class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "ViewBox.h"
  6. #include "imgtext.h"
  7. #include <stdio.h>
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CViewBox::CViewBox()
  12. {
  13. m_bText=true;       //图片还是文字?
  14. m_iLineHeight=30; //每行高
  15. m_numLine=1;     //总行数
  16. m_iShowLine=1;   //筐中容纳行数
  17. m_string=NULL;
  18. m_iHead=0;
  19. }
  20. CViewBox::~CViewBox()
  21. {
  22. if(m_string!=NULL)
  23. delete [] m_string;
  24. }
  25. void CViewBox::SetViewBox(RECT rect,char *filename)
  26. {
  27. int width=rect.right-rect.left;
  28. int height=rect.bottom-rect.top;
  29. m_iLineHeight=30;
  30. m_iShowLine=height/m_iLineHeight;
  31. rect.bottom=rect.top+ m_iShowLine*m_iLineHeight;
  32. m_rect=rect;
  33.     
  34. /////////////////// read file/////////
  35. FILE *file;
  36. file = fopen(filename, "rt");
  37.     if(file==NULL)
  38. {
  39. m_string=new char [64];
  40. strcpy(m_string,"Open file "");
  41. strcat(m_string,filename);
  42. strcat(m_string,"" failed!");
  43. m_numLine=1;  
  44. }
  45. else
  46. {
  47.         fseek(file,0,SEEK_END);
  48.     int size=ftell(file);
  49.     fseek(file,0,SEEK_SET);
  50. m_string=new char [size];
  51. int numLine=1;
  52.   for(int i=0;i<(size-1);i++)
  53. {
  54. m_string[i]=fgetc(file);
  55. if(m_string[i]==-1)m_string[i]=NULL;
  56. if(m_string[i]=='n')numLine++;
  57. }
  58.      m_numLine= numLine;
  59. fclose(file);
  60. }
  61.   ////////// scroll bar   
  62. if(m_numLine>m_iShowLine)
  63. {
  64. m_bShowScroll=true;
  65.      rect.left=rect.right-22; // 25 is scrollbar's width
  66.      m_cScrollBar.SetScrollBar(rect,false,m_numLine-m_iShowLine,0);
  67.      m_cScrollBar.SetBlockWidth(16);
  68. }
  69. else
  70. {
  71.         m_iShowLine=m_numLine;
  72. m_bShowScroll=false;
  73. }
  74. m_iHead=0;
  75. m_iPos=0;
  76. m_bText=true; 
  77. }
  78. void CViewBox::SetViewBox(RECT rect,unsigned int texID)
  79. {
  80. ///////////////////////////
  81. m_iHead=0;
  82. m_iPos=0;
  83. m_bText=false; 
  84. }
  85. void  CViewBox::RenderViewBox()
  86. {
  87. UpdateViewBox();
  88. //////////////////////////background
  89. DrawBackground();
  90. ///////////////////////Scroll
  91.     if(m_bShowScroll)
  92. {
  93. m_cScrollBar.RenderScrollBar();
  94. }
  95. ///////////////////////////////////////
  96. /////////// show text
  97.     int pos=m_iPos;
  98. int textSize=16;
  99. int x=m_rect.left+10,y=m_rect.top+5;
  100. int xbias=10;
  101. glColor3f(0,1,0);
  102. for(int i=0;i<m_iShowLine;i++)
  103. {
  104. x=m_rect.left+10;
  105.     while(m_string[pos]!='n' && m_string[pos]!=NULL)
  106. {
  107. if(m_string[pos]=='~')
  108. {
  109. textSize=18;
  110. xbias=10;
  111.     glColor3f(1,1,0);
  112. }
  113. else if(m_string[pos]=='`')
  114. {
  115. textSize=16;
  116. xbias=9;
  117.     glColor3f(0,1,0);
  118. }
  119. else
  120. {
  121.     CImgText::PrintfChar(x,y,m_string[pos],0,textSize);
  122.      x+=xbias;
  123. }
  124. pos++;
  125. };
  126. pos++;
  127.         y+=m_iLineHeight;
  128. }
  129. }
  130. void  CViewBox::UpdateViewBox()
  131. {
  132. //////////////////////////////
  133.     if(!m_bShowScroll)return;
  134. if(m_iHead!=m_cScrollBar.GetValue()) //scroll bar had been changed
  135. {
  136. m_iHead=m_cScrollBar.GetValue();
  137. //////////find new position
  138. int index=0;
  139. m_iPos=0;
  140. int len =strlen(m_string);
  141. while(index<m_iHead && m_iPos<len )
  142. {
  143. if(m_string[m_iPos]=='n')index++;
  144. m_iPos++;
  145. }
  146. }
  147. }
  148. void CViewBox::SetText(char *filename)
  149. {
  150. if(m_string!=NULL)
  151. delete [] m_string;
  152. SetViewBox(m_rect,filename);
  153. }
  154. void CViewBox::DrawBackground()
  155. {
  156. /////// draw back
  157. // glBlendFunc(GL_ONE,GL_ONE);
  158. // glEnable(GL_BLEND);
  159. glColor3f(0,0.3f,0);
  160.     glBegin(GL_QUADS);
  161.     glVertex3i(m_rect.left-400 , 300-m_rect.top , -520);
  162.     glVertex3i(m_rect.right-400 , 300-m_rect.top , -520);
  163.     glVertex3i(m_rect.right-400 , 300-m_rect.bottom ,-520);
  164.     glVertex3i(m_rect.left-400 , 300-m_rect.bottom , -520);
  165. glEnd();
  166. // glDisable(GL_BLEND);
  167. //////////////////////////background
  168. /////// draw rectangle
  169. glColor3f(0,0.8f,0);
  170. glBegin(GL_LINE_LOOP);
  171.     glVertex3i(m_rect.left-400 , 300-m_rect.top , -520);
  172.     glVertex3i(m_rect.right-400 , 300-m_rect.top , -520);
  173.     glVertex3i(m_rect.right-400 , 300-m_rect.bottom ,-520);
  174.     glVertex3i(m_rect.left-400 , 300-m_rect.bottom , -520);
  175. glEnd();
  176. }