I am currently at the last stage of the Tetris Clone using SDL library, the only thing left is rotation. For each piece I have a separate class, and in order to move the pieces down I use a method:
This method is called like that: current->movePieceDown(); where current represents a pointer to the current piece. Now to rotate the piece I have another method:
New X and Y coordinates are calculated elsewhere, with x2 and y2 taken as the origin.
My question is how do I swap current->movePieceDown() with current->rotatePiece() while the piece is moving? Taking into acount that I am handling keyboard events in another method.
I would have 3 threads. One thread (call it the display thread) listens for events. When a downEvent event comes, it calls movePieceDown. When a rotateEvent event comes, it calls rotatePiece
The second thread would run a repeating timer. Every time the timer expires it sends a downEvent to the display thread. You can shorten the timer duration to speed up the pieces if you want.
The third thread would be your keyboard handler. When the rotate key is pressed, the handler will send a rotateEvent to the display thread.
Edit: I guess you don't really need a separate thread for a timer and a callback function, but you can see that the functionality is separate from the display thread.
new values are set to 0 initially, and every time a user presses a button on the keyboard this method gets called, it calculates the new rotation in new and assigns it to x and y values. Which gives you a 90 degree counter clockwise rotation.