GithubHelp home page GithubHelp logo

C or C++ examples? about cavernos HOT 1 OPEN

Zireael07 avatar Zireael07 commented on April 30, 2024
C or C++ examples?

from cavernos.

Comments (1)

jordwest avatar jordwest commented on April 30, 2024 2

I haven't tried building anything in C/C++, but it should definitely be possible. This article might be useful for building without Emscripten:
https://surma.dev/things/c-to-webassembly/

It's been too long since I've built anything with C, but here's a quick attempt that might get you started. It's totally untested though and my C is pretty rusty (in both senses of the word):

struct Config {
  unsigned char columns;
  unsigned char rows;
  unsigned char reserved[254];
}

struct ScreenData {
  unsigned char should_update;
  unsigned char cell_data[65535];
}

struct CavernOS {
    struct Config config;
    unsigned char inputs[256];
    unsigned char reserved[2560];
    struct ScreenData character;
    struct ScreenData bg_color;
    struct ScreenData fg_color;
};

static struct CavernOS OS = {
  {80,20, {0}},
  {0},  // inputs
  {0},
  {0},  // characters
  {0},  // bg_color
  {1},  // fg_color
};

void* init (CavernOS *os) {
  // You could malloc some app state here and return a pointer, which the runtime will pass back to the `frame` function
  return 0;
}

void frame (CavernOS *os, void *app_state, double dt) {
  // This should set the top left cell to a smiley face
  OS.characters.cell_data[0] = 1;
  OS.characters.should_update = 1;
}

Combine that with the instructions here to build your .wasm file:
https://surma.dev/things/c-to-webassembly/

Build the JavaScript host bundle using (cd host-js && ./build.sh). Finally, copy the main.js, index.html from host-js/dist, all the files from /example-rs/assets, and your .wasm file into a single directory. Update the manifest.json as necessary.

It's a bit convoluted, but only because the build scripts do all that for Rust. Check build-example-rs.sh.

Once you've got it working, all the interactions are basically just reading and writing to the OS struct on each frame. See also these docs for explanations of the interface:

https://jordwest.github.io/cavernos/api.html

If you get stuck let me know, happy to help walk you through it. Or if you manage to get it working please share, I'd love to add an example to the repo.

from cavernos.

Related Issues (5)

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.