Amit's code mostly converted to use the new texture.hpp but there's an error on line amt/pixel.hpp:472
This commit is contained in:
parent
c91e8fc543
commit
4d31a4daf2
9 changed files with 98 additions and 92 deletions
18
amt/main.cpp
18
amt/main.cpp
|
@ -13,15 +13,15 @@ static const int SCREEN_HEIGHT=720;
|
||||||
static const int SCREEN_WIDTH=1280;
|
static const int SCREEN_WIDTH=1280;
|
||||||
|
|
||||||
Matrix MAP{
|
Matrix MAP{
|
||||||
{8,8,8,8,8,8,8,8,8},
|
{1,1,1,1,1,1,1,1,1},
|
||||||
{8,0,2,0,0,0,0,0,8},
|
{1,0,2,0,0,0,0,0,1},
|
||||||
{8,0,7,0,0,5,6,0,8},
|
{1,0,4,0,0,5,2,0,1},
|
||||||
{8,0,0,0,0,0,0,0,8},
|
{1,0,0,0,0,0,0,0,1},
|
||||||
{8,8,0,0,0,0,0,8,8},
|
{1,1,0,0,0,0,0,1,1},
|
||||||
{8,0,0,1,3,4,0,0,8},
|
{1,0,0,1,3,4,0,0,1},
|
||||||
{8,0,0,0,0,0,8,8,8},
|
{1,0,0,0,0,0,1,1,1},
|
||||||
{8,0,0,0,0,0,0,0,8},
|
{1,0,0,0,0,0,0,0,1},
|
||||||
{8,8,8,8,8,8,8,8,8}
|
{1,1,1,1,1,1,1,1,1}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
|
@ -1,42 +1,10 @@
|
||||||
#include "amt/raycaster.hpp"
|
#include "amt/raycaster.hpp"
|
||||||
#include "pixel.hpp"
|
#include "amt/texture.hpp"
|
||||||
|
#include "amt/pixel.hpp"
|
||||||
|
|
||||||
using namespace fmt;
|
using namespace fmt;
|
||||||
using std::make_unique;
|
using std::make_unique;
|
||||||
|
|
||||||
#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)
|
|
||||||
|
|
||||||
|
|
||||||
amt::PixelBuf TexturePack::load_image(const char *filename) {
|
|
||||||
sf::Image img;
|
|
||||||
bool good = img.loadFromFile(filename);
|
|
||||||
dbc::check(good, format("failed to load {}", filename));
|
|
||||||
return amt::PixelBuf(img.getPixelsPtr(), TEXTURE_HEIGHT, TEXTURE_WIDTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TexturePack::load_textures() {
|
|
||||||
images.emplace_back(load_image("assets/tile16.png"));
|
|
||||||
images.emplace_back(load_image("assets/tile02.png"));
|
|
||||||
images.emplace_back(load_image("assets/tile03.png"));
|
|
||||||
images.emplace_back(load_image("assets/tile32.png"));
|
|
||||||
images.emplace_back(load_image("assets/tile05.png"));
|
|
||||||
images.emplace_back(load_image("assets/tile17.png"));
|
|
||||||
images.emplace_back(load_image("assets/tile10.png"));
|
|
||||||
images.emplace_back(load_image("assets/tile01.png"));
|
|
||||||
images.emplace_back(load_image("assets/portal.png"));
|
|
||||||
}
|
|
||||||
|
|
||||||
amt::PixelBuf& TexturePack::get(size_t num) {
|
|
||||||
return images[num];
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprite &TexturePack::get_sprite(size_t sprite_num) {
|
|
||||||
return SPRITE[sprite_num];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) :
|
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) :
|
||||||
$width(width), $height(height),
|
$width(width), $height(height),
|
||||||
pixels(static_cast<size_t>(height), static_cast<std::size_t>(width)),
|
pixels(static_cast<size_t>(height), static_cast<std::size_t>(width)),
|
||||||
|
@ -80,12 +48,13 @@ void Raycaster::sprite_casting() {
|
||||||
|
|
||||||
// sort sprites from far to close
|
// sort sprites from far to close
|
||||||
for(int i = 0; i < textures.NUM_SPRITES; i++) {
|
for(int i = 0; i < textures.NUM_SPRITES; i++) {
|
||||||
|
auto& sprite = textures.get_sprite(i);
|
||||||
spriteOrder[i] = i;
|
spriteOrder[i] = i;
|
||||||
// this is just the distance calculation
|
// this is just the distance calculation
|
||||||
spriteDistance[i] = ((posX - textures.SPRITE[i].x) *
|
spriteDistance[i] = ((posX - sprite.x) *
|
||||||
(posX - textures.SPRITE[i].x) +
|
(posX - sprite.x) +
|
||||||
(posY - textures.SPRITE[i].y) *
|
(posY - sprite.y) *
|
||||||
(posY - textures.SPRITE[i].y));
|
(posY - sprite.y));
|
||||||
}
|
}
|
||||||
|
|
||||||
sort_sprites(spriteOrder, spriteDistance, textures.NUM_SPRITES);
|
sort_sprites(spriteOrder, spriteDistance, textures.NUM_SPRITES);
|
||||||
|
@ -94,9 +63,9 @@ void Raycaster::sprite_casting() {
|
||||||
for(int i = 0; i < textures.NUM_SPRITES; i++) {
|
for(int i = 0; i < textures.NUM_SPRITES; i++) {
|
||||||
int sprite_index = spriteOrder[i];
|
int sprite_index = spriteOrder[i];
|
||||||
Sprite& sprite_rec = textures.get_sprite(sprite_index);
|
Sprite& sprite_rec = textures.get_sprite(sprite_index);
|
||||||
|
auto& sprite_texture = textures.get_texture(sprite_rec.texture);
|
||||||
double spriteX = sprite_rec.x - posX;
|
double spriteX = sprite_rec.x - posX;
|
||||||
double spriteY = sprite_rec.y - posY;
|
double spriteY = sprite_rec.y - posY;
|
||||||
auto& sprite_texture = textures.get(sprite_rec.texture);
|
|
||||||
|
|
||||||
//transform sprite with the inverse camera matrix
|
//transform sprite with the inverse camera matrix
|
||||||
// [ planeX dirX ] -1 [ dirY -dirX ]
|
// [ planeX dirX ] -1 [ dirY -dirX ]
|
||||||
|
@ -229,7 +198,7 @@ void Raycaster::cast_rays() {
|
||||||
int drawEnd = lineHeight / 2 + $height / 2 + PITCH;
|
int drawEnd = lineHeight / 2 + $height / 2 + PITCH;
|
||||||
if(drawEnd >= $height) drawEnd = $height - 1;
|
if(drawEnd >= $height) drawEnd = $height - 1;
|
||||||
|
|
||||||
auto &texture = textures.get($map[mapY][mapX] - 1);
|
auto &texture = textures.get_texture($map[mapY][mapX] - 1);
|
||||||
|
|
||||||
// calculate value of wallX
|
// calculate value of wallX
|
||||||
double wallX; // where exactly the wall was hit
|
double wallX; // where exactly the wall was hit
|
||||||
|
@ -267,9 +236,6 @@ void Raycaster::draw_ceiling_floor() {
|
||||||
const size_t textureWidth = textures.TEXTURE_WIDTH;
|
const size_t textureWidth = textures.TEXTURE_WIDTH;
|
||||||
const size_t textureHeight = textures.TEXTURE_HEIGHT;
|
const size_t textureHeight = textures.TEXTURE_HEIGHT;
|
||||||
|
|
||||||
auto& floorTexture = textures.get(textures.floor);
|
|
||||||
auto& ceilingTexture = textures.get(textures.ceiling);
|
|
||||||
|
|
||||||
for(int y = $height / 2 + 1; y < $height; ++y) {
|
for(int y = $height / 2 + 1; y < $height; ++y) {
|
||||||
// rayDir for leftmost ray (x=0) and rightmost (x = w)
|
// rayDir for leftmost ray (x=0) and rightmost (x = w)
|
||||||
float rayDirX0 = dirX - planeX;
|
float rayDirX0 = dirX - planeX;
|
||||||
|
@ -320,10 +286,10 @@ void Raycaster::draw_ceiling_floor() {
|
||||||
// floorX cellX to find the texture x/y. How?
|
// floorX cellX to find the texture x/y. How?
|
||||||
|
|
||||||
// FLOOR
|
// FLOOR
|
||||||
pixels[y][x] = floorTexture[ty][tx];
|
pixels[y][x] = textures.floor[ty][tx];
|
||||||
|
|
||||||
// CEILING
|
// CEILING
|
||||||
pixels[$height - y - 1][x] = ceilingTexture[ty][tx];
|
pixels[$height - y - 1][x] = textures.ceiling[ty][tx];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,40 +10,12 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include "dbc.hpp"
|
#include "dbc.hpp"
|
||||||
#include "pixel.hpp"
|
#include "amt/pixel.hpp"
|
||||||
|
#include "amt/texture.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
using Matrix = amt::Matrix<int>;
|
using Matrix = amt::Matrix<int>;
|
||||||
|
|
||||||
struct Sprite {
|
|
||||||
double x;
|
|
||||||
double y;
|
|
||||||
int texture;
|
|
||||||
// ZED: this should be a separate transform parameter
|
|
||||||
double elevation=0;
|
|
||||||
int uDiv=1;
|
|
||||||
int vDiv=1;
|
|
||||||
};
|
|
||||||
|
|
||||||
using RGBA = uint32_t;
|
|
||||||
|
|
||||||
struct TexturePack {
|
|
||||||
int NUM_SPRITES=1;
|
|
||||||
int NUM_TEXTURES=11;
|
|
||||||
int TEXTURE_WIDTH=256; // must be power of two
|
|
||||||
int TEXTURE_HEIGHT=256; // must be power of two
|
|
||||||
|
|
||||||
std::vector<amt::PixelBuf> images;
|
|
||||||
std::vector<Sprite> SPRITE{{4.0, 3.55, 8}};
|
|
||||||
const int floor = 3;
|
|
||||||
const int ceiling = 6;
|
|
||||||
|
|
||||||
void load_textures();
|
|
||||||
amt::PixelBuf load_image(const char *filename);
|
|
||||||
Sprite &get_sprite(size_t sprite_num);
|
|
||||||
amt::PixelBuf& get(size_t num);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Raycaster {
|
struct Raycaster {
|
||||||
int PITCH=0;
|
int PITCH=0;
|
||||||
|
|
||||||
|
|
34
amt/texture.cpp
Normal file
34
amt/texture.cpp
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#include <SFML/Graphics/Image.hpp>
|
||||||
|
#include "dbc.hpp"
|
||||||
|
#include <fmt/core.h>
|
||||||
|
#include "config.hpp"
|
||||||
|
#include "amt/texture.hpp"
|
||||||
|
|
||||||
|
Image TexturePack::load_image(std::string filename) {
|
||||||
|
sf::Image img;
|
||||||
|
bool good = img.loadFromFile(filename);
|
||||||
|
dbc::check(good, format("failed to load {}", filename));
|
||||||
|
return amt::PixelBuf(img.getPixelsPtr(), TEXTURE_HEIGHT, TEXTURE_WIDTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TexturePack::load_textures() {
|
||||||
|
Config assets("assets/config.json");
|
||||||
|
for(string tile_path : assets["textures"]) {
|
||||||
|
images.emplace_back(load_image(tile_path));
|
||||||
|
}
|
||||||
|
|
||||||
|
for(string tile_path : assets["sprites"]) {
|
||||||
|
images.emplace_back(load_image(tile_path));
|
||||||
|
}
|
||||||
|
|
||||||
|
floor = load_image(assets["floor"]);
|
||||||
|
ceiling = load_image(assets["ceiling"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Image& TexturePack::get_texture(size_t num) {
|
||||||
|
return images[num];
|
||||||
|
}
|
||||||
|
|
||||||
|
Sprite &TexturePack::get_sprite(size_t sprite_num) {
|
||||||
|
return sprites[sprite_num];
|
||||||
|
}
|
34
amt/texture.hpp
Normal file
34
amt/texture.hpp
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include "amt/pixel.hpp"
|
||||||
|
|
||||||
|
struct Sprite {
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
int texture;
|
||||||
|
// ZED: this should be a separate transform parameter
|
||||||
|
double elevation=0;
|
||||||
|
int uDiv=1;
|
||||||
|
int vDiv=1;
|
||||||
|
};
|
||||||
|
|
||||||
|
using Image = amt::PixelBuf;
|
||||||
|
|
||||||
|
struct TexturePack {
|
||||||
|
int NUM_SPRITES=1;
|
||||||
|
static const int TEXTURE_WIDTH=256; // must be power of two
|
||||||
|
static const int TEXTURE_HEIGHT=256; // must be power of two
|
||||||
|
|
||||||
|
std::vector<amt::PixelBuf> images;
|
||||||
|
std::vector<Sprite> sprites{{4.0, 3.55, 6}};
|
||||||
|
Image floor;
|
||||||
|
Image ceiling;
|
||||||
|
|
||||||
|
void load_textures();
|
||||||
|
amt::PixelBuf load_image(std::string filename);
|
||||||
|
Sprite& get_sprite(size_t sprite_num);
|
||||||
|
Image& get_texture(size_t num);
|
||||||
|
};
|
|
@ -11,20 +11,20 @@ Config::Config(const std::string src_path) : $src_path(src_path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
json &Config::operator[](const std::string &key) {
|
json &Config::operator[](const std::string &key) {
|
||||||
dbc::check($config.contains(key), format("ERROR in config, key {} doesn't exist.", key));
|
dbc::check($config.contains(key), fmt::format("ERROR in config, key {} doesn't exist.", key));
|
||||||
return $config[key];
|
return $config[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring Config::wstring(const std::string key) {
|
std::wstring Config::wstring(const std::string key) {
|
||||||
dbc::check($config.contains(key), format("ERROR wstring in config, key {} doesn't exist.", key));
|
dbc::check($config.contains(key), fmt::format("ERROR wstring in config, key {} doesn't exist.", key));
|
||||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
|
||||||
const std::string& str_val = $config[key];
|
const std::string& str_val = $config[key];
|
||||||
return $converter.from_bytes(str_val);
|
return $converter.from_bytes(str_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring Config::wstring(const std::string main_key, const std::string sub_key) {
|
std::wstring Config::wstring(const std::string main_key, const std::string sub_key) {
|
||||||
dbc::check($config.contains(main_key), format("ERROR wstring main/key in config, main_key {} doesn't exist.", main_key));
|
dbc::check($config.contains(main_key), fmt::format("ERROR wstring main/key in config, main_key {} doesn't exist.", main_key));
|
||||||
dbc::check($config[main_key].contains(sub_key), format("ERROR wstring in config, main_key/key {}/{} doesn't exist.", main_key, sub_key));
|
dbc::check($config[main_key].contains(sub_key), fmt::format("ERROR wstring in config, main_key/key {}/{} doesn't exist.", main_key, sub_key));
|
||||||
|
|
||||||
const std::string& str_val = $config[main_key][sub_key];
|
const std::string& str_val = $config[main_key][sub_key];
|
||||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
|
||||||
|
|
|
@ -33,6 +33,8 @@ executable('zedcaster', [
|
||||||
|
|
||||||
executable('amtcaster', [
|
executable('amtcaster', [
|
||||||
'dbc.cpp',
|
'dbc.cpp',
|
||||||
|
'config.cpp',
|
||||||
|
'amt/texture.cpp',
|
||||||
'amt/raycaster.cpp',
|
'amt/raycaster.cpp',
|
||||||
'amt/main.cpp'
|
'amt/main.cpp'
|
||||||
],
|
],
|
||||||
|
|
|
@ -4,13 +4,11 @@
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
|
||||||
using namespace fmt;
|
|
||||||
|
|
||||||
Image TexturePack::load_image(std::string filename) {
|
Image TexturePack::load_image(std::string filename) {
|
||||||
Image texture(TEXTURE_WIDTH * TEXTURE_HEIGHT);
|
Image texture(TEXTURE_WIDTH * TEXTURE_HEIGHT);
|
||||||
sf::Image img;
|
sf::Image img;
|
||||||
bool good = img.loadFromFile(filename);
|
bool good = img.loadFromFile(filename);
|
||||||
dbc::check(good, format("failed to load {}", filename));
|
dbc::check(good, fmt::format("failed to load {}", filename));
|
||||||
|
|
||||||
uint32_t *pixbuf = (uint32_t *)img.getPixelsPtr();
|
uint32_t *pixbuf = (uint32_t *)img.getPixelsPtr();
|
||||||
std::copy_n(pixbuf, texture.size(), texture.begin());
|
std::copy_n(pixbuf, texture.size(), texture.begin());
|
||||||
|
|
|
@ -28,6 +28,6 @@ struct TexturePack {
|
||||||
|
|
||||||
void load_textures();
|
void load_textures();
|
||||||
std::vector<uint32_t> load_image(std::string filename);
|
std::vector<uint32_t> load_image(std::string filename);
|
||||||
Sprite &get_sprite(size_t sprite_num);
|
Sprite& get_sprite(size_t sprite_num);
|
||||||
Image& get_texture(size_t num);
|
Image& get_texture(size_t num);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue