void game::update() { if (mouse.ldown) { if (tool == TOOL_SMASH) wld->destroyAt(screen2world(vec2(mouse.x, mouse.y))); else if (tool == TOOL_GRAB) wld->drawTo(screen2world(vec2(mouse.x, mouse.y))); } if (running) wld->update(0.02); }
bool VisObj::select_vertex(const CGLA::Vec2i& pos) { float d; if(depth_pick(pos[0], pos[1], d)) { Vec3d c; float r; bsphere(mani, c, r); VertexID closest = InvalidVertexID; double min_dist = DBL_MAX; for(auto vid : mani.vertices()) { Vec3d wp = world2screen(mani.pos(vid)); if(sqr_length(Vec2d(wp[0],wp[1])-Vec2d(pos))<100) { double dist = sqr_length(screen2world(pos[0], pos[1], d)-mani.pos(vid)); if(dist < min_dist) { min_dist = dist; closest = vid; } } } if(closest != InvalidVertexID) { vertex_selection.resize(mani.allocated_vertices(),0); vertex_selection[closest] = !vertex_selection[closest]; active_selection = true; post_create_display_list(); return true; } } return false; }
/** * Process controllerinput into a new mapview */ void updateworld() { //Zooming if(wpaddown & WPAD_BUTTON_PLUS) { if(world_zoom_target < MAX_ZOOM) { stopslide(); world_zoom_target += 1; } else { rumble = 1; } } if(wpaddown & WPAD_BUTTON_MINUS) { if(world_zoom_target > MIN_ZOOM) { stopslide(); world_zoom_target -= 1; } else { rumble = 1; } } if(rumble == 1) WPAD_Rumble(WPAD_CHAN_0, 1); if(rumble > 2) { WPAD_Rumble(WPAD_CHAN_0, 0); rumble = 0; } if(rumble >= 1) rumble++; //Let world_zoom approach world_zoom_target world_zoom = world_zoom + ((world_zoom_target - world_zoom) / 10); world_width = coordinates2pixels(1); //Used within drawtile(); //Dragging if(wpaddown & WPAD_BUTTON_B) { stopslide(); struct point cursorworld = screen2world(cursor_x, cursor_y); world_grab_x = cursorworld.x; world_grab_y = cursorworld.y; world_grab_x -= floor(world_grab_x); } //Zooming towards a specific spot if(wpaddown & WPAD_BUTTON_A) { stopslide(); if(world_zoom_target < MAX_ZOOM) world_zoom_target++; else rumble = 1; struct point cursorworld = screen2world(cursor_x, cursor_y); world_zoomto_x = cursorworld.x; world_zoomto_y = cursorworld.y; world_zoomto_x -= floor(world_zoomto_x); if(world_zoomto_y < 0) world_zoomto_y = 0; if(world_zoomto_y > 1) world_zoomto_y = 1; world_zooming_to = TRUE; } //If the user is holding the world then move the world so that the world_grab_x & y variables //are always right under the cursor world_grabbed = wpadheld & WPAD_BUTTON_B; if(world_grabbed) { float cursor_xcenter_distance = pixels2coordinates(cursor_x - SCREEN_XCENTER); float cursor_ycenter_distance = pixels2coordinates(cursor_y - SCREEN_YCENTER); float newworld_x = world_grab_x - cursor_xcenter_distance; float newworld_y = world_grab_y - cursor_ycenter_distance; //Keep it between 0 and 1 newworld_x -= floor(newworld_x); if(newworld_y > 1) newworld_y = 1; if(newworld_y < 0) newworld_y = 0; //Store the speed in case the user lets go world_speed_x = newworld_x - world_x; world_speed_y = newworld_y - world_y; world_speed_x -= (int)(world_speed_x); world_x = newworld_x; world_y = newworld_y; } else { world_speed_x /= world_speed_x < pixels2coordinates(SPEED_CUTOFFPOINT) ? HIGHSPEED_DECAYRATE : LOWSPEED_DECAYRATE; world_speed_y /= world_speed_y < pixels2coordinates(SPEED_CUTOFFPOINT) ? HIGHSPEED_DECAYRATE : LOWSPEED_DECAYRATE; world_x += world_speed_x; world_y += world_speed_y; } //D-pad movement if(!world_grabbed) { if(wpadheld & WPAD_BUTTON_UP) { stopslide(); world_y -= pixels2coordinates(DPAD_SPEED); } if(wpadheld & WPAD_BUTTON_DOWN) { stopslide(); world_y += pixels2coordinates(DPAD_SPEED); } if(wpadheld & WPAD_BUTTON_LEFT) { stopslide(); world_x -= pixels2coordinates(DPAD_SPEED); } if(wpadheld & WPAD_BUTTON_RIGHT) { stopslide(); world_x += pixels2coordinates(DPAD_SPEED); } } //Move to point if(world_zooming_to) { world_x += (world_zoomto_x - world_x) / 10; world_y += (world_zoomto_y - world_y) / 10; } //Correct for panning across the earth world_x -= floor(world_x); if(world_y > 1) world_y = 1; if(world_y < 0) world_y = 0; //Update screen variables struct point topleft = screen2world(0, 0); struct point bottomright = screen2world(SCREEN_WIDTH, SCREEN_HEIGHT); screen_left = topleft.x; screen_top = topleft.y; screen_right = bottomright.x; screen_bottom = bottomright.y; }
void init() { #ifdef GRAPHICS if (!glfwInit()) { LOG("Failed to init GLFW."); exit(-1); } initGraph(&fps_graph, GRAPH_RENDER_FPS, "Frame Time"); initGraph(&cpu_graph, GRAPH_RENDER_MS, "CPU Time"); initGraph(&gpu_graph, GRAPH_RENDER_MS, "GPU Time"); glfwSetErrorCallback([](int error, const char* desc) {LOG("GLFW error %d: %s\n", error, desc); }); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); int height = glfwGetVideoMode(glfwGetPrimaryMonitor())->height*0.6, width = height*double(world::width) / double(world::height); window = glfwCreateWindow(width, height, "PedestrianSimulator", NULL, NULL); if (!window) { glfwTerminate(); exit(-1); } glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) { NVG_NOTUSED(scancode); NVG_NOTUSED(mods); if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE); }); glfwSetMouseButtonCallback(window, [](GLFWwindow* wind, int button, int action, int mods){ static vec_t start_pos; vecd_t pos; glfwGetCursorPos(wind, &pos.x, &pos.y); vec_t world_pos = screen2world({ pos.x, pos.y }); if (action == GLFW_RELEASE) { if (button == GLFW_MOUSE_BUTTON_1) { world::add_objective({ world_pos, 0 }); } else if (button == GLFW_MOUSE_BUTTON_2) { world::add_objective({ world_pos, 1 }); } else if (button==GLFW_MOUSE_BUTTON_3) LOG("click at screen (%.2lf, %.2lf), world (%.2f, %.2f)", pos.x, pos.y, world_pos.x, world_pos.y); } else if (action == GLFW_PRESS) { if (button == GLFW_MOUSE_BUTTON_2) { start_pos = world_pos; } } }); glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { LOG("Could not init glew.\n"); exit(-1); } glGetError(); vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS | NVG_STENCIL_STROKES); if (vg == NULL) { LOG("Could not init nanovg.\n"); exit(-1); } nvgCreateFont(vg, "sans", "roboto.ttf"); glfwSwapInterval(0); initGPUTimer(&gpu_timer); glfwSetTime(0); prev_time = glfwGetTime(); #endif }