mirror of
https://github.com/ssloy/tinyraycaster.git
synced 2025-08-31 17:41:56 +02:00
map drawing
This commit is contained in:
BIN
doc/002.png
Normal file
BIN
doc/002.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.0 KiB |
@@ -29,11 +29,43 @@ void drop_ppm_image(const std::string filename, const std::vector<uint32_t> &ima
|
||||
ofs.close();
|
||||
}
|
||||
|
||||
void draw_rectangle(std::vector<uint32_t> &img, const size_t img_w, const size_t img_h, const size_t x, const size_t y, const size_t w, const size_t h, const uint32_t color) {
|
||||
assert(img.size()==img_w*img_h);
|
||||
for (size_t i=0; i<w; i++) {
|
||||
for (size_t j=0; j<h; j++) {
|
||||
size_t cx = x+i;
|
||||
size_t cy = y+j;
|
||||
assert(cx<img_w && cy<img_h);
|
||||
img[cx + cy*img_w] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
const size_t win_w = 512; // image width
|
||||
const size_t win_h = 512; // image height
|
||||
std::vector<uint32_t> framebuffer(win_w*win_h, 255); // the image itself, initialized to white
|
||||
|
||||
const size_t map_w = 16; // map width
|
||||
const size_t map_h = 16; // map height
|
||||
const char map[] = "0000222222220000"\
|
||||
"1 0"\
|
||||
"1 11111 0"\
|
||||
"1 0 0"\
|
||||
"0 0 1110000"\
|
||||
"0 3 0"\
|
||||
"0 10000 0"\
|
||||
"0 0 11100 0"\
|
||||
"0 0 0 0"\
|
||||
"0 0 1 00000"\
|
||||
"0 1 0"\
|
||||
"2 1 0"\
|
||||
"0 0 0"\
|
||||
"0 0000000 0"\
|
||||
"0 0"\
|
||||
"0002222222200000"; // our game map
|
||||
assert(sizeof(map) == map_w*map_h+1); // +1 for the null terminated string
|
||||
|
||||
for (size_t j = 0; j<win_h; j++) { // fill the screen with color gradients
|
||||
for (size_t i = 0; i<win_w; i++) {
|
||||
uint8_t r = 255*j/float(win_h); // varies between 0 and 255 as j sweeps the vertical
|
||||
@@ -43,6 +75,17 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
const size_t rect_w = win_w/map_w;
|
||||
const size_t rect_h = win_h/map_h;
|
||||
for (size_t j=0; j<map_h; j++) { // draw the map
|
||||
for (size_t i=0; i<map_w; i++) {
|
||||
if (map[i+j*map_w]==' ') continue; // skip empty spaces
|
||||
size_t rect_x = i*rect_w;
|
||||
size_t rect_y = j*rect_h;
|
||||
draw_rectangle(framebuffer, win_w, win_h, rect_x, rect_y, rect_w, rect_h, pack_color(0, 255, 255));
|
||||
}
|
||||
}
|
||||
|
||||
drop_ppm_image("./out.ppm", framebuffer, win_w, win_h);
|
||||
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user