Advertisement
Kitomas

globals.cpp as of 2024-05-03

May 3rd, 2024
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 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. void dummy_objCallback(Object* obj_a){}
  13.  
  14.  
  15.  
  16.  
  17. char falseStr[] = "false";
  18. char trueStr[] = "true";
  19.  
  20. TimerSimple* gl_frameTimer        = nullptr;
  21. SoundEngine* gl_snd               = nullptr;
  22. MutexSimple* gl_snd_lock          = nullptr;
  23. Window*      gl_win               = nullptr;
  24. BitmapFont*  gl_text              = nullptr;
  25. FStr*        gl_fstr              = nullptr;
  26. Bitmap*      gl_spritesheetPlayer = nullptr;
  27.  
  28.  
  29.  
  30. //+1 for the length, since the first valid id is 1, not 0
  31. Scene            gl_scene;
  32. SceneDescriptor* gl_scenes[gl_scenes_len+1];
  33. Bitmap*          gl_backgrounds[gl_backgrounds_len+1];
  34. Bitmap*          gl_tileset_missing = nullptr;
  35. Bitmap*          gl_tilesets[gl_tilesets_len+1];
  36. kit::AudioData*  gl_ambience[gl_ambience_len+1];
  37. char             gl_music[gl_music_len+1][32] = { //file paths, relative to ./dat/music/
  38.   "(no change)",
  39.   "sine_half.mod",
  40. };
  41. Object_TickCallback gl_objCallbacks[gl_objCallbacks_len+1] = {
  42.   dummy_objCallback, //0 = no object
  43. };
  44.  
  45.  
  46.  
  47.  
  48.  
  49. void handleInit(){
  50.   gl_frameTimer = new TimerSimple;
  51.   gl_frameTimer->setTimer(0.101);
  52.  
  53.  
  54.   srand((u32)time::getTicks());
  55.  
  56.  
  57.   gl_snd = new SoundEngine(128);
  58.   gl_snd->streamStart();
  59.  
  60.   gl_snd_lock = new MutexSimple;
  61.  
  62.  
  63.  
  64.   gl_win = new Window(WINDOW_TITLE, CANVSIZ_X*2, CANVSIZ_Y*2,
  65.                       WINFLAG_RESIZABLE | WINFLAG_HIDDEN,
  66.                       WINPOS_CENTERED, WINPOS_CENTERED,
  67.                       CANVSIZ_X, CANVSIZ_Y, false);
  68.  
  69.   gl_text = new BitmapFont("dat/img/_font8x8.qoi", gl_win);
  70.  
  71.   gl_fstr = new FStr(4096);
  72.  
  73.   //gl_textf(0, 0, "loading...");
  74.   //gl_win->present();
  75.   //gl_win->setVisibility(true);
  76.  
  77.  
  78.  
  79.   gl_spritesheetPlayer = new Bitmap("dat/img/spritesheet_player.qoi", gl_win);
  80.  
  81.  
  82.  
  83.   //initialize asset arrays
  84.   memset0(gl_scenes); //(these memsets are probably redundant, but whatever)
  85.   memset0(gl_backgrounds);
  86.   memset0(gl_tilesets);
  87.   memset0(gl_ambience);
  88.   char* fstr_ptr = gl_fstr->ptr(); //valid until gl_fstr gets destroyed
  89.  
  90.  
  91.   //scenes
  92.   for(u32 i=1; i<(gl_scenes_len+1); ++i){
  93.     //(file check is done inside SceneDescriptor's constructor)
  94.     gl_scenes[i] = new SceneDescriptor(i);
  95.  
  96.   }
  97.  
  98.   //backgrounds
  99.   for(u32 i=1; i<(gl_backgrounds_len+1); ++i){
  100.     gl_fstr->fmt("dat/background/background_%u.qoi", i);
  101.     if(!fileio::fileExists(fstr_ptr))
  102.       throw (const char*)gl_fstr->fmt("\"dat/background/background_%u.qoi\" doesn't exist", i);
  103.     gl_backgrounds[i] = new Bitmap(fstr_ptr, gl_win);
  104.  
  105.   }
  106.  
  107.   //tilesets
  108.   for(u32 i=1; i<(gl_tilesets_len+1); ++i){
  109.     gl_fstr->fmt("dat/tileset/tileset_%u.qoi", i);
  110.     if(!fileio::fileExists(fstr_ptr))
  111.       throw (const char*)gl_fstr->fmt("\"dat/tileset/tileset_%u.qoi\" doesn't exist", i);
  112.     gl_tilesets[i] = new Bitmap(fstr_ptr, gl_win);
  113.  
  114.   }
  115.  
  116.   //ambience
  117.   for(u32 i=1; i<(gl_ambience_len+1); ++i){
  118.     gl_fstr->fmt("dat/ambience/ambience_%u.qoa", i);
  119.     if(!fileio::fileExists(fstr_ptr))
  120.       throw (const char*)gl_fstr->fmt("\"dat/ambience/ambience_%u.qoa\" doesn't exist", i);
  121.     gl_ambience[i] = new AudioData(fstr_ptr, gl_snd);
  122.  
  123.   }
  124.  
  125.  
  126.  
  127.   gl_tileset_missing = new Bitmap("dat/tileset/tileset_missing.qoi", gl_win);
  128.  
  129.  
  130.  
  131.   gl_frameTimer->wait(2000);
  132.  
  133.   gl_win->setVisibility(true);
  134.   gl_win->setFocus(true);
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141. void handleQuit(){
  142.   if(gl_frameTimer) gl_frameTimer->setTimer(0.05);
  143.  
  144.  
  145.  
  146.   if(gl_snd){ //tbd: make sure audio handler thread is killed!
  147.     gl_snd->musicStop();
  148.     gl_snd->sfxStopAll();
  149.   }
  150.  
  151.   delete gl_snd_lock;
  152.  
  153.  
  154.  
  155.   delete gl_fstr;
  156.   delete gl_text;
  157.   delete gl_win;
  158.  
  159.  
  160.   delete gl_spritesheetPlayer;
  161.  
  162.  
  163.   for(u32 i=1; i<(gl_scenes_len     +1); ++i) delete gl_scenes[i];
  164.   for(u32 i=1; i<(gl_backgrounds_len+1); ++i) delete gl_backgrounds[i];
  165.   for(u32 i=1; i<(gl_tilesets_len   +1); ++i) delete gl_tilesets[i];
  166.   for(u32 i=1; i<(gl_ambience_len   +1); ++i) delete gl_ambience[i];
  167.  
  168.  
  169.   delete gl_tileset_missing;
  170.  
  171.  
  172.   //objs is the only heap memory in gl_scene that is managed by loadScene.
  173.    //however, this memory is not freed by it, only reallocated if necessary...
  174.   memory::free(&gl_scene.objs); //...so it needs to be freed manually during uninit
  175.  
  176.  
  177.   if(gl_frameTimer) gl_frameTimer->wait(2000);
  178.  
  179.   delete gl_frameTimer;
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186. extern int gameMain(int argc, char** argv);
  187.  
  188.  
  189.  
  190. int main(int argc, char** argv){ try {
  191.   handleInit();
  192.  
  193.   int result = gameMain(argc, argv);
  194.  
  195.   handleQuit();
  196.  
  197.   printf("# OF CONTROLLED ALLOCATIONS = %llu\n",memory::getNumAllocations()-6);
  198.  
  199.   return result;
  200.  
  201.  
  202. } catch(const char* errorText){
  203. #ifdef _DEBUG
  204.   printf("FATAL EXCEPTION OCCURRED!: \"%s\"\n", errorText);
  205. #else
  206.   showMessageBox(errorText, "FATAL EXCEPTION OCCURRED! COMPLAIN TO THE DEV! (lol)", MSGBOX_ICN_ERROR);
  207. #endif /* _DEBUG */
  208.  
  209.   return -1;
  210.  
  211.  
  212. }}
  213.  
  214.  
  215.  
  216.  
  217.  
  218. f64 frand(){
  219.   u32 value = (rand()<<15) | rand();
  220.   return (f64)value/(KIT_U32_MAX>>2);
  221. }
  222.  
  223.  
  224.  
  225. f32 frandf(){
  226.   u32 value = (rand()<<15) | rand();
  227.   return (f32)value/(KIT_U32_MAX>>2);
  228. }
  229.  
  230.  
  231.  
  232.  
  233. f64 frandRange(f64 start, f64 maxDeviation){
  234.   return start + (frand()*2.0-1.0)*maxDeviation;
  235. }
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement