init.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. \file init.cpp
  3. \brief Experimental C++ wxWidgets Nviz prototype -- initialization
  4. Used by wxGUI Nviz extension.
  5. Copyright: (C) by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. \author Martin Landa <landa.martin gmail.com> (Google SoC 2008)
  10. \date 2008
  11. */
  12. #include "nviz.h"
  13. #define MSG 0
  14. #define WARN 1
  15. #define ERR 2
  16. static void swap_gl();
  17. static int print_error(const char *, const int);
  18. static int print_percent(int);
  19. static void print_sentence (PyObject*, const int, const char *);
  20. static PyObject *logStream;
  21. static int message_id = 1;
  22. /*!
  23. \brief Initialize Nviz class instance
  24. */
  25. Nviz::Nviz(PyObject *log)
  26. {
  27. G_gisinit(""); /* GRASS functions */
  28. logStream = log;
  29. G_set_error_routine(&print_error);
  30. // TODO
  31. // G_set_percent_routine(&print_percent);
  32. GS_libinit();
  33. GVL_libinit();
  34. GS_set_swap_func(swap_gl);
  35. data = (nv_data*) G_malloc(sizeof (nv_data));
  36. /* GLCanvas */
  37. glCanvas = NULL;
  38. G_debug(1, "Nviz::Nviz()");
  39. }
  40. /*!
  41. \brief Destroy Nviz class instance
  42. */
  43. Nviz::~Nviz()
  44. {
  45. G_unset_error_routine();
  46. // TODO
  47. // G_unset_percent_routine();
  48. G_free((void *) data);
  49. data = NULL;
  50. glCanvas = NULL;
  51. logStream = NULL;
  52. }
  53. /*!
  54. \brief Associate display with render window
  55. \return 1 on success
  56. \return 0 on failure
  57. */
  58. int Nviz::SetDisplay(void *display)
  59. {
  60. glCanvas = (wxGLCanvas *) display;
  61. // glCanvas->SetCurrent();
  62. G_debug(1, "Nviz::SetDisplay()");
  63. return 1;
  64. }
  65. void Nviz::InitView()
  66. {
  67. /* initialize nviz data */
  68. Nviz_init_data(data);
  69. /* define default attributes for map objects */
  70. Nviz_set_surface_attr_default();
  71. /* set background color */
  72. Nviz_set_bgcolor(data, Nviz_color_from_str("white")); /* TODO */
  73. /* initialize view */
  74. Nviz_init_view();
  75. /* set default lighting model */
  76. SetLightsDefault();
  77. /* clear window */
  78. GS_clear(data->bgcolor);
  79. G_debug(1, "Nviz::InitView()");
  80. return;
  81. }
  82. void swap_gl()
  83. {
  84. return;
  85. }
  86. /*!
  87. \brief Set background color
  88. \param color_str color string
  89. */
  90. void Nviz::SetBgColor(const char *color_str)
  91. {
  92. data->bgcolor = Nviz_color_from_str(color_str);
  93. return;
  94. }
  95. int print_error(const char *msg, const int type)
  96. {
  97. if (logStream) {
  98. print_sentence(logStream, type, msg);
  99. }
  100. else {
  101. fprintf(stderr, "Nviz: %s\n", msg);
  102. }
  103. return 0;
  104. }
  105. /*
  106. \brief Print one message, prefix inserted before each new line
  107. From lib/gis/error.c
  108. */
  109. void print_sentence (PyObject *pyFd, const int type, const char *msg)
  110. {
  111. char prefix[256];
  112. const char *start;
  113. char* sentence;
  114. switch ( type ) {
  115. case MSG:
  116. sprintf (prefix, "GRASS_INFO_MESSAGE(%d,%d): Nviz: ", getpid(), message_id);
  117. break;
  118. case WARN:
  119. sprintf (prefix, "GRASS_INFO_WARNING(%d,%d): Nviz: ", getpid(), message_id);
  120. break;
  121. case ERR:
  122. sprintf (prefix, "GRASS_INFO_ERROR(%d,%d): Nviz: ", getpid(), message_id);
  123. break;
  124. }
  125. start = msg;
  126. PyFile_WriteString("\n", pyFd);
  127. while ( *start != '\0' ) {
  128. const char *next = start;
  129. PyFile_WriteString(prefix, pyFd);
  130. while ( *next != '\0' ) {
  131. next++;
  132. if ( *next == '\n' ) {
  133. next++;
  134. break;
  135. }
  136. }
  137. sentence = (char *) G_malloc ((next - start + 1) * sizeof (char));
  138. strncpy(sentence, start, next - start + 1);
  139. sentence[next-start] = '\0';
  140. PyFile_WriteString(sentence, pyFd);
  141. G_free((void *)sentence);
  142. PyFile_WriteString("\n", pyFd);
  143. start = next;
  144. }
  145. PyFile_WriteString("\n", pyFd);
  146. sprintf(prefix, "GRASS_INFO_END(%d,%d)\n", getpid(), message_id);
  147. PyFile_WriteString(prefix, pyFd);
  148. message_id++;
  149. }
  150. int print_percent(int x)
  151. {
  152. char msg[256];
  153. sprintf(msg, "GRASS_INFO_PERCENT: %d\n", x);
  154. PyFile_WriteString(msg, logStream);
  155. return 0;
  156. }