You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.7 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #include "video.h"
  24. #include <cassert>
  25. #include <stdio.h>
  26. #include <iostream>
  27. #include <pthread.h>
  28. unsigned int* g_pImg = 0;
  29. int g_sizex=0, g_sizey=0;
  30. static video *g_video = 0;
  31. static int g_fps = 0;
  32. char *window_title=NULL;
  33. #define WINDOW_TITLE_SIZE 256
  34. int cocoa_update=0;
  35. #include <sched.h>
  36. #include <sys/time.h>
  37. struct timeval g_time;
  38. video::video()
  39. : red_mask(0xff0000), red_shift(16), green_mask(0xff00),
  40. green_shift(8), blue_mask(0xff), blue_shift(0), depth(24)
  41. {
  42. assert(g_video == 0);
  43. g_video = this; title = "Video"; cocoa_update=1; updating = true; calc_fps = false;
  44. }
  45. bool video::init_window(int x, int y)
  46. {
  47. g_sizex = x; g_sizey = y;
  48. g_pImg = new unsigned int[x*y];
  49. if( window_title==NULL )
  50. window_title = (char*)malloc(WINDOW_TITLE_SIZE);
  51. strncpy( window_title, title, WINDOW_TITLE_SIZE-1 );
  52. running = true;
  53. return true;
  54. }
  55. bool video::init_console()
  56. {
  57. running = true;
  58. return true;
  59. }
  60. void video::terminate()
  61. {
  62. if(calc_fps) {
  63. double fps = g_fps;
  64. struct timezone tz; struct timeval end_time; gettimeofday(&end_time, &tz);
  65. fps /= (end_time.tv_sec+1.0*end_time.tv_usec/1000000.0) - (g_time.tv_sec+1.0*g_time.tv_usec/1000000.0);
  66. printf("%s: %.1f fps\n", title, fps);
  67. }
  68. g_video = 0; running = false;
  69. if(g_pImg) { delete[] g_pImg; g_pImg = 0; }
  70. }
  71. video::~video()
  72. {
  73. if(g_video) terminate();
  74. }
  75. //! Count and display FPS count in titlebar
  76. bool video::next_frame()
  77. {
  78. if(calc_fps){
  79. if(!g_fps) {
  80. struct timezone tz; gettimeofday(&g_time, &tz);
  81. }
  82. g_fps++;
  83. }
  84. struct timezone tz; struct timeval now_time; gettimeofday(&now_time, &tz);
  85. double sec=((now_time.tv_sec+1.0*now_time.tv_usec/1000000.0) - (g_time.tv_sec+1.0*g_time.tv_usec/1000000.0));
  86. if( sec>1 ){
  87. if(calc_fps) {
  88. memcpy(&g_time, &now_time, sizeof(g_time));
  89. int fps;
  90. fps = g_fps/sec;
  91. cocoa_update = (int)updating;
  92. snprintf(window_title,WINDOW_TITLE_SIZE, "%s%s: %d fps", title, updating?"":" (no updating)", int(fps));
  93. g_fps=0;
  94. }
  95. }
  96. return running;
  97. }
  98. void* thread_func(void*)
  99. {
  100. g_video->on_process();
  101. exit(EXIT_SUCCESS);
  102. }
  103. extern "C" void on_mouse_func(int x, int y, int k)
  104. {
  105. g_video->on_mouse(x, y, k);
  106. return;
  107. }
  108. extern "C" void on_key_func(int x)
  109. {
  110. g_video->on_key(x);
  111. return;
  112. }
  113. extern "C" int cocoa_main( int argc, char *argv[] );
  114. //! Do standard loop
  115. void video::main_loop()
  116. {
  117. pthread_t handle;
  118. pthread_attr_t attr;
  119. pthread_attr_init(&attr);
  120. pthread_create(&handle,&attr,&thread_func,(void*)NULL);
  121. pthread_detach(handle);
  122. cocoa_main( 0, NULL );
  123. }
  124. //! Change window title
  125. void video::show_title()
  126. {
  127. strncpy( window_title, title, WINDOW_TITLE_SIZE );
  128. return;
  129. }
  130. ///////////////////////////////////////////// public methods of video class ///////////////////////
  131. drawing_area::drawing_area(int x, int y, int sizex, int sizey)
  132. : start_x(x), start_y(y), size_x(sizex), size_y(sizey), pixel_depth(24),
  133. base_index(y*g_sizex + x), max_index(g_sizex*g_sizey), index_stride(g_sizex), ptr32(g_pImg)
  134. {
  135. assert(x < g_sizex); assert(y < g_sizey);
  136. assert(x+sizex <= g_sizex); assert(y+sizey <= g_sizey);
  137. index = base_index; // current index
  138. }
  139. void drawing_area::update()
  140. {
  141. //nothing to do, updating via timer in cocoa part.
  142. }