Skip to content

Commit

Permalink
#244 updated Example
Browse files Browse the repository at this point in the history
  • Loading branch information
kyomawolf committed Feb 16, 2024
1 parent dd16f39 commit ea4ff37
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 184 deletions.
6 changes: 3 additions & 3 deletions projects/VSCode/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}",
"program": "${workspaceFolder}/game",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand Down Expand Up @@ -42,10 +42,10 @@
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"program": "${workspaceFolder}/${fileBasenameNoExtension}",
"program": "${workspaceFolder}/game",
"MIMode": "gdb",
"windows": {
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"program": "${workspaceFolder}/game.exe",
"miDebuggerPath": "C:/raylib/mingw/bin/gdb.exe"
},
"osx": {
Expand Down
44 changes: 44 additions & 0 deletions projects/VSCode/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,49 @@
"**/.DS_Store": true,
"**/*.o": true,
"**/*.exe": true,
},
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
}
}
218 changes: 39 additions & 179 deletions projects/VSCode/FirstPerson.cpp
Original file line number Diff line number Diff line change
@@ -1,191 +1,51 @@
#include "raylib.h"
#include <stdio.h>
#include "raymath.h"
#include "camera.h"
#include "include/raylib-cpp.hpp"
#include <vector>

#define MAX_COLUMNS 100
#define CollisionDetectionThreshold 15
const int screenWidth = 1280;
const int screenHeight = 720;
bool debug = false;
bool cameraCollided = false;
bool closeToCollider = false;

// TODO: WRITE CUSTOM MOVEMENT CODE
// TODO: NEED TO CHECK Y AXIS COLLISIONS
// TODO: NEED TO IMPELEMENT PHYSICS
struct BoxCollider
{
BoundingBox BBox;
Vector3 *center;
Vector3 extents;

BoxCollider() {}

BoxCollider(Vector3 &center, raylib::Vector3 extents)
{
this->center = &center;
this->extents = extents;
UpdateBoundingBox();
}
void UpdateBoundingBox()
{
BBox.max.x = center->x + extents.x / 2;
BBox.max.y = center->y + extents.y / 2;
BBox.max.z = center->z + extents.z / 2;

BBox.min.x = center->x - extents.x / 2;
BBox.min.y = center->y - extents.y / 2;
BBox.min.z = center->z - extents.z / 2;
}
};

struct Player
{
raylib::Camera camera;
BoxCollider collider;

Player()
{
camera = raylib::Camera(raylib::Vector3(4, 2, 4), raylib::Vector3(0.0f, 1.8f, 0.0f),
raylib::Vector3(0, 1, 0), 90.0f, CAMERA_PERSPECTIVE);
SetCameraMode(camera, CAMERA_FIRST_PERSON);

collider = BoxCollider(camera.position, raylib::Vector3(1, 1, 1));
}
};

float DistanceSquared(Vector3 *A, Vector3 *B)
{
float dx = A->x - B->x;
float dy = A->y - B->y;
float dz = A->z - B->z;
return (dx * dx + dy * dy + dz * dz);
}

void GetInput()
{
if (IsKeyPressed(KEY_Q))
debug = !debug;
}

void Draw3D(Player player, Vector3 positions[], float heights[], Color colors[], BoxCollider BBoxes[])
{
BeginMode3D(player.camera);
DrawPlane((Vector3){0.0f, 0.0f, 0.0f}, (Vector2){32.0f, 32.0f}, LIGHTGRAY);
DrawCube((Vector3){-16.0f, 2.5f, 0.0f}, 1.0f, 5.0f, 32.0f, BLUE);
DrawCube((Vector3){16.0f, 2.5f, 0.0f}, 1.0f, 5.0f, 32.0f, LIME);
DrawCube((Vector3){0.0f, 2.5f, 16.0f}, 32.0f, 5.0f, 1.0f, GOLD);

if (!debug)
{
for (int i = 0; i < MAX_COLUMNS; i++)
{
DrawCube(positions[i], 2.0f, heights[i], 2.0f, colors[i]);
DrawCubeWires(positions[i], 2.0f, heights[i], 2.0f, BLUE);
}
}
else
{
for (int i = 0; i < MAX_COLUMNS; i++)
{
DrawBoundingBox(BBoxes[i].BBox, GREEN);
}
DrawBoundingBox(player.collider.BBox, BLUE);
}

EndMode3D();
}

