1
1
Fork 0
Browse Source

started worldgen v2

Displays stuff, but its only garbage.
thread-physics
alistair 4 years ago
parent
commit
51508258c6
  1. 7
      Makefile
  2. 117
      draw.c
  3. 33
      environment.c
  4. 21
      environment.h
  5. 29
      game.c
  6. 8
      main.c
  7. 7
      vect.c

7
Makefile

@ -10,8 +10,8 @@ run: all @@ -10,8 +10,8 @@ run: all
all: $(EXE)
$(EXE): main.o vect.o logger.o game.o garbo.o draw.o controlscheme.o
$(CXX) $(CXXFLAGS) -o $(EXE) main.o vect.o game.o logger.o draw.o garbo.o controlscheme.o
$(EXE): main.o vect.o logger.o game.o garbo.o draw.o controlscheme.o environment.o
$(CXX) $(CXXFLAGS) -o $(EXE) main.o vect.o game.o logger.o draw.o garbo.o environment.o controlscheme.o c-colours/colours.o datastructures/datatypes.o
main.o: main.c logger.h game.h garbo.h draw.h
$(CXX) $(CXXFLAGS) -c main.c
@ -34,6 +34,9 @@ vect.o: vect.c vect.h @@ -34,6 +34,9 @@ vect.o: vect.c vect.h
controlscheme.o: controlscheme.c controlscheme.h
$(CXX) $(CXXFLAGS) -c controlscheme.c
environment.o: environment.c environment.h
$(CXX) $(CXXFLAGS) -c environment.c
clean:
rm *.o && rm $(EXE)

117
draw.c

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
#include "draw.h"
#include "vect.h"
#include "environment.h"
Vect viewport_pos;
int width, height;
@ -74,7 +75,6 @@ void draw_player(SDL_Renderer * ren, int x, int y, bool red) { @@ -74,7 +75,6 @@ void draw_player(SDL_Renderer * ren, int x, int y, bool red) {
if (!player.physics->strings[i].attached) {
continue;
}
printf("Draw string");
Vect B;
B = in_view(player.physics->position);
Vect E;
@ -117,6 +117,7 @@ void draw_floor(SDL_Renderer *ren, FloorPoly *poly, bool down) { @@ -117,6 +117,7 @@ void draw_floor(SDL_Renderer *ren, FloorPoly *poly, bool down) {
double m = (double)(right.y - left.y) / (double)(right.x - left.x);
double c = (double)left.y - ((double)left.x * m);
for (int i = left.x; i <= right.x; i++) {
int y = (int)(m * i + c);
if (y > 0) {
@ -144,7 +145,6 @@ void draw_collision_poly(SDL_Renderer* ren, Body *body) { @@ -144,7 +145,6 @@ void draw_collision_poly(SDL_Renderer* ren, Body *body) {
if (!body->strings[i].attached) {
continue;
}
printf("Draw string");
Vect B;
B = in_view(body->position);
Vect E;
@ -219,7 +219,6 @@ void draw_wall(SDL_Renderer *ren, Wall *wall) { @@ -219,7 +219,6 @@ void draw_wall(SDL_Renderer *ren, Wall *wall) {
for (int i = 1; i < wall->numNodes; i++) {
x_en = wall->nodes[i].x + wall->physics->position.x -viewport_pos.x;
y_en = wall->nodes[i].y + wall->physics->position.y -viewport_pos.y;
printf("wall: %d %d %d %d\n", x_st, y_st, x_en, y_en);
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
x_st = x_en;
y_st = y_en;
@ -273,6 +272,112 @@ void update_viewport(double x, double y) { @@ -273,6 +272,112 @@ void update_viewport(double x, double y) {
}
}
double interpolate_h(Vect left, Vect right, double x) {
static Vect v;
static Vect l;
static Vect r;
if (!(l.x == left.x && r.x == right.x
&& l.y == left.y && r.y == right.y)) {
l = left;
r = right;
v.x = (double)(right.y - left.y) / (double)(right.x - left.x);
v.y = (double)left.y - ((double)left.x * v.x);
}
return x * v.x + v.y;
}
void draw_environment(SDL_Renderer * ren, struct environment *scene) {
void **c = scene->colours.items;
struct colour *c1 = c[0];
struct colour *c2 = c[1];
struct colour *c3 = c[2];
struct colour *c4 = c[3];
c1->r = 255;
c2->g = 255;
c3->b = 255;
c4->g = 255;
c4->b = 255;
// background colour
SDL_SetRenderDrawColor(ren, c1->r, c1->g, c1->b, 255);
SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = width;
rect.h = height;
SDL_RenderFillRect(ren, &rect);
/*
* double m = (double)(right.y - left.y) / (double)(right.x - left.x);
* double c = (double)left.y - ((double)left.x * m);
*
* for (int i = left.x; i <= right.x; i++) {
* int y = (int)(m * i + c);
* if (y > 0) {
* SDL_RenderDrawLine(ren, i, y, i, end);
* }
* }
*/
int k = 0;
// printf("from 0 to (room tiles) %d - 1, res %d width %d\n", E_ROOM_TILES, E_ROOM_RES, E_ROOM_WIDTH);
for (int i = 0; i < E_ROOM_TILES - 1; i++) {
for (int j = 0; j < E_ROOM_RES; j++) {
k++;
Vect *left;
Vect *right;
int x = (i * E_ROOM_RES) + j;
if (x - viewport_pos.x > width) {
break;
}
if (x - viewport_pos.x < 0) {
continue;
}
int blob = i;
left = arlst_get(&scene->ceil, i);
right = arlst_get(&scene->ceil, i+1);
int y0 = interpolate_h(*left, *right, x) - viewport_pos.y;
left = arlst_get(&scene->bg1, i);
right = arlst_get(&scene->bg1, i+1);
int y1 = interpolate_h(*left, *right, x) - viewport_pos.y;
left = arlst_get(&scene->bg2, i);
right = arlst_get(&scene->bg2, i+1);
int y2 = interpolate_h(*left, *right, x) - viewport_pos.y;
left = arlst_get(&scene->floor, i);
right = arlst_get(&scene->floor, i+1);
int y3 = interpolate_h(*left, *right, x) - viewport_pos.y;
x = x - viewport_pos.x;
printf("x: %d, ", x);
printf("y: %d %d %d %d\n", y0, y1, y2, y3);
// 1. ceil to bg1
SDL_SetRenderDrawColor(ren, c2->r, c2->g, c2->b, 255);
SDL_RenderDrawLine(ren, x, y0, x, y1);
// 2. bg1 to bg2
SDL_SetRenderDrawColor(ren, c3->r, c3->g, c3->b, 255);
SDL_RenderDrawLine(ren, x, y1, x, y2);
// 3. bg2 to floor
SDL_SetRenderDrawColor(ren, c4->r, c4->g, c4->b, 255);
SDL_RenderDrawLine(ren, x, y2, x, y3);
}
}
}
void redraw_buffer(SDL_Renderer * ren) {
static int mousex = 0;
@ -295,10 +400,12 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -295,10 +400,12 @@ void redraw_buffer(SDL_Renderer * ren) {
//SDL_GetMouseState(&newmousex, &newmousey);
update_viewport(player.physics->position.x, player.physics->position.y);
struct environment scene = get_scene_at(viewport_pos, 0);
draw_environment(ren, &scene);
for (int i=0; i<world.size; i++) {
world_thing thing;
thing = world.things[i];
printf("draw thing: %d\n, no %d of %d\n", thing.kind, thing.nid, world.size);
switch (thing.kind) {
case PLAYER:
@ -311,12 +418,14 @@ void redraw_buffer(SDL_Renderer * ren) { @@ -311,12 +418,14 @@ void redraw_buffer(SDL_Renderer * ren) {
draw_collision_poly(ren, thing.wall->physics);
break;
case FLOOR:
break;
for (int i = 0; i < thing.floor->numPolys; i++) {
draw_floor(ren, &thing.floor->polys[i], true);
draw_collision_poly(ren, thing.floor->polys[i].physics);
}
break;
case CEILING:
break;
for (int i = 0; i < thing.floor->numPolys; i++) {
draw_floor(ren, &thing.floor->polys[i], false);
draw_collision_poly(ren, thing.floor->polys[i].physics);

33
environment.c

@ -1,25 +1,4 @@ @@ -1,25 +1,4 @@
#include "c-colours/colours.h"
#include "datastructures/datatypes.h"
#include "vect.h"
#define ROOM_WIDTH 2000
enum {
E_ROOM_WIDTH = 2000,
E_ROOM_RES = 30,
E_ROOM_TILES = E_ROOM_WIDTH / E_ROOM_RES,
};
struct environment {
int level;
Vect position;
ArrayList ceil;
ArrayList floor;
ArrayList bg1;
ArrayList bg2;
ArrayList colours;
};
#include "environment.h"
double linear_interpolate(double a0, double a1, double w) {
return (1.0f - w) * a0 + w * a1;
}
@ -66,15 +45,16 @@ ArrayList get_colours(int seed) { @@ -66,15 +45,16 @@ ArrayList get_colours(int seed) {
return l;
}
struct environment get_scene_at(Vect coordinate, int seed) {
// basic cache for the last room generated
static bool init;
static Vect last_room;
static struct environment e;
Vect room_coord;
room_coord.x = (int)coordinate.x - (int)coordinate.x % ROOM_WIDTH;
room_coord.x = (int)coordinate.x - (int)coordinate.x % E_ROOM_WIDTH;
room_coord.y = 0;
if (room_coord.x == last_room.x) {
if (init && room_coord.x == last_room.x) {
return e;
}
last_room = room_coord;
@ -88,7 +68,7 @@ struct environment get_scene_at(Vect coordinate, int seed) { @@ -88,7 +68,7 @@ struct environment get_scene_at(Vect coordinate, int seed) {
Vect bit;
bit.y = 100 * seed;
for (int i = 0; i < E_ROOM_TILES; i += E_ROOM_RES) {
for (int i = 0; i < E_ROOM_WIDTH; i += E_ROOM_RES) {
bit.x = room_coord.x + i;
double *z = malloc(sizeof(double));
*z = perlin(bit);
@ -97,6 +77,7 @@ struct environment get_scene_at(Vect coordinate, int seed) { @@ -97,6 +77,7 @@ struct environment get_scene_at(Vect coordinate, int seed) {
arlst_add(&e.bg1, z + 20);
arlst_add(&e.bg2, z + 70);
}
init = true;
return e;
}

21
environment.h

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
#include "c-colours/colours.h"
#include "datastructures/datatypes.h"
#include "vect.h"
enum {
E_ROOM_WIDTH = 10000,
E_ROOM_RES = 500,
E_ROOM_TILES = E_ROOM_WIDTH / E_ROOM_RES,
};
struct environment {
int level;
Vect position;
ArrayList ceil;
ArrayList floor;
ArrayList bg1;
ArrayList bg2;
ArrayList colours;
};
struct environment get_scene_at(Vect coordinate, int seed);

29
game.c

@ -225,8 +225,8 @@ player_st get_player(int x, int y) { @@ -225,8 +225,8 @@ player_st get_player(int x, int y) {
rect[3].x = -10; rect[3].y = 10;
// gravity
set_motor_newtons(player.physics, M_GRAVITY, 0.0,
player.physics->obj_mass * 4);
//set_motor_newtons(player.physics, M_GRAVITY, 0.0,
// player.physics->obj_mass * 4);
// walking motor
add_motor(player.physics, 0.0, player.physics->obj_mass * 9.81);
@ -436,7 +436,6 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) { @@ -436,7 +436,6 @@ bool sat_collision_check(Body *one, Body *two, Vect *translation) {
// return the MTV
*translation = trans;
printf("Trans Vect: %e, %e\n", trans.x, trans.y);
free(axes);
free(proj_one);
@ -676,7 +675,6 @@ void add_rope(int mouse_x) { @@ -676,7 +675,6 @@ void add_rope(int mouse_x) {
double delt = ceiling->polys[i].right.x - end.x;
end.y = ceiling->polys[i].right.y - (int)(m * delt);
printf("ROPEJASHD: %f, %f\n", end.y, delt);
break;
}
}
@ -783,13 +781,11 @@ void advance_thing(Body * thing) { @@ -783,13 +781,11 @@ void advance_thing(Body * thing) {
}
time_delta *= 0.000001; // convert to ms from ns
printf("delta: %f\n", time_delta);
if (time_delta > 19) {
printf("ERROR too slow: %f\n", time_delta);
thing->last_advance_time = now;
time_delta = 19;
}
printf("TimeDELTA: %f\n", time_delta);
thing->last_advance_time = now;
// collisions
if ((numcols = process_collisions(thing, &translation))) {
@ -843,7 +839,6 @@ void advance_thing(Body * thing) { @@ -843,7 +839,6 @@ void advance_thing(Body * thing) {
thing->vel.y += thing->obj_friction * friction.y;
}
printf("velocity: %f %f\n", thing->vel.x, thing->vel.y);
}
/*double norm = 9.81 * thing->obj_mass;*/
@ -883,9 +878,6 @@ void advance_thing(Body * thing) { @@ -883,9 +878,6 @@ void advance_thing(Body * thing) {
double st_len = vect_distance(thing->next_position, thing->strings[i].end_point);
double max_len = thing->strings[i].max_length;
printf("STRING len: %f, %f\n", st_len, max_len);
printf("strendpoint: %f:%f\n\n", thing->strings[i].end_point.x,
thing->strings[i].end_point.y);
if (st_len > max_len) {
Vect string_end = thing->strings[i].end_point;
Vect thing_position = thing->next_position;
@ -915,7 +907,6 @@ void advance_thing(Body * thing) { @@ -915,7 +907,6 @@ void advance_thing(Body * thing) {
if (thing->motors[i].timeout.tv_nsec < now.tv_nsec
&& thing->motors[i].timeout.tv_sec < now.tv_sec
|| thing->motors[i].stop) {
printf("%ld %ld %d\n", thing->motors[i].timeout.tv_nsec, now.tv_nsec, thing->motors[i].stop);
continue;
}
if (thing->motors[i].update_motor) {
@ -948,7 +939,6 @@ void advance_thing(Body * thing) { @@ -948,7 +939,6 @@ void advance_thing(Body * thing) {
thing->vel.y += thing->acc.y * (double)time_delta;
double velocity = sqrt((double)(thing->vel.x * thing->vel.x + thing->vel.y * thing->vel.y));
printf("\nvels %e %e\n\n", thing->vel.x, thing->vel.y);
// simple air drag
@ -978,7 +968,11 @@ void advance_thing(Body * thing) { @@ -978,7 +968,11 @@ void advance_thing(Body * thing) {
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;
//thing->last_advance_time = now; // in milliseconds
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
if (now.tv_nsec >= 1000000000) {
now.tv_nsec -= 1000000000;
}
thing->last_advance_time = now;
}
@ -1097,6 +1091,10 @@ void get_room(void) { @@ -1097,6 +1091,10 @@ void get_room(void) {
floor->numPolys = floorsize;
FloorPoly *ceil = generate_floor_simple(floorsize, false, 1000);
printf("floor: %f %f\n", polys[2].left.x, polys[2].left.y);
printf("ceil: %f %f\n", ceil[2].left.x, ceil[2].left.y);
Floor* ceiling = calloc(1, sizeof(Floor));
ceiling->polys = ceil;
ceiling->numPolys = floorsize;
@ -1130,7 +1128,8 @@ void startgame(SDL_Renderer * ren) { @@ -1130,7 +1128,8 @@ void startgame(SDL_Renderer * ren) {
SDL_GetRendererOutputSize(ren, &width, &height);
player = get_player(200,1300);
// player = get_player(200,1300);
player = get_player(0,10);
world_thing player_world;

8
main.c

@ -74,8 +74,8 @@ int game(void) { @@ -74,8 +74,8 @@ int game(void) {
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);
@ -93,7 +93,7 @@ int game(void) { @@ -93,7 +93,7 @@ int game(void) {
SDL_Thread *physics_thread;
int ignore;
physics_thread = SDL_CreateThread(physics_loop, "Physics", (void *)NULL);
// physics_thread = SDL_CreateThread(physics_loop, "Physics", (void *)NULL);
bool once = true;
startgame(ren);
@ -114,7 +114,7 @@ int game(void) { @@ -114,7 +114,7 @@ int game(void) {
}
/* Redraw Screen */
redraw(ren);
// step(10);
step(10);
}
}

7
vect.c

@ -57,11 +57,16 @@ Vect project_vect(Vect one, Vect two) { @@ -57,11 +57,16 @@ Vect project_vect(Vect one, Vect two) {
}
// project V onto P
double vect_scalar_projection(Vect V, Vect P) {
double old_vect_scalar_projection(Vect V, Vect P) {
double angle = vect_dir(V) - vect_dir(P);
return cos(angle) * vect_mag(V);
}
double vect_scalar_projection(Vect v, Vect p) {
double num = vect_dot(v, p);
return num / vect_mag(p);
}
double vect_dir(Vect V) {
return atan2(V.y, V.x);
}

Loading…
Cancel
Save