Skip to content

Commit 121fe47

Browse files
committed
WindowResize
1 parent 402c0c9 commit 121fe47

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_executable(SDL_gpu_examples
2727
Examples/Texture2DArray.c
2828
Examples/TriangleMSAA.c
2929
Examples/Cubemap.c
30+
Examples/WindowResize.c
3031
)
3132

3233
target_include_directories(SDL_gpu_examples PRIVATE .)

Examples/WindowResize.c

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#include <SDL_gpu_examples.h>
2+
3+
typedef struct Resolution
4+
{
5+
Uint32 x, y;
6+
} Resolution;
7+
8+
static const Resolution Resolutions[] =
9+
{
10+
{ 640, 480 },
11+
{ 1280, 720 },
12+
{ 1024, 1024 },
13+
{ 1600, 900 },
14+
{ 1920, 1080 },
15+
{ 3200, 1800 },
16+
{ 3840, 2160 }
17+
};
18+
static Uint32 ResolutionCount = SDL_arraysize(Resolutions);
19+
20+
static SDL_GpuGraphicsPipeline* Pipeline;
21+
static Sint32 ResolutionIndex;
22+
23+
static int Init(Context* context)
24+
{
25+
int result = CommonInit(context, 0);
26+
if (result < 0)
27+
{
28+
return result;
29+
}
30+
31+
ResolutionIndex = 0;
32+
33+
SDL_GpuShader* vertexShader = LoadShader(context->Device, "RawTriangle.vert", 0, 0, 0, 0);
34+
if (vertexShader == NULL)
35+
{
36+
SDL_Log("Failed to create vertex shader!");
37+
return -1;
38+
}
39+
40+
SDL_GpuShader* fragmentShader = LoadShader(context->Device, "SolidColor.frag", 0, 0, 0, 0);
41+
if (fragmentShader == NULL)
42+
{
43+
SDL_Log("Failed to create fragment shader!");
44+
return -1;
45+
}
46+
47+
SDL_GpuGraphicsPipelineCreateInfo pipelineCreateInfo = {
48+
.attachmentInfo = {
49+
.colorAttachmentCount = 1,
50+
.colorAttachmentDescriptions = (SDL_GpuColorAttachmentDescription[]){{
51+
.format = SDL_GpuGetSwapchainTextureFormat(context->Device, context->Window),
52+
.blendState = {
53+
.blendEnable = SDL_TRUE,
54+
.alphaBlendOp = SDL_GPU_BLENDOP_ADD,
55+
.colorBlendOp = SDL_GPU_BLENDOP_ADD,
56+
.colorWriteMask = 0xF,
57+
.srcColorBlendFactor = SDL_GPU_BLENDFACTOR_ONE,
58+
.srcAlphaBlendFactor = SDL_GPU_BLENDFACTOR_ONE,
59+
.dstColorBlendFactor = SDL_GPU_BLENDFACTOR_ZERO,
60+
.dstAlphaBlendFactor = SDL_GPU_BLENDFACTOR_ZERO
61+
}
62+
}},
63+
},
64+
.multisampleState.sampleMask = 0xFFFF,
65+
.primitiveType = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST,
66+
.vertexShader = vertexShader,
67+
.fragmentShader = fragmentShader,
68+
.rasterizerState.fillMode = SDL_GPU_FILLMODE_FILL
69+
};
70+
71+
Pipeline = SDL_GpuCreateGraphicsPipeline(context->Device, &pipelineCreateInfo);
72+
if (Pipeline == NULL)
73+
{
74+
SDL_Log("Failed to create pipeline!");
75+
return -1;
76+
}
77+
78+
SDL_GpuReleaseShader(context->Device, vertexShader);
79+
SDL_GpuReleaseShader(context->Device, fragmentShader);
80+
81+
SDL_Log("Press Left and Right to resize the window!");
82+
83+
return 0;
84+
}
85+
86+
static int Update(Context* context)
87+
{
88+
SDL_bool changeResolution = SDL_FALSE;
89+
90+
if (context->RightPressed)
91+
{
92+
ResolutionIndex = (ResolutionIndex + 1) % ResolutionCount;
93+
changeResolution = SDL_TRUE;
94+
}
95+
96+
if (context->LeftPressed)
97+
{
98+
ResolutionIndex -= 1;
99+
if (ResolutionIndex < 0)
100+
{
101+
ResolutionIndex = ResolutionCount - 1;
102+
}
103+
changeResolution = SDL_TRUE;
104+
}
105+
106+
if (changeResolution)
107+
{
108+
Resolution currentResolution = Resolutions[ResolutionIndex];
109+
SDL_Log("Setting resolution to: %u, %u", currentResolution.x, currentResolution.y);
110+
SDL_SetWindowSize(context->Window, currentResolution.x, currentResolution.y);
111+
SDL_SetWindowPosition(context->Window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
112+
SDL_SyncWindow(context->Window);
113+
}
114+
115+
return 0;
116+
}
117+
118+
static int Draw(Context* context)
119+
{
120+
SDL_GpuCommandBuffer* cmdbuf = SDL_GpuAcquireCommandBuffer(context->Device);
121+
if (cmdbuf == NULL)
122+
{
123+
SDL_Log("GpuAcquireCommandBuffer failed");
124+
return -1;
125+
}
126+
127+
Uint32 w, h;
128+
SDL_GpuTexture* swapchainTexture = SDL_GpuAcquireSwapchainTexture(cmdbuf, context->Window, &w, &h);
129+
if (swapchainTexture != NULL)
130+
{
131+
SDL_GpuColorAttachmentInfo colorAttachmentInfo = { 0 };
132+
colorAttachmentInfo.textureSlice.texture = swapchainTexture;
133+
colorAttachmentInfo.clearColor = (SDL_GpuColor){ 0.0f, 0.0f, 0.0f, 1.0f };
134+
colorAttachmentInfo.loadOp = SDL_GPU_LOADOP_CLEAR;
135+
colorAttachmentInfo.storeOp = SDL_GPU_STOREOP_STORE;
136+
137+
SDL_GpuRenderPass* renderPass = SDL_GpuBeginRenderPass(cmdbuf, &colorAttachmentInfo, 1, NULL);
138+
SDL_GpuBindGraphicsPipeline(renderPass, Pipeline);
139+
SDL_GpuDrawPrimitives(renderPass, 0, 1);
140+
SDL_GpuEndRenderPass(renderPass);
141+
}
142+
143+
SDL_GpuSubmit(cmdbuf);
144+
145+
return 0;
146+
}
147+
148+
static void Quit(Context* context)
149+
{
150+
SDL_GpuReleaseGraphicsPipeline(context->Device, Pipeline);
151+
CommonQuit(context);
152+
}
153+
154+
Example WindowResize_Example = { "WindowResize", Init, Update, Draw, Quit };

SDL_gpu_examples.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,6 @@ extern Example CopyConsistency_Example;
108108
extern Example Texture2DArray_Example;
109109
extern Example TriangleMSAA_Example;
110110
extern Example Cubemap_Example;
111+
extern Example WindowResize_Example;
111112

112113
#endif

main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static Example* Examples[] =
2222
&Texture2DArray_Example,
2323
&TriangleMSAA_Example,
2424
&Cubemap_Example,
25+
&WindowResize_Example,
2526
};
2627

2728
int main(int argc, char **argv)

0 commit comments

Comments
 (0)