Got a simple sdl3 window.

This commit is contained in:
Warwick 2025-06-12 17:38:36 +01:00
parent 5e05d36370
commit 05f71170dc
2 changed files with 30 additions and 88 deletions

View file

@ -6,20 +6,6 @@ sdl3_deps = [dependency('sdl3'), dependency('sdl3_image')]
cglm_dep = dependency('cglm') cglm_dep = dependency('cglm')
wgpu_native_dep = dependency('wgpu-native') wgpu_native_dep = dependency('wgpu-native')
pkg = import('pkgconfig')
#wgpu_native_dep = dependency('wgpu-native', fallback: ['wgpu-native','webgpunative_dep'])
#cmake = import('cmake')
#dawn_cmakeopts = cmake.subproject_options()
#dawn_cmakeopts.add_cmake_defines({'DAWN_FETCH_DEPENDENCIES': 'ON'})
#dawn_cmakeopts.add_cmake_defines({'DAWN_ENABLE_INSTALL': 'ON'})
#dawn_cmakeopts.add_cmake_defines({'CMAKE_BUILD_TYPE': 'Release'})
#dawn = cmake.subproject('dawn', options: dawn_cmakeopts)
##message('CMake targets:\n - ' + '\n - '.join(dawn.target_list()))
#webgpu_dawn_dep = dawn.dependency('webgpu_dawn')
executable('webgpu', 'src/main.c', executable('webgpu', 'src/main.c',
win_subsystem: 'windows', win_subsystem: 'windows',
dependencies: [sdl3_deps, cglm_dep, wgpu_native_dep, vulkan_dep] dependencies: [sdl3_deps, cglm_dep, wgpu_native_dep, vulkan_dep]

View file

@ -1,84 +1,40 @@
#include <SDL3/SDL.h> #include "SDL3/SDL_init.h"
#include <SDL3/SDL_video.h> #include "SDL3/SDL_log.h"
#include <SDL3/SDL_vulkan.h> #include "SDL3/SDL_render.h"
#include <assert.h> #include "SDL3/SDL_video.h"
#include <stdbool.h>
#include <stdio.h>
#include <vulkan/vulkan.h>
#include <vulkan/vulkan_core.h>
#include <webgpu/webgpu.h>
static void OnRequestAdapter(WGPURequestAdapterStatus status, int main(int argc, char *argv[]) {
WGPUAdapter adapter, WGPUStringView message, SDL_Window *window;
void *userdata1, void *userdata2) { SDL_Renderer *renderer;
// TODO handle messages and errors better SDL_Event event;
if (status != WGPURequestAdapterStatus_Success) {
printf("%s", message.data);
}
assert(status == WGPURequestAdapterStatus_Success);
WGPUAdapter *result = userdata1; if (!SDL_Init(SDL_INIT_VIDEO)) {
if (*result == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s",
*result = adapter; SDL_GetError());
} return 3;
}
void requestAdapterSync(WGPUInstance instance,
WGPURequestAdapterOptions const *options,
WGPUAdapter *adapter) {
WGPURequestAdapterCallbackInfo info = {.mode = WGPUCallbackMode_WaitAnyOnly,
.callback = &OnRequestAdapter,
.userdata1 = adapter};
WGPUFuture future = wgpuInstanceRequestAdapter(instance, options, info);
WGPUFutureWaitInfo wait = {
.future = future,
};
WGPUWaitStatus status = wgpuInstanceWaitAny(instance, 1, &wait, 0);
assert(status == WGPUWaitStatus_Success);
assert(adapter);
}
int main() {
SDL_Window *window = NULL;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "SDL failed to initialise: %s\n", SDL_GetError());
return 1;
}
window = SDL_CreateWindow("SDL3 Example", 800, 600, SDL_WINDOW_VULKAN);
if (window == NULL) {
fprintf(stderr, "SDL window failed to initialise: %s\n", SDL_GetError());
return 1;
} }
const char *driver = SDL_GetCurrentVideoDriver(); if (!SDL_CreateWindowAndRenderer("Hello SDL3", 320, 240, SDL_WINDOW_RESIZABLE,
printf("Driver: %s\n", driver); &window, &renderer)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
// We create a descriptor "Couldn't create window and renderer: %s", SDL_GetError());
WGPUInstanceDescriptor desc = {}; return 3;
desc.nextInChain = NULL;
// We create the instance using this descriptor
WGPUInstance instance = wgpuCreateInstance(&desc);
//
// We can check whether there is actually an instance created
if (!instance) {
printf("Could not initialize WebGPU!\n");
return 1;
} }
// Display the object (WGPUInstance is a simple pointer, it may be while (1) {
// copied around without worrying about its size). SDL_PollEvent(&event);
printf("WGPU instance: %p\n", instance); if (event.type == SDL_EVENT_QUIT) {
break;
}
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
printf("Requesting Adapter.\n"); SDL_DestroyRenderer(renderer);
WGPUAdapter adapter = NULL; SDL_DestroyWindow(window);
WGPURequestAdapterOptions adapterOpts = {0};
requestAdapterSync(instance, &adapterOpts, &adapter); SDL_Quit();
printf("Got Adapter: %p\n", &adapter);
// Cleanup
wgpuAdapterRelease(adapter);
wgpuInstanceRelease(instance);
return 0; return 0;
} }