void Draw2D()
{
DrawText("First person camera default controls:", 20, 20, 10, BLACK);
DrawText("- Move with keys: W, A, S, D", 40, 40, 10, DARKGRAY);
DrawText("- Mouse move to look around", 40, 60, 10, DARKGRAY);
if(closeToCollider)
{
DrawText("CHECKING COLLISIONS", 40, 100, 20, BLUE);
closeToCollider = false;
}
if (cameraCollided)
DrawText("Colliding", 40, 80, 20, GREEN);
}

void GenerateLevel(float heights[], BoxCollider BBoxes[], Vector3 positions[], Color colors[])
{
for (int i = 0; i < MAX_COLUMNS; i++)
{
heights[i] = (float)GetRandomValue(1, 12);
positions[i] = (Vector3){GetRandomValue(-15, 15), heights[i] / 2, GetRandomValue(-15, 15)};
BBoxes[i].extents = (Vector3){2.0f, heights[i], 2.0f};
BBoxes[i].center = &positions[i];
BBoxes[i].UpdateBoundingBox();
colors[i] = (Color){GetRandomValue(20, 255), GetRandomValue(10, 55), 30, 255};
}
}

void CheckPlayerCollisions(Player *player, BoxCollider BBoxes[])
{
for (int i = 0; i < MAX_COLUMNS; i++)
{
if (DistanceSquared(player->collider.center, BBoxes[i].center) < CollisionDetectionThreshold)
{
closeToCollider = true;
if (CheckCollisionBoxes(player->collider.BBox, BBoxes[i].BBox))
{
cameraCollided = true;
return;
}
}
}
cameraCollided = false;
#include <raylib-cpp.hpp>


void UpdateFrame(raylib::Window& window, raylib::Camera3D& cam) {
static raylib::Vector3 position;

window.BeginDrawing();

cam.Update(CAMERA_ORBITAL);

window.ClearBackground(raylib::Color::RayWhite());

cam.BeginMode();

position.DrawCube({2.0f, 2.0f, 2.0f}, raylib::Color::Red());
position.DrawCubeWires({2.0f, 2.0f, 2.0f}, raylib::Color::Maroon());
DrawGrid(10, 1.0f);

cam.EndMode();

raylib::Vector2 textPosition{10, 40};
raylib::Text sampleText( GetFontDefault(),"This is a raylib-cpp example", 20.0f, 20.0f /10.0f, raylib::Color::DarkGray());
sampleText.Draw(textPosition);
window.EndDrawing();

}

void Abbas()
{
for (int i = 0; i < screenWidth; i++)
{
DrawPixel(i,screenHeight/2,BLACK);
}
for (int i = 0; i < screenHeight; i++)
{
DrawPixel(screenWidth/2,i,BLACK);
}
}
int main() {

int main()
{
InitWindow(screenWidth, screenHeight, "LET ME HELP YOU SEE, SLAYER");
const int screenWidth = 800;
const int screenHeight = 450;

Player player;
float heights[MAX_COLUMNS] = {0};
Vector3 positions[MAX_COLUMNS] = {0};
Color colors[MAX_COLUMNS] = {0};
BoxCollider BBoxes[MAX_COLUMNS];
GenerateLevel(heights, BBoxes, positions, colors);
raylib::Window window(screenWidth, screenHeight);

SetTargetFPS(60);
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(WHITE);
CheckPlayerCollisions(&player, BBoxes);

Draw3D(player, positions, heights, colors, BBoxes);
Draw2D();
raylib::Vector3 position(10.0f, 10.0f, 8.0f);
raylib::Vector3 target(0.0f, 0.0f, 0.0f);
raylib::Vector3 up (0.0f, 1.0f, 0.0f);

Abbas();
float fovy = 60.0f;
raylib::Camera3D cam(position, target, up, fovy, CAMERA_PERSPECTIVE);

GetInput();
UpdateCamera(&player.camera);
player.collider.UpdateBoundingBox();
window.SetTargetFPS(60);

EndDrawing();
while (!window.ShouldClose()) {
UpdateFrame(window, cam);
}

CloseWindow();

return 0;
window.Close();
return EXIT_SUCCESS;
}
4 changes: 2 additions & 2 deletions projects/VSCode/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ SRC_DIR = src
OBJ_DIR = obj

# Define all object files from source files
SRC = $(call rwildcard, *.c, *.h)
SRC = $(call rwildcard, *.cpp, *.h)
#OBJS = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
OBJS ?= main.c
OBJS ?= FirstPerson.cpp

# For Android platform we call a custom Makefile.Android
ifeq ($(PLATFORM),PLATFORM_ANDROID)
Expand Down

0 comments on commit ea4ff37

Please sign in to comment.