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.
502 lines
15 KiB
502 lines
15 KiB
#include "draw.h" |
|
#include "game.h" |
|
|
|
Vect viewport_pos; |
|
int width, height; |
|
|
|
/* generic rendering functions */ |
|
int MAX_ONSCREEN_OBJECTS = 99; |
|
|
|
int num_onscreen = 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);*/ |
|
/*}*/ |
|
|
|
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; |
|
|
|
if (red) { |
|
SDL_SetRenderDrawColor(ren, 255, 0, 0, 255); |
|
} else { |
|
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; |
|
B = in_view(player.physics->position); |
|
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 update_viewport(double x, double y) { |
|
float xmargin = 0.5; |
|
float ymargin = 0.2; |
|
|
|
if (player.physics->position.x - viewport_pos.x > (1-xmargin) * width) { |
|
viewport_pos.x = - ((1-xmargin) * width - player.physics->position.x); |
|
} |
|
|
|
if (player.physics->position.x - viewport_pos.x < xmargin * width) { |
|
viewport_pos.x = -(xmargin * width - player.physics->position.x); |
|
} |
|
|
|
if (player.physics->position.y - viewport_pos.y > (1-ymargin) * height) { |
|
viewport_pos.y = -((1-ymargin) * height - player.physics->position.y); |
|
} |
|
|
|
if (player.physics->position.y - viewport_pos.y < ymargin * height) { |
|
viewport_pos.y = -(ymargin * height - player.physics->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_level_time(SDL_Renderer *ren, struct environment * e) { |
|
static TTF_Font* font = NULL; |
|
if (!font) { |
|
font = TTF_OpenFont("TerminusTTF.ttf", 18); |
|
const char *err = SDL_GetError(); |
|
if (err) { |
|
printf("%s\n", err); |
|
} |
|
} |
|
|
|
int margin = height / 10; |
|
SDL_Rect 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); |
|
|
|
SDL_Colour sdl_col = {colour.r, colour.g, colour.b, 255}; |
|
|
|
SDL_SetRenderDrawColor(ren, colour.r, colour.g, colour.b, 255); |
|
SDL_Surface *surf = TTF_RenderText_Solid(font, time_string, 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 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));*/ |
|
/*}*/ |
|
/*}*/ |
|
|
|
//SDL_GetMouseState(&newmousex, &newmousey); |
|
update_viewport(player.physics->position.x, player.physics->position.y); |
|
|
|
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 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); |
|
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; |
|
} |
|
} |
|
|
|
/*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]);*/ |
|
/*}*/ |
|
|
|
}
|
|
|