entry #1
written by ✧*.𝓡𝓮𝓲 .*✧
submitted at
2 likes
guesses
- Dolphy (by LyricLy)
- Dolphy (by kimapr)
- kotnen (by yui)
- yui (by theqwertiest)
- ✧*.𝓡𝓮𝓲 .*✧ (by Olivia)
- ✧*.𝓡𝓮𝓲 .*✧ (by Dolphy)
- ✧*.𝓡𝓮𝓲 .*✧ (by kotnen)
comments 0
cg.zip Zip archive data, at least v1.0 to extract, compression method=store
dir cg
dir src
app.cc ASCII text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | #include "app.hh" App::App(const char *title, int w, int h, float scale): scrW(w / scale), scrH(h / scale), scrSize(scrW * scrH), pixels(new std::uint32_t[scrSize]), winW(w), winH(h), aspectRatio((float)h / w), running(true), world(10, 10, Camera(Vec2f(3, 8), M_PI * 1.75, 70, 0.2)) { // SDL2 setup if (SDL_Init(SDL_INIT_VIDEO) < 0) { std::cerr << "Failed to initialize SDL2: " << SDL_GetError() << std::endl; std::exit(EXIT_FAILURE); } win = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_SHOWN); if (win == NULL) { std::cerr << "Failed to create window: " << SDL_GetError() << std::endl; std::exit(EXIT_FAILURE); } ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (ren == NULL) { std::cerr << "Failed to create renderer: " << SDL_GetError() << std::endl; std::exit(EXIT_FAILURE); } tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, scrW, scrH); if (tex == NULL) { std::cerr << "Failed to create screen texture: " << SDL_GetError() << std::endl; std::exit(EXIT_FAILURE); } keyboard = SDL_GetKeyboardState(NULL); // A hack to force i3wm/sway to float the window on startup SDL_SetWindowResizable(win, SDL_TRUE); // Init x dir map xDirMap.resize(scrW); double centerX = (double)(scrW / 2); double planeDist = centerX / std::tan(degToRad(world.cam.fov / 2)); for (int x = 0; x < scrW; ++ x) xDirMap[x] = std::atan2((double)x - centerX, planeDist); // Setup map world.addLines({ Linef(2, 3, 3, 2), Linef(2, 3, 2, 4), Linef(2, 3, 1.8, 3.5), Linef(2, 4, 1.8, 3.5), Linef(4, 2, 3, 2), Linef(4, 2, 5, 3), Linef(5, 3, 5, 4), Linef(5, 4, 4, 5), Linef(4, 5, 3, 5), Linef(3, 5, 3.5, 5.2), Linef(4, 5, 3.5, 5.2), Linef(7, 7, 7, 6), Linef(6.5, 7, 6.5, 6), Linef(6.5, 7, 7, 7), Linef(6.5, 6, 7, 6), Linef(6.5, 6, 7.8, 8), Linef(6.5, 6, 7.4, 7), Linef(7.8, 8, 7.4, 7), Linef(0, 6, 2, 9), Linef(0, 5, 2, 9), Linef(8, 2, 7.2, 3.2), Linef(6, 4, 7.2, 3.2), Linef(8, 2, 6.8, 2.8), Linef(6, 4, 6.8, 2.8), Linef(8, 4, 7.2, 2.8), Linef(6, 2, 7.2, 2.8), Linef(8, 4, 6.8, 3.2), Linef(6, 2, 6.8, 3.2), }); } App::~App() { free(pixels); SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); } void App::run() { uint64_t last, now = SDL_GetPerformanceCounter(); while (running) { last = now; now = SDL_GetPerformanceCounter(); double dt = (double)(now - last) * 1000 / (double)SDL_GetPerformanceFrequency(); // Render SDL_SetRenderDrawColor(ren, 0, 0, 0, SDL_ALPHA_OPAQUE); SDL_RenderClear(ren); std::memset(pixels, 0, scrSize * sizeof(*pixels)); render(dt); SDL_UpdateTexture(tex, NULL, pixels, scrW * sizeof(*pixels)); // Display float winAspectRatio = (float)winH / winW; viewport.w = winAspectRatio < aspectRatio? winH / aspectRatio : winW; viewport.h = winAspectRatio > aspectRatio? winW * aspectRatio : winH; viewport.x = winW / 2 - viewport.w / 2; viewport.y = winH / 2 - viewport.h / 2; SDL_RenderCopy(ren, tex, NULL, &viewport); SDL_RenderPresent(ren); // Handle events SDL_Event evt; while (SDL_PollEvent(&evt)) handleEvent(evt); update(dt); } } void App::render(double dt) { (void)dt; double fog = 14; for (int x = 0; x < scrW; ++ x) { RaycastResult result = world.raycast(xDirMap[x]); double dirCos = std::cos(xDirMap[x]); double dist = distance(world.cam.pos, result.hit); int wallH = std::round((double)scrH / (dist * dirCos)); int wallY = scrH / 2 - wallH / 2; if (wallH > scrH) wallH = scrH; if (wallY < 0) wallY = 0; // Render wall float t = std::max(1.0 - (result.shade * 3 + dist) / fog, 0.0); auto color = (std::uint32_t)(t * 255); for (int yo = 0; yo < wallH; ++ yo) pixels[(yo + wallY) * scrW + x] = color << 24; // Render floor for (int yo = wallH / 2; yo < scrH / 2; ++ yo) { double dist = (double)scrH / 2 / yo / dirCos; Vec2i p(world.cam.pos + Vec2f::fromDir(world.cam.dir + xDirMap[x]) * dist); float t = std::max(1.0 - dist / fog, 0.0); auto color = (std::uint32_t)(t * (p.x % 2 == (p.y % 2 == 0)? 170 : 70)); pixels[(yo + scrH / 2) * scrW + x] = color << 24 | color << 16 | color << 8; } } } template<typename T> bool lineIntersectsRect(const Line<T> &l, const Vec2<T> &p1, const Vec2<T> &p2, Vec2<T> &i) { std::array<Line<T>, 4> sides = { Line<T>(p1.x, p1.y, p1.x, p2.y), Line<T>(p1.x, p1.y, p2.x, p1.y), Line<T>(p2.x, p2.y, p1.x, p2.y), Line<T>(p2.x, p2.y, p2.x, p1.y), }; for (const auto &side : sides) { if (l.intersects(side, i)) return true; } return false; } void App::update(double dt) { Vec2f prev = world.cam.pos; double moveBy = 0.005 * dt; if (keyboard[SDL_SCANCODE_W]) world.cam.moveForward(moveBy); if (keyboard[SDL_SCANCODE_S]) world.cam.moveBackward(moveBy); if (keyboard[SDL_SCANCODE_A]) world.cam.moveLeft(moveBy); if (keyboard[SDL_SCANCODE_D]) world.cam.moveRight(moveBy); if (keyboard[SDL_SCANCODE_Q]) world.cam.turnLeft(moveBy); if (keyboard[SDL_SCANCODE_E]) world.cam.turnRight(moveBy); Vec2f p1 = world.cam.pos - world.cam.radius; Vec2f p2 = world.cam.pos + world.cam.radius; // Collision with lines for (const auto &l : world.lines) { Vec2f i; if (lineIntersectsRect(l, p1, p2, i)) { world.cam.pos = prev; break; } } // Collision with borders if (p1.x < 0) world.cam.pos.x = world.cam.radius; if (p1.y < 0) world.cam.pos.y = world.cam.radius; if (p2.x > world.w) world.cam.pos.x = world.w - world.cam.radius; if (p2.y > world.h) world.cam.pos.y = world.h - world.cam.radius; } void App::handleEvent(SDL_Event &evt) { switch (evt.type) { case SDL_QUIT: running = false; break; case SDL_WINDOWEVENT: if (evt.window.event == SDL_WINDOWEVENT_RESIZED) { winW = evt.window.data1; winH = evt.window.data2; } break; } } |
app.hh ASCII text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #ifndef APP_HH_HEADER_GUARD #define APP_HH_HEADER_GUARD #include <iostream> // std::cerr #include <cstdlib> // std::exit, EXIT_FAILURE #include <cstring> // std::memset #include <cstdint> // std::uint32_t, std::uint8_t #include <unordered_map> // std::unordered_map #include <string> // std::string #include <algorithm> // std::max #include <SDL2/SDL.h> #include "math.hh" #include "world.hh" class App { public: App(const char *title, int w, int h, float scale); ~App(); void run(); private: void render(double dt); void update(double dt); void handleEvent(SDL_Event &evt); SDL_Window *win; SDL_Renderer *ren; SDL_Texture *tex; int scrW, scrH, scrSize; std::uint32_t *pixels; SDL_Rect viewport; const std::uint8_t *keyboard; int winW, winH; float aspectRatio; bool running; World world; // Screen x to ray direction map std::vector<double> xDirMap; }; #endif |
main.cc ASCII text
1 2 3 4 5 6 7 8 9 | #include "app.cc" #include "world.cc" int main() { App *app = new App("Raycaster", 700, 450, 1); app->run(); delete app; return 0; } |
math.hh ASCII text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | #ifndef MATH_HH_HEADER_GUARD #define MATH_HH_HEADER_GUARD #include <cmath> // std::sqrt, std::sqrt, std::round, std::cos, std::sin, std::pow #include <algorithm> // std::swap #include <array> // std::array #ifndef M_PI # define M_PI 3.1415926535 #endif constexpr double lineBoundaryOffset = 0.00005; template<typename T> struct Vec2 { T x, y; static Vec2<T> fromDir(T dir) { return Vec2<T>(std::cos(dir), std::sin(dir)); } Vec2(): x(0), y(0) {} Vec2(T a): x(a), y(a) {} Vec2(T a, T b): x(a), y(b) {} #define funcTemplate(OP) \ Vec2 operator OP(const Vec2 &p) const { \ return Vec2<T>(x OP p.x, y OP p.y); \ } funcTemplate(+) funcTemplate(-) funcTemplate(*) funcTemplate(/) #undef funcTemplate #define funcTemplate(OP) \ Vec2 &operator OP (const Vec2 &p) { \ x OP p.x; \ y OP p.y; \ return *this; \ } funcTemplate(+=) funcTemplate(-=) funcTemplate(*=) funcTemplate(/=) #undef funcTemplate #define funcTemplate(T2) \ operator Vec2<T2>() const { \ return Vec2<T2>((T2)x, (T2)y); \ } funcTemplate(float) funcTemplate(int) #undef funcTemplate #define funcTemplate(NAME, FUNC) \ Vec2<T> NAME() const { \ return Vec2<T>(FUNC(x), FUNC(y)); \ } funcTemplate(round, std::round) funcTemplate(ceil, std::ceil) funcTemplate(floor, std::floor) #undef funcTemplate bool operator ==(const Vec2 &p) { return x == p.x and y == p.y; } bool operator !=(const Vec2 &p) { return x != p.x or y != p.y; } Vec2<T> swap() const { return Vec2(y, x); } Vec2<T> round(double precision) const { precision = std::pow(10, precision); return Vec2<T>(std::round(x * precision) / precision, std::round(y * precision) / precision); } friend std::ostream &operator <<(std::ostream &stream, const Vec2 &p) { stream << '{' << p.x << ", " << p.y << '}'; return stream; } }; using Vec2f = Vec2<double>; using Vec2i = Vec2<int>; template<typename T> struct Line { Vec2<T> a, b; Line() {} Line(Vec2<T> a, Vec2<T> b): a(a), b(b) {} Line(T x1, T y1, T x2, T y2): a(x1, y1), b(x2, y2) {} bool isPointBetweenVertices(const Vec2<T> &p, double boundary) const { Vec2<T> p1 = a, p2 = b; if (p1.x > p2.x) std::swap(p1.x, p2.x); if (p.x > p2.x + boundary || p.x < p1.x - boundary) return false; if (p1.y > p2.y) std::swap(p1.y, p2.y); if (p.y > p2.y + boundary || p.y < p1.y - boundary) return false; return true; } bool intersects(const Line<T> &l, Vec2<T> &i) const { double a1 = b.y - a.y; double b1 = a.x - b.x; double c1 = a1 * a.x + b1 * a.y; double a2 = l.b.y - l.a.y; double b2 = l.a.x - l.b.x; double c2 = a2 * l.a.x + b2 * l.a.y; double determinant = a1 * b2 - a2 * b1; if (determinant == 0) return false; i.x = (b2 * c1 - b1 * c2) / determinant; i.y = (a1 * c2 - a2 * c1) / determinant; return isPointBetweenVertices(i, lineBoundaryOffset) && l.isPointBetweenVertices(i, lineBoundaryOffset); } }; using Linef = Line<double>; template<typename T> double distance(const Vec2<T> &a, const Vec2<T> &b) { return std::sqrt(std::pow(a.x - b.x, 2) + std::pow(a.y - b.y, 2)); } inline double degToRad(double deg) { return deg * M_PI / 180; } inline double lerp(float a, float b, float f) { return a + f * (b - a); } #endif |
world.cc ASCII text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | #include "world.hh" Tile::Tile() {} Camera::Camera(Vec2f pos, double dir, double fov, double radius): pos(pos), dir(dir), fov(fov), radius(radius) {} void Camera::moveForward(double scale) { pos += Vec2f::fromDir(dir) * scale; } void Camera::moveBackward(double scale) { pos -= Vec2f::fromDir(dir) * scale; } void Camera::moveLeft(double scale) { pos += Vec2f::fromDir(dir).swap() * Vec2f(scale, -scale); } void Camera::moveRight(double scale) { pos -= Vec2f::fromDir(dir).swap() * Vec2f(scale, -scale); } void Camera::turnLeft(double scale) { dir -= scale; sanitizeDir(); } void Camera::turnRight(double scale) { dir += scale; sanitizeDir(); } void Camera::sanitizeDir() { while (dir > M_PI * 2) dir -= M_PI * 2; while (dir < 0) dir += M_PI * 2; } World::World(int w, int h, const Camera &cam): w(w), h(h), cam(cam) { tiles.resize(h); for (auto &row : tiles) row.resize(w); } const Tile &World::tileAt(int x, int y) const { return tiles[y][x]; } void World::initDDA(Vec2f start, Vec2f d, Vec2f &m, Vec2f &l, Vec2f &ul, Vec2i &p, Vec2i &step) const { m = Vec2f(d.x / d.y, d.y / d.x); ul = Vec2f(std::hypot(1, m.y), std::hypot(m.x, 1)); p = Vec2i(start); if (d.x < 0) { step.x = -1; l.x = (start.x - p.x) * ul.x; } else { step.x = 1; l.x = (p.x + 1 - start.x) * ul.x; } if (d.y < 0) { step.y = -1; l.y = (start.y - p.y) * ul.y; } else { step.y = 1; l.y = (p.y + 1 - start.y) * ul.y; } } void World::findClosestLineIntersect(const Vec2i &p, const Linef &line, Vec2f &closestIntersect, const Linef *&closestLine) const { closestLine = NULL; double closestDist = -1; for (const auto *l : tiles[p.y][p.x].lines) { Vec2f i; bool intersects = line.intersects(*l, i); if (intersects && i.x >= p.x - lineBoundaryOffset && i.y >= p.y - lineBoundaryOffset && i.x < p.x + lineBoundaryOffset + 1 && i.y < p.y + lineBoundaryOffset + 1) { double dist = distance(line.a, i); if (dist < closestDist || closestDist < 0) { closestDist = dist; closestIntersect = i; closestLine = l; } } } } RaycastResult World::raycast(double off) const { Vec2f d(Vec2f::fromDir(cam.dir + off)); Vec2f m, l, ul; Vec2i p, step; initDDA(cam.pos, d, m, l, ul, p, step); double dist = 0; RaycastResult result; while (true) { if (!tiles[p.y][p.x].lines.empty()) { Linef ray(cam.pos, cam.pos + d * (dist + 2)); const Linef *line; findClosestLineIntersect(p, ray, result.hit, line); if (line != NULL) { result.shade = std::fabs(line->a.y - line->b.y) / distance(line->a, line->b); return result; } } if (l.x < l.y) { dist = l.x; p.x += step.x; l.x += ul.x; result.shade = 1; } else { dist = l.y; p.y += step.y; l.y += ul.y; result.shade = 0; } if (p.x >= w || p.y >= h || p.x < 0 || p.y < 0) break; } result.hit = cam.pos + d * dist; return result; } void World::addLines(const std::vector<Linef> &toAdd) { lines.insert(lines.end(), toAdd.begin(), toAdd.end()); mapLinesToTiles(); } void World::mapLineToTiles(const Linef &line) { Vec2f a = line.a, b = line.b; if (a.y > b.y) std::swap(a, b); double dist = distance(a, b); Vec2f o(b.x - a.x, b.y - a.y); Vec2f m, l, ul; Vec2i p, step; initDDA(a, o, m, l, ul, p, step); bool skip = false; while (true) { auto &lines = tiles[p.y][p.x].lines; if (!skip && std::find(lines.begin(), lines.end(), &line) == lines.end()) lines.push_back(&line); skip = l.x == l.y; if (l.x < l.y) { if (l.x > dist) break; p.x += step.x; l.x += ul.x; } else { if (l.y > dist) break; p.y += step.y; l.y += ul.y; } if (p.x >= w || p.y >= h || p.x < 0 || p.y < 0) break; } } void World::mapLinesToTiles() { for (auto &row : tiles) { for (auto &tile : row) tile.lines.clear(); } for (const auto &line : lines) mapLineToTiles(line); } |
world.hh ASCII text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #ifndef WORLD_HH_HEADER_GUARD #define WORLD_HH_HEADER_GUARD #include <vector> // std::vector #include <algorithm> // std::swap, std::find #include "math.hh" struct Tile { std::vector<const Linef*> lines; Tile(); }; struct RaycastResult { Vec2f hit; float shade; }; struct Camera { Vec2f pos; double dir, fov, radius; Camera(Vec2f pos, double dir, double fov, double radius); void moveForward (double scale); void moveBackward(double scale); void moveLeft (double scale); void moveRight (double scale); void turnLeft(double scale); void turnRight(double scale); void sanitizeDir(); }; class World { public: int w, h; std::vector<std::vector<Tile>> tiles; std::vector<Linef> lines; Camera cam; World(int w, int h, const Camera &cam); const Tile &tileAt(int x, int y) const; RaycastResult raycast(double off) const; void addLines(const std::vector<Linef> &toAdd); private: void findClosestLineIntersect(const Vec2i &p, const Linef &line, Vec2f &closestIntersect, const Linef *&closestLine) const; void initDDA(Vec2f start, Vec2f d, Vec2f &m, Vec2f &l, Vec2f &ul, Vec2i &p, Vec2i &step) const; void mapLineToTiles(const Linef &line); void mapLinesToTiles(); }; #endif |
README.txt ASCII text
1 2 3 4 5 | Compile using make Dependency: SDL2 Controls: WASD - Movement EQ - Rotate |
makefile ASCII text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | OUT = app SRC = src/main.cc DEPS = $(wildcard src/*.cc) $(wildcard src/*.hh) STD = c++17 LIBS = -lSDL2 FLAGS = -O3 -g -Wall -Wextra -pedantic -Wno-deprecated-declarations build: $(OUT) $(OUT): $(DEPS) $(CXX) $(SRC) -std=$(STD) $(FLAGS) $(LIBS) -o $(OUT) clean: -rm $(OUT) all: @echo build, clean |
post a comment