@ -32,15 +32,27 @@ SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) {
@@ -32,15 +32,27 @@ SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) {
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 ) {
logwrite ( DEBUG , " Drawing player \n " ) ;
// 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 = x ;
player_rect . y = y ;
player_rect . x = V . x - 5 ;
player_rect . y = V . y - 5 ;
player_rect . w = 10 ;
player_rect . h = 10 ;
@ -68,32 +80,36 @@ void draw_collision_poly(SDL_Renderer* ren, Body *body) {
@@ -68,32 +80,36 @@ void draw_collision_poly(SDL_Renderer* ren, Body *body) {
}
if ( body - > collision_poly_size = = 1 ) {
SDL_Rect dot ;
dot . x = body - > collision_poly [ 0 ] . x ;
dot . y = body - > collision_poly [ 0 ] . y ;
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 ;
int y_st = body - > collision_poly [ 0 ] . y ;
int x_en = body - > collision_poly [ 1 ] . x ;
int y_en = body - > collision_poly [ 1 ] . y ;
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 ;
int y_st = body - > collision_poly [ 0 ] . y ;
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 ;
y_en = body - > collision_poly [ i ] . y ;
x_en = body - > collision_poly [ i ] . x - viewport_pos . x ;
y_en = body - > collision_poly [ i ] . y - viewport_pos . y ;
printf ( " colpoly: %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 ;
}
x_en = body - > collision_poly [ 0 ] . x ;
y_en = body - > collision_poly [ 0 ] . y ;
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 ) ;
@ -105,8 +121,8 @@ void draw_forces(SDL_Renderer *ren, Body *body) {
@@ -105,8 +121,8 @@ void draw_forces(SDL_Renderer *ren, Body *body) {
return ;
}
Vect start ;
start . x = ( int ) body - > x_pos ;
start . y = ( int ) body - > y_pos ;
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 + + ) {
@ -122,27 +138,24 @@ void draw_forces(SDL_Renderer *ren, Body *body) {
@@ -122,27 +138,24 @@ void draw_forces(SDL_Renderer *ren, Body *body) {
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 - > x_pos ;
y_st = wall - > nodes [ 0 ] . y + wall - > physics - > y_pos ;
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 - > x_pos ;
y_en = wall - > nodes [ i ] . y + wall - > physics - > y_pos ;
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 ;
}
x_en = wall - > nodes [ 0 ] . x + wall - > physics - > x_pos ;
y_en = wall - > nodes [ 0 ] . y + wall - > physics - > y_pos ;
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 ) ;
@ -167,7 +180,12 @@ int distance_colour(int x, int y, int x2, int y2, int blackpoint) {
@@ -167,7 +180,12 @@ int distance_colour(int x, int y, int x2, int y2, int blackpoint) {
}
return frac ;
}
void update_viewport ( double x , double y ) {
viewport_pos . x = x - 300 ;
viewport_pos . y = y - 300 ;
}
@ -190,6 +208,7 @@ void redraw_buffer(SDL_Renderer * ren) {
@@ -190,6 +208,7 @@ void redraw_buffer(SDL_Renderer * ren) {
int col = 0 ;
//SDL_GetMouseState(&newmousex, &newmousey);
update_viewport ( player . physics - > position . x , player . physics - > position . y ) ;
for ( int i = 0 ; i < things_in_world ; i + + ) {
world_thing thing ;
@ -198,9 +217,8 @@ void redraw_buffer(SDL_Renderer * ren) {
@@ -198,9 +217,8 @@ void redraw_buffer(SDL_Renderer * ren) {
switch ( thing . kind ) {
case PLAYER :
newmousex = ( * thing . player ) . physics - > x_pos ;
newmousey = ( * thing . player ) . physics - > y_pos ;
draw_player ( ren , ( * thing . player ) . physics - > x_pos , ( * thing . player ) . physics - > y_pos , thing . player - > colliding ) ;
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 ) ;
break ;