how to freeze and unfreeze the game loop? - c++

i have a game loop, i want to be able to make it temporary stop and then make it run again.
I TRIED to freeze and unfreeze the game by passing a boolean variable by reference to void functions, which their job is to set the variables value to true and false.
there are 3 functions in the game loop here is a pseudo code:
while(!quit)
if(!freeze):
[handle events()] -> [move()] ->[render()] (REPEAT)
void game_over(int *flag);
void restart_game(int *flag);
void game_over(int *flag)`
{
*flag = 0;
}
void restart_game(int *flag)
{
*flag = 1;
}
int main()
{
//freeze boolean flag initially false
int freeze = 0;
SDL_event e;
//game loop
int quit = 0;
while(!quit)
{
if(!freeze)
{
while(SDLPollEvent(&e) != 0)
{
switch(e.key.keysym.sym)
{
case SDLK_t:
game_over(&freeze);//passing freeze address
break;
case SDLK_r:
restart_game(&freeze);
break;
}
}
move();
render();
}
}
close();
return();
}
PROBLEM IS when i press r ( when the restart-game() is called) the program becomes so heavy that i can hardly close it (infinity loop) .
can anyone think of what needs to be fixed or what is perhaps a better approach?

Related

Error Message using SFEMP3Shield.h Library trying to implement play/pause code

I'm currently hitting a brick wall while trying to edit some code. I'm trying to input a pause/resume piece into the code, so that when the electrode is touched - if playing it will be paused and then consequently be able to be resumed by touching it again.
With my current edit of the code, the track isn't resuming once paused.
I'm learning as I go along so apologies, if it is an glaringly obvious fix. Any help will be greatly appreciated!
Link to library using is here:
https://github.com/mpflaga/Sparkfun-MP3-Player-Shield-Arduino-Library
See the code I'm working with below!
// compiler error handling
#include "Compiler_Errors.h"
// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4
// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <FreeStack.h>
#include <SFEMP3Shield.h>
// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;
// mp3 behaviour defines
#define REPLAY_MODE FALSE // By default, touching an electrode repeatedly will
// play the track again from the start each time.
//
// If you set this to FALSE, repeatedly touching an
// electrode will stop the track if it is already
// playing, or play it from the start if it is not.
// touch behaviour definitions
#define firstPin 0
#define lastPin 11
// sd card instantiation
SdFat sd;
void setup(){
Serial.begin(57600);
pinMode(LED_BUILTIN, OUTPUT);
//while (!Serial) ; {} //uncomment when using the serial monitor
Serial.println("Bare Conductive Touch MP3 player");
if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
if(!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
MPR121.setInterruptPin(MPR121_INT);
MPR121.setTouchThreshold(5);
MPR121.setReleaseThreshold(5);
result = MP3player.begin();
MP3player.setVolume(10,10);
if(result != 0) {
Serial.print("Error code: ");
Serial.print(result);
Serial.println(" when trying to start MP3 player");
}
}
void loop(){
readTouchInputs();
}
void readTouchInputs(){
if (MPR121.touchStatusChanged()) {
MPR121.updateTouchData();
// only make an action if we have one or fewer pins touched
// ignore multiple touches
if (MPR121.getNumTouches() <= 1) {
for (int i = 0; i < 12; i++) { // Check which electrodes were pressed
if (MPR121.isNewTouch(i)) {
//pin i was just touched
Serial.print("pin ");
Serial.print(i);
Serial.println(" was just touched");
digitalWrite(LED_BUILTIN, HIGH);
if (i <= lastPin && i >= firstPin) {
if (MP3player.isPlaying()) {
if (lastPlayed == i && !REPLAY_MODE) {
// if we're already playing the requested track, stop it
// (but only if we're not in REPLAY_MODE)
MP3player.pauseMusic();
Serial.print("paused_playback");
Serial.println(MP3player.getState());
}
else {
// if the track is already paused, resume the track
if (MP3player.getState() == paused_playback) {
if (lastPlayed == i) {
MP3player.resumeMusic();
Serial.print("resuming");
Serial.println(i-firstPin);
} else {
// if we're already playing a different track (or we're in
// REPLAY_MODE), stop and play the newly requested one
MP3player.stopTrack();
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);
// don't forget to update lastPlayed - without it we don't
// have a history
lastPlayed = i;
}
}
}
} else {
// if we're playing nothing, play the requested track
// and update lastplayed
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);
lastPlayed = i;
}
}
} else {
if (MPR121.isNewRelease(i)) {
Serial.print("pin ");
Serial.print(i);
Serial.println(" is no longer being touched");
digitalWrite(LED_BUILTIN, LOW);
}
}
}
}
}
}
Thanks Again!
edited code with last played included
// compiler error handling
#include "Compiler_Errors.h"
// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4
// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <FreeStack.h>
#include <SFEMP3Shield.h>
// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;
// mp3 behaviour defines
#define REPLAY_MODE FALSE // By default, touching an electrode repeatedly will
// play the track again from the start each time.
//
// If you set this to FALSE, repeatedly touching an
// electrode will stop the track if it is already
// playing, or play it from the start if it is not.
// touch behaviour definitions
#define firstPin 0
#define lastPin 11
enum Action {
DO_NOTHING,
PLAY,
PAUSE,
RESUME,
STOP_THEN_PLAY
};
Action nextAction(state_m state, int i) {
if (state == paused_playback && lastPlayed == i) {
return RESUME;
}
if (state != playback) {
return PLAY;
}
if (state == playback && lastPlayed == i){
return PAUSE;
}
if (state == playback) {
return STOP_THEN_PLAY;
}
return DO_NOTHING;
}
// sd card instantiation
SdFat sd;
void setup(){
Serial.begin(57600);
pinMode(LED_BUILTIN, OUTPUT);
//while (!Serial) ; {} //uncomment when using the serial monitor
Serial.println("Bare Conductive Touch MP3 player");
if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
if(!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
MPR121.setInterruptPin(MPR121_INT);
MPR121.setTouchThreshold(5);
MPR121.setReleaseThreshold(5);
result = MP3player.begin();
MP3player.setVolume(10,10);
if(result != 0) {
Serial.print("Error code: ");
Serial.print(result);
Serial.println(" when trying to start MP3 player");
}
}
void loop(){
readTouchInputs();
}
void readTouchInputs(){
if(MPR121.touchStatusChanged()){
MPR121.updateTouchData();
// only make an action if we have one or fewer pins touched
// ignore multiple touches
if(MPR121.getNumTouches()<=1){
for (int i = 0; i < 12; i++) { // Check which electrodes were pressed
if (MPR121.isNewTouch(i)) {
state = MP3player.getState();
Action action = nextAction(state, i); // find what to do next
switch (action) {
case PLAY:
Serial.println("play");
MP3player.playTrack(i-firstPin);
lastPlayed = i;
state = playback;
break;
case PAUSE:
Serial.println("pause");
MP3player.pauseMusic();
state = paused_playback;
lastPlayed = i;
break;
case RESUME:
Serial.println("resume");
MP3player.resumeMusic();
state = playback;
break;
case STOP_THEN_PLAY:
Serial.println("stop then play");
MP3player.stopTrack();
MP3player.playTrack(i-firstPin);
state = playback;
lastPlayed = i;
break;
default:
break;
}
}
}
}
}
}
Deeply nested if statements are very difficult to follow. I understand that you want to trigger one of four actions (play, pause, resume and stop/play) based on the state of MP3 player, last played track and electrode ID. I suggest you define a function like this to determine an action.
enum Action {
DO_NOTHING,
PLAY,
PAUSE,
RESUME,
STOP_THEN_PLAY
};
Action nextAction(state_m state, int i) {
if (state == paused_playback && lastPlayed == i) {
return RESUME;
}
if (state != playback) {
return PLAY;
}
if (state == playback && lastPlayed == i){
return PAUSE;
}
if (state == playback) {
return STOP_THEN_PLAY;
}
return DO_NOTHING;
}
Pass the state of MP3 player and the electrode's id to the function. You can then trigger an appropriate action based on the value of action.
for (int i = 0; i < 12; i++) { // Check which electrodes were pressed
if (MPR121.isNewTouch(i)) {
state_m state = MP3player.getState();
Action action = nextAction(state, i); // find what to do next
switch (action) {
case PLAY:
Serial.println("play");
MP3player.playTrack(i-firstPin);
lastPlayed = i;
state = playback;
break;
case PAUSE:
Serial.println("pause");
MP3player.pauseMusic();
state = paused_playback;
lastPlayed = i;
break;
case RESUME:
Serial.println("resume");
MP3player.resumeMusic();
state = playback;
break;
case STOP_THEN_PLAY:
Serial.println("stop then play");
MP3player.stopTrack();
MP3player.playTrack(i-firstPin);
state = playback;
lastPlayed = i;
break;
default:
break;
}
}
}

Enter and Exit menu using enum and switch sfml

In my game right now I am trying to make a menu you can access when you press 'q', but currently I am having some issues. I think it switches to the CompScreen view and then back to the currentroom view quickly, I may be wrong. I am getting the cout CompMenu, HELP, and hello readings so I know it is running through the programs, but when I press q I remain in the same spot, nothing happening.
EventManager.h
#ifndef EventManager_h
#define EventManager_h
#endif /* EventManager_h */
int windowWidth = 5000;//width of window
int windowHeight = 5000;//height of window
sf::View leveltwo(sf::FloatRect(x, y, 5000, 5000));
sf::View start(sf::FloatRect(0, 0, 2500, 1500));
sf::View ComputerScreen(sf::FloatRect(50000, 50000, 5000, 5000));
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight ), "Awesome Game" );
Character player("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Sprites/Player.png");
bool InMenu = false;
enum Levels{
StartRoom, LevelTwo
};
Levels room = StartRoom;
int currentroom;
void WhatRoom(int TheLevel){
switch (room){
case StartRoom:
currentroom = 1;
window.setView(start);
if (TheLevel == 2){
room = LevelTwo;
}
break;
case LevelTwo:
currentroom = 2;
window.setView(leveltwo);
break;
}
};
enum States{
compmenu, mainmenu, NoMenu
};
States menu = NoMenu;
void CompMenu(){
window.setView(ComputerScreen);
cout << "HELP";
InMenu = true;
}
void WhatMenu(int TheMenu){
switch (menu){
case compmenu:
cout << "CompMenu";
CompMenu();
break;
case mainmenu:
break;
case NoMenu:
if (TheMenu == 2){
menu = compmenu;
}
break;
if (TheMenu == 3){
menu = mainmenu;
}
break;
}
}
main.cpp (inside int main)
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q) and InMenu == false){
WhatMenu(2);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q) and InMenu == true){
InMenu = false;
WhatRoom(currentroom);
cout << "hello";
}
If you have any questions or need to see more of the code let me know. Thanks.
I think you've missed else after first if block so it may be executed immediately because InMenu may be changed to true at that point:
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q) and InMenu == true){
Also you should handle the situation when key is pressed for a long time (to react only when it enters pressed state) and get rid of all the global variables because they are already creating quite a mess.

SDL Text Input returning white squares

I created an event class and wanted to get text with it. Unfortunately the text isn't reading correctly...
I've set it up so that all text that is gotten is output to the command line when a new key is input, but it obviously isn't working.
Here's my "EventManager" code for my update function:
void EventManager::update(){
while(SDL_PollEvent(&e)){
switch(e.type){
case SDL_QUIT:
running = false;
break;
case SDL_MOUSEBUTTONDOWN:
mousePressed = true;
break;
case SDL_KEYDOWN:
if(shouldCollectText && e.key.keysym.sym == SDLK_BACKSPACE && currentCollectedText.length() > 0){
currentCollectedText.pop_back();
}else if(shouldCollectText && e.key.keysym.sym != SDLK_BACKSPACE){
currentCollectedText += e.text.text; //The problem
std::cout << currentCollectedText << std::endl;
}
}
}
}
I followed the Lazy Foo tutorials, and I can't find the problem.
Some other things to note:
I start text input in my "main.cpp" class:
int main( int argc, char *argv[] ) {
Game *game = new Game();
game->init();
SDL_StartTextInput();
while(game->isRunning()){
game->handleEvents();
game->update();
game->render();
}
game->close();
SDL_StopTextInput();
return 0;
}
I create some of my variables in "EventManager.h":
bool shouldCollectText;
std::string currentCollectedText;
And define them in an "init()" function:
shouldCollectText = false;
currentCollectedText = "";
Your problem is that you are using the wrong event type. Take a second look at Lazy Foo's tutorial and here:
TextInputEvent
e.text.text is reserved for Text Input events and nothing else. Being SDL_Event an union of structures, overlaps may occur in the used memory. That is exactly what you see in the terminal output.
Remove the SDL_KEYDOWN event and try this instead:
case SDL_TEXTINPUT:
currentCollectedText += e.text.text;
break;
I'm not sure about std::cout << currentCollectedText << std::endl; though. It depends on when you want to get output of the text content.
If you want to use SDL_KEYDOWN you will have to access the keysym tables and maybe calculate to which letter they correspond and print those out. I would say, that approach is more complicated for what you want to achieve.
So in conclusion e.text.text will only work if e.type has the SDL_TEXTINPUT value as event. With all the other settings in your code that event should be triggered and may get the text you type in.
So your complete example could look like this:
void EventManager::update(){
while(SDL_PollEvent(&e)){
switch(e.type){
case SDL_QUIT:
running = false;
break;
case SDL_MOUSEBUTTONDOWN:
mousePressed = true;
break;
case SDL_TEXTINPUT:
if(shouldCollectText && currentCollectedText.length() > 0){
currentCollectedText.pop_back();
}else if(shouldCollectText) {
currentCollectedText += e.text.text; //The problem
std::cout << currentCollectedText << std::endl;
}
break;
default: break;
}
}
}

Displaying from std::vector and handling events using SDL?

I am trying to make a custom app library with SDL as apart of an academic project, and I ran into an issue..
Basically, everything works fine, it compiles, does what I expect it to do, but... its extremely slow, the first element is reacting quite quickly, other elements within the set are completely unresponsive (i need to click them 20 times for the to react, they work just slow)
Below the function that draws the elements from a vector type container, the return 1 means that the handle function ran into an unexpected error or the user X'ed out the window.
Any advice on how to make this react faster?
void StoreDraw::setCamera(size_t input)
{
rectangle.x = containerArray[input].elementLocX;
rectangle.y = containerArray[input].elementLocY;
rectangle.h = containerArray[input].elementPicture->h;
rectangle.w = containerArray[input].elementPicture->w;
}
bool StoreDraw::vectorDraw()
{
/* Draw FloatingElements */
for(size_t i = 0; i < containerArray.size(); i++)
{
if(SDL_PollEvent(&event))//containerArray[i].event
{
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
locationX = event.button.x;
locationY = event.button.y;
printf("X:%d\tY:%d\n", locationX, locationY);
if(!containerArray[i].handleEvent(locationX, locationY)){drawEnvironment();}
}
}
if(event.type == SDL_QUIT)
{
return 1;
}
}
}
SDL_Flip(screen);
return 0;
}
bool StoreDraw::drawEnvironment()
{
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0,0,0));
SDL_BlitSurface(background, NULL, screen, NULL);
for(size_t i = 0; i < containerArray.size(); i++)
{
setCamera(i);
SDL_BlitSurface(containerArray[i].elementPicture, NULL, screen, &rectangle);
}
SDL_Flip(screen);
}
bool FloatingElement::handleEvent(int x, int y)
{
printf("Object responding.\n");
if((x > elementLocX) && (x < (elementLocX + (elementPicture->w))) && (y > elementLocY) && (y < (elementLocY + (elementPicture->h))))
{
x = (x - (elementPicture->w)/2);
y = (y - (elementPicture->h)/2);
setLocation(x,y);
printf("Object responding.\n");
return 0;
}
return 1;
}
As far as I can see, the problem is how frequently you are checking for events. When you call vectorDraw() it polls the events and acts as intended, but the events eventually end and the function returns. I think there should be a main loop in the code that always checks for events and calls functions when necessary, while vectorDraw() only checks the location of a click and calls the handleEvent() of the container array.
Edit:
I think I found the problem. This is how you're doing now:
bool StoreDraw::vectorDraw()
{
/* Draw FloatingElements */
for(size_t i = 0; i < containerArray.size(); i++)
{
if(SDL_PollEvent(&event))//containerArray[i].event
{
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
// Check Events
}
}
if(event.type == SDL_QUIT)
{
return 1;
}
}
}
SDL_Flip(screen);
return 0;
}
int main() {
while( ::quit == false ) {
while(SDL_PollEvent(&event) == 1) {
if( event.type != SDL_QUIT ) {
if( event.type == SDL_MOUSEBUTTONDOWN) {
storeDraw::vectorDraw();
}
} else {
quit = true;
break;
}
}
}
}
}
So, what is happening:
After the main loop polls the event, it calls vectorDraw() and vectorDraw() polls events again. By doing so, vectorDraw() doesn't get the information of the click it called, as it was already polled in the main loop. Without the information, it doesn't act upon it.
So, in order to solve the problem, you can change the functions to be somewhat like this:
bool StoreDraw::vectorDraw(SDL_Event event)
{
/* Draw FloatingElements */
for(size_t i = 0; i < containerArray.size(); i++)
{
// Check Events
}
SDL_Flip(screen);
return 0;
}
int main() {
while( ::quit == false ) {
while(SDL_PollEvent(&event) == 1) {
if( event.type != SDL_QUIT ) {
if( event.type == SDL_MOUSEBUTTONDOWN) {
if(event.button.button == SDL_BUTTON_LEFT)
{
storeDraw::vectorDraw(event);
}
}
} else {
quit = true;
break;
}
}
}
}
}
Now, only the main loop polls events and acts on them if possible. And vectorDraw() doesn't poll them anymore, only acts upon them if they meet it's criteria (left mouse button down, in this case). Now the event checking should act as intended.

FMOD - Unhandled Exception with no source code avaiable

I'm trying to get my audio track to play using FMOD but I keep getting an unhandled exception and then it says there's no source code available, and shows me disassembly code.
main.cpp
bool AudioProject::initAudio()
{
// Audio code
fmSound = new Sound();
fmSound->initialise();
fmSound->load("Music/Rocky_Theme_Tune.mp3");
fmSound->play();
return true;
}
I put break points in the see where it stopped, which was in the initialise function. It even goes into the initialise function and then just randomly breaks. I think I have every include file for fmod as I used it last year no problem.
I'll post my sound.h/.cpp files too.
.h
#include "stdafx.h"
#pragma once
#include "fmod.hpp"
#include "fmod.h"
class Sound
{
private:
bool on; //is sound on?
bool possible; //is it possible to play sound?
char * currentSound; //currently played sound
//FMOD-specific stuff
FMOD_RESULT result;
FMOD_SYSTEM * fmodsystem;
FMOD_SOUND * sound;
FMOD_CHANNEL * channel;
public:
Sound();
~Sound();
void initialise (void);
void setVolume (float v);
void load (const char * filename);
void unload (void);
void play (bool pause = false);
bool getSound (void);
void setPause (bool pause);
void setSound (bool sound);
void toggleSound (void);
void togglePause (void);
};
.cpp
#include "stdafx.h"
#include "Sound.h"
#include "fmod.h"
#include "fmod.hpp"
Sound::Sound()
{
on = true; //is sound on?
possible = true; //is it possible to play sound?
currentSound=""; //currently played sound
sound=0;
}
Sound::~Sound()
{
}
//initialises sound
void Sound::initialise (void)
{
//create the sound system. If fails, sound is set to impossible
result = FMOD_System_Create(&fmodsystem);
if (result != FMOD_OK)
possible = false;
//if initialise the sound system. If fails, sound is set to impossible
if (possible)
result = FMOD_System_Init(fmodsystem,2, FMOD_INIT_NORMAL, 0);
if (result != FMOD_OK)
possible = false;
//sets initial sound volume (mute)
if (possible)
FMOD_Channel_SetVolume(channel,1.0f);
}
//sets the actual playing sound's volume
void Sound::setVolume (float v)
{
if (possible && on && v >= 0.0f && v <= 1.0f)
{
FMOD_Channel_SetVolume(channel,v);
}
}
//loads a soundfile
void Sound::load (const char * filename)
{
currentSound = (char *)filename;
if (possible && on)
{
result = FMOD_Sound_Release(sound);
result = FMOD_System_CreateStream(fmodsystem,currentSound, FMOD_SOFTWARE, 0, &sound);
if (result != FMOD_OK)
possible = false;
}
}
//frees the sound object
void Sound::unload (void)
{
if (possible)
{
result = FMOD_Sound_Release(sound);
}
}
//plays a sound (no argument to leave pause as dafault)
void Sound::play (bool pause)
{
if (possible && on)
{
result = FMOD_System_PlaySound(fmodsystem,FMOD_CHANNEL_FREE, sound, pause, &channel);
FMOD_Channel_SetMode(channel,FMOD_LOOP_NORMAL);
}
}
//toggles sound on and off
void Sound::toggleSound (void)
{
on = !on;
if (on == true)
{
load(currentSound);
play();
}
if (on == false)
{
unload();
}
}
//pause or unpause the sound
void Sound::setPause (bool pause)
{
FMOD_Channel_SetPaused (channel, pause);
}
//turn sound on or off
void Sound::setSound (bool s)
{
on = s;
}
//toggle pause on and off
void Sound::togglePause (void)
{
FMOD_BOOL p;
FMOD_Channel_GetPaused(channel,&p);
FMOD_Channel_SetPaused (channel,!p);
}
//tells whether the sound is on or off
bool Sound::getSound (void)
{
return on;
}
Hit a brick wall here, anyone have any ideas?
You are calling FMOD_Channel_SetVolume(channel,1.0f) in initialise, but the channel variable isn't hasn't been initialized yet, it gets initialized by the FMOD_System_PlaySound(fmodsystem,FMOD_CHANNEL_FREE, sound, pause, &channel); in Sound::play