int main(int argc, char **argv) { const std::string resPath = getResourcePath("Final_Project"); if (SDL_Init(SDL_INIT_EVERYTHING) != 0){ std::ostringstream debugMsg; debugMsg << "SDL_Init Error: " << SDL_GetError() << std::endl; OutputDebugString(debugMsg.str().c_str()); std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; return 1; } if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) != IMG_INIT_PNG){ logSDLError(std::cout, "IMG_Init"); SDL_Quit(); return 1; } SDL_Window *window = SDL_CreateWindow("Sprite Demo", 800, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == nullptr){ logSDLError(std::cout, "CreateWindow"); SDL_Quit(); return 1; } SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (renderer == nullptr){ logSDLError(std::cout, "CreateRenderer"); cleanup(window); SDL_Quit(); return 1; } // background is from "http://www.snesmaps.com/maps/SuperMarioWorld/SuperMarioWorldMap10BG.html" SDL_Texture *background = loadTexture(resPath + "Background.png", renderer); //Make sure all is well if (background == nullptr){ cleanup(background, renderer, window); IMG_Quit(); SDL_Quit(); return 1; } SDL_Texture *spritesheet = loadTexture(resPath + "boo.png", renderer); // spritesheet is from "http://www.spriters-resource.com/custom_edited/mario/sheet/17842/" if (spritesheet == nullptr){ cleanup(spritesheet, renderer, window); IMG_Quit(); SDL_Quit(); return 1; } AudioHandler audiohandler = AudioHandler(); audiohandler.setMusic((resPath + "m_house.mp3"), true); audiohandler.play(0, 0); //Check if SDL_mixer is initialized if (!audiohandler.initialized()) { logSDLError(std::cout, "Mix_OpenAudio"); SDL_Quit(); return 1; } Sprite* spriteBG = new Sprite(SCREEN_WIDTH, SCREEN_HEIGHT, renderer, &audiohandler); spriteBG->setPos(0, 0); int bgFrame = spriteBG->createGraFrame(background, 0, 0); //Return SFX index int booSoundIndex = audiohandler.addSFX(-1, (resPath + "c_boo.wav")); int notUsed = audiohandler.addSFX(-1, (resPath + "c_high.wav")); if (((audiohandler.checkAudio(booSoundIndex)) && (!audiohandler.checkAudio(0)))) { cleanup(spritesheet, renderer, window); IMG_Quit(); SDL_Quit(); return 1; } // Creating Boo sprite Sprite* boo = new Sprite(28, 26, renderer, &audiohandler); // Just some vectors to simplify initialization of frames and sequences std::vector<int> x_coords = std::vector<int>{1, 31, 61, 31}; std::vector<std::string> directions = std::vector<std::string>{"down", "left", "up", "right"}; std::vector<int> y_coords = std::vector<int>{1, 29, 57, 85}; std::vector<int> audios = std::vector<int>{-1, booSoundIndex, -1, -1}; std::vector<int> intervals = std::vector<int>{50, 20, 17, 20}; std::map<std::string, std::vector<int>> graFrameMap; // createGraFrames() for (int y = 0; y < y_coords.size(); y++) { for (int x = 0; x < x_coords.size(); x++) { int graIndex = boo->createGraFrame(spritesheet, x_coords[x], y_coords[y]); graFrameMap[directions[y]].push_back(graIndex); } } // createSeqFrames for (int d = 0; d < directions.size(); d++) { for (int i = 0; i < intervals.size(); i++) { int frameIndex = boo->createSeqFrame(graFrameMap[directions[d]][i], audios[i], intervals[i]); boo->addFrameToSequence(directions[d], frameIndex); } } boo->setPos(114, 26); SDL_Event e; bool quit = false; std::string spriteDirection = "down"; while (!quit) { while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { quit = true; } if (e.type == SDL_KEYDOWN) { switch (e.key.keysym.sym) { case SDLK_1: audiohandler.setMusic((resPath + "m_chase.mp3"), true); audiohandler.play(0, 0); break; case SDLK_2: audiohandler.setMusic((resPath + "m_merry.mp3"), true); audiohandler.play(0, 0); break; case SDLK_3: audiohandler.setMusic((resPath + "m_fat.mp3"), true); audiohandler.play(0, 0); break; case SDLK_4: audiohandler.setMusic((resPath + "m_house.mp3"), true); audiohandler.play(0, 0); break; case SDLK_5: audiohandler.setMusic((resPath + "m_beat.wav"), true); audiohandler.play(0, 0); break; case SDLK_m: audiohandler.setMuting(!audiohandler.muted()); break; case SDLK_b: boo->setMuting(!boo->muted()); break; case SDLK_p: if (!audiohandler.playing()) { audiohandler.play(0, 0); } break; case SDLK_s: audiohandler.stop(); break; case SDLK_r: audiohandler.switch_pause_resume(); break; case SDLK_RIGHT: boo->movex(5); spriteDirection = "right"; break; case SDLK_LEFT: boo->movex(-5); spriteDirection = "left"; break; case SDLK_UP: boo->movey(-5); spriteDirection = "up"; break; case SDLK_DOWN: boo->movey(5); spriteDirection = "down"; break; } } } //Render the scene SDL_RenderClear(renderer); spriteBG->show(bgFrame); boo->show(spriteDirection.c_str()); SDL_RenderPresent(renderer); } cleanup(background, spritesheet, renderer, window); IMG_Quit(); SDL_Quit(); delete boo; delete spriteBG; //getchar(); return 0; }
//***************************************************************************** // // Handles the SysTick timeout interrupt. // //***************************************************************************** void SysTickIntHandler(void) { unsigned long ulData, ulDelta; // // Increment the tick count. // g_ulTickCount++; // // Indicate that a timer interrupt has occurred. // HWREGBITW(&g_ulFlags, FLAG_CLOCK_TICK) = 1; // // Increment the screen update count. // g_ucScreenUpdateCount++; // // See if 1/30th of a second has passed since the last screen update. // if(g_ucScreenUpdateCount == (CLOCK_RATE / 30)) { // // Restart the screen update count. // g_ucScreenUpdateCount = 0; // // Request a screen update. // HWREGBITW(&g_ulFlags, FLAG_UPDATE) = 1; } // // Update the music/sound effects. // AudioHandler(); // // Increment the application update count. // g_ucAppUpdateCount++; // // See if 1/100th of a second has passed since the last application update. // if(g_ucAppUpdateCount != (CLOCK_RATE / 100)) { // // Return without doing any further processing. // return; } // // Restart the application update count. // g_ucAppUpdateCount = 0; // // Run the Ethernet handler. // EnetTick(10); // // Read the state of the push buttons. // ulData = (GPIOPinRead(GPIO_PORTE_BASE, (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3)) | (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1) << 3)); // // Determine the switches that are at a different state than the debounced // state. // ulDelta = ulData ^ g_ucSwitches; // // Increment the clocks by one. // g_ucSwitchClockA ^= g_ucSwitchClockB; g_ucSwitchClockB = ~g_ucSwitchClockB; // // Reset the clocks corresponding to switches that have not changed state. // g_ucSwitchClockA &= ulDelta; g_ucSwitchClockB &= ulDelta; // // Get the new debounced switch state. // g_ucSwitches &= g_ucSwitchClockA | g_ucSwitchClockB; g_ucSwitches |= (~(g_ucSwitchClockA | g_ucSwitchClockB)) & ulData; // // Determine the switches that just changed debounced state. // ulDelta ^= (g_ucSwitchClockA | g_ucSwitchClockB); // // See if any switches just changed debounced state. // if(ulDelta) { // // Add the current tick count to the entropy pool. // RandomAddEntropy(g_ulTickCount); } // // See if the select button was just pressed. // if((ulDelta & 0x10) && !(g_ucSwitches & 0x10)) { // // Set a flag to indicate that the select button was just pressed. // HWREGBITW(&g_ulFlags, FLAG_BUTTON_PRESS) = 1; } }