From 93b7faa36956b1fc1f48ad0b9efb8c092570827a Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 14 Jan 2025 10:54:27 -0500 Subject: [PATCH] Right before bringing in the code to render sprites, but after I restructured to have the sprite data. --- scratchpad/raycaster_sprites.cpp | 76 ++++++++++++++++---------------- sfmlcaster.cpp | 71 ++++++++++++++++++++--------- 2 files changed, 88 insertions(+), 59 deletions(-) diff --git a/scratchpad/raycaster_sprites.cpp b/scratchpad/raycaster_sprites.cpp index 8120204..3e641ce 100644 --- a/scratchpad/raycaster_sprites.cpp +++ b/scratchpad/raycaster_sprites.cpp @@ -412,44 +412,44 @@ int main(int /*argc*/, char */*argv*/[]) //speed modifiers double moveSpeed = frameTime * 3.0; //the constant value is in squares/second double rotSpeed = frameTime * 2.0; //the constant value is in radians/second - - SDL_Event event; - while(SDL_PollEvent(&event)) { - if(event.type != SDL_KEYDOWN) continue; - //move forward if no wall in front of you - if(event.key.keysym.sym == SDLK_UP) - { - if(worldMap[int(posX + dirX * moveSpeed)][int(posY)] == false) posX += dirX * moveSpeed; - if(worldMap[int(posX)][int(posY + dirY * moveSpeed)] == false) posY += dirY * moveSpeed; - } - //move backwards if no wall behind you - if(event.key.keysym.sym == SDLK_DOWN) - { - if(worldMap[int(posX - dirX * moveSpeed)][int(posY)] == false) posX -= dirX * moveSpeed; - if(worldMap[int(posX)][int(posY - dirY * moveSpeed)] == false) posY -= dirY * moveSpeed; - } - //rotate to the right - if(event.key.keysym.sym == SDLK_RIGHT) - { - //both camera direction and camera plane must be rotated - double oldDirX = dirX; - dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed); - dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed); - double oldPlaneX = planeX; - planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed); - planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed); - } - //rotate to the left - if(event.key.keysym.sym == SDLK_LEFT) - { - //both camera direction and camera plane must be rotated - double oldDirX = dirX; - dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed); - dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed); - double oldPlaneX = planeX; - planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed); - planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed); - } + readKeys(); + //move forward if no wall in front of you + if (keyDown(SDLK_UP)) + { + if(worldMap[int(posX + dirX * moveSpeed)][int(posY)] == false) posX += dirX * moveSpeed; + if(worldMap[int(posX)][int(posY + dirY * moveSpeed)] == false) posY += dirY * moveSpeed; + } + //move backwards if no wall behind you + if(keyDown(SDLK_DOWN)) + { + if(worldMap[int(posX - dirX * moveSpeed)][int(posY)] == false) posX -= dirX * moveSpeed; + if(worldMap[int(posX)][int(posY - dirY * moveSpeed)] == false) posY -= dirY * moveSpeed; + } + //rotate to the right + if(keyDown(SDLK_RIGHT)) + { + //both camera direction and camera plane must be rotated + double oldDirX = dirX; + dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed); + dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed); + double oldPlaneX = planeX; + planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed); + planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed); + } + //rotate to the left + if(keyDown(SDLK_LEFT)) + { + //both camera direction and camera plane must be rotated + double oldDirX = dirX; + dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed); + dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed); + double oldPlaneX = planeX; + planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed); + planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed); + } + if(keyDown(SDLK_ESCAPE)) + { + break; } } } diff --git a/sfmlcaster.cpp b/sfmlcaster.cpp index feb69f0..13b5119 100644 --- a/sfmlcaster.cpp +++ b/sfmlcaster.cpp @@ -11,6 +11,27 @@ using matrix::Matrix; using namespace fmt; +#define texWidth 64 // must be power of two +#define texHeight 64 // must be power of two + + +#define numSprites 1 +#define numTextures 11 + +struct Sprite { + double x; + double y; + int texture; +}; + +const int RAY_VIEW_WIDTH=1280; +const int RAY_VIEW_HEIGHT=720; +const int RAY_VIEW_X=0; +const int RAY_VIEW_Y=0; + +const int SCREEN_HEIGHT=RAY_VIEW_HEIGHT; +const int SCREEN_WIDTH=1280; + Matrix MAP{ {8,8,8,8,8,8,8,8,8}, {8,0,2,0,0,0,0,0,8}, @@ -23,14 +44,6 @@ Matrix MAP{ {8,8,8,8,8,8,8,8,8} }; -const int RAY_VIEW_WIDTH=1920; -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 MAP_SIZE=matrix::width(MAP); const int TILE_SIZE=RAY_VIEW_HEIGHT / MAP_SIZE; int PITCH=0; @@ -56,17 +69,24 @@ double planeY = 0.66; #define rgba_color(r,g,b,a) (r<<(0*8))|(g<<(1*8))|(b<<(2*8))|(a<<(3*8)) #define gray_color(c) rgba_color(c, c, c, 255) -std::vector texture[8]; -#define texWidth 256 // must be power of two -#define texHeight 256 // must be power of two +Sprite sprites[numSprites] = { + {20.5, 11.5, 8} +}; -#define pixcoord(X, Y) ((Y) * RAY_VIEW_WIDTH) + (X) +double ZBuffer[RAY_VIEW_WIDTH]; +int spriteOrder[numSprites]; +double spriteDistance[numSprites]; + +std::vector texture[numTextures]; +#define pixcoord(X, Y) ((Y) * RAY_VIEW_WIDTH) + (X) uint32_t pixels[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT] = {0}; sf::Texture view_texture; sf::Sprite view_sprite; +void sortSprites(int *order, double *dist, int amount); + void loadImage(std::vector& texture, const char *filename) { sf::Image img; bool good = img.loadFromFile(filename); @@ -76,18 +96,22 @@ void loadImage(std::vector& texture, const char *filename) { } void load_textures() { - for(int i = 0; i < 8; i++) { + for(int i = 0; i < numTextures; i++) { texture[i].resize(texWidth * texHeight); } - loadImage(texture[0], "pics/tile16.png"); - loadImage(texture[1], "pics/tile02.png"); - loadImage(texture[2], "pics/tile03.png"); - loadImage(texture[3], "pics/tile32.png"); - loadImage(texture[4], "pics/tile05.png"); - loadImage(texture[5], "pics/tile17.png"); - loadImage(texture[6], "pics/tile10.png"); - loadImage(texture[7], "pics/tile01.png"); + loadImage(texture[0], "pics/eagle.png"); + loadImage(texture[1], "pics/redbrick.png"); + loadImage(texture[2], "pics/purplestone.png"); + loadImage(texture[3], "pics/greystone.png"); + loadImage(texture[4], "pics/bluestone.png"); + loadImage(texture[5], "pics/mossy.png"); + loadImage(texture[6], "pics/wood.png"); + loadImage(texture[7], "pics/colorstone.png"); + + loadImage(texture[8], "pics/barrel.png"); + loadImage(texture[9], "pics/pillar.png"); + loadImage(texture[10], "pics/greenlight.png"); } void draw_sfml_rect(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, uint8_t color) { @@ -412,3 +436,8 @@ int main() { return 0; } + +void sortSprites(int* order, double* dist, int amount) +{ + +}