Basic fenster rewrite of the original sfml version, but need to bring back the keyboard controls.

master
Zed A. Shaw 1 day ago
parent 80ac4cefba
commit 972b432d51
  1. 192
      fenscaster.cpp
  2. 2
      fenster/fenster.h
  3. 32
      scratchpad/fenstertest.cpp

@ -1,23 +1,181 @@
#include <fmt/core.h>
#include <numbers>
#include <cmath>
#include "matrix.hpp"
#include <cstdlib>
#include "fenster/fenster.h" #include "fenster/fenster.h"
#include "miniaudio.h" #include "dbc.hpp"
#define W 480*2 using matrix::Matrix;
#define H 480 using namespace fmt;
static int run() { Matrix MAP{
Fenster f(W, H, "hello c++"); {1,1,1,1,1,1,1,1,1},
int t = 0; {1,0,1,0,0,0,0,0,1},
while (f.loop(60)) { {1,0,1,0,0,1,1,0,1},
for (int i = 0; i < W; i++) { {1,0,0,0,0,0,0,0,1},
for (int j = 0; j < H; j++) { {1,1,0,0,0,0,0,0,1},
f.px(i, j) = i ^ j ^ t; {1,0,0,1,1,1,0,0,1},
{1,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1}
};
const int SCREEN_HEIGHT=480;
const int THREED_VIEW_WIDTH=480;
const int THREED_VIEW_HEIGHT=480;
const int SCREEN_WIDTH=SCREEN_HEIGHT * 2;
const int MAP_SIZE=matrix::width(MAP);
const int TILE_SIZE=(SCREEN_WIDTH/2) / MAP_SIZE;
const float FOV = std::numbers::pi / 3.0;
const float HALF_FOV = FOV / 2;
const int CASTED_RAYS=120;
const float STEP_ANGLE = FOV / CASTED_RAYS;
const int MAX_DEPTH = MAP_SIZE * TILE_SIZE;
const float SCALE = (SCREEN_WIDTH / 2) / CASTED_RAYS;
float player_x = SCREEN_WIDTH / 4;
float player_y = SCREEN_WIDTH / 4;
float player_angle = std::numbers::pi;
#define rgba_color(r,g,b,a) (b<<(0*8))|(g<<(1*8))|(r<<(2*8))|(a<<(3*8))
#define gray_color(c) rgba_color(c, c, c, 255)
void draw_rect(Fenster &window, Point pos, Point size, uint32_t color) {
size_t x_start = size_t(pos.x);
size_t y_start = size_t(pos.y);
size_t width = size_t(size.x);
size_t height = size_t(size.y);
dbc::check(x_start <= size_t(window.f.width), format("pos.x {} is greater than width {}", x_start, window.f.width));
dbc::check(y_start <= size_t(window.f.height), format("pos.y {} is greater than height {}", y_start, window.f.height));
dbc::check(x_start + width <= size_t(window.f.width), format("size width {} is greater than width {}", x_start + width, window.f.width));
dbc::check(y_start + height <= size_t(window.f.height), format("size height {} is greater than height {}", y_start + height, window.f.height));
for(size_t y = y_start; y < y_start + height; y++) {
for(size_t x = x_start; x < x_start + width; x++) {
window.px(x, y) = color;
}
}
}
void draw_map_rect(Fenster &window, int x, int y, uint8_t color) {
draw_rect(window,
{size_t(x * TILE_SIZE), size_t(y * TILE_SIZE)},
{size_t(TILE_SIZE-1), size_t(TILE_SIZE-1)},
gray_color(color));
}
void draw_map(Fenster &window, Matrix &map) {
uint8_t light_grey = 191;
uint8_t dark_grey = 65;
for(size_t y = 0; y < matrix::height(map); y++) {
for(size_t x = 0; x < matrix::width(map); x++) {
draw_map_rect(window, x, y, map[y][x] == 1 ? light_grey : dark_grey);
}
}
}
void draw_line(Fenster &window, Point start, Point end) {
}
void clear(Fenster &window) {
for(int y = 0; y < window.f.height; y++) {
for(int x = 0; x < window.f.width; x++) {
window.px(x, y) = 0;
}
}
}
void draw_map_rays(Fenster &window, int col, int row, Point target) {
draw_map_rect(window, col, row, 100);
draw_line(window, {size_t(player_x), size_t(player_y)}, target);
}
void draw_3d_view(Fenster &window, int depth, float start_angle, int ray) {
uint8_t color = 255 / (1 + depth * depth * 0.0001);
float fixed_depth = depth * std::cos(player_angle - start_angle);
float wall_height = 21000 / fixed_depth;
if(wall_height > SCREEN_HEIGHT){
wall_height = SCREEN_HEIGHT;
}
draw_rect(window,
{size_t(SCREEN_HEIGHT + ray * SCALE),
size_t((SCREEN_HEIGHT / 2) - wall_height / 2)},
{size_t(SCALE), size_t(wall_height)},
gray_color(color));
}
void ray_casting(Fenster &window, Matrix& map) {
float start_angle = player_angle - HALF_FOV;
for(int ray = 0; ray < CASTED_RAYS; ray++, start_angle += STEP_ANGLE)
{
for(int depth = 1; depth < MAX_DEPTH; depth++) {
float target_x = player_x - std::sin(start_angle) * depth;
float target_y = player_y + std::cos(start_angle) * depth;
int col = int(target_x / TILE_SIZE);
int row = int(target_y / TILE_SIZE);
if(map[row][col] == 1) {
draw_map_rays(window, col, row, {size_t(target_x), size_t(target_y)});
draw_3d_view(window, depth, start_angle, ray);
break;
} }
} }
if (f.key(0x1b)) { }
break; }
void draw_ceiling_floor(Fenster &window) {
draw_rect(window,
{size_t(SCREEN_HEIGHT), 0},
{size_t(SCREEN_HEIGHT), size_t(SCREEN_HEIGHT / 2)},
gray_color(100));
draw_rect(window,
{size_t(SCREEN_HEIGHT), size_t(SCREEN_HEIGHT / 2)},
{size_t(SCREEN_HEIGHT), size_t(SCREEN_HEIGHT / 2)},
gray_color(200));
}
void draw_everything(Fenster &window) {
clear(window);
draw_map(window, MAP);
draw_ceiling_floor(window);
ray_casting(window, MAP);
}
bool collision(float x, float y) {
int col = int(x / TILE_SIZE);
int row = int(y / TILE_SIZE);
return MAP[row][col] == 1;
}
int main() {
Fenster window(SCREEN_WIDTH, SCREEN_HEIGHT, "Fenscaster");
while(window.loop(60)) {
draw_everything(window);
float x = player_x;
float y = player_y;
player_angle -= 0.01;
if(!collision(x, y)) {
player_x = x;
player_y = y;
} }
t++;
} }
return 0; return 0;
} }
@ -25,8 +183,6 @@ static int run() {
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
int nCmdShow) { int nCmdShow) {
(void)hInstance, (void)hPrevInstance, (void)pCmdLine, (void)nCmdShow; (void)hInstance, (void)hPrevInstance, (void)pCmdLine, (void)nCmdShow;
return run(); return main();
} }
#else
int main() { return run(); }
#endif #endif

@ -334,10 +334,10 @@ FENSTER_API int64_t fenster_time(void) {
#ifdef __cplusplus #ifdef __cplusplus
class Fenster { class Fenster {
public:
struct fenster f; struct fenster f;
int64_t now; int64_t now;
public:
Fenster(const int w, const int h, const char *title) Fenster(const int w, const int h, const char *title)
: f{.title = title, .width = w, .height = h} { : f{.title = title, .width = w, .height = h} {
this->f.buf = new uint32_t[w * h]; this->f.buf = new uint32_t[w * h];

@ -0,0 +1,32 @@
#include "fenster/fenster.h"
#include "miniaudio.h"
#define W 480*2
#define H 480
static int run() {
Fenster f(W, H, "hello c++");
int t = 0;
while (f.loop(60)) {
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
f.px(i, j) = i ^ j ^ t;
}
}
if (f.key(0x1b)) {
break;
}
t++;
}
return 0;
}
#if defined(_WIN32)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
int nCmdShow) {
(void)hInstance, (void)hPrevInstance, (void)pCmdLine, (void)nCmdShow;
return run();
}
#else
int main() { return run(); }
#endif
Loading…
Cancel
Save