summaryrefslogtreecommitdiffstats
path: root/examples/example-base/example-base.h
blob: ab7522f06ef72e119de9fc556e7b272376e35a06 (plain)
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
#pragma once

#include "slang-gfx.h"
#include "tools/platform/window.h"
#include "source/core/slang-basic.h"

struct WindowedAppBase
{
protected:
    static const int kSwapchainImageCount = 2;

    Slang::RefPtr<platform::Window> gWindow;
    uint32_t windowWidth;
    uint32_t windowHeight;

    Slang::ComPtr<gfx::IDevice> gDevice;

    Slang::ComPtr<gfx::ISwapchain> gSwapchain;
    Slang::ComPtr<gfx::IFramebufferLayout> gFramebufferLayout;
    Slang::List<Slang::ComPtr<gfx::IFramebuffer>> gFramebuffers;
    Slang::List<Slang::ComPtr<gfx::ITransientResourceHeap>> gTransientHeaps;
    Slang::ComPtr<gfx::IRenderPassLayout> gRenderPass;
    Slang::ComPtr<gfx::ICommandQueue> gQueue;

    Slang::Result initializeBase(const char* titile, int width, int height);
    void createSwapchainFramebuffers();
    void mainLoop();

    virtual void windowSizeChanged();

protected:
    virtual void renderFrame(int framebufferIndex) = 0;
public:
    platform::Window* getWindow() { return gWindow.Ptr(); }
    virtual void finalize() { gQueue->wait(); }
};

template<typename TApp>
int innerMain()
{
    TApp app;

    if (SLANG_FAILED(app.initialize()))
    {
        return -1;
    }

    platform::Application::run(app.getWindow());

    app.finalize();
    return 0;
}