Skip to content

Instantly share code, notes, and snippets.

@ColleagueRiley
Last active September 14, 2024 20:35
Show Gist options
  • Save ColleagueRiley/1daec08245aa659f26443e834112876b to your computer and use it in GitHub Desktop.
Save ColleagueRiley/1daec08245aa659f26443e834112876b to your computer and use it in GitHub Desktop.
raycaster in RSGL
#define RSGL_IMPLEMENTATION
#include "RSGL.h"
int main(void) {
RGFW_window* win = RGFW_createWindow("name", RGFW_RECT(0, 0, 800, 600), RGFW_CENTER);
srand(time(NULL));
RSGL_init(RSGL_AREA(win->r.w, win->r.h), RGFW_getProcAddress);
u32 map[] = {
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 0, 0, 0, 0, 1,
1, 0, 1, 1, 0, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 1, 1,
1, 1, 0, 0, 0, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1
};
RSGL_area mapArea = RGFW_AREA(8, 8);
const float precision = 64;
const float fov = 90;
float playerAngle = 0;
RSGL_pointF player = RSGL_POINTF(4, 4);
RGFW_window_showMouse(win, 0);
RGFW_window_mouseHold(win, RGFW_AREA(0, 0));
while (RGFW_window_shouldClose(win) == RGFW_FALSE) {
while (RSGL_checkEvent(win)) {
if (win->event.type == RGFW_quit)
break;
if (win->event.type == RGFW_mousePosChanged) {
playerAngle += win->event.point.x / 10;
}
}
if (RGFW_isPressed(win, RGFW_Left)) {
playerAngle--;
}
if (RGFW_isPressed(win, RGFW_Right)) {
playerAngle++;
}
if (RGFW_isPressed(win, RGFW_w)) {
RSGL_pointF next = RSGL_POINTF(player.x + (cos(playerAngle * DEG2RAD) / precision),
player.y + (sin(playerAngle * DEG2RAD) / precision));
if (map[((size_t)next.y * mapArea.w) + (size_t) next.x] == 0)
player = next;
}
if (RGFW_isPressed(win, RGFW_s)) {
RSGL_pointF next = RSGL_POINTF(player.x - (cos(playerAngle * DEG2RAD) / precision),
player.y - (sin(playerAngle * DEG2RAD) / precision));
if (map[((size_t)next.y * mapArea.w) + (size_t) next.x] == 0)
player = next;
}
if (RGFW_isPressed(win, RGFW_a)) {
RSGL_pointF next = RSGL_POINTF(player.x - (cos((playerAngle + 90) * DEG2RAD) / precision),
player.y - (sin((playerAngle + 90) * DEG2RAD) / precision));
if (map[((size_t)next.y * mapArea.w) + (size_t) next.x] == 0)
player = next;
}
if (RGFW_isPressed(win, RGFW_d)) {
RSGL_pointF next = RSGL_POINTF(player.x - (cos((playerAngle - 90) * DEG2RAD) / precision),
player.y - (sin((playerAngle - 90) * DEG2RAD) / precision));
if (map[((size_t)next.y * mapArea.w) + (size_t) next.x] == 0)
player = next;
}
float rayAngle = playerAngle - (fov / 2);
//RSGL_drawRectF(RSGL_RECTF(0, 0, win->r.w, win->r.h / 4), RSGL_RGB(10, 50, 220));
for (size_t i = 0; i < win->r.w; i++) {
RSGL_pointF ray = player;
rayAngle += (fov / win->r.w);
while (map[((size_t)ray.y * mapArea.w) + (size_t) ray.x] == 0) {
ray.x += cos(rayAngle * DEG2RAD) / precision;
ray.y += sin(rayAngle * DEG2RAD) / precision;
}
if (RGFW_isPressed(win, RGFW_Tab))
RSGL_drawLineF(RSGL_POINTF(player.x * 50, player.y * 50), RSGL_POINTF(ray.x * 50, ray.y * 50), 1, RSGL_RGB(200, 100, 20));
float dist = (sqrt(pow(player.x - ray.x, 2) + pow(player.y - ray.y, 2)));
dist *= cos((rayAngle - playerAngle) * DEG2RAD);
float height = (win->r.h / 1) / dist;
RSGL_drawRectF(RSGL_RECTF(i, win->r.h / 3, 1, height), RSGL_RGB(200, 200, 200));
}
if (RGFW_isPressed(win, RGFW_Tab)) {
for (size_t y = 0; y < mapArea.h; y++) {
for (size_t x = 0; x < mapArea.w; x++) {
if (map[(y * mapArea.h) + x])
RSGL_drawRect(RSGL_RECT(x * 50, y * 50, 50, 50), RSGL_RGB(100, 100, 100));
}
}
RSGL_drawCircle(RSGL_CIRCLE((player.x * 50) - 10, (player.y * 50) - 10, 20), RSGL_RGB(255, 0, 0));
}
RSGL_clear(RSGL_RGB(0, 0, 0));
RGFW_window_swapBuffers(win);
}
RSGL_free();
RGFW_window_close(win);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment