Advertisement
Guest User

bridge.cpp

a guest
Sep 25th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. /*
  2. * Bridge from JS into the cellmap computation code in Wasm
  3. */
  4.  
  5. #include <gameoflife.h>
  6. #include <constants.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <iostream>
  10. #include <cstring>
  11. /**
  12. * A struct that manages our instantiation of
  13. * the game of life
  14. */
  15. typedef struct {
  16. cellmap *cm; /** holds the instance of the cellmap */
  17. unsigned char *cells; /** holds the cell vals for current iteration */
  18. } GOL_Instance;
  19. /**
  20. * Constructor for the game of life engine
  21. */
  22. GOL_Instance *
  23. GOL_Instance_new() {
  24. GOL_Instance *gol;
  25. gol = (GOL_Instance *)malloc(sizeof *gol);
  26. if (gol) {
  27. memset(gol, 0, sizeof *gol);
  28. }
  29. gol->cm = new_cellmap();
  30. return gol;
  31. }
  32. /**
  33. * Method to destroy the gol instance when the user is done
  34. */
  35. void
  36. GOL_Instance_destroy(GOL_Instance *gol) {
  37. gol->cm->~cellmap();
  38. delete[] gol->cells;
  39. free(gol);
  40. }
  41. /**
  42. * Method to initialize the gol instance
  43. */
  44. void
  45. GOL_Init(GOL_Instance *gol) {
  46. gol->cm->init();
  47. gol->cells = gol->cm->return_cells();
  48. }
  49. /**
  50. * Method to run the gol instance by one step
  51. */
  52. void
  53. GOL_Step(GOL_Instance *gol) {
  54. gol->cm->next_generation();
  55. gol->cells = gol->cm->return_cells();
  56. }
  57. /**
  58. * Method to get the cell values
  59. */
  60. void
  61. GOL_get_values(GOL_Instance *gol, int8_t *arr) {
  62. unsigned char *c = gol->cells;
  63. unsigned int total_cells = cellmap_width * cellmap_height;
  64. for (unsigned int i = 0; i < total_cells; i++) {
  65. unsigned int curr_ind = i * 8;
  66. int8_t curr = (int8_t) *(c+i) & 0x01;
  67. *(arr + curr_ind) = curr;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement