Why are the enemies not appearing on the screen?

I got the cout to work by putting the lives if outside the game state if. Now I will investigate running the high score screen

Let me explain:

--with state machines, you must act with caution, please, this code:

while(!quit)
{
  myGameState = GAMESCREEN;
  //std::cout<<"state = "<<myGameState<<std::endl;

  if(lives <=0)
  {
   myGameState=HIGHSCORESCREEN;
   //std::cout<<"myGameState = " <<myGameState<<std::endl;
  }

it’s not good!

You should put the if(lives<=0) inside the if(state == GAMESCREEN)! Why this? Cause it give a lot of order to the code! The if(lives<=0) is in truth one event which occours only on the game screen!

So, to clarify:

while(!quit)
{
  if (myGameState is GAMESCREEN) 
  {
    here you run all the logic from the game running!
    if (lives <= 0)
    {
      this if only run inside the gamescreen! never at the highscores
      If this "if" is true, you must change to the highscore screen, but how?
      1--if you needs, restart the values of higscore
      2--so, just set the value of myGameState to?
    }
  }
  else if (myGameState is HIGHSCORESCREEN)
  {
    if the game press ctrl, what I should do WHEN at in the highscore?
    answer: you just need to check if the player pressed the key crtl!
    So, how you do this? The same way as the sdl2 run the event loop!
    here you also should run the draw() from high scores and 
    a new highScoreLogic().
    --what happens on highscore, come into the "if(state is highscore)"!
    --but, if the player is here, what do I should do to change to the game screen?
    Answer:
    1--check if the player pressed ctrl in the event loop inside this if
    2--if pressed ctl, inside the if, restart the stage
    3--set the value of myGameState to the... Which one?
  }
}

You can see, when the myGameState changes its values, so the flow of the entire game changes!

So state machines just uses if/else ladder to change the flow of the entire logic! It’s like each if was a entire new game logic, splitted at if/else areas! The events are just if’s which change this flow, but the events are always inside the top if/else states! At each states you can have so many event ifs, this event ifs are the who will define the value of the myGameState, and in the next iteration, the if(myGameState == CURReNT_STATE) will run the logic to the CURRENT_STATE.

Just for review:

1–one variable controls the state

2–all states are a if/else ladder and this ladder comes inside the main loop.

3–all states have “ifs” inside them to change the value of the state variable at some point when they are true.

4–you can go from one state to another just by events whose are true!

You are near to understand it, just need some time thinking and you make it!

I am trying to build this the way you suggester but I get a segfault when I put the lives logic under the gameState logic. If I put it above it runs. What should I do?

I have it partially running now but I get a segfault when changing to HIGHSCORESCREEN

I am getting different results with rearranging the logic now I have both states running. Please bear with me.

Now g++ is not making an output file. I think my system may be overheating. I will have to leave it for a day before trying again.

I succeeded. It is now running, displaying high scores and changing states, the only thing now is it says &03 in the top right corner. Is tat right? how do I fix it?

Thanks.

That happens because the drawText() function is being called with the format parameter with wrong pattern!

See at the source of drawText() the parameter format is being passed to the vsprintf() and it will fill the drawTextBuffer with the “args” variable. So, in C, all the functions which works like printf have the pattern to parameters inside format like “%d” to int, “%s” to string, “%c” to char, “%f” to float and so on.

If you look at the pattern of format at the moment you call the drawText(), you are using the “&03d” to vsprintf a int inside the drawTextBuffer. See it? Look at the bold pattern of format which I wrote above. What did you made different?

Other way:

go to the source of drawHud(), compare all the 3 drawText() calls, at the “format” parameter: what is different between the three? What is wrong in one which is corret in other?

The correct calling is just the line 994! Compare with the others 2.

ok now I am trying to add a third gamestate for entering a highscore but when I press ctrl the gamestate does not change, please show me why

thanks

Now I have it starting when I press ctrl but now the enter high score screen is not working. Please advise

Now I am trying to test ENTER HIGHSCORESCREEN game state by changing it in function declarations.h I get a segfault when I change game state. Can you tell me why? Thanks

I also changed newHichscore to 014 to try and trigger the enter HIGHSCORESCREEN

How can I test ENTER HIGHSCORESCREEN is working?

I keep trying to set the value of newHighscore to 014 but I get a segfault every time.

I added some cout to the game state and I can tell it never enters into ENTER HIGHSCORESCREEN

I now managed to have it changing state based on a highscore being achieved but it has a segfault as soon as it enters the ENTERHIGHSCORESCREEN gamestate. Can you help me find out why?

gdb reports:

Thread 1 “a.out” received signal SIGSEGV, Segmentation fault.
__strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
Download failed: Invalid argument. Continuing without source file ./string/../sysdeps/x86_64/multiarch/strlen-avx2.S.
74 ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
(gdb)

Run bt inside gdb to get a stack trace of the crash.

#0 __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74

Is that all it outputs? Normally you would be able to see the whole call stack, so that you can see where the strlen function was called from.

Make sure you compiled your program with debug symbols turned on (with GCC/Clang it’s the -g flag). Turning off compiler optimizations might also make things more understandable (assuming the crash does still happen).