123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- /**
- \file init.cpp
-
- \brief Experimental C++ wxWidgets Nviz prototype -- initialization
- Used by wxGUI Nviz extension.
- Copyright: (C) by the GRASS Development Team
- This program is free software under the GNU General Public
- License (>=v2). Read the file COPYING that comes with GRASS
- for details.
- \author Martin Landa <landa.martin gmail.com> (Google SoC 2008)
- \date 2008
- */
- #include "nviz.h"
- #define MSG 0
- #define WARN 1
- #define ERR 2
- static void swap_gl();
- static int print_error(const char *, const int);
- static int print_percent(int);
- static void print_sentence (PyObject*, const int, const char *);
- static PyObject *logStream;
- static int message_id = 1;
- /*!
- \brief Initialize Nviz class instance
- */
- Nviz::Nviz(PyObject *log)
- {
- G_gisinit(""); /* GRASS functions */
- logStream = log;
- G_set_error_routine(&print_error);
- // TODO
- // G_set_percent_routine(&print_percent);
- GS_libinit();
- GVL_libinit();
- GS_set_swap_func(swap_gl);
- data = (nv_data*) G_malloc(sizeof (nv_data));
- /* GLCanvas */
- glCanvas = NULL;
- G_debug(1, "Nviz::Nviz()");
- }
- /*!
- \brief Destroy Nviz class instance
- */
- Nviz::~Nviz()
- {
- G_unset_error_routine();
- // TODO
- // G_unset_percent_routine();
- G_free((void *) data);
- data = NULL;
- glCanvas = NULL;
- logStream = NULL;
- }
- /*!
- \brief Associate display with render window
- \return 1 on success
- \return 0 on failure
- */
- int Nviz::SetDisplay(void *display)
- {
- glCanvas = (wxGLCanvas *) display;
- // glCanvas->SetCurrent();
- G_debug(1, "Nviz::SetDisplay()");
- return 1;
- }
- void Nviz::InitView()
- {
- /* initialize nviz data */
- Nviz_init_data(data);
- /* define default attributes for map objects */
- Nviz_set_surface_attr_default();
- /* set background color */
- Nviz_set_bgcolor(data, Nviz_color_from_str("white")); /* TODO */
- /* initialize view */
- Nviz_init_view();
- /* set default lighting model */
- SetLightsDefault();
- /* clear window */
- GS_clear(data->bgcolor);
- G_debug(1, "Nviz::InitView()");
- return;
- }
- /*!
- \brief Reset session
- Unload all data layers
- @todo vector, volume
- */
- void Nviz::Reset()
- {
- int i;
- int *surf_list, nsurfs;
- surf_list = GS_get_surf_list(&nsurfs);
- for (i = 0; i < nsurfs; i++) {
- GS_delete_surface(surf_list[i]);
- }
- }
- void swap_gl()
- {
- return;
- }
- /*!
- \brief Set background color
- \param color_str color string
- */
- void Nviz::SetBgColor(const char *color_str)
- {
- data->bgcolor = Nviz_color_from_str(color_str);
- return;
- }
- int print_error(const char *msg, const int type)
- {
- if (logStream) {
- print_sentence(logStream, type, msg);
- }
- else {
- fprintf(stderr, "Nviz: %s\n", msg);
- }
- return 0;
- }
- /*
- \brief Print one message, prefix inserted before each new line
- From lib/gis/error.c
- */
- void print_sentence (PyObject *pyFd, const int type, const char *msg)
- {
- char prefix[256];
- const char *start;
- char* sentence;
- switch ( type ) {
- case MSG:
- sprintf (prefix, "GRASS_INFO_MESSAGE(%d,%d): Nviz: ", getpid(), message_id);
- break;
- case WARN:
- sprintf (prefix, "GRASS_INFO_WARNING(%d,%d): Nviz: ", getpid(), message_id);
- break;
- case ERR:
- sprintf (prefix, "GRASS_INFO_ERROR(%d,%d): Nviz: ", getpid(), message_id);
- break;
- }
- start = msg;
- PyFile_WriteString("\n", pyFd);
- while ( *start != '\0' ) {
- const char *next = start;
- PyFile_WriteString(prefix, pyFd);
- while ( *next != '\0' ) {
- next++;
-
- if ( *next == '\n' ) {
- next++;
- break;
- }
- }
- sentence = (char *) G_malloc ((next - start + 1) * sizeof (char));
- strncpy(sentence, start, next - start + 1);
- sentence[next-start] = '\0';
- PyFile_WriteString(sentence, pyFd);
- G_free((void *)sentence);
- PyFile_WriteString("\n", pyFd);
- start = next;
- }
- PyFile_WriteString("\n", pyFd);
- sprintf(prefix, "GRASS_INFO_END(%d,%d)\n", getpid(), message_id);
- PyFile_WriteString(prefix, pyFd);
- message_id++;
- }
- int print_percent(int x)
- {
- char msg[256];
- sprintf(msg, "GRASS_INFO_PERCENT: %d\n", x);
- PyFile_WriteString(msg, logStream);
- return 0;
- }
|