Skip to content

Commit

Permalink
Integer-only arithmetic when drawing the vertical wall strips
Browse files Browse the repository at this point in the history
  • Loading branch information
wernsey committed Mar 6, 2023
1 parent 71797cc commit e0f5cbb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ The rest of the original tutorials are here [Lode's Computer Graphics Tutorial][

## Step 1

* Fixed som compiler warnings
commit 71797ccd2dc8cd6d633d8ef4bb3a4e2ccc1cc242

* Fixed some compiler warnings
* Replaced the Wolfenstein graphics with CC0 graphics from Dungeon Crawl Stone Soup (DCSS)

The Dungeon Crawl Stone Soup graphics are from here:
<https://opengameart.org/content/dungeon-crawl-32x32-tiles>

# Step 2: Better vertical strips

Step 2 is to convert the code that

37 changes: 35 additions & 2 deletions raycaster_sprites.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,7 @@ int main(int /*argc*/, char */*argv*/[])

//calculate lowest and highest pixel to fill in current stripe
int drawStart = -lineHeight / 2 + h / 2;
if(drawStart < 0) drawStart = 0;
int drawEnd = lineHeight / 2 + h / 2;
if(drawEnd >= h) drawEnd = h - 1;
//texturing calculations
int texNum = worldMap[mapX][mapY] - 1; //1 subtracted from it so that texture 0 can be used!

Expand All @@ -310,6 +308,40 @@ int main(int /*argc*/, char */*argv*/[])
if(side == 0 && rayDirX > 0) texX = texWidth - texX - 1;
if(side == 1 && rayDirY < 0) texX = texWidth - texX - 1;

#if 1
int texY = 0, c = 0;
int dy = drawEnd - drawStart;

if(drawStart < 0) {
c = -drawStart * texHeight;
if(c > dy) {
div_t res = div(c, dy);
texY += res.quot;
c = res.rem;
}
drawStart = 0;
}
if(drawEnd >= h)
drawEnd = (h-1);

for(int y = drawStart; y < drawEnd; y++) {

Uint32 color = texture[texNum][texHeight * texY + texX];
//make color darker for y-sides: R, G and B byte each divided through two with a "shift" and an "and"
if(side == 1) color = (color >> 1) & 8355711;
buffer[y][x] = color;

c += texHeight;
while(c > dy) {
texY++;
c -= dy;
}
}

#else
if(drawStart < 0) drawStart = 0;
if(drawEnd >= h) drawEnd = h - 1;
// TODO: an integer-only bresenham or DDA like algorithm could make the texture coordinate stepping faster
// How much to increase the texture coordinate per screen pixel
double step = 1.0 * texHeight / lineHeight;
Expand All @@ -325,6 +357,7 @@ int main(int /*argc*/, char */*argv*/[])
if(side == 1) color = (color >> 1) & 8355711;
buffer[y][x] = color;
}
#endif

//SET THE ZBUFFER FOR THE SPRITE CASTING
ZBuffer[x] = perpWallDist; //perpendicular distance is used
Expand Down

0 comments on commit e0f5cbb

Please sign in to comment.