Forums > Off-topic > Jatte programming help please. Thread Synchronization
Auios
Newborn
Joined 4880 days ago
Last seen 2157 days ago
Jatte programming help please. Thread Synchronization
Posted 4207 days ago
Im working on a project in freebasic and it's just a fun little toy project. But I was wondering if in FreeBasic you can do Thread Synchronization. I want multiple blocks of code to run at the same time.

What is really happening:
Ex: My guy fires a bullet. My guy cannot move anymore because the program is doing the math for the bullet and moving it the correct direction.

Because the bullet was fired a loop to keep updating the new location of the bullet has been set off. While this loop is running nothing else in the program can work until the bullet destroys itself by hitting a wall or moving too far away.

What I want to happen:
The bullet fires and the bullet moves away from the player AND the guy can also move. The program would have 2 loops running at the same time.

one loop for the bullet's location.
one loop for the player's location and everything else he does.

Thread Synchronization
===================================
Oh and also I know that this is not the freebasic forums nor any other official forums, but I wanted to get advice from you if possible.

I found something related. But I was wondering if you can dumb it down for me
http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgThreadCreate

And here's my code:
Auios
#include once ''fbgfx.bi''
using fb
randomize timer

dim as integer scrnx,scrny
screenres 1600,900,8,2,1
'screenres 1024,768,8,2,1
'screenres 950,600,8,2,1
'screenres 800,600,8,2,1
'screenres 640,480,8,2,1
screeninfo scrnx,scrny

sub north(byval x as integer,byref y as integer)
dim as integer col = point(x,y-1)

if y-1 > 0 and col = 0 then
y-=1
end if
end sub

sub south(byval x as integer, byref y as integer, byval max as integer)
dim as integer col = point(x,y+1)

if y+1 > 0 and col = 0 and y+1 < max then
y+=1
end if
end sub

sub west(byref x as integer,byval y as integer)
dim as integer col = point(x-1,y)

if x-1 > 0 and col = 0 then
x-=1
end if
end sub

sub east(byref x as integer, byval y as integer, byval max as integer)
dim as integer col = point(x+1,y)

if x+1 > 0 and col = 0 and x+1 < max then
x+=1
end if
end sub sub subup(byval x as integer,byval y as integer,byref carry as integer,byref carrycol as integer)
dim as integer col
y-=1

col=point(x,y)
While multikey(sc_up) = -1: Wend

select case carry
case 0
if col > 0 then
carry=1
carrycol=col
pset(x,y),0
end if
exit select
case 1
if col = 0 then
carry=0
pset(x,y),carrycol
end if
exit select
end select
end sub

sub subdown(byval x as integer,byval y as integer,byref carry as integer,byref carrycol as integer)
dim as integer col
y+=1

col=point(x,y)
While multikey(sc_down) = -1: Wend

select case carry
case 0
if col > 0 then
carry=1
carrycol=col
pset(x,y),0
end if
exit select
case 1
if col = 0 then
carry=0
pset(x,y),carrycol
end if
exit select
end select

end sub

sub subleft(byval x as integer,byval y as integer,byref carry as integer,byref carrycol as integer)
dim as integer col
x-=1

col=point(x,y)
While multikey(sc_left) = -1: Wend

select case carry
case 0
if col > 0 then
carry=1
carrycol=col
pset(x,y),0
end if
exit select
case 1
if col = 0 then
carry=0
pset(x,y),carrycol
end if
exit select
end select

end sub

sub subright(byval x as integer,byval y as integer,byref carry as integer,byref carrycol as integer)
dim as integer col
x+=1

col=point(x,y)
While multikey(sc_right) = -1: Wend

select case carry
case 0
if col > 0 then
carry=1
carrycol=col
pset(x,y),0
end if
exit select
case 1
if col = 0 then
carry=0
pset(x,y),carrycol
end if
exit select
end select

end sub

sub createmap(byval max as integer)
dim as integer scx,scy
screeninfo scx,scy

dim as integer char,col,x,y,numchars

do
char = 1000*rnd
x=scx*rnd
y=scy*rnd
col = 50*rnd

draw string(x,y),chr(char),col

numchars+=1
loop until numchars=max
end sub

sub gravity(byval x as integer,byref y as integer)
dim as integer col = point(x,y+1)

if col > 0 then
y+=1
end if
end sub

sub projup(byval x as integer,byval y as integer)
'While multikey(sc_i) = -1: Wend

y-=1
dim as integer yy,col = point(x,y)

do
yy=y
y-=1

screenlock
pset(x,y),80
pset(x,yy),0
screenunlock

sleep 25
loop while y > 0 and col = 0
pset(x,y),0
end sub

sub projdown(byval x as integer,byval y as integer,byval scrny as integer)
'While multikey(sc_k) = -1: Wend

y+=1
dim as integer yy,col = point(x,y)

do
yy=y
y+=1

