1
1
Fork 0
Browse Source

thread experiment

thread-physics
alistair 4 years ago
parent
commit
52533a86ac
  1. 34
      src/draw.c
  2. 22
      src/game.c
  3. 1
      src/game.h
  4. 16
      src/main.c
  5. 2
      src/types.h

34
src/draw.c

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
#include "draw.h"
#include <SDL2/SDL_mutex.h>
Vect viewport_pos;
int width, height;
@ -58,13 +59,9 @@ void draw_player(SDL_Renderer * ren, int x, int y, bool red) { @@ -58,13 +59,9 @@ void draw_player(SDL_Renderer * ren, int x, int y, bool red) {
player_rect.w = 20;
player_rect.h = 20;
if (red) {
SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);
} else {
struct colour c = get_scene_at(viewport_pos, level).colours.bg;
struct colour c = get_scene_at(viewport_pos, level).colours.bg;
SDL_SetRenderDrawColor(ren, c.r, c.g, c.b, 255);
}
SDL_SetRenderDrawColor(ren, c.r, c.g, c.b, 255);
SDL_RenderDrawRect(ren, &player_rect);
SDL_RenderFillRect(ren, &player_rect);
@ -73,8 +70,8 @@ void draw_player(SDL_Renderer * ren, int x, int y, bool red) { @@ -73,8 +70,8 @@ void draw_player(SDL_Renderer * ren, int x, int y, bool red) {
if (!player.physics->strings[i].attached) {
continue;
}
Vect B;
B = in_view(player.physics->position);
Vect B = {x, y};
B = in_view(B);
Vect E;
E = in_view(player.physics->strings[i].end_point);
SDL_RenderDrawLine(ren, B.x, B.y, E.x, E.y);
@ -393,9 +390,17 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -393,9 +390,17 @@ void redraw_buffer(SDL_Renderer * ren) {
/*}*/
int col = 0;
Body lplayer;
if (SDL_LockMutex(player.physics->lock) == 0){
lplayer = *player.physics;
SDL_UnlockMutex(player.physics->lock);
} else {
return;
}
//SDL_GetMouseState(&newmousex, &newmousey);
update_viewport(player.physics->position.x, player.physics->position.y);
update_viewport(lplayer.position.x, lplayer.position.y);
struct environment scene = get_scene_at(viewport_pos, level);
draw_environment(ren, &scene);
@ -405,11 +410,6 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -405,11 +410,6 @@ void redraw_buffer(SDL_Renderer * ren) {
thing = world.get(i);
switch (thing.kind) {
case PLAYER:
draw_player(ren, (*thing.player).physics->position.x, (*thing.player).physics->position.y, thing.player->colliding);
draw_collision_poly(ren, thing.player->physics);
draw_forces(ren, thing.player->physics);
continue;
case STATIC_WALL_W:
draw_wall(ren, thing.wall);
draw_collision_poly(ren, thing.wall->physics);
@ -438,6 +438,12 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -438,6 +438,12 @@ void redraw_buffer(SDL_Renderer * ren) {
}
}
draw_player(ren, lplayer.position.x, lplayer.position.y,
lplayer.colliding);
draw_collision_poly(ren, &lplayer);
draw_forces(ren, &lplayer);
/*if (newmousex != mousex || newmousey != mousey) {*/ /*mousey = newmousey;*/
/*mousex = newmousex;*/
/*for (int i = 0; i < 256; i++) {*/

22
src/game.c

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
#include "game.h"
#include <SDL2/SDL_mutex.h>
#define FLOOR_THICKNESS 200
#define MAX_ROPE_GRAB_LEN 80000
@ -211,6 +212,7 @@ player_st get_player(int x, int y) { @@ -211,6 +212,7 @@ player_st get_player(int x, int y) {
// physics settings
get_new_physics(&player.physics);
player.physics->dynamics = true;
player.physics->lock = SDL_CreateMutex();
player.physics->position.x = x;
player.physics->position.y = y;
@ -1051,9 +1053,9 @@ void advance_thing(Body * thing) { @@ -1051,9 +1053,9 @@ void advance_thing(Body * thing) {
// get time delta
struct timespec now = get_now();
clock_gettime(CLOCK_MONOTONIC, &now);
if (now.tv_nsec >= 1000000000) {
now.tv_nsec -= 1000000000;
}
/*if (now.tv_nsec >= 1000000000) {*/
/*now.tv_nsec -= 1000000000;*/
/*}*/
double time_delta = now.tv_nsec - thing->last_advance_time.tv_nsec;
@ -1066,6 +1068,7 @@ void advance_thing(Body * thing) { @@ -1066,6 +1068,7 @@ void advance_thing(Body * thing) {
// cap the time delta: simulatino will slow down
// printf("delta: %f\n", time_delta);
printf("ERROR too slow: %f\n", time_delta);
if (time_delta > 19) {
printf("ERROR too slow: %f\n", time_delta);
time_delta = 19;
@ -1122,7 +1125,6 @@ void advance_thing(Body * thing) { @@ -1122,7 +1125,6 @@ void advance_thing(Body * thing) {
} else {
thing->vel.y += thing->obj_friction * friction.y;
}
}
/*double norm = 9.81 * thing->obj_mass;*/
@ -1188,7 +1190,6 @@ void advance_thing(Body * thing) { @@ -1188,7 +1190,6 @@ void advance_thing(Body * thing) {
// motors
for (int i = 0; i < thing->num_motors; i++) {
if (!get_motor_active(thing, i)) {
continue;
}
@ -1247,11 +1248,16 @@ void advance_thing(Body * thing) { @@ -1247,11 +1248,16 @@ void advance_thing(Body * thing) {
(*thing).vel.y = 0;
}
double oldx = thing->position.x;
double oldy = thing->position.y;
thing->next_position.x += (thing->vel.x * 1/2 * (double)time_delta);
thing->next_position.y += (thing->vel.y * 1/2 * (double)time_delta);
thing->position = thing->next_position;
if (thing->lock) {
if (SDL_LockMutex(thing->lock) == 0) {
thing->position = thing->next_position;
SDL_UnlockMutex(thing->lock);
}
} else {
thing->position = thing->next_position;
}
if (!thing->collision_poly[0].y) {
thing->updateCollisionPoly(thing);

1
src/game.h

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
#ifndef _DEFGAME
#define _DEFGAME
#include <time.h>
#include "environment.h"
#include "datastructures/datatypes.h"
#include "vect.h"

16
src/main.c

@ -20,7 +20,7 @@ struct SDL_Window* make_window(void) { @@ -20,7 +20,7 @@ struct SDL_Window* make_window(void) {
}
return SDL_CreateWindow("sdl_tester",
return SDL_CreateWindow("space game",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
0, 0,
@ -39,8 +39,8 @@ void redraw(struct SDL_Renderer * ren) { @@ -39,8 +39,8 @@ void redraw(struct SDL_Renderer * ren) {
int physics_loop(void *ptr) {
while (true) {
SDL_Delay(10);
step(10);
SDL_Delay(2);
}
}
@ -49,12 +49,12 @@ int game(void) { @@ -49,12 +49,12 @@ int game(void) {
LOGLEVEL = DEBUG;
STDOUTLEVEL = DEBUG;
SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "2" );
// SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "2" );
logwrite(INFO, "Starting\n");
SDL_Window * win = make_window();
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
// | SDL_RENDERER_PRESENTVSYNC);
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED
| SDL_RENDERER_PRESENTVSYNC);
queue_for_cleanup(win, WINDOW);
queue_for_cleanup(ren, RENDERER);
@ -72,10 +72,12 @@ int game(void) { @@ -72,10 +72,12 @@ int game(void) {
SDL_Thread *physics_thread;
int ignore;
// physics_thread = SDL_CreateThread(physics_loop, "Physics", (void *)NULL);
bool once = true;
startgame(ren);
physics_thread = SDL_CreateThread(physics_loop, "Physics", (void *)NULL);
while (!close) {
SDL_Event event;
@ -92,7 +94,7 @@ int game(void) { @@ -92,7 +94,7 @@ int game(void) {
}
}
/* Redraw Screen */
step(10);
// step(10);
redraw(ren);
}

2
src/types.h

@ -126,7 +126,7 @@ typedef struct BodyStruct{ @@ -126,7 +126,7 @@ typedef struct BodyStruct{
int num_strings;
int max_strings;
String *strings;
SDL_mutex *lock;
} Body;
typedef struct {

Loading…
Cancel
Save