First commit
This commit is contained in:
99
main.cpp
Normal file
99
main.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
class Entity {
|
||||
|
||||
protected:
|
||||
Vector3 position{};
|
||||
|
||||
public:
|
||||
virtual ~Entity() = default;
|
||||
|
||||
explicit Entity(Vector3 position) {
|
||||
this->position = position;
|
||||
}
|
||||
|
||||
virtual void updatePos() = 0;
|
||||
};
|
||||
|
||||
|
||||
class Player: Entity {
|
||||
Camera3D camera{};
|
||||
Vector3 camera_offset = {0, 4.0f, 0};
|
||||
|
||||
void updateCameraPos() {
|
||||
camera.position = Vector3Add(position, camera_offset);
|
||||
UpdateCamera(&camera, CAMERA_FIRST_PERSON);
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
Player(Vector3 position) : Entity(position) {
|
||||
camera.target = (Vector3){0.0f, 0.0f, 0.0f};
|
||||
camera.up = (Vector3){0.0f, 1.0f, 0.0f};
|
||||
camera.fovy = 60.0f;
|
||||
camera.projection = CAMERA_PERSPECTIVE;
|
||||
}
|
||||
|
||||
Camera3D& getCamera() {
|
||||
return this->camera;
|
||||
}
|
||||
|
||||
void updatePos() override {
|
||||
|
||||
if (IsKeyDown(KEY_W)) {
|
||||
position.x+=0.1;
|
||||
};
|
||||
|
||||
|
||||
|
||||
updateCameraPos();
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
int main(void) {
|
||||
const int screenWidth = 1500;
|
||||
const int screenHeight = 800;
|
||||
|
||||
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
||||
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "Simple 3D cube");
|
||||
|
||||
SetTargetFPS(75);
|
||||
|
||||
Mesh cubeMesh = GenMeshCube(1.0f, 1.0f, 1.0f);
|
||||
Model cubeModel = LoadModelFromMesh(cubeMesh);
|
||||
|
||||
Player player(Vector3{4, 0, 4});
|
||||
|
||||
Camera3D& cur_camera = player.getCamera();
|
||||
|
||||
while (!WindowShouldClose()) {
|
||||
|
||||
|
||||
player.updatePos();
|
||||
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(cur_camera);
|
||||
|
||||
static float rotation = 0.0f;
|
||||
rotation += 0.01f; // Increment for smooth spin
|
||||
|
||||
DrawModelEx(cubeModel, (Vector3){0, 0, 0}, (Vector3){0, 1, 0}, rotation * RAD2DEG, (Vector3){2, 2, 2}, RED);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawText("3D mode", 10, 10, 20, DARKGRAY);
|
||||
EndDrawing();
|
||||
|
||||
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user