quadrants/source/main.cpp

165 lines
5.3 KiB
C++

#include <string.h>
#include <stdio.h>
#include "world.cpp"
Texture SpaceshipFusilageTexture;
Texture SpaceshipEnginesTexture;
Texture AsteroidTexture;
void LoadAssets() {
SpaceshipFusilageTexture = LoadTexture("./spaceshipFusilage.png");
SpaceshipEnginesTexture = LoadTexture("./spaceshipEngines.png");
}
int main(int argc, char const** argv) {
int const viewportWidth = 640;
int const viewportHeight = 360;
Vector2 const viewportOrigin = Vector2{(viewportWidth / 2), (viewportHeight / 2)};
int unsigned const windowFlags = 0;
int unsigned const rendererFlags = 0;
InitWindow(viewportWidth, viewportHeight, "Quadrants");
HideCursor();
LoadAssets();
Texture nebulaTexture = LoadTexture("./nebula.png");
Texture sunTexture = LoadTexture("./sun.png");
Texture moonTexture = LoadTexture("./moon.png");
Texture asteroidTexture = LoadTexture("./asteroid01.png");
Worldspace world = CreateWorldspace(10);
Camera2D camera = {0};
camera.target = viewportOrigin;
camera.zoom = 1.0f;
GenerateWorldspace(world);
SetTargetFPS(60);
while (!WindowShouldClose()) {
float const scale = (1.0f + (1.0f - camera.zoom));
Vector2 const mousePos = GetMousePosition();
int const mouseWheel = GetMouseWheelMove();
float const lookAtEngageDistance = 32.0f;
float const accel = 0.005f;
// Relative position checks.
if (Vector2Distance(mousePos, viewportOrigin) > lookAtEngageDistance) {
float const lerpStep =
AngleDifference(world.player.entity.orientation, AngleToPoint(viewportOrigin, mousePos));
world.player.delayedOrientation = WrapAngle(world.player.entity.orientation + (lerpStep * 0.0005f));
world.player.entity.orientation = WrapAngle(world.player.entity.orientation + (lerpStep * 0.2f));
}
if (IsKeyDown(KEY_W)) {
AddEntityForce(world.player.entity, world.player.entity.orientation, accel);
}
if (IsKeyDown(KEY_A)) {
AddEntityForce(world.player.entity, (world.player.entity.orientation - HALF_PI), accel);
}
if (IsKeyDown(KEY_S)) {
AddEntityForce(world.player.entity, (world.player.entity.orientation + PI), accel);
}
if (IsKeyDown(KEY_D)) {
AddEntityForce(world.player.entity, (world.player.entity.orientation + HALF_PI), accel);
}
if (IsKeyDown(KEY_SPACE)) {
RemoveEntityForce(world.player.entity, accel);
}
camera.target = Vector2{-mousePos.x, -mousePos.y};
if (mouseWheel) {
camera.zoom = CLAMP(camera.zoom - (((float)mouseWheel) * 0.05f), 0.5f, 1.5f);
}
EntityUpdateInfo const updateInfo = UpdateEntity(world.player.entity);
if (updateInfo.hasMoved) {
Vector2 const nebulaParallaxIncrement = (world.nebulaParallaxScroll + (world.player.entity.speed * 0.005f));
world.nebulaParallaxScroll.x = wrap(nebulaParallaxIncrement.x, 0.0f, ((float)nebulaTexture.width));
world.nebulaParallaxScroll.y = wrap(nebulaParallaxIncrement.y, 0.0f, ((float)nebulaTexture.height));
if (updateInfo.hasPassedQuadrant) {
GenerateWorldspace(world);
}
}
BeginDrawing();
{
ClearBackground(BLACK);
BeginMode2D(camera);
{
Vector2 const quadrantOffsets[] = {
{-QUADRANT_SIZE, -QUADRANT_SIZE},
{0.0f, -QUADRANT_SIZE},
{QUADRANT_SIZE, -QUADRANT_SIZE},
{-QUADRANT_SIZE, 0.0f},
{0.0f, 0.0f},
{QUADRANT_SIZE, 0.0f},
{-QUADRANT_SIZE, QUADRANT_SIZE},
{0.0f, QUADRANT_SIZE},
{QUADRANT_SIZE, QUADRANT_SIZE}
};
// DrawTextureQuad(nebulaTexture, Vector2{1, 1}, world.nebulaParallaxScroll, Rectangle{0, 0, viewportWidth, viewportHeight}, RED);
for (size_t i = 0; i < 9; i += 1) {
Worldspace::Quadrant const* quadrant = (&world.loadedQuadrantMap[i]);
Vector2 const quadrantOffset = (viewportOrigin + quadrantOffsets[i]);
for (size_t j = 0; j < quadrant->starCount; j += 1) {
DrawCircleV(((quadrantOffset + quadrant->stars[j]) - world.player.entity.localPosition), 1.0f, WHITE);
}
for (size_t j = 0; j < quadrant->orbitalCount; j += 1) {
Worldspace::Orbital const quadrantOrbital = quadrant->orbitals[j];
DrawTextureEx(
asteroidTexture,
((quadrantOffset + quadrantOrbital.localPosition) - world.player.entity.localPosition),
(quadrantOrbital.orientation * RAD2DEG),
quadrantOrbital.scale,
WHITE
);
}
}
// Draw player.
DrawTexturePro(
SpaceshipEnginesTexture,
(Rectangle){0, 0, ((float)SpaceshipEnginesTexture.width), ((float)SpaceshipEnginesTexture.height)},
(Rectangle){viewportOrigin.x, viewportOrigin.y, ((float)SpaceshipEnginesTexture.width), ((float)SpaceshipEnginesTexture.height)},
(Vector2){(float)(SpaceshipEnginesTexture.width / 2), (float)(SpaceshipEnginesTexture.height / 2)},
(world.player.delayedOrientation * RAD2DEG),
WHITE
);
DrawTexturePro(
SpaceshipFusilageTexture,
(Rectangle){0, 0, (float)(SpaceshipFusilageTexture.width), (float)(SpaceshipFusilageTexture.height)},
(Rectangle){viewportOrigin.x, viewportOrigin.y, ((float)SpaceshipFusilageTexture.width), ((float)SpaceshipFusilageTexture.height)},
(Vector2){(float)(SpaceshipFusilageTexture.width / 2), (float)(SpaceshipFusilageTexture.height / 2)},
(world.player.entity.orientation * RAD2DEG),
WHITE
);
}
EndMode2D();
// Draw UI.
DrawCircleLines(((int)mousePos.x), ((int)mousePos.y), 10.0f, WHITE);
DrawText(TextFormat("Scale: %f", scale), 0, 0, 22, WHITE);
}
EndDrawing();
}
CloseWindow();
return 0;
}