imgui如何嵌入SDL窗口

#include <SDL.h>
#include <windows.h>
#include <iostream>
#include <d3d11.h>
#include <dxgi1_2.h>
#include <wrl/client.h>
#include <stdbool.h>
#include <stdlib.h>
#include <SDL_test.h>
#include <string.h>
#include "Overlay.h"
#include <imgui.h>
using namespace Microsoft::WRL;
//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

HRESULT STDMETHODCALLTYPE ReleaseFrame();

// Assume that the necessary structures and types are declared appropriately.
HRESULT STDMETHODCALLTYPE AcquireNextFrame(
    /* [annotation][in] */
    _In_  UINT TimeoutInMilliseconds,
    /* [annotation][out] */
    _Out_  DXGI_OUTDUPL_FRAME_INFO* pFrameInfo,
    /* [annotation][out] */
    _COM_Outptr_  IDXGIResource** ppDesktopResource);

void 渲染主窗口()
{
    ImGui::Begin("主窗口");

    // 主窗口的内容在这里...

    // 嵌套另一个窗口
    if (ImGui::Begin("嵌套窗口"))
    {
        // 嵌套窗口的内容在这里...
        ImGui::Text("来自嵌套窗口的问候!");

        ImGui::End();
    }

    ImGui::End();
}

int main() {
    // 初始化SDL
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
        std::cerr << "SDL initialization failed: " << SDL_GetError() << std::endl;
        return 1;
    }

    // 创建窗口
    SDL_Window* window = SDL_CreateWindow(
        "SDL Window",
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        800, 600,
        SDL_WINDOW_SHOWN
    );

    if (!window) {
        std::cerr << "Failed to create SDL window: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    // 创建渲染器
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    if (!renderer) {
        std::cerr << "Failed to create SDL renderer: " << SDL_GetError() << std::endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }
    // 初始化Dear ImGui
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO();
    (void)io;

    // 配置Dear ImGui的SDL实现
    ImGui_ImplSDL2_InitForD3D(window);
    ImGui_ImplSDL2_InitForOpenGL(window, nullptr);
    SDL_Rect rect_ = { 0,0,0,0 };
    // 配置Dear ImGui的SDL实现
    ImGui_ImplSDL2_InitForD3D(window);
    ImGui_ImplSDL2_InitForOpenGL(window, nullptr);
    int x, y;
    SDL_GetWindowPosition(window, &x, &y);
    rect_.x = x;
    rect_.y = y;
    std::cout << "Window position: (" << x << ", " << y << ")" << std::endl;
    // 获取窗口大小
    int width, height;
    SDL_GetWindowSize(window, &width, &height);
    rect_.w = width;
    rect_.h = height;
    std::cout << "Window size: " << width << " x " << height << std::endl; 
   
    // 获取键盘输入
    SDL_Event event;
    bool quit = false;

    while (!quit) {
        while (SDL_PollEvent(&event) != 0) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
            else if (event.type == SDL_KEYDOWN) {
                std::cout << "Key pressed: " << SDL_GetKeyName(event.key.keysym.sym) << std::endl;
            }
            else if (event.type == SDL_MOUSEBUTTONDOWN) {
                std::cout << "Mouse button pressed at (" << event.button.x << ", " << event.button.y << ")" << std::endl;
            }
            ImGui_ImplSDL2_ProcessEvent(&event);
        }
        渲染主窗口();
        ImGui::Render();
        glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
        glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        // 渲染窗口
        SDL_RenderClear(renderer);
        // 在这里添加你的渲染代码
        SDL_RenderPresent(renderer);
    }

    // 获取音频设备信息
    int audioDeviceCount = SDL_GetNumAudioDevices(SDL_FALSE);
    std::cout << "Number of audio devices: " << audioDeviceCount << std::endl;

    for (int i = 0; i < audioDeviceCount; ++i) {
        const char* audioDeviceName = SDL_GetAudioDeviceName(i, SDL_FALSE);
        std::cout << "Audio device " << i << ": " << audioDeviceName << std::endl;
    }

    // 清理SDL资源
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}


以上代码编译出来的窗口闪退,求大佬指点

  • 17
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值