1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
|
// MIT License
// Copyright (c) 2019 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#define _CRT_SECURE_NO_WARNINGS
#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS 1
#include "imgui/imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "draw.h"
#include "settings.h"
#include "test.h"
#include <algorithm>
#include <stdio.h>
#include <thread>
#include <chrono>
#if defined(_WIN32)
#include <crtdbg.h>
#endif
GLFWwindow* g_mainWindow = nullptr;
static int32 s_testSelection = 0;
static Test* s_test = nullptr;
static Settings s_settings;
static bool s_rightMouseDown = false;
static b2Vec2 s_clickPointWS = b2Vec2_zero;
void glfwErrorCallback(int error, const char* description)
{
fprintf(stderr, "GLFW error occured. Code: %d. Description: %s\n", error, description);
}
static inline bool CompareTests(const TestEntry& a, const TestEntry& b)
{
int result = strcmp(a.category, b.category);
if (result == 0)
{
result = strcmp(a.name, b.name);
}
return result < 0;
}
static void SortTests()
{
std::sort(g_testEntries, g_testEntries + g_testCount, CompareTests);
}
static void CreateUI(GLFWwindow* window, const char* glslVersion = NULL)
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
bool success;
success = ImGui_ImplGlfw_InitForOpenGL(window, false);
if (success == false)
{
printf("ImGui_ImplGlfw_InitForOpenGL failed\n");
assert(false);
}
success = ImGui_ImplOpenGL3_Init(glslVersion);
if (success == false)
{
printf("ImGui_ImplOpenGL3_Init failed\n");
assert(false);
}
// Search for font file
const char* fontPath1 = "data/droid_sans.ttf";
const char* fontPath2 = "../data/droid_sans.ttf";
const char* fontPath = nullptr;
FILE* file1 = fopen(fontPath1, "rb");
FILE* file2 = fopen(fontPath2, "rb");
if (file1)
{
fontPath = fontPath1;
fclose(file1);
}
if (file2)
{
fontPath = fontPath2;
fclose(file2);
}
if (fontPath)
{
ImGui::GetIO().Fonts->AddFontFromFileTTF(fontPath, 13.0f);
}
}
static void ResizeWindowCallback(GLFWwindow*, int width, int height)
{
g_camera.m_width = width;
g_camera.m_height = height;
s_settings.m_windowWidth = width;
s_settings.m_windowHeight = height;
}
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
ImGui_ImplGlfw_KeyCallback(window, key, scancode, action, mods);
if (ImGui::GetIO().WantCaptureKeyboard)
{
return;
}
if (action == GLFW_PRESS)
{
switch (key)
{
case GLFW_KEY_ESCAPE:
// Quit
glfwSetWindowShouldClose(g_mainWindow, GL_TRUE);
break;
case GLFW_KEY_LEFT:
// Pan left
if (mods == GLFW_MOD_CONTROL)
{
b2Vec2 newOrigin(2.0f, 0.0f);
s_test->ShiftOrigin(newOrigin);
}
else
{
g_camera.m_center.x -= 0.5f;
}
break;
case GLFW_KEY_RIGHT:
// Pan right
if (mods == GLFW_MOD_CONTROL)
{
b2Vec2 newOrigin(-2.0f, 0.0f);
s_test->ShiftOrigin(newOrigin);
}
else
{
g_camera.m_center.x += 0.5f;
}
break;
case GLFW_KEY_DOWN:
// Pan down
if (mods == GLFW_MOD_CONTROL)
{
b2Vec2 newOrigin(0.0f, 2.0f);
s_test->ShiftOrigin(newOrigin);
}
else
{
g_camera.m_center.y -= 0.5f;
}
break;
case GLFW_KEY_UP:
// Pan up
if (mods == GLFW_MOD_CONTROL)
{
b2Vec2 newOrigin(0.0f, -2.0f);
s_test->ShiftOrigin(newOrigin);
}
else
{
g_camera.m_center.y += 0.5f;
}
break;
case GLFW_KEY_HOME:
// Reset view
g_camera.m_zoom = 1.0f;
g_camera.m_center.Set(0.0f, 20.0f);
break;
case GLFW_KEY_Z:
// Zoom out
g_camera.m_zoom = b2Min(1.1f * g_camera.m_zoom, 20.0f);
break;
case GLFW_KEY_X:
// Zoom in
g_camera.m_zoom = b2Max(0.9f * g_camera.m_zoom, 0.02f);
break;
case GLFW_KEY_R:
// Reset test
delete s_test;
s_test = g_testEntries[s_settings.m_testIndex].createFcn();
break;
case GLFW_KEY_SPACE:
// Launch a bomb.
if (s_test)
{
s_test->LaunchBomb();
}
break;
case GLFW_KEY_O:
s_settings.m_singleStep = true;
break;
case GLFW_KEY_P:
s_settings.m_pause = !s_settings.m_pause;
break;
case GLFW_KEY_LEFT_BRACKET:
// Switch to previous test
--s_testSelection;
if (s_testSelection < 0)
{
s_testSelection = g_testCount - 1;
}
break;
case GLFW_KEY_RIGHT_BRACKET:
// Switch to next test
++s_testSelection;
if (s_testSelection == g_testCount)
{
s_testSelection = 0;
}
break;
case GLFW_KEY_TAB:
g_debugDraw.m_showUI = !g_debugDraw.m_showUI;
default:
if (s_test)
{
s_test->Keyboard(key);
}
}
}
else if (action == GLFW_RELEASE)
{
s_test->KeyboardUp(key);
}
// else GLFW_REPEAT
}
static void CharCallback(GLFWwindow* window, unsigned int c)
{
ImGui_ImplGlfw_CharCallback(window, c);
}
static void MouseButtonCallback(GLFWwindow* window, int32 button, int32 action, int32 mods)
{
ImGui_ImplGlfw_MouseButtonCallback(window, button, action, mods);
double xd, yd;
glfwGetCursorPos(g_mainWindow, &xd, &yd);
b2Vec2 ps((float)xd, (float)yd);
// Use the mouse to move things around.
if (button == GLFW_MOUSE_BUTTON_1)
{
//<##>
//ps.Set(0, 0);
b2Vec2 pw = g_camera.ConvertScreenToWorld(ps);
if (action == GLFW_PRESS)
{
if (mods == GLFW_MOD_SHIFT)
{
s_test->ShiftMouseDown(pw);
}
else
{
s_test->MouseDown(pw);
}
}
if (action == GLFW_RELEASE)
{
s_test->MouseUp(pw);
}
}
else if (button == GLFW_MOUSE_BUTTON_2)
{
if (action == GLFW_PRESS)
{
s_clickPointWS = g_camera.ConvertScreenToWorld(ps);
s_rightMouseDown = true;
}
if (action == GLFW_RELEASE)
{
s_rightMouseDown = false;
}
}
}
static void MouseMotionCallback(GLFWwindow*, double xd, double yd)
{
b2Vec2 ps((float)xd, (float)yd);
b2Vec2 pw = g_camera.ConvertScreenToWorld(ps);
s_test->MouseMove(pw);
if (s_rightMouseDown)
{
b2Vec2 diff = pw - s_clickPointWS;
g_camera.m_center.x -= diff.x;
g_camera.m_center.y -= diff.y;
s_clickPointWS = g_camera.ConvertScreenToWorld(ps);
}
}
static void ScrollCallback(GLFWwindow* window, double dx, double dy)
{
ImGui_ImplGlfw_ScrollCallback(window, dx, dy);
if (ImGui::GetIO().WantCaptureMouse)
{
return;
}
if (dy > 0)
{
g_camera.m_zoom /= 1.1f;
}
else
{
g_camera.m_zoom *= 1.1f;
}
}
static void RestartTest()
{
delete s_test;
s_test = g_testEntries[s_settings.m_testIndex].createFcn();
}
static void UpdateUI()
{
int menuWidth = 180;
if (g_debugDraw.m_showUI)
{
ImGui::SetNextWindowPos(ImVec2((float)g_camera.m_width - menuWidth - 10, 10));
ImGui::SetNextWindowSize(ImVec2((float)menuWidth, (float)g_camera.m_height - 20));
ImGui::Begin("Tools", &g_debugDraw.m_showUI, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
if (ImGui::BeginTabBar("ControlTabs", ImGuiTabBarFlags_None))
{
if (ImGui::BeginTabItem("Controls"))
{
ImGui::SliderInt("Vel Iters", &s_settings.m_velocityIterations, 0, 50);
ImGui::SliderInt("Pos Iters", &s_settings.m_positionIterations, 0, 50);
ImGui::SliderFloat("Hertz", &s_settings.m_hertz, 5.0f, 120.0f, "%.0f hz");
ImGui::Separator();
ImGui::Checkbox("Sleep", &s_settings.m_enableSleep);
ImGui::Checkbox("Warm Starting", &s_settings.m_enableWarmStarting);
ImGui::Checkbox("Time of Impact", &s_settings.m_enableContinuous);
ImGui::Checkbox("Sub-Stepping", &s_settings.m_enableSubStepping);
ImGui::Separator();
ImGui::Checkbox("Shapes", &s_settings.m_drawShapes);
ImGui::Checkbox("Joints", &s_settings.m_drawJoints);
ImGui::Checkbox("AABBs", &s_settings.m_drawAABBs);
ImGui::Checkbox("Contact Points", &s_settings.m_drawContactPoints);
ImGui::Checkbox("Contact Normals", &s_settings.m_drawContactNormals);
ImGui::Checkbox("Contact Impulses", &s_settings.m_drawContactImpulse);
ImGui::Checkbox("Friction Impulses", &s_settings.m_drawFrictionImpulse);
ImGui::Checkbox("Center of Masses", &s_settings.m_drawCOMs);
ImGui::Checkbox("Statistics", &s_settings.m_drawStats);
ImGui::Checkbox("Profile", &s_settings.m_drawProfile);
ImVec2 button_sz = ImVec2(-1, 0);
if (ImGui::Button("Pause (P)", button_sz))
{
s_settings.m_pause = !s_settings.m_pause;
}
if (ImGui::Button("Single Step (O)", button_sz))
{
s_settings.m_singleStep = !s_settings.m_singleStep;
}
if (ImGui::Button("Restart (R)", button_sz))
{
RestartTest();
}
if (ImGui::Button("Quit", button_sz))
{
glfwSetWindowShouldClose(g_mainWindow, GL_TRUE);
}
ImGui::EndTabItem();
}
ImGuiTreeNodeFlags leafNodeFlags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
leafNodeFlags |= ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen;
ImGuiTreeNodeFlags nodeFlags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick;
if (ImGui::BeginTabItem("Tests"))
{
int categoryIndex = 0;
const char* category = g_testEntries[categoryIndex].category;
int i = 0;
while (i < g_testCount)
{
bool categorySelected = strcmp(category, g_testEntries[s_settings.m_testIndex].category) == 0;
ImGuiTreeNodeFlags nodeSelectionFlags = categorySelected ? ImGuiTreeNodeFlags_Selected : 0;
bool nodeOpen = ImGui::TreeNodeEx(category, nodeFlags | nodeSelectionFlags);
if (nodeOpen)
{
while (i < g_testCount && strcmp(category, g_testEntries[i].category) == 0)
{
ImGuiTreeNodeFlags selectionFlags = 0;
if (s_settings.m_testIndex == i)
{
selectionFlags = ImGuiTreeNodeFlags_Selected;
}
ImGui::TreeNodeEx((void*)(intptr_t)i, leafNodeFlags | selectionFlags, "%s", g_testEntries[i].name);
if (ImGui::IsItemClicked())
{
delete s_test;
s_settings.m_testIndex = i;
s_test = g_testEntries[i].createFcn();
s_testSelection = i;
}
++i;
}
ImGui::TreePop();
}
else
{
while (i < g_testCount && strcmp(category, g_testEntries[i].category) == 0)
{
++i;
}
}
if (i < g_testCount)
{
category = g_testEntries[i].category;
categoryIndex = i;
}
}
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
ImGui::End();
s_test->UpdateUI();
}
}
//
int main(int, char**)
{
#if defined(_WIN32)
// Enable memory-leak reports
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
#endif
char buffer[128];
s_settings.Load();
SortTests();
glfwSetErrorCallback(glfwErrorCallback);
g_camera.m_width = s_settings.m_windowWidth;
g_camera.m_height = s_settings.m_windowHeight;
if (glfwInit() == 0)
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
#if __APPLE__
const char* glslVersion = "#version 150";
#else
const char* glslVersion = NULL;
#endif
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
sprintf(buffer, "Box2D Testbed Version %d.%d.%d", b2_version.major, b2_version.minor, b2_version.revision);
bool fullscreen = false;
if (fullscreen)
{
g_mainWindow = glfwCreateWindow(1920, 1080, buffer, glfwGetPrimaryMonitor(), NULL);
}
else
{
g_mainWindow = glfwCreateWindow(g_camera.m_width, g_camera.m_height, buffer, NULL, NULL);
}
if (g_mainWindow == NULL)
{
fprintf(stderr, "Failed to open GLFW g_mainWindow.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(g_mainWindow);
// Load OpenGL functions using glad
int version = gladLoadGL(glfwGetProcAddress);
printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
printf("OpenGL %s, GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));
glfwSetScrollCallback(g_mainWindow, ScrollCallback);
glfwSetWindowSizeCallback(g_mainWindow, ResizeWindowCallback);
glfwSetKeyCallback(g_mainWindow, KeyCallback);
glfwSetCharCallback(g_mainWindow, CharCallback);
glfwSetMouseButtonCallback(g_mainWindow, MouseButtonCallback);
glfwSetCursorPosCallback(g_mainWindow, MouseMotionCallback);
glfwSetScrollCallback(g_mainWindow, ScrollCallback);
g_debugDraw.Create();
CreateUI(g_mainWindow, glslVersion);
s_settings.m_testIndex = b2Clamp(s_settings.m_testIndex, 0, g_testCount - 1);
s_testSelection = s_settings.m_testIndex;
s_test = g_testEntries[s_settings.m_testIndex].createFcn();
// Control the frame rate. One draw per monitor refresh.
//glfwSwapInterval(1);
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
std::chrono::duration<double> frameTime(0.0);
std::chrono::duration<double> sleepAdjust(0.0);
while (!glfwWindowShouldClose(g_mainWindow))
{
std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
glfwGetWindowSize(g_mainWindow, &g_camera.m_width, &g_camera.m_height);
int bufferWidth, bufferHeight;
glfwGetFramebufferSize(g_mainWindow, &bufferWidth, &bufferHeight);
glViewport(0, 0, bufferWidth, bufferHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if (g_debugDraw.m_showUI)
{
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowSize(ImVec2(float(g_camera.m_width), float(g_camera.m_height)));
ImGui::SetNextWindowBgAlpha(0.0f);
ImGui::Begin("Overlay", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar);
ImGui::End();
const TestEntry& entry = g_testEntries[s_settings.m_testIndex];
sprintf(buffer, "%s : %s", entry.category, entry.name);
s_test->DrawTitle(buffer);
}
s_test->Step(s_settings);
UpdateUI();
// ImGui::ShowDemoWindow();
if (g_debugDraw.m_showUI)
{
sprintf(buffer, "%.1f ms", 1000.0 * frameTime.count());
g_debugDraw.DrawString(5, g_camera.m_height - 20, buffer);
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(g_mainWindow);
if (s_testSelection != s_settings.m_testIndex)
{
s_settings.m_testIndex = s_testSelection;
delete s_test;
s_test = g_testEntries[s_settings.m_testIndex].createFcn();
g_camera.m_zoom = 1.0f;
g_camera.m_center.Set(0.0f, 20.0f);
}
glfwPollEvents();
// Throttle to cap at 60Hz. This adaptive using a sleep adjustment. This could be improved by
// using mm_pause or equivalent for the last millisecond.
std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
std::chrono::duration<double> target(1.0 / 60.0);
std::chrono::duration<double> timeUsed = t2 - t1;
std::chrono::duration<double> sleepTime = target - timeUsed + sleepAdjust;
if (sleepTime > std::chrono::duration<double>(0))
{
std::this_thread::sleep_for(sleepTime);
}
std::chrono::steady_clock::time_point t3 = std::chrono::steady_clock::now();
frameTime = t3 - t1;
// Compute the sleep adjustment using a low pass filter
sleepAdjust = 0.9 * sleepAdjust + 0.1 * (target - frameTime);
}
delete s_test;
s_test = nullptr;
g_debugDraw.Destroy();
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
glfwTerminate();
s_settings.Save();
return 0;
}
|