Advertisement
pseud0_

Untitled

Aug 27th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <BasicTerm.h>
  3.  
  4. BasicTerm term(&Serial);
  5.  
  6. #define VRy A0 //VRx
  7. #define VRx A1 //VRy
  8. #define SW 2
  9.  
  10. #define LEN(arr) ((unsigned) (sizeof (arr) / sizeof (arr)[0]))
  11.  
  12. /* a map consists of its map data ("tiles") and its dimensions*/
  13. struct Map {
  14.   unsigned width;
  15.   unsigned height;
  16.   const char* data; // in PROGMEM
  17.   const char lvlname[20]; //optional meta info, whatever you need
  18. };
  19.  
  20. /* load data pointer at x,y */
  21. /* reference a 1D array like a 2D array */
  22. #define GET_MAPDATA(map,x,y) pgm_read_byte((unsigned short)(&map->data[x + map->width*y]))
  23.  
  24. const char Level1[] PROGMEM =  
  25. "###############"
  26. "#             #"
  27. "#             #"
  28. " #           # "
  29. "  #         #  "
  30. "   #       #   "
  31. "    #     #    "
  32. "     #   #     "
  33. "      # #      "
  34. "     #   #     "
  35. "    #     #    "
  36. "   #       #   "
  37. "  #         #  "
  38. " #           # "
  39. "#             #"
  40. "#             #"
  41. "###############";
  42.  
  43. Map allMaps[] = {
  44.   {
  45.     .width = 15, // can't be computed at compile time anymore, needs constant data
  46.     .height = 17,
  47.     .data = Level1,
  48.     {"LVL 1"}
  49.   }
  50. };
  51.  
  52. Map* currentMap;
  53. int mapY;
  54. int mapX;
  55.  
  56. void MapSetup(Map* pMap) {
  57.   // just repoint the pionter
  58.   currentMap = pMap;
  59.   mapY = currentMap->height;
  60.   mapX = currentMap->width;
  61. }
  62.  
  63. void drawMap() {
  64.   term.cls();
  65.   for (uint8_t y = 0; y < mapY; y++)
  66.   {
  67.     for(uint8_t x = 0; x < mapX; x++) {
  68.       // load character at given X,Y coordinate from PROGMEM
  69.       char c = GET_MAPDATA(currentMap, 0, y);
  70.       term.print(c);
  71.     }
  72.     term.println(); // new line
  73.   }
  74.  
  75. }
  76.  
  77. void setup() {
  78.   // put your setup code here, to run once:
  79.   pinMode(VRx,INPUT);
  80.   pinMode(VRy,INPUT);
  81.   pinMode(SW,INPUT_PULLUP);
  82.   Serial.begin(115200);
  83.   term.show_cursor(false); //disable the cursor
  84.   Serial.println((String)"C++: " + __cplusplus + "\r\nPinovi:\r\nVRx = " + VRy + "\r\nVRy = " + VRx + "\r\nSW = " + SW + "\r\nPritisni joystick kada si spreman."); //windows: \r\n | linux: \n | mac: \r
  85.   while (digitalRead(SW) == 1)
  86.   {
  87.     //literally nothing just wait
  88.   }
  89.   term.init();
  90.   term.cls();
  91.   MapSetup(&allMaps[0]);
  92.   Serial.println("Map dimensions" + String(mapX) + " x " + String(mapY));
  93.   // print the whole map at once, since its one continous string
  94.   // but will do so without line breaks
  95.   Serial.println((const __FlashStringHelper*) currentMap->data);
  96. }
  97.  
  98. void loop() {
  99.   // put your main code here, to run repeatedly:
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement