So from what I can figure the shaking graphics (only characters are shaking, the background tiles are not) comes from the fact that the main thread is rendering the characters while the additional thread is still processing the characters coordinates when they move. So I have no choice but to include the procedure in the main thread instead. I will find some other use for the additional thread, for example quicksaves without interrupting gameplay. I was thinking I could also use an additional thread for the calculations relating to combat in the game. Then have the main thread render the combat graphics separately but I will have to only process variables that have no impact on the graphics at all. Like how much hp a target loses and what status effects are applied but that's so low in computational demands that I'm starting to wonder why have multithreading at all except for quicksaves. Sound effects maybe.
I'm using:
uses sdl2, sdl2_image, sdl2_ttf, sdl2_mixer, sdl_gfx, crt,
{$ifdef unix}
cthreads,
{$endif}
Classes;
I have my own implementation of vsync in the main thread in the main loop:
var
fpscapstate:boolean; { =true: cap fps }
selfps:byte;
fpstick:uint32;
selfps:=60;
if fpscapstate=true then fpstick:=sdl_getticks;
{ code: calculations, keyboard, mouse and controller input, etc }
{ render graphics }
if fpscapstate=true then
begin
fpstick:=sdl_getticks-fpstick;
if fpstick<1000/selfps then sdl_delay(round((1000/selfps)-fpstick));
end;
I'm not familiar with programming for buffers except maybe when I was using sdl 1. But in sdl 2 now I am rendering all graphics using hardware. I only use surfaces to load bmp files (outside of the main loop) and then load those surfaces into textures only because sdl 2 does not offer any other solution I know of to apply a color key to a texture loaded from the hard drive. I understand the idea behind using two buffers but it feels like a waste to me when I could be making the game only render (sdl_renderpresent) when everything is ready. Which I tried to do already. I know about that saying from the Lazyfoo tutorials. I'm not trying to be like professionals. I did mention earlier in the post that multithreading could be too advanced for me. But I have been programming for 21 years and with Pascal since 2004. I've never studied computer programming. I learned all on my own. I actually work in Accounts
. I tried following free pascal multithreading tutorials but they seem very poor to me. Despite reading them over and over, I can't understand all of it because there are a lot of assumptions being made in the tutorial regarding prerequisite knowledge. Anyway, I have multithreading working. It's just that I need to figure out how to restructure my code in order to avoid that shaking effect.