1
1
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

656 lines
19 KiB

#include "draw.h"
#include "game.h"
#include <SDL2/SDL_mutex.h>
#include <time.h>
Vect viewport_pos;
int width, height;
/* generic rendering functions */
int MAX_ONSCREEN_OBJECTS = 99;
int num_onscreen = 0;
double time_delta = 0;
void render_texture_at(struct SDL_Renderer * ren, struct SDL_Texture * texture,int x, int y) {
/* draw a texture at x.y */
SDL_Rect destination;
destination.x = x;
destination.y = y;
SDL_QueryTexture(texture, NULL, NULL, &destination.w, &destination.h);
SDL_RenderCopy(ren, texture, NULL, &destination);
}
/*SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) {*/
/*[> Load an image into a texture <]*/
/*SDL_Surface * surface = IMG_Load(fname) ;*/
/*if(!surface) {*/
/*printf("IMG_Load: %s\n", IMG_GetError());*/
/*// handle error*/
/*}*/
/*SDL_Texture * png_image = SDL_CreateTextureFromSurface(ren, surface);*/
/*cleanup(surface, SURFACE);*/
/*return (png_image);*/
/*}*/
double sigmoid(double x) {
return exp(x) / (exp(x) + 1);
}
struct timespec get_now_d() {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (now.tv_nsec >= 1000000000) {
now.tv_nsec -= 1000000000;
}
return now;
}
Vect in_view(Vect V) {
Vect ret;
ret.x = V.x -viewport_pos.x;
ret.y = V.y -viewport_pos.y;
return ret;
}
/* Game Specific rendering functions */
void draw_player(SDL_Renderer * ren, int x, int y, bool red) {
// translate to viewport
Vect V;
V.x = x; V.y = y;
V = in_view(V);
/* draw the player as a coloured rect */
SDL_Rect player_rect;
player_rect.x = V.x -10;
player_rect.y = V.y -10;
player_rect.w = 20;
player_rect.h = 20;
struct colour c = get_scene_at(viewport_pos, level).colours.bg;
SDL_SetRenderDrawColor(ren, c.r, c.g, c.b, 255);
SDL_RenderDrawRect(ren, &player_rect);
SDL_RenderFillRect(ren, &player_rect);
// draw strings
for (int i = 0; i < player.physics->num_strings; i++) {
if (!player.physics->strings[i].attached) {
continue;
}
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);
}
SDL_SetRenderDrawColor(ren, 0,0,0, 255);
}
void draw_floor(SDL_Renderer *ren, FloorPoly *poly, bool down) {
SDL_Point left;
SDL_Point right;
left.x = poly->left.x - viewport_pos.x;
if (left.x > width) {
return;
}
right.x = poly->right.x - viewport_pos.x;
if (right.x < 0) {
return;
}
left.y = poly->left.y - viewport_pos.y;
right.y = poly->right.y - viewport_pos.y;
if (left.y <= 0 && right.y <= 0) {
return;
}
if (left.y >= height && right.y >= height) {
return;
}
SDL_SetRenderDrawColor(ren, 100,100,100, 255);
int end = down ? height : 0;
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);
}
}
SDL_SetRenderDrawColor(ren, 0,0,0, 255);
}
// draw collision poly
void draw_collision_poly(SDL_Renderer* ren, Body *body) {
if (!SHOWCOLLISION) {
return;
}
if (body->colliding) {
SDL_SetRenderDrawColor(ren, 255, 0, 0, 255);
} else {
SDL_SetRenderDrawColor(ren, 0, 255, 0, 255);
}
// draw strings
for (int i = 0; i < body->num_strings; i++) {
if (!body->strings[i].attached) {
continue;
}
Vect B;
B = in_view(body->position);
Vect E;
E = in_view(body->strings[i].end_point);
SDL_RenderDrawLine(ren, B.x, B.y, E.x, E.y);
}
if (body->collision_poly_size == 1) {
SDL_Rect dot;
Vect V;
V.x = body->collision_poly[0].x;
V.y = body->collision_poly[0].y;
Vect T = in_view(V);
dot.x = T.x;
dot.y = T.y;
dot.w = 4;
dot.h = 4;
SDL_RenderDrawRect(ren, &dot);
SDL_RenderFillRect(ren, &dot);
} else if (body->collision_poly_size == 2) {
int x_st = body->collision_poly[0].x -viewport_pos.x;
int y_st = body->collision_poly[0].y -viewport_pos.y;
int x_en = body->collision_poly[1].x -viewport_pos.x;
int y_en = body->collision_poly[1].y -viewport_pos.y;
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
} else if (body->collision_poly_size > 2) {
int x_st = body->collision_poly[0].x -viewport_pos.x;
int y_st = body->collision_poly[0].y -viewport_pos.y;
int x_en, y_en;
for (int i = 1; i < body->collision_poly_size; i++) {
x_en = body->collision_poly[i].x -viewport_pos.x;
y_en = body->collision_poly[i].y -viewport_pos.y;
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
x_st = x_en;
y_st = y_en;
}
x_en = body->collision_poly[0].x-viewport_pos.x;
y_en = body->collision_poly[0].y-viewport_pos.y;
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
}
SDL_SetRenderDrawColor(ren, 0,0,0, 255);
}
// draw indicator of forces acting on an object
void draw_forces(SDL_Renderer *ren, Body *body) {
if (!SHOWFORCES) {
return;
}
Vect start;
start.x = (int)body->position.x -viewport_pos.x;
start.y = (int)body->position.y -viewport_pos.y;
Vect F;
for (int i = 0; i < body->num_motors; i++) {
if (!get_motor_active(body, i)) {
continue;
}
F.x += body->motors[i].x;
F.y += body->motors[i].y;
Vect end = vect_add(start, F);
SDL_SetRenderDrawColor(ren, 255,0,0, 255);
SDL_RenderDrawLine(ren, start.x, start.y, start.x, end.y);
SDL_SetRenderDrawColor(ren, 0,0,255, 255);
SDL_RenderDrawLine(ren, start.x, start.y, end.x, start.y);
}
SDL_SetRenderDrawColor(ren, 0,0,0, 255);
}
void draw_wall(SDL_Renderer *ren, Wall *wall) {
SDL_SetRenderDrawColor(ren, 120, 85, 188, 255);
int x_st, x_en, y_st, y_en;
x_st = wall->nodes[0].x + wall->physics->position.x -viewport_pos.x;
y_st = wall->nodes[0].y + wall->physics->position.y -viewport_pos.y;
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;
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
x_st = x_en;
y_st = y_en;
}
x_en = wall->nodes[0].x + wall->physics->position.x -viewport_pos.x;
y_en = wall->nodes[0].y + wall->physics->position.y -viewport_pos.y;
SDL_RenderDrawLine(ren, x_st, y_st, x_en, y_en);
SDL_SetRenderDrawColor(ren, 0,0,0, 255);
}
int distance_colour(int x, int y, int x2, int y2, int blackpoint) {
int dist = (int) sqrt((x2 - x)*(x2 - x) + (y2 - y)*(y2 - y) );
if (dist == 0) {
return 255;
}
if (dist > blackpoint) {
return 0;
}
int frac = blackpoint / dist;
// int frac = 255 * (int) sin((double)dist / blackpoint);
if (frac > 255) {
return 255;
}
return frac;
}
void new_update_viewport(Body const * const pl) {
int const xmargin = 100; // pixels
int const ymargin = 100; // pixels
int const xmovmagin = width / 4;
int const ymovmagin = height / 3;
double const xrange = width - (2 * xmargin);
double const yrange = height - (2 * ymargin);
double const x_max = 2;
double const y_max = 3;
static Vect target = {};
static double dirchange = 0;
static Vect last_plpos = {};
if (last_plpos.x == 0 && last_plpos.y == 0)
last_plpos = player.physics->position;
printf("%f %f\n", pl->vel.x, pl->vel.y);
static Vect lmove = {};
Vect iv = in_view(pl->position);
printf("player pos: %f %f\n", pl->position.x, pl->position.y);
Vect left_pos = {0, 2 * height / 4};
left_pos = vect_add(pl->position, vect_scalar(left_pos, -1));
Vect right_pos = {7 * (width / 8), 2 * height / 4};
right_pos = vect_add(pl->position, vect_scalar(right_pos, -1));
bool recover = false;
Vect screen_dist = vect_add(in_view(pl->position), vect_scalar(lmove, -1));
if (iv.x > width
|| (iv.x < 0
|| iv.y > height
|| iv.y < 0))
recover = true;
Vect delta = vect_add(vect_scalar(player.physics->position, -1), last_plpos);
double dist_change = vect_mag(delta);
if (true || (recover) //|| vect_mag((Vect){pl->vel.x, 0}) > 0.4
|| (dist_change > width / 100)) {
if (iv.x < width / 6) {
printf("RIGHT\n");
target = right_pos;
} else {
printf("LEFT\n");
target = left_pos;
}
lmove = target;
last_plpos = player.physics->position;
}
if (recover)
viewport_pos = target;
// emergency
/*if (!(iv.x > width / 3 && iv.x < (2 * (width / 3)))) {*/
/*if (!(iv.y > height / 3 && iv.y < (2 * (height / 3)))) {*/
/*if ( true ||*/
/*( in_view(pl->position).x > xmargin + xrange */
/*|| in_view(pl->position).x < xmargin)*/
/*) */
/*{*/
/*double m = (width / 2) / (2.0 * x_max);*/
/*double x = m * v;*/
/*target.x = pl->position.x - x;*/
/*}*/
//target.y = pl->position.y - height/2; // + y;
printf("viewport pos: %f %f\n", iv.x, iv.y);
printf("target pos: %f %f\n", target.x, iv.y);
/*}*/
/*}*/
double v = pl->vel.x;
if (v > x_max)
v = x_max;
if (v < -x_max)
v = -x_max;
if (v < 0)
v = -v;
// move towards target a set proportion
Vect urgency = vect_add(target, vect_scalar(player.physics->position, -1));
Vect p = vect_add(target, vect_scalar(viewport_pos, -1));
printf("drawtime: %f", time_delta);
double proportion = (sigmoid(time_delta) / 100);
proportion = 2 * player.physics->vel.x / (width);
proportion = proportion < 0 ? -proportion : proportion;
printf("prop: %f", proportion);
proportion > 0.4 ? proportion = 0.4 : 0;
proportion < 0 ? proportion = 0.000001 : 0;
p = vect_scalar(p, proportion);
viewport_pos = vect_add(viewport_pos, p);
}
void update_viewport(Body *pl) {
float xmargin = 0.5;
float ymargin = 0.2;
if (pl->position.x - viewport_pos.x > (1-xmargin) * width) {
viewport_pos.x = - ((1-xmargin) * width - pl->position.x);
}
if (pl->position.x - viewport_pos.x < xmargin * width) {
viewport_pos.x = -(xmargin * width - pl->position.x);
}
if (pl->position.y - viewport_pos.y > (1-ymargin) * height) {
viewport_pos.y = -((1-ymargin) * height - pl->position.y);
}
if (pl->position.y - viewport_pos.y < ymargin * height) {
viewport_pos.y = -(ymargin * height - pl->position.y);
}
}
double interpolate_h(Vect left, Vect right, double x) {
static Vect v;
double m = (double)(right.y - left.y) / (double)(right.x - left.x);
double c = (double)left.y - ((double)left.x * m);
return x * m + c;
}
void draw_environment(SDL_Renderer * ren, struct environment *scene) {
struct colour c1 = scene->colours.bg;
struct colour c2 = scene->colours.fg1;
struct colour c3 = scene->colours.fg2;
struct colour c4 = scene->colours.fg3;
/*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;
}
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);*/
// 3. bg2 to floor
SDL_SetRenderDrawColor(ren, c4.r, c4.g, c4.b, 255);
SDL_RenderDrawLine(ren, x, y2, x, 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);
}
}
}
void draw_text(SDL_Renderer *rend, Vect pos, struct colour colour, char *text) {
static SDL_Renderer *ren = NULL;
static TTF_Font* font = NULL;
if (!font) {
font = TTF_OpenFont("TerminusTTF.ttf", 18);
const char *err = SDL_GetError();
if (err) {
printf("%s\n", err);
}
}
if (!ren) {
ren = rend;
}
SDL_Colour sdl_col = {colour.r, colour.g, colour.b, 255};
SDL_Rect position = {.x = pos.x, .y = pos.y};
SDL_SetRenderDrawColor(ren, colour.r, colour.g, colour.b, 255);
SDL_Surface *surf = TTF_RenderText_Solid(font, text, sdl_col);
SDL_Texture *texture = SDL_CreateTextureFromSurface(ren, surf);
SDL_QueryTexture(texture, NULL, NULL, &position.w, &position.h);
SDL_RenderCopy(ren, texture, NULL, &position);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surf);
}
void draw_level_time(SDL_Renderer *ren, struct environment * e) {
int margin = height / 10;
Vect position = {.x = margin, .y = height - margin};
struct colour colour = e->colours.fg1;
int minutes = level_time / 60000;
float seconds = level_time * 0.001;
char time_string[250];
snprintf(time_string, 250, "%d:%.4f", minutes, seconds);
draw_text(ren, position, colour, time_string);
}
void redraw_buffer(SDL_Renderer * ren) {
static int mousex = 0;
static int mousey = 0;
static int newmousex = 0;
static int newmousey = 0;
static SDL_Point *bgPixels[256];
static int numpixels[256];
/*if (!width && !height) {*/
/*for (int i = 0; i < 256; i++) {*/
/*bgPixels[i] = malloc(sizeof(SDL_Point) * width * height);*/
/*memset(bgPixels[i], 0, sizeof(SDL_Point) * width * height);*/
/*memset(numpixels, 0, (sizeof(int) * 256));*/
/*}*/
/*}*/
int col = 0;
Body lplayer;
if (SDL_LockMutex(player.physics->lock) == 0){
lplayer = *player.physics;
SDL_UnlockMutex(player.physics->lock);
} else {
return;
}
new_update_viewport(&lplayer);
static struct timespec last = {};
static struct timespec now;
now = get_now_d();
time_delta = now.tv_nsec - last.tv_nsec;
time_delta *= 0.000001; // convert to ms from ns
last = now;
//SDL_GetMouseState(&newmousex, &newmousey);
struct environment scene = get_scene_at(viewport_pos, level);
draw_environment(ren, &scene);
draw_level_time(ren, &scene);
for (int i=0; i < world.items.size; i++) {
world_thing thing;
thing = world.get(i);
switch (thing.kind) {
case STATIC_WALL_W:
draw_wall(ren, thing.wall);
draw_collision_poly(ren, thing.wall->physics);
continue;
case FLOOR:
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);
}
continue;
case CEILING:
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);
}
continue;
case ROOM_W:
for (int i = 0; i < thing.room->ceil.numItems; i++) {
draw_collision_poly(ren, thing.room->ceil.items[i]);
}
for (int i = 0; i < thing.room->floor.numItems; i++) {
draw_collision_poly(ren, thing.room->floor.items[i]);
}
default:
continue;
}
}
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++) {*/
/*memset(bgPixels[i], 0, sizeof(SDL_Point) * width * height);*/
/*memset(numpixels, 0, (sizeof(int) * 256));*/
/*}*/
/*for (int i=0; i < width; i++) {*/
/*for (int j = 0; j < height; j++) {*/
/*col = distance_colour(i, j, mousex, mousey, 10000);*/
/*SDL_Point *row = bgPixels[col];*/
/*SDL_Point point;*/
/*point.x = i;*/
/*point.y = j;*/
/*row[numpixels[col]] = point;*/
/*numpixels[col] += 1;*/
/*}*/
/*}*/
/*}*/
/*for (int i = 0; i < 255; i++) {*/
/*SDL_Point *row = bgPixels[i];*/
/*col = i;*/
/*SDL_SetRenderDrawColor(ren, col, col, col, 255);*/
/*SDL_RenderDrawPoints(ren, row, numpixels[i]);*/
/*}*/
}