Advertisement
Kitomas

2024-07-19 (11/13)

Jul 19th, 2024
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 31.73 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"kw32g\include\arrlen.hpp":
  4. #ifndef _ARRLEN_HPP
  5. #define _ARRLEN_HPP
  6.  
  7. #define gl_scenes_len      5
  8. #define gl_backgrounds_len 4
  9. #define gl_tilesets_len    2
  10. #define gl_objImg_len      11
  11. #define gl_ambience_len    4
  12.  
  13. #endif /* _ARRLEN_HPP */
  14. /******************************************************************************/
  15. /******************************************************************************/
  16. //"kw32g\include\globals.hpp":
  17. #ifndef _GLOBALS_HPP
  18. #define _GLOBALS_HPP
  19.  
  20. #include <kit/all.hpp>
  21. #include <kit/xmp_sfx.hpp>
  22.  
  23. #include <math.h>
  24.  
  25.  
  26.  
  27. #ifdef _DEBUG
  28. #include <stdio.h>
  29. #define _printf(_fmt, ...) printf(_fmt, __VA_ARGS__)
  30.  
  31. #else
  32. #define _printf(_fmt, ...)
  33.  
  34. #endif /* _DEBUG */
  35.  
  36. #define _getnumallocs _printf("line %i: %llu\n",__LINE__,memory::getNumAllocations()-2);
  37. #define PRINT_RECT(_r) _printf("(%3i,%3i, %3i,%3i)\n",(_r).x,(_r).y,(_r).w,(_r).h)
  38.  
  39.  
  40.  
  41.  
  42.  
  43. #define SFXPTR(_index) gl_sfx[_index]
  44. #define SFXPTR_PLAY(_obj_sfx_ptr) { if(_obj_sfx_ptr) _obj_sfx_ptr->play(); }
  45.  
  46. #define SFXPTR_PLAYER_FOOTSTEP SFXPTR( 1)
  47. #define SFXPTR_PLAYER_JUMPING  SFXPTR( 2)
  48. #define SFXPTR_PLAYER_LANDING  SFXPTR( 3)
  49. #define SFXPTR_ACTIVATE_1      SFXPTR( 4)
  50. #define SFXPTR_ABILITY_PICKUP  SFXPTR( 5)
  51. #define SFXPTR_ABILITY_REMOVED SFXPTR( 6)
  52. #define SFXPTR_NOPE_1          SFXPTR( 7)
  53. #define SFXPTR_DLELELE         SFXPTR( 8) //gonna start naming most of them by onomatopoeia
  54. #define SFXPTR_FWSHWSHWSH      SFXPTR( 9)
  55. #define SFXPTR_DEATH           SFXPTR(10) //(has an attack of ~5 ticks!)
  56.  
  57.  
  58.  
  59.  
  60.  
  61. #define DISABLE_NS_EDGES 0
  62. #define SND_TRACK_COUNT 64
  63. #define _WINDOW_TITLE "platformer prototype"
  64. #define CANVSIZ_X 768 //32 tiles wide (assuming tiles are 24x24)
  65. #define CANVSIZ_Y 432 //18 tiles tall
  66. #define TILESIZ_X 32
  67. #define TILESIZ_Y 18
  68. #define lengthof(_array, _type) (sizeof(_array)/sizeof(_type))
  69. #define memset0( _array       ) kit::memory::set((_array), 0, sizeof(_array))
  70. #define memset0s(_array, _size) kit::memory::set((_array), 0, (_size)       )
  71. #define NULLDELETE(_ptr_to_thing) { delete _ptr_to_thing;  _ptr_to_thing = nullptr; }
  72. #define PTR_OFFSET(_ptr, _offset, _type)                 ( \
  73.   (_type*)( ((kit::u64)(_ptr)) + ((kit::s64)(_offset)) )   )
  74. #define PTR_OFFSETB(_ptr, _offset, _type)                              ( \
  75.   (_type*)( ((kit::u64)(_ptr)) + ((kit::s64)(_offset)*sizeof(_type)) )   )
  76. #define ARR2D(_x, _y, _x_len) ((_x) + (_y)*(_x_len))
  77. #define PUN_DEREF(_thing, _to_type) (*(_to_type*)&(_thing));
  78. #define RND(_v) ( (kit::s32)(  ((_v)>=0) ? (_v)+0.5f : (_v-0.5f)  ) ) //rounding
  79. #define TNC(_v) ( (kit::s32) (_v) ) //truncation
  80. #define LERP(_v0, _v1, _t) (  (1.0f-(_t)) * (_v0)  +  (_t) * (_v1)  )
  81.  
  82.  
  83.  
  84. //(this is jank, and i wouldn't have done this at all if i hadn't reduced the ticks
  85.  //per frame from 4 to 2 AFTER already having set the arbitrary physics values)
  86. #define TPF 2 //(ticks per frame)
  87. #define TPF_MOD (4.0f/TPF)
  88. #define TPFVALF(_flt)            ((_flt)*(TPF_MOD))
  89. #define TPFVALI(_int) ((kit::s32)((_int)*(TPF_MOD)))
  90.  
  91. // >3 frames instead of >0 to account for ghost step
  92. #define _MID_AIR (gl_player.ticksInAir>(TPF*3))
  93. #define PLAYER_JUMPED (gl_player.first_jmp || gl_player.second_jmp)
  94. #define PLAYER_CAN_GND_JMP (!gl_player.first_jmp  && !_MID_AIR)
  95. #define PLAYER_CAN_AIR_JMP (!gl_player.second_jmp && gl_player.has_dbljmp)
  96. #define PLAYER_CAN_JUMP (  PLAYER_CAN_GND_JMP  || PLAYER_CAN_AIR_JMP  )
  97. #define PLAYER_SET_JMP_FLAGS { if(     PLAYER_CAN_GND_JMP){ gl_player.first_jmp  = true; } \
  98.                                else if(PLAYER_CAN_AIR_JMP){ gl_player.second_jmp = true; } }
  99. #define MID_AIR (_MID_AIR || PLAYER_JUMPED)
  100.  
  101. #define VELX_MAX             TPFVALF(1.5f   )
  102. #define VELY_MAX             TPFVALF(2.5f   )
  103. #define GRAVITY              TPFVALF(0.0195f*(TPF_MOD)) //completely arbitrary lol
  104. #define PLAYER_JUMP_STRENGTH TPFVALF(2.0f*sqrtf(gl_player.scale/2))
  105. #define PLAYER_JUMP_CANCEL          (0.33f  ) //multiplier of vel.y when canceling a jump while ascending
  106. #define PLAYER_SPEED         TPFVALF(0.020f*(TPF_MOD))
  107. #define PLAYER_SCALE                (2.0f   ) //initial player scale (8px * 2.0f for a 16x16 player sprite)
  108. #define PLAYER_NEUTRAL       TPFVALF(0.1f   ) //what +/- range should be considered 'neutral' for vel/acc
  109. #define PLAYER_AIR_FRICTION         (0.5f   ) //multiplier for acc.x when adding to vel.x in mid-air
  110. #define PLAYER_GND_FRICTION         (1.0f   ) //multiplier of PLAYER_SPEED for on-ground deceleration
  111. #define PLAYER_RUN_MUL              (0.075f/(gl_player.scale/2)) //vel.x's multiplier when adding to runningState
  112. #define PLAYER_LANDING_VOLUME       (0.05f)
  113.  
  114.  
  115.  
  116. extern char falseStr[]; // = "false"
  117. extern char trueStr[];  // = "true"
  118. #define boolStr(_bool_value) ( (_bool_value) ? trueStr : falseStr )
  119.  
  120. #define DEATH_PROG_MAX (3.0f)
  121. extern kit::f32          gl_death_prog;
  122. extern kit::s32          audioManager(void* userdata);
  123. //(gl_ as in gl[obal], not [open]gl)
  124. extern kit::Thread*      gl_audioManager_thread;
  125. extern bool              gl_audioManager_exit;    //poor mans' semaphores, since
  126. extern bool              gl_audioManager_occ_thr;  //my mutexes don't seem to
  127. extern bool              gl_audioManager_occ_sfx;  //work across multiple threads :(
  128. extern bool              gl_saveRead_called; //'was saveRead() called this tick?'
  129. extern kit::u64          gl_tickCounter;
  130. extern kit::TimerSimple* gl_frameTimer;
  131. extern kit::SoundEngine* gl_snd;
  132. extern kit::Window*      gl_win;
  133. extern kit::BitmapFont*  gl_text;
  134. extern kit::FStr*        gl_fstr;
  135. #define gl_textf(_x, _y, _fmt,           ...) \
  136.     gl_text->print((_x), (_y), gl_fstr->fmt(_fmt, __VA_ARGS__), 0)
  137. #define gl_textfs(_x, _y, _fmt, _maxLen, ...) \
  138.     gl_text->print((_x), (_y), gl_fstr->fmt(_fmt, __VA_ARGS__), (_maxLen))
  139. extern kit::Bitmap* gl_spritesheetPlayer;
  140. extern kit::Bitmap* gl_spritesheetFont; //a copy of gl_text's texture atlas
  141. extern kit::Bitmap* gl_spritesheetAbilityIndicators;
  142.  
  143.  
  144.  
  145. struct ControlStates;
  146. struct Player;
  147. struct Scene;
  148. struct SceneDescriptor;
  149. struct Object;
  150. struct SoundEffect;
  151. typedef bool (*Object_TickCallback)(Object* _obj_a, bool render_only);
  152.  
  153. #include <arrlen.hpp>
  154. #define                 gl_sfx_len          10
  155. #define                 gl_music_len         4
  156. #define                 gl_objCallbacks_len  9
  157. //+1 for the array lengths, since the first valid id is 1, not 0
  158. extern ControlStates    gl_ctrlStates;
  159. extern Player           gl_player;
  160. extern Scene            gl_scene;
  161. extern SceneDescriptor* gl_scenes[gl_scenes_len+1];
  162. extern kit::u16         gl_scene_indices[gl_scenes_len+1];
  163. extern Object*          gl_objsCache[gl_scenes_len+1];
  164. extern kit::Bitmap*     gl_backgrounds[gl_backgrounds_len+1];
  165. extern kit::Bitmap*     gl_tileset_missing;
  166. extern kit::Bitmap*     gl_tilesets[gl_tilesets_len+1];
  167. extern kit::Bitmap*     gl_objImg[gl_objImg_len+1];
  168. extern kit::AudioData*  gl_ambience[gl_ambience_len+1];
  169. extern SoundEffect*     gl_sfx[gl_sfx_len+1];
  170.  
  171. extern const char                gl_sfxNames[gl_sfx_len+1][32]; //sfx file paths
  172. extern const kit::shape::fpoint  gl_sfxSettings[gl_sfx_len+1]; //volume and speedRange
  173. extern const char                gl_music[gl_music_len+1][32]; //the music's file paths
  174. extern const Object_TickCallback gl_objCallbacks[gl_objCallbacks_len+1];
  175. #include <text_lines.hpp>
  176.  
  177.  
  178.  
  179.  
  180.  
  181. #ifdef _DEBUG
  182. #define DBG_TXT(_row, _fmt, ...) gl_textf(1,1+(_row)*9, _fmt, __VA_ARGS__)
  183. #define WINDOW_TITLE _WINDOW_TITLE" (Debug Build)"
  184. #if gl_scenes_len > 1
  185. #  define STARTING_SCENE 2
  186. #else
  187. #  define STARTING_SCENE 1 //if there's only 1 scene you can't go to #2 lol
  188. #endif
  189. #define STARTING_POSITION { CANVSIZ_X/2 - 350, CANVSIZ_Y/2 - 80 }
  190.  
  191. #else
  192. #define DBG_TXT(_row, _fmt, ...)
  193. #define WINDOW_TITLE _WINDOW_TITLE
  194. #define STARTING_SCENE 1
  195. #define STARTING_POSITION { CANVSIZ_X/2 - 350, CANVSIZ_Y/2 - 80 }
  196.  
  197. #endif
  198.  
  199.  
  200.  
  201.  
  202.  
  203. void gl_win_drawRectEmpty(kit::shape::rect rect, kit::color::ARGB color,
  204.                           kit::u32 width = 1);
  205.  
  206.  
  207. kit::f64 frand();
  208. kit::f32 frandf();
  209.  
  210. kit::f64 frandRange(kit::f64 start, kit::f64 maxDeviation);
  211.  
  212.  
  213. //"delta_p" should be a pointer to the point to be filled in with
  214.  //the number of pixels to shift the position by to avoid a collision
  215.  //(passing nullptr to delta_p will result in it being ignored)
  216. bool rects_overlapping(kit::shape::rect& object_rect, kit::shape::rect& obstacle_rect,
  217.                        kit::shape::point* delta_p = nullptr);
  218.  
  219. bool subtiles_overlapping(kit::shape::rect& object_rect_ref,
  220.                           kit::shape::point* delta_p = nullptr);
  221.  
  222.  
  223.  
  224. void handleEdgeWrap();
  225.  
  226. void drawStuff_and_processObjects();
  227.  
  228.  
  229.  
  230. kit::color::ARGB unit_to_ryg(kit::f32 unit);
  231.  
  232.  
  233.  
  234. //assumes 8x8 monospaced with 1 pixel of spacing in both x and y
  235. kit::shape::point get_text_size(const char* str);
  236.  
  237. //x&y determine the CENTER position of the text, not its top-left corner
  238. //calculates text_size if either text_size.x or .y is 0
  239. void drawTextBox(kit::s32 x, kit::s32 y, const char* str,
  240.                  kit::shape::point text_size = {0,0});
  241.  
  242.  
  243.  
  244.  
  245.  
  246. struct SoundEffect { //40B (not including the size of the AudioData class)
  247.   kit::AudioData* sfx = nullptr;
  248.   kit::f32       volL = 1.0f;
  249.   kit::f32       volR = 1.0f;
  250.   kit::f64  speedBase = 1.0 ;
  251.   kit::f64 speedRange = 0.0 ;
  252.   kit::f32        pan = 0.0f;
  253.   kit::u32 _padding32;
  254.  
  255.  
  256.   SoundEffect(const char* filePath, kit::f32 volume = 1.0f,
  257.                                     kit::f64 _speedRange = 0.0)
  258.   {
  259.     sfx = new kit::AudioData(filePath, gl_snd);
  260.     volL = volume,  volR = volume;
  261.     speedRange = _speedRange;
  262.   }
  263.  
  264.  
  265.   ~SoundEffect(){ delete sfx; }
  266.  
  267.  
  268.   kit::s32 play(){
  269.     if(sfx != nullptr){
  270.       gl_audioManager_occ_sfx = true;
  271.       while(gl_audioManager_occ_thr) kit::time::sleep(0);
  272.  
  273.  
  274.       kit::s32 trackNum;
  275.  
  276.       if(speedRange < 0.0) speedRange = -speedRange;
  277.       if(!speedRange) trackNum = gl_snd->sfxPlay(sfx, volL, volR, 1.0, pan);
  278.       else trackNum = gl_snd->sfxPlay(sfx, volL, volR, frandRange(speedBase,speedRange), pan);
  279.  
  280.  
  281.       gl_audioManager_occ_sfx = false;
  282.       return trackNum;
  283.  
  284.     } else {
  285.       return -1;
  286.  
  287.     }
  288.   }
  289. };
  290.  
  291.  
  292.  
  293.  
  294.  
  295. //differs from shape::rect in that the rectangle is defined
  296.  //by 2 points instead of a top-left position + w,h
  297. struct shape_quad {
  298.   kit::shape::point a, b; //top-left corner, bottom-right corner
  299.  
  300.   shape_quad() : a(0, 0), b(0, 0) {}
  301.   shape_quad(kit::shape::rect& r) :  a(r.x, r.y), b(r.w, r.h)  {  b += a;  }
  302.  
  303.   inline void operator-=(const shape_quad& q){ a -= q.a;  b -= q.b; }
  304.   inline void operator+=(const shape_quad& q){ a += q.a;  b += q.b; }
  305.   inline void operator*=(const kit::s32  & v){ a.x *= v;  a.y *= v;  b.x *= v;  b.y *= v; }
  306.   inline void operator/=(const kit::s32  & v){ a.x /= v;  a.y /= v;  b.x /= v;  b.y /= v; }
  307.  
  308. };
  309.  
  310.  
  311.  
  312.  
  313.  
  314. //keyboard action states for the current tick
  315. //(_p, _r for p[ressed] and r[eleased])
  316. struct ControlStates {
  317.   struct {
  318.     bool    up_p,    up_r;
  319.     bool  down_p,  down_r;
  320.     bool  left_p,  left_r;
  321.     bool right_p, right_r;
  322.   } inst; //instantaneous control states
  323.  
  324.   bool esc_h;
  325.  
  326.   //bool action_p, action_r;
  327.  
  328. };
  329.  
  330. #define CTRLSTATE_SET(_which) gl_ctrlStates._which = true
  331. #define CTRLSTATE_CLR(_which) gl_ctrlStates._which = false
  332. #define CTRLSTATE_GET(_which) (gl_ctrlStates._which)
  333. static inline void clearControlStates(){
  334.   kit::memory::set(&gl_ctrlStates.inst, 0, sizeof(gl_ctrlStates.inst));
  335. }
  336.  
  337.  
  338.  
  339.  
  340.  
  341. static inline void resetEnvFlags(){
  342.   gl_saveRead_called = false;
  343. }
  344.  
  345.  
  346.  
  347.  
  348.  
  349. extern bool gl_fullscreen;
  350. bool handleEvents();
  351.  
  352.  
  353.  
  354.  
  355.  
  356. #define exit_prog_inc 0.013f
  357. static inline bool handleEscExit(){
  358.   static kit::f32 exit_prog = 0.0f;
  359.  
  360.   if(CTRLSTATE_GET(esc_h)){
  361.     exit_prog += exit_prog_inc;
  362.     if(exit_prog >= 1.0f) return false;
  363.  
  364.   } else if(exit_prog > 0.0f){
  365.     exit_prog -= exit_prog_inc;
  366.     exit_prog  = MAX(exit_prog, 0.0f);
  367.     goto _draw_rectangle;
  368.  
  369.   }
  370.  
  371.   if(exit_prog > 0.0f){
  372.     _draw_rectangle:
  373.     kit::shape::rect visual(0,0, (kit::s32)(CANVSIZ_X*exit_prog),16);
  374.     kit::color::ARGB visual_color = unit_to_ryg(1.0f-exit_prog);
  375.     gl_win->drawRectangles(&visual, 1, visual_color);
  376.   }
  377.  
  378.   return true;
  379.  
  380. }
  381.  
  382.  
  383.  
  384.  
  385.  
  386. void attemptReset(bool skipRead = false);
  387.  
  388.  
  389.  
  390.  
  391.  
  392. void deathAnim();
  393.  
  394.  
  395.  
  396.  
  397.  
  398. #endif /* _GLOBALS_HPP */
  399. /******************************************************************************/
  400. /******************************************************************************/
  401. //"kw32g\include\object.hpp":
  402. #ifndef _OBJECT_HPP
  403. #define _OBJECT_HPP
  404.  
  405. #include <globals.hpp>
  406.  
  407.  
  408.  
  409.  
  410.  
  411. //if scene_id is 0, the current active scene is used instead
  412.  //(also if 0, this should be called only AFTER loading tile data via loadScene)
  413. void loadObjects(bool forced = false, kit::u16 scene_id = 0);
  414.  
  415.  
  416.  
  417.  
  418.  
  419. //returns whether the scene's state changed
  420. bool processObjects(bool in_front, bool render_only = false);
  421.  
  422.  
  423.  
  424.  
  425.  
  426. //struct Object;
  427. //typedef bool (*Object_TickCallback)(Object* _obj_a, bool render_only);
  428. //(^^ already defined in globals.hpp)
  429.  
  430. struct Object { //48B (28 bytes reserved for the user data)
  431.   kit::u64           _user_0; //data depends on object type
  432.   kit::u64           _user_1;  //
  433.   kit::u64           _user_2;  //
  434.   kit::u32           _user_3;  //
  435.  
  436.   struct {
  437.     kit::u16         x;
  438.     kit::u16         y;
  439.   } /*-----------*/ size;
  440.   struct {
  441.     kit::u8          x; //hitbox's offset relative to the
  442.     kit::u8          y;  //object's actual position
  443.   } /*---------*/ offset;
  444.  
  445.   kit::s16                 x;
  446.   kit::s16                 y;
  447.  
  448.   struct {
  449.     kit::u16            type : 14;
  450.     kit::u16      persistent :  1; //'never reset this object's cached data?'
  451.     kit::u16        in_front :  1; //to either display before player or after foreground
  452.   };
  453.  
  454.   Object_TickCallback update; //called for every tick that the object is active
  455. };
  456.  
  457.  
  458.  
  459.  
  460.  
  461. //--OBJECT 1--
  462. //do not use this object in a scene with more than 2 states
  463. //also, there can in theory be multiple 1lever(s) in a scene,
  464.  //as long as they share the same values of scene_old&new
  465. struct _obj_1lever { //24B; 8x24px sprite
  466.   kit::u16 scene_old; //2B
  467.   kit::u16 scene_new; //2B
  468.   kit::u16    sfx_id; //2B
  469.   //-END OF INIT DATA-
  470.   bool          init; //1B
  471.   bool       flipped; //1B
  472.   kit::Bitmap*   img; //8B
  473.   SoundEffect*   sfx; //8B
  474. };
  475.  
  476.  
  477. //--OBJECT 2--
  478. //0 = shrink
  479. //1 = double jump
  480. //2 = speed
  481. struct _obj_ability { //24B; 24x24px sprite, 24 frames (spritesheet of 24x1)
  482.   kit::u8         which; //1B
  483.   //-END OF INIT DATA-
  484.   bool             init; //1B
  485.   kit::u16 frameCounter; //2B
  486.   kit::u32   _padding32; //4B
  487.   kit::Bitmap*      img; //8B
  488.   bool*     has_ability; //8B; ptr to bool that determines if ability is enabled
  489. };
  490.  
  491.  
  492. //--OBJECT 3--
  493. //0 = shrink
  494. //1 = double jump
  495. //2 = speed
  496. struct _obj_ability_remover { //24B; 24x24px sprite, spritesheet of 3x1
  497.   kit::u8       which; //1B
  498.   bool      dontReset; //1B; disables resetting player scale back to 2 if .which == 0, etc.
  499.   //-END OF INIT DATA-
  500.   bool           init; //1B
  501.   kit::u8   _padding8; //1B
  502.   kit::u32 _padding32; //4B
  503.   kit::Bitmap*    img; //8B
  504.   bool*   has_ability; //8B; ptr to bool that determines if ability is enabled
  505. };
  506.  
  507.  
  508. //--OBJECT 4--
  509. //(not specifying w or h results in the default of _obj_a->size.x/y being used!)
  510. struct _obj_generic_sprite { //16B
  511.   kit::u16       which; //2B; objimg index
  512.   kit::u8          w,h; //2B; width and height of source sprite (1B each)
  513.   kit::u8    numFrames; //1B; <2 for no animation (static sprite)
  514.   kit::u8   tick_delay; //1B; 'how many ticks per frame?' (ignored if num_frames < 2)
  515.   //-END OF INIT DATA-
  516.   bool            init; //1B
  517.   kit::u8 frameCounter; //1B
  518.   kit::Bitmap*     img; //8B
  519. };
  520.  
  521.  
  522. //--OBJECT 5--
  523. //if both pos_x&y are 0, text box will draw just above the collider
  524. //otherwise, if one of pos_x&y are 0, center their position based on axis
  525. struct _obj_msgbox { //24B
  526.   kit::s16      pos_x, pos_y; //4B (2B each)
  527.   kit::u16             which; //2B; gl_text_lines index
  528.   bool           show_always; //1B; always display box, instead of on player collide
  529.   //-END OF INIT DATA-
  530.   bool                  init; //1B
  531.   kit::shape::point str_size; //8B
  532.   const char*            str; //8B
  533. };
  534.  
  535.  
  536. //--OBJECT 6--
  537. struct _obj_save_interface { //8B
  538.   kit::u8       which; //1B; 0,1,2 = read,write,wipe
  539.   bool        passive; //1B; 'bypass need to interact with this object to activate it?'
  540.   //-END OF INIT DATA-
  541.   bool           init; //1B
  542.   bool       touching; //1B; prevents invoking save function every tick
  543.   kit::s32 box_height; //4B; used for saveWrite's visual effect
  544. };
  545.  
  546.  
  547. //--OBJECT 7--
  548. struct _obj_teleporter { //8B
  549.   kit::s16 pos_x, pos_y; //4B; (2B each); where to teleport to on-screen
  550.   kit::u16        scene; //2B; what scene to teleport to (0 for current scene)
  551.   bool          passive; //1B; 'bypass need to interact with this object to activate it?'
  552.   //-END OF INIT DATA-
  553.   bool         init : 4; //4b
  554.   bool     touching : 4; //4b; prevents teleport every tick
  555. };
  556.  
  557.  
  558. //--OBJECT 8--
  559. struct _obj_death_zone { //17B
  560.   //-END OF INIT DATA-
  561.   kit::shape::rect collider; //16B
  562.   bool                 init; // 1B
  563. };
  564.  
  565.  
  566. //--OBJECT 9--
  567. #pragma pack(push, 1)
  568. struct _obj_death_ball { //28B (the absolute maximum, so pack this struct!)
  569.   kit::s16    b_x, b_y; //4B (2B each); waypoint b
  570.   kit::f32       speed; //4B; how much to increment t_value by every tick
  571.   kit::s16    a_x, a_y; //4B (2B each); waypoint a (this defaults to _obj_a->x&y)
  572.   //-END OF INIT DATA-
  573.   kit::u16  _padding16; //2B
  574.   bool            init; //1B
  575.   kit::u8 frameCounter; //1B
  576.   kit::Bitmap*     img; //8B
  577.   kit::f32     t_value; //4B; interpolation value; a->b->a = 0.0f->1.0f->2.0f
  578. };
  579. #pragma pack(pop)
  580.  
  581.  
  582.  
  583.  
  584.  
  585. bool obj_1lever         (Object* _obj_a, bool render_only); //1
  586. bool obj_ability        (Object* _obj_a, bool render_only); //2
  587. bool obj_ability_remover(Object* _obj_a, bool render_only); //3
  588. bool obj_generic_sprite (Object* _obj_a, bool render_only); //4
  589. bool obj_msgbox         (Object* _obj_a, bool render_only); //5
  590. bool obj_save_interface (Object* _obj_a, bool render_only); //6
  591. bool obj_teleporter     (Object* _obj_a, bool render_only); //7
  592. bool obj_death_zone     (Object* _obj_a, bool render_only); //8
  593. bool obj_death_ball     (Object* _obj_a, bool render_only); //9
  594.  
  595.  
  596.  
  597.  
  598. #endif /* _OBJECT_HPP */
  599. /******************************************************************************/
  600. /******************************************************************************/
  601. //"kw32g\include\player.hpp":
  602. #ifndef _PLAYER_HPP
  603. #define _PLAYER_HPP
  604.  
  605. #include <globals.hpp>
  606.  
  607.  
  608.  
  609.  
  610.  
  611. struct Player { //80B
  612.   SoundEffect* sfx_footstep = nullptr;
  613.   SoundEffect* sfx_jumping  = nullptr;
  614.   SoundEffect* sfx_landing  = nullptr;
  615.  
  616.   kit::shape::point  pos; //the player's CENTER position, in pixels
  617.   kit::shape::fpoint rem; //[position] rem[ainder]; (basically pos % 1)
  618.   kit::shape::fpoint vel; //how many pixels to move pos by
  619.   kit::shape::fpoint acc; //how many pixels to change vel by
  620.  
  621.   kit::u32 ticksInAir = 0; //game ticks, which are 1/4 a frame long (approx. 4.1ms)
  622.  
  623.   kit::f32 runningState = 0.0f; //current animation frame when running
  624.  
  625.   kit::f32 scale = PLAYER_SCALE;
  626.  
  627.   bool facingRight   = true;
  628.   bool visible       = true;
  629.   bool enforceMaxVel = true;
  630.   bool confused      = false;
  631.   bool first_jmp     = false;
  632.   bool second_jmp    = false;
  633.  
  634.   bool has_dbljmp = false;
  635.   bool has_shrink = false;
  636.   bool has_speed  = false;
  637.  
  638.   bool going_left  = false;
  639.   bool going_right = false;
  640.  
  641.   bool dying = false;
  642.  
  643.  
  644.  
  645.   //player collider rect is the same as its blit rect for simplicity's sake
  646.   inline kit::shape::rect getRect(){
  647.     kit::shape::rect result;
  648.  
  649.     //&(~1) for locking size to a multiple of 2 pixels (for even division by 2)
  650.     result.w = abs( RND(8.0f*scale) & (~1) );
  651.     result.h = result.w;
  652.     result.x = RND(pos.x) - result.w/2;
  653.     result.y = RND(pos.y) - result.h/2;
  654.  
  655.     return result;
  656.   }
  657.  
  658.   inline void resetAbilities(){
  659.     has_dbljmp = false;
  660.     has_shrink = false;
  661.     has_speed  = false;
  662.   }
  663.  
  664.   inline bool addScale(kit::f32 scale_delta){ return setScale(scale+scale_delta); }
  665.  
  666.   inline void play_sfx_footstep(bool b=true){ if(sfx_footstep && b) sfx_footstep->play(); }
  667.   inline void play_sfx_jumping (bool b=true){ if(sfx_jumping  && b) sfx_jumping ->play(); }
  668.   inline void play_sfx_landing (bool b=true){ if(sfx_landing  && b) sfx_landing ->play(); }
  669.  
  670.  
  671.   bool setScale(kit::f32 scale_new);
  672.  
  673.   kit::shape::rect blit(bool only_return_src_rect = false);
  674.  
  675.   void update();
  676.  
  677.   kit::shape::rect move(kit::f32 deltaX, kit::f32 deltaY);
  678.  
  679.   void kill();
  680.  
  681. };
  682.  
  683.  
  684.  
  685.  
  686.  
  687. #endif /* _PLAYER_HPP */
  688. /******************************************************************************/
  689. /******************************************************************************/
  690. //"kw32g\include\save.hpp":
  691. #ifndef _SAVE_HPP
  692. #define _SAVE_HPP
  693.  
  694. #include <globals.hpp>
  695.  
  696.  
  697.  
  698.  
  699.  
  700. void saveReadSystem();
  701.  
  702.  
  703.  
  704. void saveWriteSystem();
  705.  
  706.  
  707.  
  708. void saveWipeSystem();
  709.  
  710.  
  711.  
  712.  
  713. bool saveRead();
  714.  
  715.  
  716.  
  717. void saveWrite();
  718.  
  719.  
  720.  
  721. void saveWipe();
  722.  
  723.  
  724.  
  725.  
  726.  
  727. #endif /* _SAVE_HPP */
  728. /******************************************************************************/
  729. /******************************************************************************/
  730. //"kw32g\include\text_lines.hpp":
  731. #ifndef TEXT_LINES_DEFINITION
  732. extern size_t gl_text_lines_len;
  733. extern const char* gl_text_lines[];
  734.  
  735.  
  736. #else
  737. size_t gl_text_lines_len;
  738. const char* gl_text_lines[] = {
  739. /*0*/ "\
  740. <INVALID TEXT ID>",
  741.  
  742. /*1*/ "\
  743. Welcome to a demo\n\
  744. of the current\n\
  745. features in the\n\
  746. platformer!",
  747.  
  748. /*2*/ "\
  749. You can use levers to change\n\
  750. the state of scenes",
  751.  
  752. /*3*/ "\
  753. The shrink ability is used to\n\
  754. squeeze through tight gaps\n\
  755. (bound to the \"Z\" key)",
  756.  
  757. /*4*/ "\
  758. The speed ability is used to uncap\n\
  759. the player's max horizontal speed\n\
  760. (bound to the \"X\" key)",
  761.  
  762. /*5*/ "\
  763. These objects are used\n\
  764. to remove one of the\n\
  765. player's abilities",
  766.  
  767. /*6*/ "\
  768. The double jump ability is\n\
  769. pretty self-explanatory",
  770.  
  771. /*7*/ "\
  772. There are also teleporters!\n\
  773. Their usefulness is obvious",
  774.  
  775. /*8*/ "\
  776. You can save your game\n\
  777. using this handy egg\n\
  778. (though you can use\n\
  779. any sprite for this)",
  780.  
  781. /*9*/ "\
  782. Now you're thinking with portals",
  783.  
  784. /*10*/ "\
  785. ~This water is deadly~",
  786.  
  787. /*11*/ "\
  788. This thing also kills you\n\
  789. (big surprise, I know)",
  790.  
  791. /*12*/ "\
  792. You can get softlocked\n\
  793. if you touch the shrink\n\
  794. ability remover (lol)",
  795.  
  796.  
  797. //(this must be here in order for the length
  798.  //of this array to be properly calculated!)
  799. nullptr,
  800.  
  801. };
  802.  
  803.  
  804. #endif /* TEXT_LINES_DEFINITION */
  805. /******************************************************************************/
  806. /******************************************************************************/
  807. //"kw32g\include\tile.hpp":
  808. #ifndef _TILE_HPP
  809. #define _TILE_HPP
  810.  
  811. #include <globals.hpp>
  812.  
  813.  
  814.  
  815.  
  816.  
  817. union Tile {
  818.   kit::u16 value;
  819.  
  820.   struct {
  821.     kit::u8      tile;
  822.     kit::u8 collision;
  823.   };
  824.  
  825.   struct {
  826.     kit::u16        id : 7; //id of what tile to use
  827.     kit::u16   tileset : 1; //whether to use tileset a or b
  828.  
  829.     kit::u16 collide_a : 1; //'collide with top-left 12x12 section of tile?'
  830.     kit::u16 collide_b : 1; //'collide with top-right 12x12 section of tile?'
  831.     kit::u16 collide_c : 1; //'collide with bottom-left 12x12 section of tile?'
  832.     kit::u16 collide_d : 1; //'collide with bottom-right 12x12 section of tile?'
  833.  
  834.     kit::u16  platform : 1; //allow falling through top side of tile conditionally
  835.  
  836.     kit::u16  slippery : 1; //does tile have a lower friction coefficient?
  837.  
  838.     kit::u16   _unused : 1;
  839.  
  840.     kit::u16      pass : 1; //allow passing through tile (overrides collision)
  841.     //(pass is always 0 inside the scene descriptor)
  842.   };
  843.  
  844.   Tile(               ) : value(             0) {}
  845.   Tile(kit::u8 _lobyte) : value(0x0f00|_lobyte) {} //assumes full collision
  846.   Tile(kit::u16 _value) : value(        _value) {}
  847.   Tile(kit::s32 _value) : value(0xffff& _value) {}
  848. };
  849.  
  850.  
  851.  
  852.  
  853.  
  854. struct Object;
  855.  
  856. //uncompressed scene data
  857. #define OBJ_RESET_VALUE 3 //# of deaths until non-persistent scene objects reset
  858. #define PATTERN_LEN (TILESIZ_X*TILESIZ_Y)
  859. #define CURRENT_SCENE_PTR (gl_scenes[gl_scene.scene])
  860. struct Scene { //2328B
  861.   kit::Bitmap* bmp_bg = nullptr; //bitmap for background layer (references gl_backgrounds)
  862.   Tile         pat_mg[PATTERN_LEN]; //pattern for midground (collision) layer
  863.   Tile         pat_fg[PATTERN_LEN]; //pattern for foreground layer
  864.  
  865.   //(associated scene edges can be manipulated by game states)
  866.   kit::u16    scene; //scene id for the current scene
  867.  
  868.   kit::u16      music = 0xFFFF; //music id;  0 for no change, -1 (65535) to stop
  869.   kit::u16 ambience_a = 0xFFFF; //ambient track id a;  0 for no change, -1 to stop
  870.   kit::u16 ambience_b = 0xFFFF; //ambient track id b;  0 for no change, -1 to stop
  871.  
  872.   kit::u16 tileset_a = 0; //1st associated tileset
  873.   kit::u16 tileset_b = 0; //2nd associated tileset
  874.   kit::u8  obj_reset = 0; //used to reset object properties between deaths
  875.   bool     repeat_bg = false; //'should bg repeat?' (stretches to screen otherwise)
  876.  
  877.   char _padding8[2];
  878.  
  879.  
  880.   void drawBg();
  881.  
  882.   void drawTiles(bool drawForeground = false); //draws midground if false
  883. };
  884.  
  885.  
  886.  
  887.  
  888.  
  889. //for storing the contents of a .ksd file (compressed scene data)
  890. #define KSD_MAGIC 0x4644536B //"kSDF"
  891. #define SD_FILELEN(_scene_desc) ((_scene_desc).dataSize+sizeof(SceneDescriptor))
  892. struct SceneDescriptor { //64B
  893. /*0x00*/  kit::u32      magic; //= 0x4644536B = "kSDF" (no null terminator)
  894. /*0x04*/  kit::u32   dataSize; //size of file in bytes, minus the header (which is always 64B)
  895.  
  896.           //offsets to array data in file (if nullptr, data is assumed to not be present!)
  897.           //(also, objs[x].type refers to the index inside gl_objCallbacks used by the object.
  898.            //each element of gl_objCallbacks is of type Object_TickCallback)
  899. /*0x08*/  Object*        objs; //contains the original states of each object in the scene
  900. /*0x10*/  Tile*        pat_mg; //pattern data is compressed using RLE, where the 1st element's .value
  901. /*0x18*/  Tile*        pat_fg;  //member is the run length, with the 2nd being the actual tile data
  902.  
  903.         struct {
  904. /*0x20*/  kit::u16     bmp_bg : 15; //associated background id
  905. /*0x21*/  kit::u16  repeat_bg :  1; //'should bg repeat?' (stretches to screen otherwise)
  906.         };
  907.  
  908.         struct {
  909. /*0x22*/  kit::u16   objs_len : 15; //number of objects in scene
  910. /*0x23*/  kit::u16    visited :  1; //used to help determine if objects should reset on load
  911.         };
  912.  
  913. /*0x24*/  kit::u16  tileset_a; //1st associated tileset
  914. /*0x26*/  kit::u16  tileset_b; //2nd associated tileset
  915.  
  916. /*0x28*/  kit::u16     edge_n; //scene id for north edge
  917. /*0x2A*/  kit::u16     edge_s; //scene id for south edge
  918. /*0x2C*/  kit::u16     edge_w; //scene id for west edge
  919. /*0x2E*/  kit::u16     edge_e; //scene id for east edge
  920. /*0x30*/  kit::u16      scene; //scene id for scene itself
  921.  
  922. /*0x32*/  kit::u16      music; //music id;  0 for no change, -1 (65535) to stop
  923. /*0x34*/  kit::u16 ambience_a; //ambient track id a;  0 for no change, -1 to stop
  924. /*0x36*/  kit::u16 ambience_b; //ambient track id b;  0 for no change, -1 to stop
  925.  
  926. /*0x38*/  kit::BinaryData* fileData = nullptr; //raw file data; appears as nullptr in file
  927.  
  928. /*0x40*/  //... (array data is stored in order of: objs, pat_mg, and pat_fg)
  929.  
  930.           void unload();
  931.  
  932.           ~SceneDescriptor(){ unload(); }
  933.           SceneDescriptor(){ kit::memory::set(this, 0, sizeof(SceneDescriptor)); }
  934.           SceneDescriptor(kit::u16 scene_id);
  935. };
  936.  
  937.  
  938.  
  939. #ifdef _DEBUG
  940. #include <stdio.h>
  941. #define printSceneDescHeader(_scene_id) _printSceneDescHeader(_scene_id)
  942. static inline void _printSceneDescHeader(kit::u16 scene_id){
  943.   if(scene_id > gl_scenes_len) throw "scene_id out of range";
  944.   SceneDescriptor* scene = gl_scenes[scene_id];
  945.   printf("{\n");
  946.  
  947.  
  948.   printf("  magic      = \"%.4s\" (0x%08X)\n", (char*)&scene->magic, scene->magic);
  949.   printf("  dataSize   = %u\n", scene->dataSize);
  950.  
  951.   printf("  objs       = %p\n", scene->objs);
  952.   printf("  pat_mg     = %p\n", scene->pat_mg);
  953.   printf("  pat_fg     = %p\n", scene->pat_fg);
  954.  
  955.   printf("  bmp_bg     = %u\n", scene->bmp_bg);
  956.   printf("  repeat_bg  = %s\n", boolStr(scene->repeat_bg));
  957.  
  958.   printf("  objs_len   = %u\n", scene->objs_len);
  959.   printf("  visited    = %s\n", boolStr(scene->visited));
  960.  
  961.   printf("  tileset_a  = %u\n", scene->tileset_a);
  962.   printf("  tileset_b  = %u\n", scene->tileset_b);
  963.  
  964.   printf("  edge_n     = %u\n", scene->edge_n);
  965.   printf("  edge_s     = %u\n", scene->edge_s);
  966.   printf("  edge_w     = %u\n", scene->edge_w);
  967.   printf("  edge_e     = %u\n", scene->edge_e);
  968.   printf("  scene      = %u\n", scene->scene);
  969.  
  970.   printf("  music      = %u\n", scene->music);
  971.   printf("  ambience_a = %u\n", scene->ambience_a);
  972.   printf("  ambience_b = %u\n", scene->ambience_b);
  973.  
  974.   printf("  fileData   = %p\n", scene->fileData);
  975.  
  976.  
  977.   printf("}\n");
  978.  
  979. }
  980.  
  981. #else
  982. #define printSceneDescHeader(_scene_id)
  983.  
  984. #endif /* _DEBUG */
  985.  
  986.  
  987.  
  988.  
  989.  
  990. //(scene_id references gl_scenes, whose elements are of type SceneDescriptor*)
  991. void loadScene(kit::u16 scene_id);
  992.  
  993.  
  994.  
  995.  
  996.  
  997. //swap the associations of two scenes, including the scene references themselves
  998.  //(you should never swap out a scene for one that is already active,
  999.  //as this will cause major edge association conflicts!)
  1000. void swapScene(kit::u16 scene_id_old, kit::u16 scene_id_new);
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006. #endif /* _TILE_HPP */
  1007. /******************************************************************************/
  1008. /******************************************************************************/
  1009. //"kw32g\asset_scripts\convert_all_ambience.py":
  1010. _PRINT = True
  1011. src_folder = '.\\_wav\\'
  1012. dst_folder = '.\\_qoa\\'
  1013. tst_folder = '.\\_test\\'
  1014. def_prefix = '_opt_'
  1015.  
  1016. from time import time
  1017. from os import system as cmd, walk, path, chdir
  1018. import subprocess
  1019.  
  1020.  
  1021.  
  1022. def tryInt(string):
  1023.     try:
  1024.         return int(string)
  1025.     except ValueError:
  1026.         return None
  1027.     except TypeError:
  1028.         return None
  1029.  
  1030.  
  1031. def run_program(args, fatal=False):
  1032.     if type(args) != list:
  1033.         print('args must be of type list')
  1034.         exit(-1)
  1035.    
  1036.     if _PRINT: print((' ').join(args))
  1037.    
  1038.     returnCode = subprocess.call(args, shell=True)
  1039.     if returnCode >= 2**31: returnCode -= 2**32
  1040.    
  1041.     if fatal:
  1042.         if returnCode != 0:
  1043.             if _PRINT: print(f'failed to execute "{args}"')
  1044.             exit(-1)
  1045.         return True
  1046.     else:
  1047.         return returnCode == 0
  1048.        
  1049.        
  1050. def tryCmd(s):
  1051.     if _PRINT: print(s)
  1052.     cmd("powershell -command "+s)
  1053.    
  1054.  
  1055. def convertPath(path, prefix = def_prefix):
  1056.     #assumes something like '0_opt_name.wav'
  1057.     id = tryInt(path.split(prefix)[0])
  1058.     if id == None:
  1059.         print(f'failed to get ambience id from "{path}"')
  1060.         exit(-1)
  1061.     return dst_folder + f'ambience_{id}.qoa'
  1062.    
  1063.    
  1064.    
  1065. def main(cwd = None):
  1066.     if cwd != None: chdir(cwd)
  1067.    
  1068.     timeStart = time()
  1069.  
  1070.  
  1071.     tryCmd(f'rm {dst_folder}*.*')
  1072.  
  1073.  
  1074.     #assumes there are no folders in .\_wav\
  1075.     files = []
  1076.     for root, dirs, _files in walk(src_folder):
  1077.         files += _files
  1078.        
  1079.  
  1080.     for file in files:
  1081.         file_src = path.join(src_folder,file)
  1082.         file_dst = convertPath(file)
  1083.         file_tst = path.join(tst_folder,file)
  1084.        
  1085.         #first, convert all wavs to qoa
  1086.         run_program(['qoa_convert.exe', file_src, file_dst])
  1087.        
  1088.         #then, do the reverse to check its output for
  1089.         #any artifacts or 'rip headphone users' moments
  1090.         run_program(['qoa_convert.exe', file_dst, file_tst])
  1091.  
  1092.  
  1093.     print(f'AMBIENCE CONVERTED IN {time()-timeStart} SECONDS')
  1094.     if __name__ == '__main__':
  1095.         tryCmd("pause")
  1096.         exit(0)
  1097.  
  1098.  
  1099.  
  1100. if __name__ == '__main__': main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement