I have the following code for displaying respawning message for my character.It basically displays the strings : Respawing in ... 3,Respawing in ... 2,Respawing in ... 1 with 1 sec interval in between. Here's the code
but it fails with this error message:
/build/allegro5-F6iykb/allegro5-5.2.6.0/src/transformations.c:32: al_copy_transform: Assertion `src' failed.
Aborted (core dumped)
why so ...?
I don't see anything wrong with the code in the function you posted.
Have you changed the projection transform at all?
It's complaining about a null src transform, and from the code you showed me I don't see where it would be null.
EDIT
I see you must be using this code from outside the function. The local_trans will go out of scope. Same with the local_font. Declare them outside the function if you're calling it from your game loop.
All right, solved!!! I extended its scope as you said and It worked as expected. Thankyou😄
Congrats. Also, don't forget to destroy fonts when you are done with them.
Happy coding!
The code worked fine for a few "respawns" but after that I noticed that a bug has found its way in the code for the function respawn_wait() which I modified a little as the previous was a little off( the al_draw_text was meant to be in the "if(event=timer)" code block. Anyways the bug that has arisen causes my game to freeze completely when the character dies,and this one is very hard to trace as it does not happen everytime... and I know this is caused by respawn_wait() as without it, the game works just fine. Here's respawn_wait() v2.0:
I'm asking here as I thought this might be an issue relating to ALLEGRO EVENTS and I'm struggling to understand the documentation,and to be frank the documentation is not that good for beginners.
Just a quick note, as it is called by the main game loop, which also contains an invocation of al_wait_for_event() and acts based on the event registered ( eg. if the event is timer event, it triggers drawing operation,if event is keyboard key it moves the character around or escapes to the menu).
Do you mind showing more code? Timer events may be piling up in the exit of the function, causing a lag in main and the subsequent freeze of the game.
Just show me the main loop. You could try pausing the timer until you return, or clearing the event queue.
Are you using the official docs on liballeg.org? What do you feel is lacking? We can always edit them to be more clear based on your feedback.
Ok here's the main loop, there are very little comments on how everything works, but not that hard to understand, since I am a beginner
PS : About the game...its actually a really amateur clone of the legendary arcade game "blasteroids"
Ok. Here goes.
Since you're still a beginner, I recommend serial programming instead of parallel programming, but since you're already in the deep end, I'll take a guess and say that you're stuck in a classic deadlocked thread scenario.
Without seeing the functions run by your threads, I can only guess at what is wrong.
You have 4 threads (or more) running in parallel counting main. If you're not careful, you'll end up locking one thread that depends on another that is already locked that depends on the first. This is a deadlock.
Deadlocks are also known as The Dining Philosopher's problem. You can read more about it on wikipedia.
Are you familiar with debugging tools? When your game freezes, break execution and see which threads are where. You should easily be able to see which thread is waiting on which.
Other than that, try serial programming or show the other functions that are running concurrently to main. Blasteroids is a relatively simple program and can easily be done without the use of threads.
Hope this helps,
Edgar
I knew what a deadlock was, I am not that much of a beginner, but I didn't see one in my threads, maybe I glossed over something, so here are all the thread functions:
first one ( handle_blast(), checks whether any of the blast hit any of the asteroids)
..second one (collider() handles collision b/w spaceship and asteroids)
... and the last(generate_new_asteroids(), generates new asteroids when the count falls below 3 )
I am using threads to speed up my game as I am not using sprite animation, but actual math and physics for animating the asteroids and collision b/w them which is computationally intensive and thus to make my game playable(fast enough), I divided the work among threads.
Just a quick note: The game works just fine after I comment out the respawn_wait() function
To be honest, you could have 100s of asteroids and 100s of blasts and still not need threads. All they do is complicate things, and most the time they are unnecessary.
That said, your handle_blast function has a mismatched lock / unlock. They are in different braces. So lock is only called when not respawning, but unlock is always called. This is probably UB.
Second, your generate new asteroids function is not protected by a mutex. This can lead to a race condition in your asteroid list.
Another thing, you can only draw to the display that is current for the calling thread. This means your respawn wait function is drawing to a display that is not current.
Honestly, you're making this much harder than it needs to be. The A5teroids demo included with allegro is probably not threaded. You could look to it for inspiration if you like. It's called cosmic protector.
Other than that, the threading seems clean, but there is a potential for deadlocks when modifying asteroids since more than one thread changes the asteroid list.
General things to think about when threading : Make your critical sections as small and as fast as possible. Avoid race conditions by specifying which data each thread will modify.
Again, a debugger would probably solve this quickly.
Happy hunting.
Edgar
I don't think that if I had 100 asteroids and 100 blasts on screen, my game would remain playable without threads, as currently, even with the use of threads and just 8 asteroids on the screen , my game sucks😅(lags terribly). Also please look again carefully at the handle blast function, the mutex unlock falls within the brace of if(! Respawn) condition. Further, if you take a look at the generate new asteroids function, you will see that I have deferred actually changing the as_null list till the last second line of the function, and all the threads that read the list of asteroids will not be affected by any other thread that just increases the number of asteroids as they will(for only a short amount of time till the number of asteroids on screen variable is incremented) just read the shorter version of list and that will not cause SIGSEGV fault as the memory being accessed is completely reliable..... and as for the debugging tools I don't know how to debug deadlocks using GDB😅 but if you can suggest some better debugger then I would really appreciate it😊
[SOLVED!!!]
The bug was in this condition in the main loop coupled by the racing collider thread that set the game state to respawning
but if it set respawning before the if(!respawning) condition then the respawn_wait function will never be called as it is enclosed by a condition block in which one of the condition is that the redraw variable be set to true
.. ....which is only done by the if(event=timer) condition block
and that block will never get executed if we don't wait for event and thus the game is stuck in a while(1) like state thus frozen forever. Man now I see how peculiar deadlock bugs can get:o:o
I misread handle_blast. You are correct. The poor indentation makes it hard to read properly.
What kind of collision checks are you doing? Circle overlap? Pixel perfect? A 2 GHZ processor could handle millions of checks per second and not be stressed. What is your target platform?
About GDB, when your program freezes, hit CTRL-C inside GDB and then do :
thread apply all bt
It will show you a backtrace for every thread in existence. One thread is bound to be stuck on pthread_mutex_lock, and if deadlocked another thread will be inside a different mutex lock.
Would you mind packaging a zip of your sources and resources for me? I can debug it much more effectively that way.
No need, I am saying that I solved the bug, please read the [SOLVED] part that I have added later, and regarding collision checks... to check between two asteroids I am using circle overlap, because the shape of asteroids is mostly circular and for the collision between spaceship(which is essentially triangular) and asteroid, I am performing a completely different chicanery, involving use of Pythagoras theorem and distance between a point and a line, that is something only I can read and understand😅😅, but it is working like a treat!!! And the platform on which I am currently running the game is an Ubuntu guest OS(alloc 6gb ram and both the cores) running on Windows (Intel i3 6100U 12 gb ram, 2.3GHz clock speed dual core) as host and VirtualBox manager. Although in future I would like to port it to different OS and architectures
Well, I'm glad you solved it. Hopefully I was of some help.
Yeah you were a great guiding hand for a beginner like me. Thanks a lot😄, without your help, I would have felt so lost😅
Thanks a lot😄, without your help, I would have felt so lost😅
Search his comment history, he's been doing this for years.
Yeah, a real life saver👏👏👍👍
Didn't you guys know, my only porpoise in life is to help teh newbs.
{"name":"613024","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/f\/ff2ef87e297798db70f09f8d3e782a4f.png","w":457,"h":429,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/f\/ff2ef87e297798db70f09f8d3e782a4f"}