Advertisement
Kitomas

globals.cpp as of 2024-04-26

Apr 26th, 2024
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <globals.hpp>
  2. #include <tile.hpp>
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. using namespace kit;
  8.  
  9.  
  10.  
  11.  
  12.  
  13. TimerSimple* gl_frameTimer        = nullptr;
  14. SoundEngine* gl_snd               = nullptr;
  15. MutexSimple* gl_snd_lock          = nullptr;
  16. Window*      gl_win               = nullptr;
  17. BitmapFont*  gl_text              = nullptr;
  18. FStr*        gl_fstr              = nullptr;
  19. Bitmap*      gl_spritesheetPlayer = nullptr;
  20.  
  21.  
  22. u16     gl_sceneCurrent = 0;
  23. Scene   gl_scenes[gl_scenes_len];
  24. Bitmap* gl_tilesets[gl_tilesets_len];
  25. Bitmap* gl_tileset_missing = nullptr;
  26.  
  27.  
  28.  
  29.  
  30. void handleInit(){
  31.   gl_frameTimer = new TimerSimple;
  32.   gl_frameTimer->setTimer(0.101);
  33.  
  34.  
  35.   srand((u32)time::getTicks());
  36.  
  37.  
  38.   gl_snd = new SoundEngine(128);
  39.   gl_snd->streamStart();
  40.  
  41.   gl_snd_lock = new MutexSimple;
  42.  
  43.  
  44.  
  45.   gl_win = new Window(WINDOW_TITLE, CANVSIZ_X*2, CANVSIZ_Y*2,
  46.                       WINFLAG_RESIZABLE | WINFLAG_HIDDEN,
  47.                       WINPOS_CENTERED, WINPOS_CENTERED,
  48.                       CANVSIZ_X, CANVSIZ_Y, false);
  49.  
  50.   gl_text = new BitmapFont("dat/img/_font8x8.qoi", gl_win);
  51.  
  52.   gl_fstr = new FStr(4096);
  53.  
  54.   //gl_textf(0, 0, "loading");
  55.   //gl_win->present();
  56.   //gl_win->setVisibility(true);
  57.  
  58.  
  59.  
  60.   gl_spritesheetPlayer = new Bitmap("dat/img/spritesheet_player.qoi", gl_win);
  61.  
  62.  
  63.  
  64.   //memset just in case, though i'm pretty sure this redundant
  65.   memset0(gl_tilesets);
  66.  
  67.   char* fstr_ptr = gl_fstr->ptr();
  68.  
  69.   for(u32 i=0; i<gl_tilesets_len; ++i){
  70.     gl_fstr->fmt("dat/tilesets/tileset_%u.qoi", i);
  71.  
  72.     if(!fileio::fileExists(fstr_ptr))
  73.       throw (const char*)gl_fstr->fmt("\"dat/tilesets/tileset_%u.qoi\" doesn't exist",i);
  74.  
  75.     gl_tilesets[i] = new Bitmap(fstr_ptr, gl_win);
  76.  
  77.   }
  78.  
  79.  
  80.  
  81.   gl_tileset_missing = new Bitmap("dat/tilesets/tileset_missing.qoi", gl_win);
  82.  
  83.  
  84.  
  85.   gl_frameTimer->wait(2000);
  86.  
  87.   gl_win->setVisibility(true);
  88.   gl_win->setFocus(true);
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95. void handleQuit(){
  96.   if(gl_frameTimer) gl_frameTimer->setTimer(0.05);
  97.  
  98.  
  99.  
  100.   if(gl_snd){
  101.     gl_snd->musicStop();
  102.     gl_snd->sfxStopAll();
  103.   }
  104.  
  105.   delete gl_snd_lock;
  106.  
  107.  
  108.  
  109.   delete gl_fstr;
  110.   delete gl_text;
  111.   delete gl_win;
  112.  
  113.  
  114.   delete gl_spritesheetPlayer;
  115.  
  116.  
  117.   for(u32 i=0; i<gl_tilesets_len; ++i) delete gl_tilesets[i];
  118.  
  119.  
  120.   delete gl_tileset_missing;
  121.  
  122.  
  123.   if(gl_frameTimer) gl_frameTimer->wait(2000);
  124.  
  125.   delete gl_frameTimer;
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. extern int gameMain(int argc, char** argv);
  133.  
  134.  
  135.  
  136. int main(int argc, char** argv){ try {
  137.   handleInit();
  138.  
  139.   int result = gameMain(argc, argv);
  140.  
  141.   handleQuit();
  142.  
  143.   printf("# OF CONTROLLED ALLOCATIONS = %llu\n",memory::getNumAllocations()-6);
  144.  
  145.   return result;
  146.  
  147.  
  148. } catch(const char* errorText){
  149. #ifdef _DEBUG
  150.   printf("FATAL EXCEPTION OCCURRED!: \"%s\"\n", errorText);
  151. #else
  152.   showMessageBox(errorText, "FATAL EXCEPTION OCCURRED! COMPLAIN TO THE DEV! (lol)", MSGBOX_ICN_ERROR);
  153. #endif /* _DEBUG */
  154.  
  155.   return -1;
  156.  
  157.  
  158. }}
  159.  
  160.  
  161.  
  162.  
  163.  
  164. f64 frand(){
  165.   u32 value = (rand()<<15) | rand();
  166.   return (f64)value/(KIT_U32_MAX>>2);
  167. }
  168.  
  169.  
  170.  
  171. f32 frandf(){
  172.   u32 value = (rand()<<15) | rand();
  173.   return (f32)value/(KIT_U32_MAX>>2);
  174. }
  175.  
  176.  
  177.  
  178.  
  179. f64 frandRange(f64 start, f64 maxDeviation){
  180.   return start + (frand()*2.0-1.0)*maxDeviation;
  181. }
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement