- Prototip de dibuixat de sprites funcional
This commit is contained in:
37
main.cpp
37
main.cpp
@@ -103,6 +103,16 @@ void putpd(int x, int y, Uint8 color, float depth)
|
||||
depth_buffer[x+y*320]=depth;
|
||||
}
|
||||
|
||||
void putps(int x, int y, Uint8 color, float depth)
|
||||
{
|
||||
if (x<0 || y<0 || x>=320 || y>=240) return;
|
||||
if (depth_buffer[x+y*320]>=depth)
|
||||
{
|
||||
screen[x+y*320]=color;
|
||||
depth_buffer[x+y*320]=depth;
|
||||
}
|
||||
}
|
||||
|
||||
void line(int x1, int y1, int x2, int y2, Uint8 color)
|
||||
{
|
||||
float dx = float(x2-x1);
|
||||
@@ -258,7 +268,7 @@ void drawColumn(sector &s, int screen_column, int start, int end, float a_inc, v
|
||||
float actual_dist = straight_dist / SDL_cosf(a_inc*DEG_TO_RAD);
|
||||
int tx = abs(int(actual_dist * SDL_cosf(angle*DEG_TO_RAD) - position.x)) % tex_height;
|
||||
int ty = abs(int(actual_dist * SDL_sinf(angle*DEG_TO_RAD) - position.y)) % tex_height;
|
||||
putpd(screen_column, y, gif[tx+ty*tex_height], straight_dist);
|
||||
putpd(screen_column, y, gif[tx+ty*tex_height], -straight_dist);
|
||||
}
|
||||
|
||||
|
||||
@@ -419,6 +429,9 @@ int main(int argc, char *argv[])
|
||||
palette = LoadPalette(buffer);
|
||||
free(buffer);
|
||||
|
||||
// [DEBUG] Paleta per al depth buffer
|
||||
//for(int i=0;i<256;++i) palette[i] = (255-i) | ((255-i)<<8) | ((255-i)<<16);
|
||||
|
||||
createMap();
|
||||
|
||||
SDL_Event e;
|
||||
@@ -517,11 +530,26 @@ int main(int argc, char *argv[])
|
||||
map::putp(enemy.x, enemy.y, 9);
|
||||
|
||||
float angle_to_enemy = is_enemy_in_fov(position.x, position.y, orientation, enemy.x, enemy.y);
|
||||
if (SDL_fabsf(angle_to_enemy) <= 32.0f)
|
||||
if (SDL_fabsf(angle_to_enemy) <= 64.0f)
|
||||
{
|
||||
const float d = distance(position, enemy);// * SDL_cosf(a_inc*DEG_TO_RAD);
|
||||
float dist = d * SDL_cosf(angle_to_enemy*DEG_TO_RAD);
|
||||
float wall_height = (32.0f*277)/dist;
|
||||
debug::print("enemy in fov: "); debug::print(angle_to_enemy); debug::newline();
|
||||
int column = int((angle_to_enemy+32.0f)/0.2f);
|
||||
line(column, 0, column, 239, 0);
|
||||
for (int i=0; i<wall_height;++i)
|
||||
{
|
||||
int tx = i * 32 / wall_height;
|
||||
const int c = column - (wall_height/2)+i;
|
||||
if (c>=0 && c<320) {
|
||||
for (int j=0;j<wall_height;++j)
|
||||
{
|
||||
int ty = j * 32 / wall_height;
|
||||
putps(c, 120-(wall_height/2)+j, gif[tx+ty*64], d);
|
||||
}
|
||||
}
|
||||
}
|
||||
//line(column, 0, column, 239, 0);
|
||||
}
|
||||
/*
|
||||
float angle = SDL_atan2f(enemy.y-position.y, enemy.x-position.x)*RAD_TO_DEG;
|
||||
@@ -557,6 +585,9 @@ int main(int argc, char *argv[])
|
||||
for (int i=0; i<(320*240); ++i)
|
||||
{
|
||||
pixels[i] = 0xFF000000 | palette[screen[i]];
|
||||
|
||||
// [DEBUG] Visualització del depth buffer
|
||||
//pixels[i] = 0xFF000000 | palette[SDL_min(int(depth_buffer[i]),255)];
|
||||
}
|
||||
SDL_UnlockTexture(sdl_texture);
|
||||
SDL_RenderTexture(sdl_renderer, sdl_texture, NULL, NULL);
|
||||
|
||||
Reference in New Issue
Block a user