Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <globals.hpp>
- #include <tile.hpp>
- #include <stdio.h>
- #include <stdlib.h>
- using namespace kit;
- void dummy_objCallback(Object* obj_a){}
- char falseStr[] = "false";
- char trueStr[] = "true";
- TimerSimple* gl_frameTimer = nullptr;
- SoundEngine* gl_snd = nullptr;
- MutexSimple* gl_snd_lock = nullptr;
- Window* gl_win = nullptr;
- BitmapFont* gl_text = nullptr;
- FStr* gl_fstr = nullptr;
- Bitmap* gl_spritesheetPlayer = nullptr;
- //+1 for the length, since the first valid id is 1, not 0
- Scene gl_scene;
- SceneDescriptor* gl_scenes[gl_scenes_len+1];
- Bitmap* gl_backgrounds[gl_backgrounds_len+1];
- Bitmap* gl_tileset_missing = nullptr;
- Bitmap* gl_tilesets[gl_tilesets_len+1];
- kit::AudioData* gl_ambience[gl_ambience_len+1];
- char gl_music[gl_music_len+1][32] = { //file paths, relative to ./dat/music/
- "(no change)",
- "sine_half.mod",
- };
- Object_TickCallback gl_objCallbacks[gl_objCallbacks_len+1] = {
- dummy_objCallback, //0 = no object
- };
- void handleInit(){
- gl_frameTimer = new TimerSimple;
- gl_frameTimer->setTimer(0.101);
- srand((u32)time::getTicks());
- gl_snd = new SoundEngine(128);
- gl_snd->streamStart();
- gl_snd_lock = new MutexSimple;
- gl_win = new Window(WINDOW_TITLE, CANVSIZ_X*2, CANVSIZ_Y*2,
- WINFLAG_RESIZABLE | WINFLAG_HIDDEN,
- WINPOS_CENTERED, WINPOS_CENTERED,
- CANVSIZ_X, CANVSIZ_Y, false);
- gl_text = new BitmapFont("dat/img/_font8x8.qoi", gl_win);
- gl_fstr = new FStr(4096);
- //gl_textf(0, 0, "loading...");
- //gl_win->present();
- //gl_win->setVisibility(true);
- gl_spritesheetPlayer = new Bitmap("dat/img/spritesheet_player.qoi", gl_win);
- //initialize asset arrays
- memset0(gl_scenes); //(these memsets are probably redundant, but whatever)
- memset0(gl_backgrounds);
- memset0(gl_tilesets);
- memset0(gl_ambience);
- char* fstr_ptr = gl_fstr->ptr(); //valid until gl_fstr gets destroyed
- //scenes
- for(u32 i=1; i<(gl_scenes_len+1); ++i){
- //(file check is done inside SceneDescriptor's constructor)
- gl_scenes[i] = new SceneDescriptor(i);
- }
- //backgrounds
- for(u32 i=1; i<(gl_backgrounds_len+1); ++i){
- gl_fstr->fmt("dat/background/background_%u.qoi", i);
- if(!fileio::fileExists(fstr_ptr))
- throw (const char*)gl_fstr->fmt("\"dat/background/background_%u.qoi\" doesn't exist", i);
- gl_backgrounds[i] = new Bitmap(fstr_ptr, gl_win);
- }
- //tilesets
- for(u32 i=1; i<(gl_tilesets_len+1); ++i){
- gl_fstr->fmt("dat/tileset/tileset_%u.qoi", i);
- if(!fileio::fileExists(fstr_ptr))
- throw (const char*)gl_fstr->fmt("\"dat/tileset/tileset_%u.qoi\" doesn't exist", i);
- gl_tilesets[i] = new Bitmap(fstr_ptr, gl_win);
- }
- //ambience
- for(u32 i=1; i<(gl_ambience_len+1); ++i){
- gl_fstr->fmt("dat/ambience/ambience_%u.qoa", i);
- if(!fileio::fileExists(fstr_ptr))
- throw (const char*)gl_fstr->fmt("\"dat/ambience/ambience_%u.qoa\" doesn't exist", i);
- gl_ambience[i] = new AudioData(fstr_ptr, gl_snd);
- }
- gl_tileset_missing = new Bitmap("dat/tileset/tileset_missing.qoi", gl_win);
- gl_frameTimer->wait(2000);
- gl_win->setVisibility(true);
- gl_win->setFocus(true);
- }
- void handleQuit(){
- if(gl_frameTimer) gl_frameTimer->setTimer(0.05);
- if(gl_snd){ //tbd: make sure audio handler thread is killed!
- gl_snd->musicStop();
- gl_snd->sfxStopAll();
- }
- delete gl_snd_lock;
- delete gl_fstr;
- delete gl_text;
- delete gl_win;
- delete gl_spritesheetPlayer;
- for(u32 i=1; i<(gl_scenes_len +1); ++i) delete gl_scenes[i];
- for(u32 i=1; i<(gl_backgrounds_len+1); ++i) delete gl_backgrounds[i];
- for(u32 i=1; i<(gl_tilesets_len +1); ++i) delete gl_tilesets[i];
- for(u32 i=1; i<(gl_ambience_len +1); ++i) delete gl_ambience[i];
- delete gl_tileset_missing;
- //objs is the only heap memory in gl_scene that is managed by loadScene.
- //however, this memory is not freed by it, only reallocated if necessary...
- memory::free(&gl_scene.objs); //...so it needs to be freed manually during uninit
- if(gl_frameTimer) gl_frameTimer->wait(2000);
- delete gl_frameTimer;
- }
- extern int gameMain(int argc, char** argv);
- int main(int argc, char** argv){ try {
- handleInit();
- int result = gameMain(argc, argv);
- handleQuit();
- printf("# OF CONTROLLED ALLOCATIONS = %llu\n",memory::getNumAllocations()-6);
- return result;
- } catch(const char* errorText){
- #ifdef _DEBUG
- printf("FATAL EXCEPTION OCCURRED!: \"%s\"\n", errorText);
- #else
- showMessageBox(errorText, "FATAL EXCEPTION OCCURRED! COMPLAIN TO THE DEV! (lol)", MSGBOX_ICN_ERROR);
- #endif /* _DEBUG */
- return -1;
- }}
- f64 frand(){
- u32 value = (rand()<<15) | rand();
- return (f64)value/(KIT_U32_MAX>>2);
- }
- f32 frandf(){
- u32 value = (rand()<<15) | rand();
- return (f32)value/(KIT_U32_MAX>>2);
- }
- f64 frandRange(f64 start, f64 maxDeviation){
- return start + (frand()*2.0-1.0)*maxDeviation;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement