IMGUI_API bool ImGui_Service_Init(SDL_Window* window) { // detect opengl major version char* versionStr = (char*)malloc(1024); memcpy(versionStr, (char*)glGetString(GL_VERSION), strlen((char*)glGetString(GL_VERSION))); char* piece = NULL; char* nextPiece = NULL; piece = strtok_s(versionStr, ".", &nextPiece); if (piece != NULL) glMajorVersion = atoi(piece); if (glMajorVersion < 3) return ImGui_ImplSdl_Init(window); else return ImGui_ImplSdlGL3_Init(window); }
void RecastToolKit::InitImGui() { if (!ImGui_ImplSdl_Init(m_Window.get())) { std::stringstream s = std::stringstream(); s << "Could not init GUI renderer.\n"; m_Renderer.release(); SDL_GL_DeleteContext(m_GLContext); m_Window.release(); SDL_Quit(); throw std::runtime_error(s.str()); } ImGuiIO& io = ImGui::GetIO(); io.Fonts->AddFontDefault(); io.Fonts->AddFontFromFileTTF("DroidSans.ttf", 15.0); }
bool ImGuiRendererSDLGL1::init() { return ImGuiRendererSDL::init() && ImGui_ImplSdl_Init(window); }
int SDL_main(int, char**) { // Setup SDL if (SDL_Init(SDL_INIT_EVERYTHING) != 0) return -1; // Setup window SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_DisplayMode current; SDL_GetCurrentDisplayMode(0, ¤t); SDL_Window *window = SDL_CreateWindow("ImGui SDL2+OpenGL example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); SDL_GLContext glcontext = SDL_GL_CreateContext(window); // Setup ImGui binding ImGui_ImplSdl_Init(window); // Load Fonts // (see extra_fonts/README.txt for more details) //ImGuiIO& io = ImGui::GetIO(); //io.Fonts->AddFontDefault(); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/Cousine-Regular.ttf", 15.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyClean.ttf", 13.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/ProggyTiny.ttf", 10.0f); //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); // Merge glyphs from multiple fonts into one (e.g. combine default font with another with Chinese glyphs, or add icons) //ImWchar icons_ranges[] = { 0xf000, 0xf3ff, 0 }; //ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true; //io.Fonts->AddFontFromFileTTF("../../extra_fonts/DroidSans.ttf", 18.0f); //io.Fonts->AddFontFromFileTTF("../../extra_fonts/fontawesome-webfont.ttf", 18.0f, &icons_config, icons_ranges); bool show_test_window = true; bool show_another_window = false; ImVec4 clear_color = ImColor(114, 144, 154); // Main loop bool done = false; while (!done) { SDL_Event event; while (SDL_PollEvent(&event)) { ImGui_ImplSdl_ProcessEvent(&event); if (event.type == SDL_QUIT) done = true; } ImGui_ImplSdl_NewFrame(window); // 1. Show a simple window // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug" { static float f = 0.0f; ImGui::Text("Hello, world!"); ImGui::SliderFloat("float", &f, 0.0f, 1.0f); ImGui::ColorEdit3("clear color", (float*)&clear_color); if (ImGui::Button("Test Window")) show_test_window ^= 1; if (ImGui::Button("Another Window")) show_another_window ^= 1; ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } // 2. Show another simple window, this time using an explicit Begin/End pair if (show_another_window) { ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver); ImGui::Begin("Another Window", &show_another_window); ImGui::Text("Hello"); ImGui::End(); } // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow() if (show_test_window) { ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver); ImGui::ShowTestWindow(&show_test_window); } // Rendering glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y); glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); glClear(GL_COLOR_BUFFER_BIT); ImGui::Render(); SDL_GL_SwapWindow(window); } // Cleanup ImGui_ImplSdl_Shutdown(); SDL_GL_DeleteContext(glcontext); SDL_DestroyWindow(window); SDL_Quit(); return 0; }
int main(int, char**) { // Setup SDL if (SDL_Init(SDL_INIT_EVERYTHING) != 0) return -1; // Setup window SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_DisplayMode current; SDL_GetCurrentDisplayMode(0, ¤t); SDL_Window *sdl_window = SDL_CreateWindow("testbed", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); SDL_GLContext glcontext = SDL_GL_CreateContext(sdl_window); // Setup ImGui binding ImGui_ImplSdl_Init(sdl_window); // OpenAL: Open and initialize a device with default settings // and set current context, making the program ready to call OpenAL functions. ALCdevice* alc_device = NULL; ALCcontext* alc_ctx = NULL; const char* alc_device_spec = NULL; const char* alc_ext = NULL; const char* al_vendor = NULL; const char* al_renderer = NULL; const char* al_version = NULL; const char* al_ext = NULL; { alc_device = alcOpenDevice(NULL); if(!alc_device) { ERR("Could not open a device!\n"); return 1; } alc_ctx = alcCreateContext(alc_device, NULL); if(alc_ctx == NULL || alcMakeContextCurrent(alc_ctx) == ALC_FALSE) { if(alc_ctx != NULL) alcDestroyContext(alc_ctx); alcCloseDevice(alc_device); ERR("Could not set a context!\n"); return 1; } alc_device_spec = alcGetString(alc_device, ALC_DEVICE_SPECIFIER); alc_ext = alcGetString(alc_device, ALC_EXTENSIONS); //alcGetIntegerv(alc_device, ALC_MAJOR_VERSION, 1, &alc_major); //alcGetIntegerv(alc_device, ALC_MINOR_VERSION, 1, &alc_minor); al_vendor = alGetString(AL_VENDOR); al_renderer = alGetString(AL_RENDERER); al_version = alGetString(AL_VERSION); al_ext = alGetString(AL_EXTENSIONS); } // Load resource SResources Resources; { bool ok = LoadResources(Resources); if (!ok) { ERR("Could not load all program resource.\n"); return 1; } } // openal sources SMgrState MgrState; SEmitter* SpatialEmit; SEmitter* AmbiantLoop; { Mgr_Init(MgrState); SpatialEmit = &MgrState.Emitters[0]; AmbiantLoop = &MgrState.Emitters[1]; alSourcei(SpatialEmit->Source, AL_BUFFER, Resources.albuf_monoloop); alSourcei(SpatialEmit->Source, AL_LOOPING, AL_TRUE); alSourcei(SpatialEmit->Source, AL_DIRECT_CHANNELS_SOFT, AL_FALSE); SpatialEmit->active = false; SpatialEmit->dB = 0.f; SpatialEmit->pos[0] = .5f; SpatialEmit->pos[1] = .75f; SpatialEmit->pos[2] = -3; SpatialEmit->radius = 0.01f; alSourcei(AmbiantLoop->Source, AL_BUFFER, Resources.albuf_stereoloop); alSourcei(AmbiantLoop->Source, AL_LOOPING, AL_TRUE); alSourcei(AmbiantLoop->Source, AL_DIRECT_CHANNELS_SOFT, AL_TRUE); AmbiantLoop->active = false; AmbiantLoop->dB = -9.f; } // Main loop bool done = false; while (!done) { SDL_Event event; while (SDL_PollEvent(&event)) { ImGui_ImplSdl_ProcessEvent(&event); if (event.type == SDL_QUIT) done = true; } uint CurTimeMs = SDL_GetTicks(); int ActiveSources = Mgr_Update(MgrState); ImGui_ImplSdl_NewFrame(sdl_window); ImGui::SetNextWindowPos(ImVec2(10, 10)); ImGui::SetNextWindowSize(ImVec2(500, 700)); ImGui::Begin("main", NULL, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove); // top bar. { if (ImGui::Button("Quit")) break; ImGui::SameLine(); static bool ShowImguiHelp = false; ImGui::Checkbox("show imgui help", &ShowImguiHelp); if (ShowImguiHelp) ImGui::ShowTestWindow(); // documentation shortcut } ImGui::Spacing(); // ----------------- // Openal info: if (ImGui::CollapsingHeader("OpenAL info")) { ImGui::Columns(2, "OpenAL_info"); ImGui::Text("device"); ImGui::NextColumn(); ImGui::TextWrapped(alc_device_spec); ImGui::NextColumn(); ImGui::Text("vendor"); ImGui::NextColumn(); ImGui::TextWrapped(al_vendor); ImGui::NextColumn(); ImGui::Text("renderer"); ImGui::NextColumn(); ImGui::TextWrapped(al_renderer); ImGui::NextColumn(); ImGui::Text("version"); ImGui::NextColumn(); ImGui::TextWrapped(al_version); ImGui::NextColumn(); ImGui::Separator(); ImGui::Text("ALC extensions"); ImGui::NextColumn(); ImGui::TextWrapped(alc_ext); ImGui::NextColumn(); ImGui::Separator(); ImGui::Text("AL extensions"); ImGui::NextColumn(); ImGui::TextWrapped(al_ext); ImGui::NextColumn(); ImGui::Columns(1); } ImGui::Spacing(); // ----------------- // basic test if (ImGui::CollapsingHeader("Basic", NULL, true, true)) { static float mono_gaindB = -3.f; static float stereo_gaindB = -3.f; if (ImGui::Button("Play mono")) { Mgr_Play(MgrState, Resources.albuf_mono, mono_gaindB); } ImGui::SameLine(); ImGui::SliderFloat("##vol1", &mono_gaindB, -60, 6, "%.1fdB"); if (ImGui::Button("Play stereo")) { Mgr_Play(MgrState, Resources.albuf_stereo, stereo_gaindB); } ImGui::SameLine(); ImGui::SliderFloat("##vol2", &stereo_gaindB, -60, 6, "%.1fdB"); } ImGui::Spacing(); // ----------------- // Direct if (ImGui::CollapsingHeader("Direct", NULL, true, true)) { ImGui::Checkbox("Ambiance", &AmbiantLoop->active); ImGui::SameLine(); ImGui::SliderFloat("##vol0", &AmbiantLoop->dB, -60, 6, "%.1fdB"); } ImGui::Spacing(); // ----------------- // Spatialized if (ImGui::CollapsingHeader("Spatialized", NULL, true, true)) { ImGui::Checkbox("Mosquito", &SpatialEmit->active); ImGui::SameLine(); ImGui::SliderFloat("##vol4", &SpatialEmit->dB, -60, 6, "%.1fdB"); ImGui::SliderFloat("radius", &SpatialEmit->radius, 0, 5); static uint PrevTime = 0; static float PrevPos[3]; static bool automove = false; ImGui::Checkbox("Auto move", &automove); if (automove) { struct SLocal { static void anim(float t, float v[3]) { float w = (t*2*PI); v[0] = 5*sinf(w*2+1) + 3*sinf(w*6+2) + 2*sinf(w*6+3) + 1*sinf(w*9+4); v[1] = 5*sinf(w*3+5) + 3*sinf(w*4+6) + 2*sinf(w*7+7) + 1*sinf(w*8+8); v[2] = 5*sinf(w*4+9) + 3*sinf(w*5+1) + 2*sinf(w*8+2) + 1*sinf(w*7+3); } }; float t = (CurTimeMs % 60000) / 60000.f; SLocal::anim(t, SpatialEmit->pos); } ImGui::InputFloat3("pos", SpatialEmit->pos); ImGui::InputFloat3("vel", SpatialEmit->vel); ImGuiPointOnMap("top", &SpatialEmit->pos[0], &SpatialEmit->pos[2], SpatialEmit->radius, 10, 0.25f); ImGui::SameLine(); ImGuiPointOnMap("front", &SpatialEmit->pos[0], &SpatialEmit->pos[1], SpatialEmit->radius, 10, 0.25f); if (PrevTime != 0 && CurTimeMs > PrevTime) { float dt = 0.001f*(CurTimeMs-PrevTime); float k = 1.f; SpatialEmit->vel[0] = k * ((SpatialEmit->pos[0] - PrevPos[0]) / dt); SpatialEmit->vel[1] = k * ((SpatialEmit->pos[1] - PrevPos[1]) / dt); SpatialEmit->vel[2] = k * ((SpatialEmit->pos[2] - PrevPos[2]) / dt); } PrevTime = CurTimeMs; memcpy(PrevPos, SpatialEmit->pos, sizeof(PrevPos)); } ImGui::Spacing(); // ----------------- // basic test if (ImGui::CollapsingHeader("Tests", NULL, true, true)) { static const float Front[3] = {0,0,-1}; if (ImGui::Button("stereo base")) { Mgr_Play(MgrState, Resources.albuf_stereo, -3, false); } ImGui::SameLine(); if (ImGui::Button("stereo direct")) { Mgr_Play(MgrState, Resources.albuf_stereo, -3, true); } if (ImGui::Button("mono base")) { Mgr_Play(MgrState, Resources.albuf_mono, -3, false); } ImGui::SameLine(); if (ImGui::Button("mono direct")) { Mgr_Play(MgrState, Resources.albuf_mono, -3, true); } ImGui::SameLine(); if (ImGui::Button("mono 3d narrow")) { Mgr_Play(MgrState, Resources.albuf_mono, -3, Front, 0.01f); } ImGui::SameLine(); if (ImGui::Button("mono 3d wide")) { Mgr_Play(MgrState, Resources.albuf_mono, -3, Front, 1.f); } ImGui::SameLine(); if (ImGui::Button("mono 3d omni")) { Mgr_Play(MgrState, Resources.albuf_mono, -3, Front, 10.f); } } ImGui::Spacing(); // ----------------- // status { ImGui::Separator(); ImGui::Text("Active Sources: %d / %d\n", ActiveSources, MGR_MAX_SOURCES); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } ImGui::End(); // Rendering ImVec4 clear_color = ImColor(114, 144, 154); glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y); glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); glClear(GL_COLOR_BUFFER_BIT); ImGui::Render(); SDL_GL_SwapWindow(sdl_window); } Mgr_Destroy(MgrState); FreeResources(Resources); // OpenAL: cleanup { alcMakeContextCurrent(NULL); alcDestroyContext(alc_ctx); alcCloseDevice(alc_device); } // Cleanup ImGui_ImplSdl_Shutdown(); SDL_GL_DeleteContext(glcontext); SDL_DestroyWindow(sdl_window); SDL_Quit(); return 0; }
int main(int argc, char *argv[]) { if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { Printf("Failed to initialize SDL: %s\n", SDL_GetError()); return -1; } VideoMode mode = {}; mode.width = 800; mode.height = 600; mode.gl_major = 1; mode.gl_minor = 5; mode.double_buffer = 1; mode.depth_bits = 24; mode.stencil_bits = 8; mode.multisamples = 4; mode.swap_interval = 1; SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, mode.gl_major); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, mode.gl_minor); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, mode.double_buffer); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, mode.depth_bits); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, mode.stencil_bits); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, mode.multisamples>0?1:0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, mode.multisamples); mode.window = SDL_CreateWindow( "World Simulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, mode.width, mode.height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); if (!mode.window) { Printf("Failed to create a window: %s\n", SDL_GetError()); return -1; } SDL_GLContext context = SDL_GL_CreateContext(mode.window); // Note: This must be set on a valid context SDL_GL_SetSwapInterval(mode.swap_interval); SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &mode.gl_major); SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &mode.gl_minor); SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &mode.double_buffer); SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &mode.depth_bits); SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &mode.stencil_bits); SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &mode.multisamples); mode.swap_interval = SDL_GL_GetSwapInterval(); sim_init_msgs(false); ImGui_ImplSdl_Init(mode.window); STATE = sim_init((u32)get_tick()); HISTORY_LENGTH = 0; bool running = true; u64 initial_tick = get_tick(); u64 last_frame_t = initial_tick; r32 elapsed_time = 0.0f; r32 delta_time = 1.0f / 60.0f; while (running) { SDL_Event event; while (SDL_PollEvent(&event)) { ImGui_ImplSdl_ProcessEvent(&event); switch (event.type) { case SDL_WINDOWEVENT: { switch (event.window.event) { case SDL_WINDOWEVENT_SIZE_CHANGED: { Printf("Window %d size changed to %dx%d\n", event.window.windowID, event.window.data1, event.window.data2); mode.width = event.window.data1; mode.height = event.window.data2; } break; } } break; case SDL_QUIT: { running = false; } break; } } gui_tick(mode, elapsed_time, delta_time); SDL_GL_SwapWindow(mode.window); delta_time = time_since(last_frame_t); if (mode.fps_lock > 0) { r32 target_time = 1.0f / (r32)mode.fps_lock; r32 sleep_time = target_time - delta_time; if (sleep_time >= 0.01f) SDL_Delay((u32)(sleep_time * 1000.0f)); delta_time = time_since(last_frame_t); } last_frame_t = get_tick(); elapsed_time = time_since(initial_tick); GLenum error = glGetError(); if (error != GL_NO_ERROR) { Printf("An error occurred: %s\n", gl_error_message(error)); running = false; } } ImGui_ImplSdl_Shutdown(); SDL_GL_DeleteContext(context); SDL_DestroyWindow(mode.window); SDL_Quit(); return 0; }