int main(int argc, char *argv[]) { init(); dbgoutf("Initialisation terminee! Chargement des planetes..."); bool planets_loaded = false; if(argc > 1 && argv[1] && *argv[1] != '\0') { char *filename = argv[1]; planets_file = newstring(argv[1]); dbgoutf("Lecture du fichier \"%s\"", filename); planets_loaded = load_planets(filename); } if(!planets_loaded) { planets_loaded = load_planets(DEFAULT_PLANETS_FILE); if(!planets_loaded) { dbgoutf("Impossible de charger les planetes. Fermeture du programme."); callback_quit(); return EXIT_FAILURE; } } compute_thread = SDL_CreateThread(compute_thread_func, NULL); SDL_Event event; if(invecrange(planets, 0)) curplanet = planets[0]; render_millis.maxfps = MAXFPS; compute_millis.maxfps = MAXCPS; // main loop for(;;) { render_millis.last = render_millis.current; render_millis.current = SDL_GetTicks(); render_millis.diff = render_millis.current-render_millis.last; SDL_PollEvent(&event); bool pressed = false; switch(event.type) { case SDL_KEYDOWN: { pressed = true; // a key has been pressed switch (event.key.keysym.sym) { case SDLK_ESCAPE: callback_quit(); break; case SDLK_s: start_pause(1); break; case SDLK_p: start_pause(0); break; case SDLK_7: case SDLK_KP7: if(keyreleased) precision /= 1.1; break; case SDLK_8: case SDLK_KP8: if(keyreleased) precision *= 1.1; break; case SDLK_F1: if(keyreleased) smoother(); break; case SDLK_F2: if(keyreleased) harder(); break; case SDLK_o: { if(keyreleased) save_planets(); } break; case SDLK_F4: { if(keyreleased) save_datas(); } break; //case SDLK_F11: { if((keyreleased || 1) && !print_frame) print_frame = true; } break; case SDLK_F12: { char buf[64] = ""; sprintf(buf, "../images/screenshot_%d.bmp", render_millis.current); takeScreenshot(buf); } break; case SDLK_KP_PLUS: if(keyreleased) zoom_in(); break; case SDLK_KP_MINUS: if(keyreleased) zoom_out(); break; case SDLK_KP0: if(keyreleased) skip_hists = !skip_hists; break; case SDLK_F3: if(keyreleased) draw_area = !draw_area; break; case SDLK_q: case SDLK_a: if(keyreleased) draw_accel = !draw_accel; break; case SDLK_f: SDL_WM_ToggleFullScreen(screen); break; case SDLK_4: case SDLK_KP4: if(keyreleased) increase_speed(); break; case SDLK_5: case SDLK_KP5: if(keyreleased) lower_speed(); break; case SDLK_RETURN: if(keyreleased) studied(); break; case SDLK_t: if(keyreleased) curplanet->showhist = !curplanet->showhist; break; case SDLK_1: case SDLK_KP1: if(keyreleased) prev_ref(); break; case SDLK_2: case SDLK_KP2: if(keyreleased) next_ref(); break; case SDLK_RIGHT: { angle.y -= 20.0*double(render_millis.diff)/1000.0; angle.y = angle.y > 360 ? int(angle.y) % 360 : angle.y; } break; case SDLK_LEFT: { angle.y += 20.0*double(render_millis.diff)/1000.0; angle.y = angle.y > 360 ? int(angle.y) % 360 : angle.y; } case SDLK_UP: { angle.z += 20.0*double(render_millis.diff)/1000.0; angle.z = angle.z > 360 ? int(angle.z) % 360 : angle.z; } break; case SDLK_DOWN: { angle.z -= 20.0*double(render_millis.diff)/1000.0; angle.z = angle.z > 360 ? int(angle.z) % 360 : angle.z; } default: { } } } break; case SDL_QUIT: callback_quit(); break; case SDL_VIDEORESIZE: { w = event.resize.w; h = event.resize.h; glViewport(0, 0, w, h); start_pause(0); } case SDL_MOUSEBUTTONDOWN: { if(!mousedown && event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT) mouse_click(event.button.button, event.button.x, h-event.button.y, true); if(event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT) mousedown = true; } break; case SDL_MOUSEBUTTONUP: { if(event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT && mousedown) { mousedown = false; mouse_click(event.button.button, event.button.x, h-event.button.y, false); } } break; case SDL_MOUSEMOTION: { mouse_x = event.motion.x; mouse_y = h-event.motion.y; } break; } if(pressed && keyreleased) keyreleased = false; else if(!pressed && !keyreleased) keyreleased = true; limitfps(render_millis); render(); } system("pause"); callback_quit(); return EXIT_SUCCESS; }
// The initialization method caused when a world object is created World6::World6(WindowFramework* windowFrameworkPtr) : m_windowFramework(windowFrameworkPtr), m_title("title", COnscreenText::TS_plain), m_sizescale(1), m_sky(), m_skyTex(NULL), m_sun(), m_sunTex(NULL), m_orbitscale(1), m_orbitRootMercury(), m_orbitRootVenus(), m_orbitRootMars(), m_orbitRootEarth(), m_orbitRootMoon(), m_mercury(), m_mercuryTex(NULL), m_venus(), m_venusTex(NULL), m_mars(), m_marsTex(NULL), m_earth(), m_earthTex(NULL), m_moon(), m_moonTex(NULL), m_yearscale(1), m_dayscale(1), m_dayPeriodSun(NULL), m_orbitPeriodMercury(NULL), m_dayPeriodMercury(NULL), m_orbitPeriodVenus(NULL), m_dayPeriodVenus(NULL), m_orbitPeriodEarth(NULL), m_dayPeriodEarth(NULL), m_orbitPeriodMoon(NULL), m_dayPeriodMoon(NULL), m_orbitPeriodMars(NULL), m_dayPeriodMars(NULL), m_mouse1EventText("label"), m_skeyEventText("label"), m_ykeyEventText("label"), m_vkeyEventText("label"), m_ekeyEventText("label"), m_mkeyEventText("label"), m_yearCounterText("label"), m_yearCounter(0), m_simRunning(true) { // The standard camera position and background initialization m_windowFramework->get_graphics_window()->get_active_display_region(0)-> set_clear_color(Colorf(0, 0, 0, 0)); // base.disableMouse() // Note: mouse ain't enable by default in C++ NodePath camera = m_windowFramework->get_camera_group(); camera.set_pos(0, 0, 45); camera.set_hpr(0, -90, 0); // The global variables we used to control the speed and size of objects m_yearscale = 60; m_dayscale = m_yearscale / 365.0 * 5; m_orbitscale = 10; m_sizescale = 0.6; load_planets(); // Load, texture, and position the planets rotate_planets(); // Set up the motion to start them moving // The standard title text that's in every tutorial // Things to note: // -fg represents the forground color of the text in (r,g,b,a) format // -pos represents the position of the text on the screen. // The coordinate system is a x-y based wih 0,0 as the center of the // screen // -align sets the alingment of the text relative to the pos argument. // Default is center align. // -scale set the scale of the text // -mayChange argument lets us change the text later in the program. // By default mayChange is set to 0. Trying to change text when // mayChange is set to 0 will cause the program to crash. m_title.set_text("Panda3D: Tutorial 1 - Solar System"); m_title.set_fg(Colorf(1, 1, 1, 1)); m_title.set_pos(LVecBase2f(0.8, -0.95)); m_title.set_scale(0.07); m_title.reparent_to(m_windowFramework->get_aspect_2d()); m_mouse1EventText = gen_label_text( "Mouse Button 1: Toggle entire Solar System [RUNNING]", 0); m_skeyEventText = gen_label_text("[S]: Toggle Sun [RUNNING]", 1); m_ykeyEventText = gen_label_text("[Y]: Toggle Mercury [RUNNING]", 2); m_vkeyEventText = gen_label_text("[V]: Toggle Venus [RUNNING]", 3); m_ekeyEventText = gen_label_text("[E]: Toggle Earth [RUNNING]", 4); m_mkeyEventText = gen_label_text("[M]: Toggle Mars [RUNNING]", 5); m_yearCounterText = gen_label_text("0 Earth years completed", 6); m_yearCounter = 0; // year counter for earth years m_simRunning = true; // boolean to keep track of the // state of the global simulation // Events // Each self.accept statement creates an event handler object that will call // the specified function when that event occurs. // Certain events like "mouse1", "a", "b", "c" ... "z", "1", "2", "3"..."0" // are references to keyboard keys and mouse buttons. You can also define // your own events to be used within your program. In this tutorial, the // event "newYear" is not tied to a physical input device, but rather // is sent by the function that rotates the Earth whenever a revolution // completes to tell the counter to update // Note: need to listen to input events m_windowFramework->enable_keyboard(); PandaFramework* pfw = m_windowFramework->get_panda_framework(); // Exit the program when escape is pressed pfw->define_key("escape", "sysExit", sys_exit, NULL); pfw->define_key("mouse1", "handleMouseClick", call_handle_mouse_click, this); pfw->define_key("e", "handleEarth", call_handle_earth, this); pfw->define_key("s", // message name "togglePlanetSun", // Note: event description call_toggle_planet<P_sun>, // function to call this); // arguments to be passed to togglePlanet // See togglePlanet's definition below for // an explanation of what they are // Repeat the structure above for the other planets pfw->define_key("y", "togglePlanetMercury", call_toggle_planet<P_mercury>, this); pfw->define_key("v", "togglePlanetVenus", call_toggle_planet<P_venus>, this); pfw->define_key("m", "togglePlanetMars", call_toggle_planet<P_mars>, this); EventHandler::get_global_event_handler()->add_hook("newYear", call_inc_year, this); }
// The initialization method caused when a world object is created World5::World5(WindowFramework* windowFrameworkPtr) : m_windowFrameworkPtr(windowFrameworkPtr), m_title("title", COnscreenText::TS_plain), m_sizescale(1), m_sky(), m_skyTex(NULL), m_sun(), m_sunTex(NULL), m_orbitscale(1), m_orbitRootMercury(), m_orbitRootVenus(), m_orbitRootMars(), m_orbitRootEarth(), m_orbitRootMoon(), m_mercury(), m_mercuryTex(NULL), m_venus(), m_venusTex(NULL), m_mars(), m_marsTex(NULL), m_earth(), m_earthTex(NULL), m_moon(), m_moonTex(NULL), m_yearscale(1), m_dayscale(1), m_dayPeriodSun(NULL), m_orbitPeriodMercury(NULL), m_dayPeriodMercury(NULL), m_orbitPeriodVenus(NULL), m_dayPeriodVenus(NULL), m_orbitPeriodEarth(NULL), m_dayPeriodEarth(NULL), m_orbitPeriodMoon(NULL), m_dayPeriodMoon(NULL), m_orbitPeriodMars(NULL), m_dayPeriodMars(NULL) { // This is the initialization we had before m_title.set_text("Panda3D: Tutorial 1 - Solar System"); m_title.set_fg(Colorf(1, 1, 1, 1)); m_title.set_pos(LVecBase2f(0.8, -0.95)); m_title.set_scale(0.07); m_title.reparent_to(m_windowFrameworkPtr->get_aspect_2d()); // Set the background to black m_windowFrameworkPtr->set_background_type(WindowFramework::BT_black); // Note: mouse ain't enable by default in C++ // base.disableMouse() // disable mouse control of the camera NodePath camera = m_windowFrameworkPtr->get_camera_group(); camera.set_pos(0, 0, 45); // Set the camera position (X, Y, Z) camera.set_hpr(0, -90, 0); // Set the camera orientation // (heading, pitch, roll) in degrees // Here again is where we put our global variables. Added this time are // variables to control the relative speeds of spinning and orbits in the // simulation // Number of seconds a full rotation of Earth around the sun should take m_yearscale = 60; // Number of seconds a day rotation of Earth should take. // It is scaled from its correct value for easier visability m_dayscale = m_yearscale / 365.0 * 5; m_sizescale = 0.6; // relative size of planets m_orbitscale = 10; // relative size of orbits load_planets(); // Load our models and make them render // Finally, we call the rotatePlanets function which puts the planets, // sun, and moon into motion. rotate_planets(); }