screenlock
pset(x,y),80
pset(x,yy),0
screenunlock

sleep 25
loop while y < scrny and col = 0
pset(x,y),0
end sub

sub projleft(byval x as integer,byval y as integer)
'While multikey(sc_j) = -1: Wend

x-=1
dim as integer xx,col = point(x,y)

do
xx=x
x-=1

screenlock
pset(x,y),80
pset(xx,y),0
screenunlock

sleep 25
loop while y > 0 and col = 0
pset(x,y),0
end sub

sub projright(byval x as integer,byval y as integer,byval scrnx as integer)
'While multikey(sc_l) = -1: Wend

x+=1
dim as integer xx,col = point(x,y)

do
xx=x
x+=1

screenlock
pset(x,y),80
pset(xx,y),0
screenunlock

sleep 25
loop while y < scrnx and col = 0
pset(x,y),0
end sub

dim as integer max = 5
dim as integer x(max),y(max)
dim as integer col,carry,carrycol

dim as integer pointresult,mouseresult,clip

dim as integer temp(max)

col = 32
temp(0)=0

screenlock
createmap(1000)
screenunlock

do
mouseresult=getmouse(x(5),y(5),clip)

if multikey(sc_up) then subup(x(0),y(0),carry,carrycol)
if multikey(sc_down) then subdown(x(0),y(0),carry,carrycol)
if multikey(sc_right) then subright(x(0),y(0),carry,carrycol)
if multikey(sc_left) then subleft(x(0),y(0),carry,carrycol)

if multikey(sc_i) then projup(x(0),y(0))
if multikey(sc_k) then projdown(x(0),y(0),scrny)
if multikey(sc_j) then projleft(x(0),y(0))
if multikey(sc_l) then projright(x(0),y(0),scrnx)

x(1)=x(0)
y(1)=y(0)

screenlock
if multikey(sc_w) then north(x(0),y(0))
if multikey(sc_d) then east(x(0),y(0),scrnx)
if multikey(sc_s) then south(x(0),y(0),scrny)
if multikey(sc_a) then west(x(0),y(0))

pset(x(1),y(1)),0

if multikey(sc_space) then
pset(x(0),y(0)),0

x(0)=x(5)
y(0)=y(5)
end if

gravity(x(0),y(0))
pset(x(0),y(0)),15

line(0,0)-(100,35),32,''bf''
draw string(5,5),''Carry: '' & carry
draw string(5,15),''X: '' & x(0)
draw string(5,25),''Y: '' & y(0)
screenunlock

sleep 75,1
loop until multikey(sc_escape)
Jattenalle
Developer
Joined 6113 days ago
Last seen 3 hours ago
Re. Jatte programming help please. Thread Synchronization
Posted 4207 days ago
You use mutexes.

Any code that is inside the same mutex can not execute ''at the same time''
Declarations
'// On program start:
dim shared as any ptr myMytex
myMutex = mutexcreate

'// On program end:
mutexdestroy myMutex

In the main loop
mutexlock myMutex
'// Do stuff here!
mutexunlock myMutex

In the thread
mutexlock myMutex
'// Do stuff here!
mutexunlock myMutex

The ''Do stuff here'' parts in the thread and main can not run at the same time since they're inside the same mutex. You can have multiple mutexes by simply creating more.


However, using threads for what you want is not the easiest approach. What you want is to process both any traveling bullets and anything else that is going on inside the main loop. As in, your program should only have one loop, which is the main one.
Inside it you handle everything.

Super simplified and broken semi-pseudo code:
Code
do

'// Handle player movement
if left then player_position.x -= 1
if right then player_position.x += 1
if space then
numBullets += 1
bullet(numBullets).position = player_position
bullet(numBullets).velocity = player_direction
end if

'// Handle any bullets
if numBullets > 0 then
for i as integer = 1 to numBullets
bullet(i).position.x += bullet(i).velocity.x
bullet(i).position.y += bullet(i).velocity.y
if bulletHitSomething then
'// This can be a bit confusing because we're moving
'// the last existing bullet to the current bullets slot in
'// the array. Basically to keep it all tight and tidy
bullet(i) = bullet(numBullets)
numBullets -= 1
i -= 1 '// Step back one in the for loop since we just moved a bullet back there
if numBullets = 0 then exit for '// Just stop processing if that was the last bullet
end if
next
end if

loop
Auios
Newborn
Joined 4880 days ago
Last seen 2157 days ago
Re. Jatte programming help please. Thread Synchronization
Posted 4207 days ago
Sweet. Thanks. I'm gonna use this knowledge for the good of the world!!! :D
Forums > Off-topic > Jatte programming help please. Thread Synchronization
Steam Early-access
Gods and Idols
Gods and Idols is copyright © Johannes Pihl 2007-2023, all rights reserved;
Shadowbox.js is © Michael J. I. Jackson;
All other trademarks, logos and copyrights are the property of their respective owners.