@ -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 ) ;