コード例 #1
0
ファイル: test_sdl.cpp プロジェクト: septag/termite
int main(int argc, char* argv[])
{
    bx::enableLogToFileHandle(stdout, stderr);
    bx::setLogTag("Termite");

    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        BX_FATAL("SDL Init failed");
        return -1;
    }

    termite::Config conf;
    bx::Path pluginPath(argv[0]);
    strcpy(conf.pluginPath, pluginPath.getDirectory().cstr());

#if BX_PLATFORM_ANDROID
    SDL_DisplayMode disp;
    if (SDL_GetCurrentDisplayMode(0, &disp) == 0) {
        g_displaySize.x = disp.w;
        g_displaySize.y = disp.h;
    }    
#endif

    g_window = SDL_CreateWindow("TestSDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                                g_displaySize.x, g_displaySize.y, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
    if (!g_window) {
        BX_FATAL("SDL window creation failed");
        termite::shutdown();
        return -1;
    }

    conf.gfxWidth = g_displaySize.x;
    conf.gfxHeight = g_displaySize.y;

    termite::sdlMapImGuiKeys(&conf);

    termite::GfxPlatformData platformData;
    termite::sdlGetNativeWindowHandle(g_window, &platformData.nwh, &platformData.ndt);
    
    if (termite::initialize(conf, update, &platformData) ||
        T_FAILED(termite::registerFont("fonts/fixedsys.fnt", "fixedsys")))
    {
        BX_FATAL(termite::getErrorString());
        BX_VERBOSE(termite::getErrorCallstack());
        termite::shutdown();
        SDL_DestroyWindow(g_window);
        SDL_Quit();
        return -1;
    }

    g_vg = termite::createVectorGfxContext(101);
    g_debug = termite::createDebugDrawContext(100);
    termite::camInit(&g_cam);
    termite::camLookAt(&g_cam, termite::vec3_t(0, 1.0f, -12.0f), termite::vec3_t(0, 0, 0));

    // reset graphics driver
    termite::getGfxDriver()->reset(g_displaySize.x, g_displaySize.y, 0);
    
    SDL_Event ev;
    while (true) {
        if (termite::sdlHandleEvent(&ev)) {
            if (ev.type == SDL_QUIT)
                break;
            if (ev.type == SDL_WINDOWEVENT) {
                if (ev.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
                    BX_VERBOSE("Resume");
                    termite::sdlGetNativeWindowHandle(g_window, &platformData.nwh);
                    termite::getGfxDriver()->setPlatformData(platformData);
                    termite::getGfxDriver()->reset(g_displaySize.x, g_displaySize.y, 0);
                    termite::resume();
                } else if (ev.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
                    BX_VERBOSE("Pause");
                    termite::pause();
                }
            }
        }
        termite::doFrame();
    }

    termite::destroyDebugDrawContext(g_debug);
    termite::destroyVectorGfxContext(g_vg);
    termite::shutdown();
    SDL_DestroyWindow(g_window);
    SDL_Quit();

    return 0;
}
コード例 #2
0
ファイル: imgui_impl.cpp プロジェクト: lordhippo/ImmortalsDBG
int imguiInit(uint16_t viewWidth, uint16_t viewHeight)
{
    if (gIm) {
        assert(false);
        return -1;
    }

    BX_BEGINP("Initializing ImGui Integration");

    bx::AllocatorI* alloc = &gAlloc;
    gIm = BX_NEW(alloc, ImGuiImpl);
    if (!gIm) {
        BX_END_FATAL();
        return -2;
    }
    gIm->alloc = alloc;
    VertexPosCoordColor::init();

    // Create Graphic objects
    bgfx::ShaderHandle fragmentShader = bgfx::createShader(bgfx::makeRef(imgui_fsb_h, sizeof(imgui_fsb_h)));
	if (!IS_VALID(fragmentShader)) {
        BX_END_FATAL();
		BX_FATAL("Creating fragment-shader failed");
        return -1;
    }

	bgfx::ShaderHandle vertexShader = bgfx::createShader(bgfx::makeRef(imgui_vsb_h, sizeof(imgui_vsb_h)));
	if (!IS_VALID(vertexShader)) {
        BX_END_FATAL();
		BX_FATAL("Creating vertex-shader failed");
        return -1;
    }

    gIm->progHandle = bgfx::createProgram(vertexShader, fragmentShader, true);
	if (!IS_VALID(gIm->progHandle)) {
        BX_END_FATAL();
		BX_FATAL("Creating GPU Program failed");
        return -1;
    }
    gIm->uniformTexture = bgfx::createUniform("u_texture", bgfx::UniformType::Int1);

    gIm->vertexBuffer = bgfx::createDynamicVertexBuffer(MAX_VERTICES, VertexPosCoordColor::Decl);
    gIm->indexBuffer = bgfx::createDynamicIndexBuffer(MAX_INDICES);
	if (!IS_VALID(gIm->vertexBuffer) || !IS_VALID(gIm->indexBuffer)) {
		BX_END_FATAL();
		BX_FATAL("Creating GPU VertexBuffer failed");
		return -1;
	}

    // Setup ImGui
    ImGuiIO& conf = ImGui::GetIO();
    conf.DisplaySize = ImVec2((float)viewWidth, (float)viewHeight);
    conf.RenderDrawListsFn = imguiDrawLists;
    conf.MemAllocFn = imguiAlloc;
    conf.MemFreeFn = imguiFree;

    conf.KeyMap[ImGuiKey_Tab] = SDLK_TAB;
    conf.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
    conf.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
    conf.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
    conf.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
    conf.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
    conf.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
	conf.KeyMap[ImGuiKey_Delete] = SDLK_DELETE;
	conf.KeyMap[ImGuiKey_Backspace] = SDLK_BACKSPACE;
    conf.KeyMap[ImGuiKey_Enter] = SDLK_RETURN;
    conf.KeyMap[ImGuiKey_Escape] = SDLK_ESCAPE;
    conf.KeyMap[ImGuiKey_A] = SDLK_a;
    conf.KeyMap[ImGuiKey_C] = SDLK_c;
    conf.KeyMap[ImGuiKey_V] = SDLK_v;
    conf.KeyMap[ImGuiKey_X] = SDLK_x;
    conf.KeyMap[ImGuiKey_Y] = SDLK_y;
    conf.KeyMap[ImGuiKey_Z] = SDLK_z;

    uint8_t* fontData;
    int fontWidth, fontHeight, bpp;
    conf.Fonts->GetTexDataAsAlpha8(&fontData, &fontWidth, &fontHeight, &bpp);

	gIm->fontTexHandle = bgfx::createTexture2D((uint16_t)fontWidth, (uint16_t)fontHeight, 1, bgfx::TextureFormat::A8,
		BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT,
		bgfx::makeRef(fontData, fontWidth*fontHeight*bpp));
	if (!IS_VALID(gIm->fontTexHandle)) {
        BX_END_FATAL();
		BX_FATAL("ImGui: Could not create font texture");
        return -1;
    }
    conf.Fonts->TexID = (void*)&gIm->fontTexHandle;

	ImGui::GetStyle().AntiAliasedLines = false;

    ImGui::NewFrame();

    BX_END_OK();
    return 0;
}