Now the sprites are rendered in the 3d scene with just SFML sprites.

master
Zed A. Shaw 2 months ago
parent da7075864b
commit ad38f575a3
  1. BIN
      assets/armored_knight_1-256.png
  2. BIN
      assets/evil_eye_test-256.png
  3. 19
      raycaster.cpp
  4. 68
      scratchpad/raycaster_sprites.cpp
  5. 3
      texture.cpp

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 59 KiB

@ -41,6 +41,7 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh
$view_sprite.setPosition({0, 0});
$pixels = make_unique<RGBA[]>($width * $height);
$textures.load_textures();
$view_texture.setSmooth(false);
}
void Raycaster::set_position(int x, int y) {
@ -111,11 +112,21 @@ void Raycaster::sprite_casting() {
// calculate width the the sprite
// same as height of sprite, given that it's square
int spriteWidth = abs(int($height / transformY)) / sprite_rec.uDiv;
int drawStartX = -spriteWidth / 2 + spriteScreenX;
if(drawStartX < 0) drawStartX = 0;
int drawEndX = spriteWidth / 2 + spriteScreenX;
if(drawEndX > $width) drawEndX = $width;
int stripe = drawStartX;
for(; stripe < drawEndX; stripe++) {
//the conditions in the if are:
//1) it's in front of camera plane so you don't see things behind you
//2) ZBuffer, with perpendicular distance
if(!(transformY > 0 && transformY < ZBuffer[stripe])) break;
}
int texX_end = int(256 * (stripe - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / 256;
if(drawStartX < drawEndX && transformY > 0 && transformY < ZBuffer[drawStartX]) {
int vMoveScreen = int(sprite_rec.elevation * -1 / transformY);
@ -125,18 +136,18 @@ void Raycaster::sprite_casting() {
int drawEndY = spriteHeight / 2 + $height / 2 + vMoveScreen;
if(drawEndY >= $height) drawEndY = $height - 1;
int texX = int(256 * (drawStartX - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / 256;
float x = float(drawStartX + RAY_VIEW_X);
float y = float(drawStartY + RAY_VIEW_Y);
float sprite_w = float(spriteWidth) / float(textureWidth);
float sprite_h = float(spriteHeight) / float(textureHeight);
int texX = int(256 * (drawStartX - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / 256;
int texX_end = int(256 * (drawEndX - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / 256;
int d = (y - vMoveScreen) * 256 - $height * 128 + spriteHeight * 128;
int texY = ((d * textureHeight) / spriteHeight) / 256;
sf_sprite->setScale({sprite_h, sprite_w});
sf_sprite->setTextureRect(sf::IntRect({texX, texY}, {texX_end, textureHeight}));
sf_sprite->setScale({sprite_w, sprite_h});
sf_sprite->setTextureRect(sf::IntRect({texX, texY}, {texX_end - texX, textureHeight}));
sf_sprite->setPosition({x, y});
$window.draw(*sf_sprite);
}

@ -1,25 +1,25 @@
/*
Copyright (c) 2004-2020, Lode Vandevenne
Copyright (c) 2004-2020, Lode Vandevenne
All rights reserved.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cmath>
#include <string>
@ -30,9 +30,9 @@
using namespace QuickCG;
/*
g++ *.cpp -lSDL -O3 -W -Wall -ansi -pedantic
g++ *.cpp -lSDL
*/
g++ *.cpp -lSDL -O3 -W -Wall -ansi -pedantic
g++ *.cpp -lSDL
*/
#define screenWidth 640
@ -135,14 +135,14 @@ int main(int /*argc*/, char */*argv*/[])
//load some textures
unsigned long tw, th, error = 0;
error |= loadImage(texture[0], tw, th, "assets/eagle.png");
error |= loadImage(texture[1], tw, th, "assets/redbrick.png");
error |= loadImage(texture[2], tw, th, "assets/purplestone.png");
error |= loadImage(texture[3], tw, th, "assets/greystone.png");
error |= loadImage(texture[4], tw, th, "assets/bluestone.png");
error |= loadImage(texture[5], tw, th, "assets/mossy.png");
error |= loadImage(texture[6], tw, th, "assets/wood.png");
error |= loadImage(texture[7], tw, th, "assets/colorstone.png");
error |= loadImage(texture[0], tw, th, "pics/eagle.png");
error |= loadImage(texture[1], tw, th, "pics/redbrick.png");
error |= loadImage(texture[2], tw, th, "pics/purplestone.png");
error |= loadImage(texture[3], tw, th, "pics/greystone.png");
error |= loadImage(texture[4], tw, th, "pics/bluestone.png");
error |= loadImage(texture[5], tw, th, "pics/mossy.png");
error |= loadImage(texture[6], tw, th, "pics/wood.png");
error |= loadImage(texture[7], tw, th, "pics/colorstone.png");
if(error) { std::cout << "error loading images" << std::endl; return 1; }
//load some sprite textures
@ -335,11 +335,7 @@ int main(int /*argc*/, char */*argv*/[])
for(int i = 0; i < numSprites; i++)
{
spriteOrder[i] = i;
spriteDistance[i] =
((posX - sprite[i].x) *
(posX - sprite[i].x) +
(posY - sprite[i].y) *
(posY - sprite[i].y)); //sqrt not taken, unneeded
spriteDistance[i] = ((posX - sprite[i].x) * (posX - sprite[i].x) + (posY - sprite[i].y) * (posY - sprite[i].y)); //sqrt not taken, unneeded
}
sortSprites(spriteOrder, spriteDistance, numSprites);
@ -363,9 +359,9 @@ int main(int /*argc*/, char */*argv*/[])
int spriteScreenX = int((w / 2) * (1 + transformX / transformY));
//parameters for scaling and moving the sprites
#define uDiv 1
#define vDiv 1
#define vMove 0.0
#define uDiv 1
#define vDiv 1
#define vMove 0.0
int vMoveScreen = int(vMove / transformY);
//calculate height of the sprite on screen

@ -29,7 +29,8 @@ void TexturePack::load_textures() {
floor = load_image(assets["floor"]);
ceiling = load_image(assets["ceiling"]);
sf::Texture* sprite_texture = new sf::Texture("assets/undead_peasant-256.png");
sf::Texture* sprite_texture = new sf::Texture("assets/evil_eye_test-256.png");
sprite_texture->setSmooth(false);
sf::Sprite* sf_sprite = new sf::Sprite(*sprite_texture);
sprites.push_back({4.0, 3.55, 6, sf_sprite, sprite_texture});

Loading…
Cancel
Save