OpenGLSample.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #include "Gwen/Gwen.h"
  2. #include "Gwen/Skins/Simple.h"
  3. #include "../OpenGLWindow/OpenGLInclude.h"
  4. #include "UnitTest.h"
  5. extern unsigned char OpenSansData[];
  6. #include "Gwen/Renderers/OpenGL_DebugFont.h"
  7. #ifdef __APPLE__
  8. #include "OpenGLWindow/MacOpenGLWindow.h"
  9. #else
  10. //#include "CustomGL/glew.h"
  11. #ifdef _WIN32
  12. #include "OpenGLWindow/Win32OpenGLWindow.h"
  13. #else
  14. //let's cross the fingers it is Linux/X11
  15. #include "OpenGLWindow/X11OpenGLWindow.h"
  16. #endif //_WIN32
  17. #endif//__APPLE__
  18. #include "OpenGLWindow/opengl_fontstashcallbacks.h"
  19. #ifndef NO_OPENGL3
  20. #include "OpenGLWindow/GwenOpenGL3CoreRenderer.h"
  21. #include "OpenGLWindow/GLPrimitiveRenderer.h"
  22. #endif
  23. #include <assert.h>
  24. Gwen::Controls::Canvas* pCanvas = NULL;
  25. Gwen::Skin::Simple skin;
  26. void MyMouseMoveCallback( float x, float y)
  27. {
  28. //b3DefaultMouseCallback(button,state,x,y);
  29. static int m_lastmousepos[2] = {0,0};
  30. static bool isInitialized = false;
  31. if (pCanvas)
  32. {
  33. if (!isInitialized)
  34. {
  35. isInitialized = true;
  36. m_lastmousepos[0] = x+1;
  37. m_lastmousepos[1] = y+1;
  38. }
  39. bool handled = pCanvas->InputMouseMoved(x,y,m_lastmousepos[0],m_lastmousepos[1]);
  40. }
  41. }
  42. void MyMouseButtonCallback(int button, int state, float x, float y)
  43. {
  44. //b3DefaultMouseCallback(button,state,x,y);
  45. if (pCanvas)
  46. {
  47. bool handled = pCanvas->InputMouseMoved(x,y,x, y);
  48. if (button>=0)
  49. {
  50. handled = pCanvas->InputMouseButton(button,state);
  51. if (handled)
  52. {
  53. if (!state)
  54. return;
  55. }
  56. }
  57. }
  58. }
  59. int sWidth = 800;//1050;
  60. int sHeight = 600;//768;
  61. GLPrimitiveRenderer* primRenderer=0;
  62. //GwenOpenGL3CoreRenderer* gwenRenderer=0;
  63. Gwen::Renderer::Base* gwenRenderer =0;
  64. static void MyResizeCallback( float width, float height)
  65. {
  66. sWidth = width;
  67. sHeight = height;
  68. // printf("resize(%d,%d)\n",sWidth,sHeight);
  69. #ifndef NO_OPENGL3
  70. if (primRenderer)
  71. {
  72. primRenderer->setScreenSize(width,height);
  73. }
  74. #endif
  75. if (gwenRenderer)
  76. {
  77. gwenRenderer->Resize(width,height);
  78. }
  79. if (pCanvas)
  80. {
  81. pCanvas->SetSize( sWidth, sHeight);
  82. }
  83. }
  84. int droidRegular;//, droidItalic, droidBold, droidJapanese, dejavu;
  85. #ifndef NO_OPENGL3
  86. sth_stash* initFont(GLPrimitiveRenderer* primRenderer)
  87. {
  88. GLint err;
  89. struct sth_stash* stash = 0;
  90. OpenGL2RenderCallbacks* renderCallbacks = new OpenGL2RenderCallbacks(primRenderer);
  91. stash = sth_create(512,512,renderCallbacks);//256,256);//,1024);//512,512);
  92. err = glGetError();
  93. assert(err==GL_NO_ERROR);
  94. if (!stash)
  95. {
  96. fprintf(stderr, "Could not create stash.\n");
  97. return 0;
  98. }
  99. #ifdef LOAD_FONTS_FROM_FILE
  100. int datasize;
  101. unsigned char* data;
  102. float sx,sy,dx,dy,lh;
  103. GLuint texture;
  104. const char* fontPaths[]={
  105. "./",
  106. "../../bin/",
  107. "../bin/",
  108. "bin/"
  109. };
  110. int numPaths=sizeof(fontPaths)/sizeof(char*);
  111. // Load the first truetype font from memory (just because we can).
  112. FILE* fp = 0;
  113. const char* fontPath ="./";
  114. char fullFontFileName[1024];
  115. for (int i=0;i<numPaths;i++)
  116. {
  117. fontPath = fontPaths[i];
  118. //sprintf(fullFontFileName,"%s%s",fontPath,"OpenSans.ttf");//"DroidSerif-Regular.ttf");
  119. sprintf(fullFontFileName,"%s%s",fontPath,"DroidSerif-Regular.ttf");//OpenSans.ttf");//"DroidSerif-Regular.ttf");
  120. fp = fopen(fullFontFileName, "rb");
  121. if (fp)
  122. break;
  123. }
  124. err = glGetError();
  125. assert(err==GL_NO_ERROR);
  126. assert(fp);
  127. if (fp)
  128. {
  129. fseek(fp, 0, SEEK_END);
  130. datasize = (int)ftell(fp);
  131. fseek(fp, 0, SEEK_SET);
  132. data = (unsigned char*)malloc(datasize);
  133. if (data == NULL)
  134. {
  135. assert(0);
  136. return 0;
  137. }
  138. else
  139. fread(data, 1, datasize, fp);
  140. fclose(fp);
  141. fp = 0;
  142. }
  143. if (!(droidRegular = sth_add_font_from_memory(stash, data)))
  144. {
  145. assert(0);
  146. return 0;
  147. }
  148. err = glGetError();
  149. assert(err==GL_NO_ERROR);
  150. // Load the remaining truetype fonts directly.
  151. sprintf(fullFontFileName,"%s%s",fontPath,"DroidSerif-Italic.ttf");
  152. if (!(droidItalic = sth_add_font(stash,fullFontFileName)))
  153. {
  154. assert(0);
  155. return 0;
  156. }
  157. sprintf(fullFontFileName,"%s%s",fontPath,"DroidSerif-Bold.ttf");
  158. if (!(droidBold = sth_add_font(stash,fullFontFileName)))
  159. {
  160. assert(0);
  161. return 0;
  162. }
  163. err = glGetError();
  164. assert(err==GL_NO_ERROR);
  165. sprintf(fullFontFileName,"%s%s",fontPath,"DroidSansJapanese.ttf");
  166. if (!(droidJapanese = sth_add_font(stash,fullFontFileName)))
  167. {
  168. assert(0);
  169. return 0;
  170. }
  171. #else
  172. unsigned char* data = OpenSansData;
  173. if (!(droidRegular = sth_add_font_from_memory(stash, data)))
  174. {
  175. printf("error!\n");
  176. }
  177. #endif
  178. err = glGetError();
  179. assert(err==GL_NO_ERROR);
  180. return stash;
  181. }
  182. #endif
  183. void keyCallback(int key, int value)
  184. {
  185. printf("key = %d, value = %d\n", key,value);
  186. //pCanvas->InputKey(key,value==1);
  187. int gwenKey = -1;
  188. switch (key)
  189. {
  190. case B3G_LEFT_ARROW:
  191. {
  192. gwenKey = Gwen::Key::Left;
  193. break;
  194. }
  195. case B3G_RIGHT_ARROW:
  196. {
  197. gwenKey = Gwen::Key::Right;
  198. break;
  199. }
  200. case B3G_UP_ARROW:
  201. {
  202. gwenKey = Gwen::Key::Up;
  203. break;
  204. }
  205. case B3G_DOWN_ARROW:
  206. {
  207. gwenKey = Gwen::Key::Down;
  208. break;
  209. }
  210. case B3G_BACKSPACE:
  211. {
  212. gwenKey = Gwen::Key::Backspace;
  213. break;
  214. }
  215. case B3G_DELETE:
  216. {
  217. gwenKey = Gwen::Key::Delete;
  218. break;
  219. }
  220. case B3G_HOME:
  221. {
  222. gwenKey = Gwen::Key::Home;
  223. break;
  224. }
  225. case B3G_END:
  226. {
  227. gwenKey = Gwen::Key::End;
  228. break;
  229. }
  230. case B3G_SHIFT:
  231. {
  232. gwenKey = Gwen::Key::Shift;
  233. break;
  234. }
  235. case B3G_CONTROL:
  236. {
  237. gwenKey = Gwen::Key::Control;
  238. break;
  239. }
  240. default:
  241. {
  242. }
  243. };
  244. if (gwenKey>=0)
  245. {
  246. pCanvas->InputKey(gwenKey,value==1);
  247. } else
  248. {
  249. if (key<256 && value)
  250. {
  251. Gwen::UnicodeChar c = ( Gwen::UnicodeChar ) key;
  252. pCanvas->InputCharacter(c);
  253. }
  254. }
  255. }
  256. extern int avoidUpdate;
  257. int main()
  258. {
  259. b3gDefaultOpenGLWindow* window = new b3gDefaultOpenGLWindow();
  260. window->setKeyboardCallback(keyCallback);
  261. b3gWindowConstructionInfo wci;
  262. #ifndef NO_OPENGL3
  263. wci.m_openglVersion = 3;
  264. #else
  265. wci.m_openglVersion = 2;
  266. #endif
  267. wci.m_width = sWidth;
  268. wci.m_height = sHeight;
  269. // wci.m_resizeCallback = MyResizeCallback;
  270. window->createWindow(wci);
  271. window->setResizeCallback(MyResizeCallback);
  272. int majorGlVersion, minorGlVersion;
  273. if (!sscanf((const char*)glGetString(GL_VERSION), "%d.%d", &majorGlVersion, &minorGlVersion)==2)
  274. {
  275. printf("Exit: Error cannot extract OpenGL version from GL_VERSION string\n");
  276. exit(0);
  277. }
  278. char title[1024];
  279. if (wci.m_openglVersion>2)
  280. {
  281. sprintf(title,"Gwen with OpenGL %d.%d\n",majorGlVersion,minorGlVersion);
  282. } else
  283. {
  284. sprintf(title,"Gwen with OpenGL %d\n",wci.m_openglVersion);
  285. }
  286. window->setWindowTitle(title);
  287. #ifndef NO_OPENGL3
  288. if (majorGlVersion>=3 && wci.m_openglVersion>=3)
  289. {
  290. float retinaScale = 1.f;
  291. #ifndef __APPLE__
  292. #ifndef _WIN32
  293. //we need glewExperimental on Linux
  294. glewExperimental = GL_TRUE;
  295. #endif // _WIN32
  296. glewInit();
  297. #endif
  298. //we ned to call glGetError twice, because of some Ubuntu/Intel/OpenGL issue
  299. GLuint err = glGetError();
  300. err = glGetError();
  301. assert(err==GL_NO_ERROR);
  302. retinaScale = window->getRetinaScale();
  303. primRenderer = new GLPrimitiveRenderer(sWidth,sHeight);
  304. sth_stash* font = initFont(primRenderer );
  305. gwenRenderer = new GwenOpenGL3CoreRenderer(primRenderer,font,sWidth,sHeight,retinaScale);
  306. } else
  307. #endif
  308. {
  309. //OpenGL 2.x
  310. gwenRenderer = new Gwen::Renderer::OpenGL_DebugFont();
  311. skin.SetRender( gwenRenderer );
  312. glClearColor(1,0,0,1);
  313. }
  314. //
  315. // Create a GWEN OpenGL Renderer
  316. //
  317. // Gwen::Renderer::OpenGL_DebugFont * pRenderer = new Gwen::Renderer::OpenGL_DebugFont();
  318. //
  319. // Create a GWEN skin
  320. //
  321. #ifdef USE_TEXTURED_SKIN
  322. Gwen::Skin::TexturedBase skin;
  323. skin.SetRender( pRenderer );
  324. skin.Init("DefaultSkin.png");
  325. #else
  326. skin.SetRender( gwenRenderer );
  327. #endif
  328. //
  329. // Create a Canvas (it's root, on which all other GWEN panels are created)
  330. //
  331. pCanvas = new Gwen::Controls::Canvas( &skin );
  332. pCanvas->SetSize( sWidth, sHeight);
  333. pCanvas->SetDrawBackground( true );
  334. pCanvas->SetBackgroundColor( Gwen::Color( 150, 170, 170, 255 ) );
  335. window->setMouseButtonCallback(MyMouseButtonCallback);
  336. window->setMouseMoveCallback(MyMouseMoveCallback);
  337. //
  338. // Create our unittest control (which is a Window with controls in it)
  339. //
  340. UnitTest* pUnit = new UnitTest( pCanvas );
  341. pUnit->SetPos( 10, 10 );
  342. //
  343. // Create a Windows Control helper
  344. // (Processes Windows MSG's and fires input at GWEN)
  345. //
  346. //Gwen::Input::Windows GwenInput;
  347. //GwenInput.Initialize( pCanvas );
  348. //
  349. // Begin the main game loop
  350. //
  351. // MSG msg;
  352. while( !window->requestedExit() )
  353. {
  354. if (majorGlVersion<3 || wci.m_openglVersion<3)
  355. {
  356. saveOpenGLState(sWidth,sHeight);
  357. }
  358. // Skip out if the window is closed
  359. //if ( !IsWindowVisible( g_pHWND ) )
  360. //break;
  361. // If we have a message from windows..
  362. // if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  363. {
  364. // .. give it to the input handler to process
  365. // GwenInput.ProcessMessage( msg );
  366. // if it's QUIT then quit..
  367. // if ( msg.message == WM_QUIT )
  368. // break;
  369. // Handle the regular window stuff..
  370. // TranslateMessage(&msg);
  371. // DispatchMessage(&msg);
  372. }
  373. window->startRendering();
  374. // Main OpenGL Render Loop
  375. {
  376. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  377. glEnable(GL_BLEND);
  378. GLint err = glGetError();
  379. assert(err==GL_NO_ERROR);
  380. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  381. err = glGetError();
  382. assert(err==GL_NO_ERROR);
  383. err = glGetError();
  384. assert(err==GL_NO_ERROR);
  385. glDisable(GL_DEPTH_TEST);
  386. err = glGetError();
  387. assert(err==GL_NO_ERROR);
  388. //glColor4ub(255,0,0,255);
  389. err = glGetError();
  390. assert(err==GL_NO_ERROR);
  391. err = glGetError();
  392. assert(err==GL_NO_ERROR);
  393. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  394. // saveOpenGLState(width,height);//m_glutScreenWidth,m_glutScreenHeight);
  395. err = glGetError();
  396. assert(err==GL_NO_ERROR);
  397. err = glGetError();
  398. assert(err==GL_NO_ERROR);
  399. glDisable(GL_CULL_FACE);
  400. glDisable(GL_DEPTH_TEST);
  401. err = glGetError();
  402. assert(err==GL_NO_ERROR);
  403. err = glGetError();
  404. assert(err==GL_NO_ERROR);
  405. glEnable(GL_BLEND);
  406. err = glGetError();
  407. assert(err==GL_NO_ERROR);
  408. pCanvas->RenderCanvas();
  409. if (avoidUpdate<=0)
  410. avoidUpdate++;
  411. // SwapBuffers( GetDC( g_pHWND ) );
  412. }
  413. window->endRendering();
  414. if (majorGlVersion<3 || wci.m_openglVersion<3)
  415. {
  416. restoreOpenGLState();
  417. }
  418. }
  419. window->closeWindow();
  420. delete window;
  421. }