yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Jay KwakAdd command-line arguments to examples (#7835)13dd01489

master
5.0 KiB172 linesraw
1#include "core/slang-basic.h"
2#include "examples/example-base/example-base.h"
3#include "platform/window.h"
4#include "slang-com-ptr.h"
5#include "slang-rhi.h"
6#include "slang.h"
7
8#include <cstdio>
9
10using namespace rhi;
11using namespace Slang;
12
13struct PlatformTest : public WindowedAppBase
14{
15
16    void onSizeChanged()
17    {
18        printf("onSizeChanged\n");
19        fflush(stdout);
20    }
21
22    void onFocus()
23    {
24        printf("onFocus\n");
25        fflush(stdout);
26    }
27
28    void onLostFocus()
29    {
30        printf("onLostFocus\n");
31        fflush(stdout);
32    }
33
34    void onKeyDown(platform::KeyEventArgs args)
35    {
36        printf("onKeyDown(key=0x%02x, buttons=0x%02x)\n", (uint32_t)args.key, args.buttons);
37        fflush(stdout);
38    }
39
40    void onKeyUp(platform::KeyEventArgs args)
41    {
42        printf("onKeyUp(key=0x%02x, buttons=0x%02x)\n", (uint32_t)args.key, args.buttons);
43        fflush(stdout);
44    }
45
46    void onKeyPress(platform::KeyEventArgs args)
47    {
48        printf("onKeyPress(keyChar=0x%02x)\n", args.keyChar);
49        fflush(stdout);
50    }
51
52    void onMouseMove(platform::MouseEventArgs args)
53    {
54        // Throttle mouse move events using a simple counter
55        static int mouseMoveCounter = 0;
56        mouseMoveCounter++;
57        if (mouseMoveCounter % 50 == 0) // Only print every 50th mouse move event
58        {
59            printf(
60                "onMouseMove(x=%d, y=%d, delta=%d, buttons=0x%02x)\n",
61                args.x,
62                args.y,
63                args.delta,
64                args.buttons);
65            fflush(stdout);
66        }
67    }
68
69    void onMouseDown(platform::MouseEventArgs args)
70    {
71        printf(
72            "onMouseDown(x=%d, y=%d, delta=%d, buttons=0x%02x)\n",
73            args.x,
74            args.y,
75            args.delta,
76            args.buttons);
77        fflush(stdout);
78    }
79
80    void onMouseUp(platform::MouseEventArgs args)
81    {
82        printf(
83            "onMouseUp(x=%d, y=%d, delta=%d, buttons=0x%02x)\n",
84            args.x,
85            args.y,
86            args.delta,
87            args.buttons);
88        fflush(stdout);
89    }
90
91    void onMouseWheel(platform::MouseEventArgs args)
92    {
93        printf(
94            "onMouseWheel(x=%d, y=%d, delta=%d, buttons=0x%02x\n",
95            args.x,
96            args.y,
97            args.delta,
98            args.buttons);
99        fflush(stdout);
100    }
101
102    Slang::Result initialize()
103    {
104        SLANG_RETURN_ON_FAIL(initializeBase("platform-test", 1024, 768, getDeviceType()));
105
106        // We may not have a window if we're running in test mode
107        SLANG_ASSERT(isTestMode() || gWindow);
108        if (gWindow)
109        {
110            printf("Setting up event handlers...\n");
111            fflush(stdout);
112
113            gWindow->events.sizeChanged = [this]() { onSizeChanged(); };
114            gWindow->events.focus = [this]() { onFocus(); };
115            gWindow->events.lostFocus = [this]() { onLostFocus(); };
116            gWindow->events.keyDown = [this](const platform::KeyEventArgs& e) { onKeyDown(e); };
117            gWindow->events.keyUp = [this](const platform::KeyEventArgs& e) { onKeyUp(e); };
118            gWindow->events.keyPress = [this](const platform::KeyEventArgs& e) { onKeyPress(e); };
119            gWindow->events.mouseMove = [this](const platform::MouseEventArgs& e)
120            { onMouseMove(e); };
121            gWindow->events.mouseDown = [this](const platform::MouseEventArgs& e)
122            { onMouseDown(e); };
123            gWindow->events.mouseUp = [this](const platform::MouseEventArgs& e) { onMouseUp(e); };
124            gWindow->events.mouseWheel = [this](const platform::MouseEventArgs& e)
125            { onMouseWheel(e); };
126            printf("Event handlers set up successfully.\n");
127        }
128        else
129        {
130            printf("No window available for event setup.\n");
131            fflush(stdout);
132        }
133
134        return SLANG_OK;
135    }
136
137    virtual void renderFrame(ITexture* texture) override
138    {
139        auto commandEncoder = gQueue->createCommandEncoder();
140
141        ComPtr<ITextureView> textureView = gDevice->createTextureView(texture, {});
142        RenderPassColorAttachment colorAttachment = {};
143        colorAttachment.view = textureView;
144        colorAttachment.loadOp = LoadOp::Clear;
145
146        RenderPassDesc renderPass = {};
147        renderPass.colorAttachments = &colorAttachment;
148        renderPass.colorAttachmentCount = 1;
149
150        auto renderEncoder = commandEncoder->beginRenderPass(renderPass);
151
152        RenderState renderState = {};
153        renderState.viewports[0] = Viewport::fromSize(windowWidth, windowHeight);
154        renderState.viewportCount = 1;
155        renderState.scissorRects[0] = ScissorRect::fromSize(windowWidth, windowHeight);
156        renderState.scissorRectCount = 1;
157
158        renderEncoder->setRenderState(renderState);
159
160        renderEncoder->end();
161        gQueue->submit(commandEncoder->finish());
162
163        if (!isTestMode())
164        {
165            gSurface->present();
166        }
167    }
168};
169
170// This macro instantiates an appropriate main function to
171// run the application defined above.
172EXAMPLE_MAIN(innerMain<PlatformTest>);