mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-09-02 04:22:34 +02:00
Asynchronous thread for gravity processing
This commit is contained in:
@@ -2,11 +2,15 @@
|
|||||||
#define AIR_H
|
#define AIR_H
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
|
||||||
extern float ogravmap[YRES/CELL][XRES/CELL];
|
extern float gravmap[YRES/CELL][XRES/CELL]; //Maps to be used by the main thread
|
||||||
extern float gravmap[YRES/CELL][XRES/CELL];
|
|
||||||
extern float gravx[YRES/CELL][XRES/CELL];
|
extern float gravx[YRES/CELL][XRES/CELL];
|
||||||
extern float gravy[YRES/CELL][XRES/CELL];
|
extern float gravy[YRES/CELL][XRES/CELL];
|
||||||
|
|
||||||
|
extern float th_ogravmap[YRES/CELL][XRES/CELL]; // Maps to be processed by the gravity thread
|
||||||
|
extern float th_gravmap[YRES/CELL][XRES/CELL];
|
||||||
|
extern float th_gravx[YRES/CELL][XRES/CELL];
|
||||||
|
extern float th_gravy[YRES/CELL][XRES/CELL];
|
||||||
|
|
||||||
extern float vx[YRES/CELL][XRES/CELL], ovx[YRES/CELL][XRES/CELL];
|
extern float vx[YRES/CELL][XRES/CELL], ovx[YRES/CELL][XRES/CELL];
|
||||||
extern float vy[YRES/CELL][XRES/CELL], ovy[YRES/CELL][XRES/CELL];
|
extern float vy[YRES/CELL][XRES/CELL], ovy[YRES/CELL][XRES/CELL];
|
||||||
extern float pv[YRES/CELL][XRES/CELL], opv[YRES/CELL][XRES/CELL];
|
extern float pv[YRES/CELL][XRES/CELL], opv[YRES/CELL][XRES/CELL];
|
||||||
|
24
src/air.c
24
src/air.c
@@ -4,11 +4,15 @@
|
|||||||
#include <defines.h>
|
#include <defines.h>
|
||||||
float kernel[9];
|
float kernel[9];
|
||||||
|
|
||||||
float ogravmap[YRES/CELL][XRES/CELL];
|
float gravmap[YRES/CELL][XRES/CELL]; //Maps to be used by the main thread
|
||||||
float gravmap[YRES/CELL][XRES/CELL];
|
|
||||||
float gravx[YRES/CELL][XRES/CELL];
|
float gravx[YRES/CELL][XRES/CELL];
|
||||||
float gravy[YRES/CELL][XRES/CELL];
|
float gravy[YRES/CELL][XRES/CELL];
|
||||||
|
|
||||||
|
float th_ogravmap[YRES/CELL][XRES/CELL]; // Maps to be processed by the gravity thread
|
||||||
|
float th_gravmap[YRES/CELL][XRES/CELL];
|
||||||
|
float th_gravx[YRES/CELL][XRES/CELL];
|
||||||
|
float th_gravy[YRES/CELL][XRES/CELL];
|
||||||
|
|
||||||
float vx[YRES/CELL][XRES/CELL], ovx[YRES/CELL][XRES/CELL];
|
float vx[YRES/CELL][XRES/CELL], ovx[YRES/CELL][XRES/CELL];
|
||||||
float vy[YRES/CELL][XRES/CELL], ovy[YRES/CELL][XRES/CELL];
|
float vy[YRES/CELL][XRES/CELL], ovy[YRES/CELL][XRES/CELL];
|
||||||
float pv[YRES/CELL][XRES/CELL], opv[YRES/CELL][XRES/CELL];
|
float pv[YRES/CELL][XRES/CELL], opv[YRES/CELL][XRES/CELL];
|
||||||
@@ -44,7 +48,7 @@ void update_grav(void)
|
|||||||
break;
|
break;
|
||||||
for (j=0; j<XRES/CELL; j++)
|
for (j=0; j<XRES/CELL; j++)
|
||||||
{
|
{
|
||||||
if(ogravmap[i][j]!=gravmap[i][j]){
|
if(th_ogravmap[i][j]!=th_gravmap[i][j]){
|
||||||
changed = 1;
|
changed = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -52,13 +56,13 @@ void update_grav(void)
|
|||||||
}
|
}
|
||||||
if(changed)
|
if(changed)
|
||||||
{
|
{
|
||||||
memset(gravy, 0, sizeof(gravy));
|
memset(th_gravy, 0, sizeof(th_gravy));
|
||||||
memset(gravx, 0, sizeof(gravx));
|
memset(th_gravx, 0, sizeof(th_gravx));
|
||||||
for (i=0; i<YRES/CELL; i++)
|
for (i=0; i<YRES/CELL; i++)
|
||||||
{
|
{
|
||||||
for (j=0; j<XRES/CELL; j++)
|
for (j=0; j<XRES/CELL; j++)
|
||||||
{
|
{
|
||||||
if(gravmap[i][j]>0.0f) //Only calculate with populated or changed cells.
|
if(th_gravmap[i][j]>0.0f) //Only calculate with populated or changed cells.
|
||||||
for (y=0; y<YRES/CELL; y++)
|
for (y=0; y<YRES/CELL; y++)
|
||||||
{
|
{
|
||||||
for (x=0; x<XRES/CELL; x++)
|
for (x=0; x<XRES/CELL; x++)
|
||||||
@@ -66,15 +70,15 @@ void update_grav(void)
|
|||||||
if(x == j && y == i)//Ensure it doesn't calculate with itself
|
if(x == j && y == i)//Ensure it doesn't calculate with itself
|
||||||
continue;
|
continue;
|
||||||
float distance = sqrt(pow(j - x, 2) + pow(i - y, 2));
|
float distance = sqrt(pow(j - x, 2) + pow(i - y, 2));
|
||||||
gravx[y][x] += M_GRAV*gravmap[i][j]*(j - x)/pow(distance, 3);
|
th_gravx[y][x] += M_GRAV*th_gravmap[i][j]*(j - x)/pow(distance, 3);
|
||||||
gravy[y][x] += M_GRAV*gravmap[i][j]*(i - y)/pow(distance, 3);
|
th_gravy[y][x] += M_GRAV*th_gravmap[i][j]*(i - y)/pow(distance, 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
memcpy(ogravmap, gravmap, sizeof(gravmap));
|
memcpy(th_ogravmap, th_gravmap, sizeof(th_gravmap));
|
||||||
memset(gravmap, 0, sizeof(gravmap));
|
memset(th_gravmap, 0, sizeof(th_gravmap));
|
||||||
}
|
}
|
||||||
void update_air(void)
|
void update_air(void)
|
||||||
{
|
{
|
||||||
|
52
src/main.c
52
src/main.c
@@ -43,6 +43,7 @@ char pygood=1;
|
|||||||
#include <SDL/SDL_audio.h>
|
#include <SDL/SDL_audio.h>
|
||||||
#include <bzlib.h>
|
#include <bzlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#include <direct.h>
|
#include <direct.h>
|
||||||
@@ -189,6 +190,10 @@ sign signs[MAXSIGNS];
|
|||||||
|
|
||||||
int numCores = 4;
|
int numCores = 4;
|
||||||
|
|
||||||
|
pthread_t gravthread;// = NULL;
|
||||||
|
pthread_mutex_t gravmutex = NULL;
|
||||||
|
int grav_ready = 0;
|
||||||
|
|
||||||
int core_count()
|
int core_count()
|
||||||
{
|
{
|
||||||
int numCPU = 1;
|
int numCPU = 1;
|
||||||
@@ -2559,6 +2564,24 @@ int process_command_old(pixel *vid_buf,char *console,char *console_error) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void update_grav_async()
|
||||||
|
{
|
||||||
|
int done = 0;
|
||||||
|
while(1){
|
||||||
|
if(!done){
|
||||||
|
update_grav();
|
||||||
|
done = 1;
|
||||||
|
pthread_mutex_lock(&gravmutex);
|
||||||
|
grav_ready = done;
|
||||||
|
pthread_mutex_unlock(&gravmutex);
|
||||||
|
} else {
|
||||||
|
pthread_mutex_lock(&gravmutex);
|
||||||
|
done = grav_ready;
|
||||||
|
pthread_mutex_unlock(&gravmutex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef RENDERER
|
#ifdef RENDERER
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -2632,15 +2655,11 @@ int main(int argc, char *argv[])
|
|||||||
char heattext[256] = "";
|
char heattext[256] = "";
|
||||||
char coordtext[128] = "";
|
char coordtext[128] = "";
|
||||||
int currentTime = 0;
|
int currentTime = 0;
|
||||||
int FPS = 0;
|
int FPS = 0, pastFPS = 0, elapsedTime = 0, limitFPS = 60;
|
||||||
int pastFPS = 0;
|
void *http_ver_check, *http_session_check = NULL;
|
||||||
int elapsedTime = 0;
|
|
||||||
int limitFPS = 60;
|
|
||||||
void *http_ver_check;
|
|
||||||
void *http_session_check = NULL;
|
|
||||||
char *ver_data=NULL, *check_data=NULL, *tmp;
|
char *ver_data=NULL, *check_data=NULL, *tmp;
|
||||||
//char console_error[255] = "";
|
//char console_error[255] = "";
|
||||||
int i, j, bq, fire_fc=0, do_check=0, do_s_check=0, old_version=0, http_ret=0,http_s_ret=0, major, minor, old_ver_len;
|
int result, i, j, bq, fire_fc=0, do_check=0, do_s_check=0, old_version=0, http_ret=0,http_s_ret=0, major, minor, old_ver_len;
|
||||||
#ifdef INTERNAL
|
#ifdef INTERNAL
|
||||||
int vs = 0;
|
int vs = 0;
|
||||||
#endif
|
#endif
|
||||||
@@ -2844,7 +2863,11 @@ int main(int argc, char *argv[])
|
|||||||
http_session_check = http_async_req_start(NULL, "http://" SERVER "/Login.api?Action=CheckSession", NULL, 0, 0);
|
http_session_check = http_async_req_start(NULL, "http://" SERVER "/Login.api?Action=CheckSession", NULL, 0, 0);
|
||||||
http_auth_headers(http_session_check, svf_user_id, NULL, svf_session_id);
|
http_auth_headers(http_session_check, svf_user_id, NULL, svf_session_id);
|
||||||
}
|
}
|
||||||
|
pthread_mutexattr_t gma;
|
||||||
|
|
||||||
|
pthread_mutexattr_init(&gma);
|
||||||
|
pthread_mutex_init (&gravmutex, NULL);
|
||||||
|
pthread_create(&gravthread, NULL, update_grav_async, NULL); //Asynchronous gravity simulation //(void *) &thread_args[i]);
|
||||||
while (!sdl_poll()) //the main loop
|
while (!sdl_poll()) //the main loop
|
||||||
{
|
{
|
||||||
frameidx++;
|
frameidx++;
|
||||||
@@ -2882,8 +2905,21 @@ int main(int argc, char *argv[])
|
|||||||
if (bsy<0)
|
if (bsy<0)
|
||||||
bsy = 0;
|
bsy = 0;
|
||||||
|
|
||||||
|
memset(gravmap, 0, sizeof(gravmap)); //Clear the old gravmap
|
||||||
update_particles(vid_buf); //update everything
|
update_particles(vid_buf); //update everything
|
||||||
update_grav();
|
|
||||||
|
pthread_mutex_lock(&gravmutex);
|
||||||
|
result = grav_ready;
|
||||||
|
//pthread_mutex_unlock(&gravmutex);
|
||||||
|
if(result) //Did the gravity thread finish?
|
||||||
|
{
|
||||||
|
memcpy(th_gravmap, gravmap, sizeof(gravmap)); //Move our current gravmap to be processed other thread
|
||||||
|
memcpy(gravy, th_gravy, sizeof(gravy)); //Hmm, Gravy
|
||||||
|
memcpy(gravx, th_gravx, sizeof(gravx)); //Move the processed velocity maps to be used
|
||||||
|
grav_ready = 0; //Tell the other thread that we're ready for it to continue
|
||||||
|
}
|
||||||
|
pthread_mutex_unlock(&gravmutex);
|
||||||
|
//update_grav();
|
||||||
draw_parts(vid_buf); //draw particles
|
draw_parts(vid_buf); //draw particles
|
||||||
|
|
||||||
if (cmode==CM_PERS)
|
if (cmode==CM_PERS)
|
||||||
|
Reference in New Issue
Block a user