GithubHelp home page GithubHelp logo

xernerac / sdl2webgpu Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eliemichel/sdl2webgpu

0.0 0.0 0.0 24 KB

An extension for the SDL2 library for using WebGPU native.

License: MIT License

C 92.20% CMake 7.80%

sdl2webgpu's Introduction

CMake Badge

SDL2 WebGPU Extension

This is an extension for the great SDL2 library for using it with WebGPU native. It was written as part of the Learn WebGPU for native C++ tutorial series.

Table of Contents

Overview

This extension simply provides the following function:

WGPUSurface SDL_GetWGPUSurface(WGPUInstance instance, SDL_Window* window);

Given an SDL window, SDL_GetWGPUSurface returns a WebGPU surface that corresponds to the window's backend. This is a process that is highly platform-specific, which is why I believe it belongs to SDL.

Usage

Your project must link to an implementation of WebGPU (providing webgpu.h) and of course to GLFW. Then:

Option A If you use CMake, you can simply include this project as a subdirectory with add_subdirectory(sdl2webgpu) (see the content of CMakeLists.txt).

Option B Just copy sdl2webgpu.h and sdl2webgpu.c to your project's source tree. On MacOS, you must add the compile option -x objective-c and the link libraries -framework Cocoa, -framework CoreVideo, -framework IOKit, and -framework QuartzCore.

Example

Thanks to this extension it is possible to simply write a fully cross-platform WebGPU hello world:

#define SDL_MAIN_HANDLED
#include "sdl2webgpu.h"
#include <SDL2/SDL.h>
#include <webgpu/webgpu.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
	// Init WebGPU
	WGPUInstanceDescriptor desc;
	desc.nextInChain = NULL;
	WGPUInstance instance = wgpuCreateInstance(&desc);

	// Init SDL
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window *window = SDL_CreateWindow("Learn WebGPU", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);

	// Here we create our WebGPU surface from the window!
	WGPUSurface surface = SDL_GetWGPUSurface(instance, window);
	printf("surface = %p", surface);

	// Wait for close
	SDL_Event event;
	while (1)
		while (SDL_PollEvent(&event))
			if (event.type == SDL_QUIT)
				break;
	// Terminate SDL
	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}

NB The linking process depends on the implementation of WebGPU that you are using. You can find detailed instructions for the wgpu-native implementation in this Hello WebGPU chapter. You may also check out examples/CMakeLists.txt.

License

See LICENSE.txt.

sdl2webgpu's People

Contributors

eliemichel avatar waywardmonkeys avatar 0x000c0a71 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.