Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

multi_window.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. (c) Copyright 2001 convergence integrated media GmbH.
  3. All rights reserved.
  4. Written by Denis Oliver Kropp <dok@convergence.de> and
  5. Andreas Hundt <andi@convergence.de>.
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the
  16. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <directfb.h>
  26. #include <directfbgl.h>
  27. typedef struct {
  28. IDirectFBWindow *window;
  29. IDirectFBSurface *surface;
  30. IDirectFBGL *gl;
  31. int width;
  32. int height;
  33. unsigned long last_time;
  34. int frames;
  35. float fps;
  36. } Context;
  37. static const GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
  38. static IDirectFB *dfb;
  39. static IDirectFBDisplayLayer *layer;
  40. static IDirectFBFont *font;
  41. static IDirectFBEventBuffer *events = NULL;
  42. /* macro for a safe call to DirectFB functions */
  43. #define DFBCHECK(x...) \
  44. do { \
  45. ret = x; \
  46. if (ret != DFB_OK) { \
  47. fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \
  48. DirectFBErrorFatal( #x, ret ); \
  49. } \
  50. } while (0)
  51. static inline unsigned long get_millis()
  52. {
  53. struct timeval tv;
  54. gettimeofday (&tv, NULL);
  55. return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
  56. }
  57. static void
  58. setup( Context *context )
  59. {
  60. GLfloat pos[4] = {5.0, 5.0, 10.0, 0.0};
  61. context->surface->GetSize( context->surface,
  62. &context->width, &context->height );
  63. context->gl->Lock( context->gl );
  64. glLightfv(GL_LIGHT0, GL_POSITION, pos);
  65. glEnable(GL_CULL_FACE);
  66. glEnable(GL_LIGHTING);
  67. glEnable(GL_LIGHT0);
  68. glEnable(GL_DEPTH_TEST);
  69. glViewport(0, 0, context->width, context->height);
  70. glMatrixMode(GL_PROJECTION);
  71. glLoadIdentity();
  72. gluPerspective(70.0, context->width / (float) context->height, 1.0, 80.0);
  73. glMatrixMode(GL_MODELVIEW);
  74. glLoadIdentity();
  75. glTranslatef(0.0, 0.0, -40.0);
  76. context->gl->Unlock( context->gl );
  77. }
  78. static void
  79. update( Context *context )
  80. {
  81. unsigned long t;
  82. IDirectFBSurface *surface = context->surface;
  83. static __u8 r = 0, g = 0, b = 0;
  84. context->gl->Lock( context->gl );
  85. glClearColor( r++/255.0, g++/255.0, b++/255.0, 1.0 );
  86. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  87. context->gl->Unlock( context->gl );
  88. if (context->fps) {
  89. char buf[16];
  90. snprintf(buf, sizeof(buf), "%.1f FPS\n", context->fps);
  91. surface->SetColor( surface, 0xff, 0x00, 0x00, 0xff );
  92. surface->DrawString( surface, buf, -1,
  93. context->width - 5, 5, DSTF_TOPRIGHT );
  94. }
  95. surface->Flip( surface, NULL, 0 );
  96. context->frames++;
  97. t = get_millis();
  98. if (t - context->last_time >= 2000) {
  99. float seconds = (t - context->last_time) / 1000.0f;
  100. context->fps = context->frames / seconds;
  101. context->last_time = t;
  102. context->frames = 0;
  103. }
  104. }
  105. int
  106. main( int argc, char *argv[] )
  107. {
  108. DFBResult ret;
  109. int i;
  110. int quit = 0;
  111. const int num = 2;
  112. Context contexts[num];
  113. DFBCHECK(DirectFBInit( &argc, &argv ));
  114. /* create the super interface */
  115. DFBCHECK(DirectFBCreate( &dfb ));
  116. DFBCHECK(dfb->GetDisplayLayer( dfb, DLID_PRIMARY, &layer ));
  117. /* create the default font */
  118. DFBCHECK(dfb->CreateFont( dfb, NULL, NULL, &font ));
  119. for (i=0; i<num; i++) {
  120. IDirectFBWindow *window;
  121. IDirectFBSurface *surface;
  122. IDirectFBGL *gl;
  123. DFBWindowDescription desc;
  124. desc.flags = DWDESC_POSX | DWDESC_POSY |
  125. DWDESC_WIDTH | DWDESC_HEIGHT;
  126. desc.posx = (i%3) * 200 + 10;
  127. desc.posy = (i/3) * 200 + 10;
  128. desc.width = 180;
  129. desc.height = 180;
  130. DFBCHECK(layer->CreateWindow( layer, &desc, &window ));
  131. DFBCHECK(window->GetSurface( window, &surface ));
  132. DFBCHECK(surface->GetGL( surface, &gl ));
  133. contexts[i].window = window;
  134. contexts[i].surface = surface;
  135. contexts[i].gl = gl;
  136. contexts[i].last_time = get_millis();
  137. contexts[i].frames = 0;
  138. contexts[i].fps = 0;
  139. setup( &contexts[i] );
  140. if (events)
  141. DFBCHECK(window->AttachEventBuffer( window, events ));
  142. else
  143. DFBCHECK(window->CreateEventBuffer( window, &events ));
  144. DFBCHECK(surface->SetFont( surface, font ));
  145. window->SetOpacity( window, 0xff );
  146. }
  147. while (!quit) {
  148. DFBWindowEvent evt;
  149. for (i=0; i<num; i++)
  150. update( &contexts[i] );
  151. while (events->GetEvent( events, DFB_EVENT(&evt) ) == DFB_OK) {
  152. switch (evt.type) {
  153. case DWET_KEYDOWN:
  154. switch (evt.key_symbol) {
  155. case DIKS_ESCAPE:
  156. quit = 1;
  157. break;
  158. default:
  159. break;
  160. }
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. }
  167. events->Release( events );
  168. for (i=0; i<num; i++) {
  169. contexts[i].gl->Release( contexts[i].gl );
  170. contexts[i].surface->Release( contexts[i].surface );
  171. contexts[i].window->Release( contexts[i].window );
  172. }
  173. font->Release( font );
  174. layer->Release( layer );
  175. dfb->Release( dfb );
  176. return 0;
  177. }