Permalink
Browse files

refactoring time!

  • Loading branch information...
ssloy committed Feb 9, 2019
1 parent 8e221cb commit 80e6e4a815e6b17ad731d4c5bd9ca34698fe2cfe
Showing with 294 additions and 160 deletions.
  1. BIN doc/011.png
  2. BIN doc/012.gif
  3. BIN doc/012.mkv
  4. +30 −0 framebuffer.cpp
  5. +17 −0 framebuffer.h
  6. +36 −0 map.cpp
  7. +14 −0 map.h
  8. +11 −0 player.h
  9. +61 −0 textures.cpp
  10. +15 −0 textures.h
  11. +66 −160 tinyraycaster.cpp
  12. +31 −0 utils.cpp
  13. +13 −0 utils.h
BIN +23.1 KB doc/011.png
Binary file not shown.
BIN +9.84 MB doc/012.gif
Binary file not shown.
BIN +945 KB doc/012.mkv
Binary file not shown.
@@ -0,0 +1,30 @@
#include <iostream>
#include <vector>
#include <cstdint>
#include <cassert>

#include "framebuffer.h"
#include "utils.h"

void FrameBuffer::set_pixel(const size_t x, const size_t y, const uint32_t color) {
assert(img.size()==w*h && x<w && y<h);
img[x+y*w] = color;
}

void FrameBuffer::draw_rectangle(const size_t rect_x, const size_t rect_y, const size_t rect_w, const size_t rect_h, const uint32_t color) {
assert(img.size()==w*h);
for (size_t i=0; i<rect_w; i++) {
for (size_t j=0; j<rect_h; j++) {
size_t cx = rect_x+i;
size_t cy = rect_y+j;
if (cx<w && cy<h) // no need to check for negative values (unsigned variables)
set_pixel(cx, cy, color);
}
}
}

void FrameBuffer::clear(const uint32_t color) {
img = std::vector<uint32_t>(w*h, color);
}


@@ -0,0 +1,17 @@
#ifndef FRAMEBUFFER_H
#define FRAMEBUFFER_H

#include <cstdlib>
#include <vector>

struct FrameBuffer {
size_t w, h; // image dimensions
std::vector<uint32_t> img; // storage container

void clear(const uint32_t color);
void set_pixel(const size_t x, const size_t y, const uint32_t color);
void draw_rectangle(const size_t x, const size_t y, const size_t w, const size_t h, const uint32_t color);
};

#endif // FRAMEBUFFER_H

36 map.cpp
@@ -0,0 +1,36 @@
#include <cstdlib>
#include <cassert>

#include "map.h"

static const char map[] = "0000222222220000"\
"1 0"\
"1 11111 0"\
"1 0 0"\
"0 0 1110000"\
"0 3 0"\
"0 10000 0"\
"0 3 11100 0"\
"5 4 0 0"\
"5 4 1 00000"\
"0 1 0"\
"2 1 0"\
"0 0 0"\
"0 0000000 0"\
"0 0"\
"0002222222200000";

Map::Map() : w(16), h(16) {
assert(sizeof(map) == w*h+1); // +1 for the null terminated string
}

int Map::get(const size_t i, const size_t j) {
assert(i<w && j<h && sizeof(map) == w*h+1);
return map[i+j*w] - '0';
}

bool Map::is_empty(const size_t i, const size_t j) {
assert(i<w && j<h && sizeof(map) == w*h+1);
return map[i+j*w] == ' ';
}

14 map.h
@@ -0,0 +1,14 @@
#ifndef MAP_H
#define MAP_H

#include <cstdlib>

struct Map {
size_t w, h; // overall map dimensions
Map();
int get(const size_t i, const size_t j);
bool is_empty(const size_t i, const size_t j);
};

#endif // MAP_H

@@ -0,0 +1,11 @@
#ifndef PLAYER_H
#define PLAYER_H

struct Player {
float x, y; // position
float a; // view direction
float fov; // field of view
};

#endif // PLAYER_H

@@ -0,0 +1,61 @@
#include <iostream>
#include <vector>
#include <cstdint>
#include <cassert>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#include "utils.h"
#include "textures.h"

Texture::Texture(const std::string filename) : img_w(0), img_h(0), count(0), size(0), img() {
int nchannels = -1, w, h;
unsigned char *pixmap = stbi_load(filename.c_str(), &w, &h, &nchannels, 0);
if (!pixmap) {
std::cerr << "Error: can not load the textures" << std::endl;
return;
}

if (4!=nchannels) {
std::cerr << "Error: the texture must be a 32 bit image" << std::endl;
stbi_image_free(pixmap);
return;
}

if (w!=h*int(w/h)) {
std::cerr << "Error: the texture file must contain N square textures packed horizontally" << std::endl;
stbi_image_free(pixmap);
return;
}
count = w/h;
size = w/count;
img_w = w;
img_h = h;

img = std::vector<uint32_t>(w*h);
for (int j=0; j<h; j++) {
for (int i=0; i<w; i++) {
uint8_t r = pixmap[(i+j*w)*4+0];
uint8_t g = pixmap[(i+j*w)*4+1];
uint8_t b = pixmap[(i+j*w)*4+2];
uint8_t a = pixmap[(i+j*w)*4+3];
img[i+j*w] = pack_color(r, g, b, a);
}
}
stbi_image_free(pixmap);
}

uint32_t &Texture::get(const size_t i, const size_t j, const size_t idx) {
assert(i<size && j<size && idx<count);
return img[i+idx*size+j*img_w];
}

std::vector<uint32_t> Texture::get_scaled_column(const size_t texture_id, const size_t tex_coord, const size_t column_height) {
assert(tex_coord<size && texture_id<count);
std::vector<uint32_t> column(column_height);
for (size_t y=0; y<column_height; y++) {
column[y] = get(tex_coord, (y*size)/column_height, texture_id);
}
return column;
}

@@ -0,0 +1,15 @@
#ifndef TEXTURES_H
#define TEXTURES_H

struct Texture {
size_t img_w, img_h; // overall image dimensions
size_t count, size; // number of textures and size in pixels
std::vector<uint32_t> img; // textures storage container

Texture(const std::string filename);
uint32_t &get(const size_t i, const size_t j, const size_t idx); // get the pixel (i,j) from the textrue idx
std::vector<uint32_t> get_scaled_column(const size_t texture_id, const size_t tex_coord, const size_t column_height); // retrieve one column (tex_coord) from the texture texture_id and scale it to the destination size
};

#endif // TEXTURES_H

Oops, something went wrong.

0 comments on commit 80e6e4a

Please sign in to comment.