Now rendering at 1080p with no map, and can render the raycasting side at arbitrary dimensions.

master
Zed A. Shaw 2 months ago
parent eee66720b7
commit 0828fb584e
  1. 63
      sfmlcaster.cpp

@ -23,26 +23,23 @@ Matrix MAP{
{8,8,8,8,8,8,8,8,8} {8,8,8,8,8,8,8,8,8}
}; };
const int SCREEN_HEIGHT=480; const int RAY_VIEW_WIDTH=1920;
const int SCREEN_WIDTH=SCREEN_HEIGHT * 2; const int RAY_VIEW_HEIGHT=1080;
const int RAY_VIEW_X=0;
const int RAY_VIEW_Y=0;
const int SCREEN_HEIGHT=RAY_VIEW_HEIGHT;
const int SCREEN_WIDTH=1920;
const int THREED_VIEW_WIDTH=480;
const int THREED_VIEW_HEIGHT=480;
const int MAP_SIZE=matrix::width(MAP); const int MAP_SIZE=matrix::width(MAP);
const int TILE_SIZE=(SCREEN_WIDTH/2) / MAP_SIZE; const int TILE_SIZE=RAY_VIEW_HEIGHT / MAP_SIZE;
const float FOV = std::numbers::pi / 3.0;
const float HALF_FOV = FOV / 2;
const int CASTED_RAYS=120;
const float STEP_ANGLE = FOV / CASTED_RAYS;
const int MAX_DEPTH = MAP_SIZE * TILE_SIZE;
const float SCALE = (SCREEN_WIDTH / 2) / CASTED_RAYS;
int PITCH=0; int PITCH=0;
// I chose fixed textures for this instead // I chose fixed textures for this instead
const int floorTexture = 3; const int floorTexture = 3;
const int ceilingTexture = 6; const int ceilingTexture = 6;
float player_x = SCREEN_WIDTH / 4; float player_x = RAY_VIEW_HEIGHT / 2;
float player_y = SCREEN_WIDTH / 4; float player_y = RAY_VIEW_HEIGHT / 2;
// x and y start position // x and y start position
double posX = player_x / TILE_SIZE; double posX = player_x / TILE_SIZE;
@ -63,9 +60,9 @@ std::vector<uint32_t> texture[8];
#define texWidth 256 // must be power of two #define texWidth 256 // must be power of two
#define texHeight 256 // must be power of two #define texHeight 256 // must be power of two
#define pixcoord(X, Y) ((Y) * SCREEN_HEIGHT) + (X) #define pixcoord(X, Y) ((Y) * RAY_VIEW_WIDTH) + (X)
uint32_t pixels[SCREEN_HEIGHT * SCREEN_HEIGHT] = {0}; uint32_t pixels[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT] = {0};
sf::Texture view_texture; sf::Texture view_texture;
sf::Sprite view_sprite; sf::Sprite view_sprite;
@ -100,22 +97,8 @@ void draw_sfml_rect(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f siz
window.draw(rect); window.draw(rect);
} }
void draw_pixel_rect(sf::RenderWindow &window, Point pos, Point size, uint32_t color) {
size_t x_start = size_t(pos.x - SCREEN_HEIGHT);
size_t y_start = size_t(pos.y);
size_t width = size_t(size.x);
size_t height = size_t(size.y);
for(size_t y = y_start; y < y_start + height; y++) {
for(size_t x = x_start; x < x_start + width; x++) {
size_t pixel_index = (y * SCREEN_HEIGHT) + x;
pixels[pixel_index] = color;
}
}
}
void draw_pixel_buffer(sf::RenderWindow &window) { void draw_pixel_buffer(sf::RenderWindow &window) {
view_texture.update((uint8_t *)pixels, SCREEN_HEIGHT, SCREEN_HEIGHT, 0, 0); view_texture.update((uint8_t *)pixels, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT, 0, 0);
// BUG: can I do this once and just update it? // BUG: can I do this once and just update it?
window.draw(view_sprite); window.draw(view_sprite);
} }
@ -167,7 +150,7 @@ void draw_line(sf::RenderWindow &window, Point start, Point end, uint32_t color)
} }
void clear(sf::RenderWindow &window) { void clear(sf::RenderWindow &window) {
std::fill_n(pixels, SCREEN_HEIGHT * SCREEN_HEIGHT, 0); std::fill_n(pixels, RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT, 0);
window.clear(); window.clear();
} }
@ -176,8 +159,8 @@ void draw_map_blocks(sf::RenderWindow &window, int col, int row) {
} }
void ray_casting(sf::RenderWindow &window, Matrix& map) { void ray_casting(sf::RenderWindow &window, Matrix& map) {
int w = THREED_VIEW_WIDTH; int w = RAY_VIEW_WIDTH;
int h = THREED_VIEW_HEIGHT; int h = RAY_VIEW_HEIGHT;
for(int x = 0; x < w; x++) { for(int x = 0; x < w; x++) {
// calculate ray position and direction // calculate ray position and direction
@ -241,9 +224,7 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) {
perpWallDist = (sideDistY - deltaDistY); perpWallDist = (sideDistY - deltaDistY);
} }
draw_map_blocks(window, mapX, mapY); // draw_map_blocks(window, mapX, mapY);
// TODO: player direction ray
int lineHeight = int(h / perpWallDist); int lineHeight = int(h / perpWallDist);
@ -287,8 +268,8 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) {
} }
void draw_ceiling_floor(sf::RenderWindow &window) { void draw_ceiling_floor(sf::RenderWindow &window) {
int screenHeight = THREED_VIEW_HEIGHT; int screenHeight = RAY_VIEW_HEIGHT;
int screenWidth = THREED_VIEW_HEIGHT; int screenWidth = RAY_VIEW_WIDTH;
for(int y = screenHeight / 2 + 1; y < screenHeight; ++y) { for(int y = screenHeight / 2 + 1; y < screenHeight; ++y) {
// rayDir for leftmost ray (x=0) and rightmost (x = w) // rayDir for leftmost ray (x=0) and rightmost (x = w)
@ -353,7 +334,7 @@ void draw_ceiling_floor(sf::RenderWindow &window) {
void draw_everything(sf::RenderWindow &window) { void draw_everything(sf::RenderWindow &window) {
clear(window); clear(window);
draw_map(window, MAP); // draw_map(window, MAP);
draw_ceiling_floor(window); draw_ceiling_floor(window);
ray_casting(window, MAP); ray_casting(window, MAP);
draw_pixel_buffer(window); draw_pixel_buffer(window);
@ -374,9 +355,9 @@ int main() {
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "SFMLCaster"); sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "SFMLCaster");
window.setVerticalSyncEnabled(true); window.setVerticalSyncEnabled(true);
view_texture.create(SCREEN_HEIGHT, SCREEN_HEIGHT); view_texture.create(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT);
view_sprite.setTexture(view_texture); view_sprite.setTexture(view_texture);
view_sprite.setPosition(THREED_VIEW_WIDTH, 0); view_sprite.setPosition(RAY_VIEW_X, 0);
load_textures(); load_textures();

Loading…
Cancel
Save