Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Bridge from JS into the cellmap computation code in Wasm
- */
- #include <gameoflife.h>
- #include <constants.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <cstring>
- /**
- * A struct that manages our instantiation of
- * the game of life
- */
- typedef struct {
- cellmap *cm; /** holds the instance of the cellmap */
- unsigned char *cells; /** holds the cell vals for current iteration */
- } GOL_Instance;
- /**
- * Constructor for the game of life engine
- */
- GOL_Instance *
- GOL_Instance_new() {
- GOL_Instance *gol;
- gol = (GOL_Instance *)malloc(sizeof *gol);
- if (gol) {
- memset(gol, 0, sizeof *gol);
- }
- gol->cm = new_cellmap();
- return gol;
- }
- /**
- * Method to destroy the gol instance when the user is done
- */
- void
- GOL_Instance_destroy(GOL_Instance *gol) {
- gol->cm->~cellmap();
- delete[] gol->cells;
- free(gol);
- }
- /**
- * Method to initialize the gol instance
- */
- void
- GOL_Init(GOL_Instance *gol) {
- gol->cm->init();
- gol->cells = gol->cm->return_cells();
- }
- /**
- * Method to run the gol instance by one step
- */
- void
- GOL_Step(GOL_Instance *gol) {
- gol->cm->next_generation();
- gol->cells = gol->cm->return_cells();
- }
- /**
- * Method to get the cell values
- */
- void
- GOL_get_values(GOL_Instance *gol, int8_t *arr) {
- unsigned char *c = gol->cells;
- unsigned int total_cells = cellmap_width * cellmap_height;
- for (unsigned int i = 0; i < total_cells; i++) {
- unsigned int curr_ind = i * 8;
- int8_t curr = (int8_t) *(c+i) & 0x01;
- *(arr + curr_ind) = curr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement