Advertisement
Kitomas

tile.hpp as of 2024-05-03

May 3rd, 2024
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.56 KB | None | 0 0
  1. #ifndef _TILE_HPP
  2. #define _TILE_HPP
  3.  
  4. #include <globals.hpp>
  5.  
  6.  
  7.  
  8.  
  9.  
  10. union Tile {
  11.   kit::u16 value;
  12.  
  13.   struct {
  14.     kit::u16        id : 7; //id of what tile to use
  15.  
  16.     kit::u16   tileset : 1; //whether to use tileset a or b
  17.  
  18.     kit::u16 collide_a : 1; //'collide with top-left 12x12 section of tile?'
  19.     kit::u16 collide_b : 1; //'collide with top-right 12x12 section of tile?'
  20.     kit::u16 collide_c : 1; //'collide with bottom-left 12x12 section of tile?'
  21.     kit::u16 collide_d : 1; //'collide with bottom-right 12x12 section of tile?'
  22.  
  23.     kit::u16  platform : 1; //allow falling through top side of tile conditionally
  24.     kit::u16      pass : 1; //allow passing through tile (overrides collision)
  25.     //(pass is always 0 inside the scene descriptor)
  26.  
  27.     kit::u16   _unused : 2;
  28.   };
  29.  
  30.   Tile(               ) : value(             0) {}
  31.   Tile(kit::u8 _lobyte) : value(0x0f00|_lobyte) {} //assumes full collision
  32.   Tile(kit::u16 _value) : value(        _value) {}
  33.   Tile(kit::s32 _value) : value(0xffff& _value) {}
  34. };
  35.  
  36.  
  37.  
  38.  
  39.  
  40. struct Object;
  41.  
  42. //uncompressed scene data
  43. #define OBJ_RESET_VALUE 3 //# of deaths until the scene objects reset
  44. #define PATTERN_LEN (TILESIZ_X*TILESIZ_Y)
  45. struct Scene { //2352B
  46.   kit::Bitmap* bmp_bg = nullptr; //bitmap for background layer (references gl_backgrounds)
  47.   Tile         pat_mg[PATTERN_LEN]; //pattern for midground (collision) layer
  48.   Tile         pat_fg[PATTERN_LEN]; //pattern for foreground layer
  49.  
  50.   //(reallocate only if new scene objects len > objs_lenmax!)
  51.   Object*  objs          = nullptr; //pointer to associated object array (memory::alloc'd)
  52.   kit::u16 objs_len      = 0;
  53.   kit::u16 objs_lenmax   = 0;
  54.  
  55.   //associated edges can be manipulated by game states
  56.   kit::u16   edge_n; //scene id for north edge
  57.   kit::u16   edge_s; //scene id for south edge
  58.   kit::u16   edge_w; //scene id for west edge
  59.   kit::u16   edge_e; //scene id for east edge
  60.   kit::u16    scene; //scene id for scene itself
  61.  
  62.   kit::u16      music = 0; //music id;  0 for no change, -1 (65535) to stop
  63.   kit::u16 ambience_a = 0; //ambient track id a;  0 for no change, -1 to stop
  64.   kit::u16 ambience_b = 0; //ambient track id b;  0 for no change, -1 to stop
  65.  
  66.   kit::u16 tileset_a = 0; //1st associated tileset
  67.   kit::u16 tileset_b = 0; //2nd associated tileset
  68.   kit::u8  obj_reset = 0; //used to reset object (including npc) properties between deaths
  69.   bool     repeat_bg = false; //'should bg repeat?' (stretches to screen otherwise)
  70.  
  71.   char _padding8[6];
  72.  
  73.  
  74.   void drawBg();
  75.  
  76.   void drawTiles(bool drawForeground = false); //draws midground if false
  77. };
  78.  
  79.  
  80.  
  81.  
  82.  
  83. //for storing the contents of a .ksd file (compressed scene data)
  84. #define KSD_MAGIC 0x4644536B //"kSDF"
  85. #define SD_FILELEN(_scene_desc) ((_scene_desc).dataSize+sizeof(SceneDescriptor))
  86. struct SceneDescriptor { //64B
  87. /*0x00*/  kit::u32      magic; //= 0x4644536B = "kSDF" (no null terminator)
  88. /*0x04*/  kit::u32   dataSize; //size of file in bytes, minus the header (which is always 64B)
  89.  
  90.           //offsets to array data in file (if nullptr, data is assumed to not be present!)
  91.           //(also, objs[x].type refers to the index inside gl_objCallbacks used by the object.
  92.            //each element of gl_objCallbacks is of type Object_TickCallback)
  93. /*0x08*/  Object*        objs; //contains the original states of each object in the scene
  94. /*0x10*/  Tile*        pat_mg; //pattern data is compressed using RLE, where the 1st element's .value
  95. /*0x18*/  Tile*        pat_fg;  //member is the run length, with the 2nd being the actual tile data
  96.  
  97.         struct {
  98. /*0x20*/  kit::u16     bmp_bg : 15; //associated background id
  99. /*0x21*/  kit::u16  repeat_bg :  1; //'should bg repeat?' (stretches to screen otherwise)
  100.         };
  101.  
  102.         struct {
  103. /*0x22*/  kit::u16   objs_len : 15; //number of objects in scene
  104. /*0x23*/  kit::u16    visited :  1; //used to help determine if objects should reset on load
  105.         };
  106.  
  107. /*0x24*/  kit::u16  tileset_a; //1st associated tileset
  108. /*0x26*/  kit::u16  tileset_b; //2nd associated tileset
  109.  
  110. /*0x28*/  kit::u16     edge_n; //scene id for north edge
  111. /*0x2A*/  kit::u16     edge_s; //scene id for south edge
  112. /*0x2C*/  kit::u16     edge_w; //scene id for west edge
  113. /*0x2E*/  kit::u16     edge_e; //scene id for east edge
  114. /*0x30*/  kit::u16      scene; //scene id for scene itself
  115.  
  116. /*0x32*/  kit::u16      music; //music id;  0 for no change, -1 (65535) to stop
  117. /*0x34*/  kit::u16 ambience_a; //ambient track id a;  0 for no change, -1 to stop
  118. /*0x36*/  kit::u16 ambience_b; //ambient track id b;  0 for no change, -1 to stop
  119.  
  120. /*0x38*/  kit::BinaryData* fileData = nullptr; //raw file data; appears as nullptr in file
  121.  
  122. /*0x40*/  //... (array data is stored in order of: objs, pat_mg, and pat_fg)
  123.  
  124.           void unload();
  125.  
  126.           ~SceneDescriptor(){ unload(); }
  127.           SceneDescriptor(){ kit::memory::set(this, 0, sizeof(SceneDescriptor)); }
  128.           SceneDescriptor(kit::u16 scene_id);
  129. };
  130.  
  131.  
  132.  
  133. #ifdef _DEBUG
  134. #include <stdio.h>
  135. #define printSceneDescHeader(_scene_id) _printSceneDescHeader(_scene_id)
  136. static inline void _printSceneDescHeader(kit::u16 scene_id){
  137.   if(scene_id > gl_scenes_len) throw "scene_id out of range";
  138.   SceneDescriptor* scene = gl_scenes[scene_id];
  139.   printf("{\n");
  140.  
  141.  
  142.   printf("  magic      = \"%.4s\" (0x%08X)\n", (char*)&scene->magic, scene->magic);
  143.   printf("  dataSize   = %u\n", scene->dataSize);
  144.  
  145.   printf("  objs       = %p\n", scene->objs);
  146.   printf("  pat_mg     = %p\n", scene->pat_mg);
  147.   printf("  pat_fg     = %p\n", scene->pat_fg);
  148.  
  149.   printf("  bmp_bg     = %u\n", scene->bmp_bg);
  150.   printf("  repeat_bg  = %s\n", boolStr(scene->repeat_bg));
  151.  
  152.   printf("  objs_len   = %u\n", scene->objs_len);
  153.   printf("  visited    = %s\n", boolStr(scene->visited));
  154.  
  155.   printf("  tileset_a  = %u\n", scene->tileset_a);
  156.   printf("  tileset_b  = %u\n", scene->tileset_b);
  157.  
  158.   printf("  edge_n     = %u\n", scene->edge_n);
  159.   printf("  edge_s     = %u\n", scene->edge_s);
  160.   printf("  edge_w     = %u\n", scene->edge_w);
  161.   printf("  edge_e     = %u\n", scene->edge_e);
  162.   printf("  scene      = %u\n", scene->scene);
  163.  
  164.   printf("  music      = %u\n", scene->music);
  165.   printf("  ambience_a = %u\n", scene->ambience_a);
  166.   printf("  ambience_b = %u\n", scene->ambience_b);
  167.  
  168.   printf("  fileData   = %p\n", scene->fileData);
  169.  
  170.  
  171.   printf("}\n");
  172.  
  173. }
  174.  
  175. #else
  176. #define printSceneDescHeader(_scene_id)
  177.  
  178. #endif /* _DEBUG */
  179.  
  180.  
  181.  
  182.  
  183.  
  184. //scene_id references gl_scenes, whose elements are of type SceneDescriptor*
  185. void loadScene(kit::u16 scene_id, bool loadObjects = true, bool loadRest = true);
  186.  
  187.  
  188.  
  189.  
  190.  
  191. #endif /* _TILE_HPP */
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement