Compare commits

..

No commits in common. "d24f0972235283e1410566c0d96c1b42a7cf37b8" and "99e637fc8ba44d746e67dc49a31e5eadf6fe582c" have entirely different histories.

4 changed files with 29 additions and 83 deletions

View File

@ -3,10 +3,10 @@
#ifndef CONST_NS #ifndef CONST_NS
#define CONST_NS #define CONST_NS
constexpr int SCREEN_WIDTH = 1600; constexpr int SCREEN_WIDTH = 800;
constexpr int SCREEN_HEIGHT = 1200; constexpr int SCREEN_HEIGHT = 600;
constexpr int WORLD_WIDTH = 20000; constexpr int WORLD_WIDTH = 1600;
constexpr int WORLD_HEIGHT = 200000; constexpr int WORLD_HEIGHT = 1200;
constexpr int TARGET_FPS = 60; constexpr int TARGET_FPS = 60;
constexpr Uint64 TARGET_FRAME_TIME_NS = 1'000'000'000 / TARGET_FPS; constexpr Uint64 TARGET_FRAME_TIME_NS = 1'000'000'000 / TARGET_FPS;

View File

@ -41,6 +41,4 @@ void init_entity(Entity &entity, SDL_Renderer *renderer, std::string type,
entities[type][name]["height"].get<int>()}; entities[type][name]["height"].get<int>()};
entity.speed = entities[type][name]["speed"].get<float>(); entity.speed = entities[type][name]["speed"].get<float>();
entity.gotoT = false;
} }

View File

@ -7,8 +7,6 @@
#include <csignal> #include <csignal>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <ctime>
#include <iomanip>
std::vector<Entity *> loaded_entities; std::vector<Entity *> loaded_entities;
sdl_session main_sdl_session; sdl_session main_sdl_session;
@ -39,40 +37,22 @@ int main() {
Uint64 lastFrameTime = SDL_GetTicksNS(); Uint64 lastFrameTime = SDL_GetTicksNS();
float deltaTime = 0.0f; float deltaTime = 0.0f;
time_t start_time = std::time(nullptr);
while (running) { while (running) {
const Uint64 frameStart = SDL_GetTicksNS(); const Uint64 frameStart = SDL_GetTicksNS();
// Event handling // Event handling
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) { if (event.type == SDL_EVENT_QUIT)
running = false; running = false;
}
if (event.type == SDL_EVENT_MOUSE_WHEEL) {
float zoom_factor = (event.wheel.y > 0) ? 0.9f : 1.1f;
camera.view.w *= zoom_factor;
camera.view.h *= zoom_factor;
// Zoom towards mouse position
SDL_FPoint mouse_world_pos = {
camera.view.x + event.wheel.mouse_x,
camera.view.y + event.wheel.mouse_y
};
camera.view.x = mouse_world_pos.x - (event.wheel.mouse_x * zoom_factor);
camera.view.y = mouse_world_pos.y - (event.wheel.mouse_y * zoom_factor);
}
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
// Check specific button (e.g., left mouse button) // Check specific button (e.g., left mouse button)
if (event.button.button == SDL_BUTTON_LEFT) { if (event.button.button == SDL_BUTTON_LEFT) {
std::clog << "Left mouse clicked at (" printf("Left mouse clicked at (%.1f, %.1f)\nCamera position: (%.1f, "
<< std::fixed << std::setprecision(1) << event.button.x << ", " << event.button.y << ")\n" "%.1f)\nTarget position: (%.1f, %.1f)\n",
<< "Camera position: (" << camera.view.x << ", " << camera.view.y << ")\n" event.button.x, event.button.y, camera.view.x, camera.view.y,
<< "Target position: (" << (camera.view.x + event.button.x) << ", " camera.view.x + event.button.x,
<< (camera.view.y + event.button.y) << ")\ntime since start: " << (std::time(nullptr) - start_time) << "\n"; camera.view.y + event.button.y);
destroyer.Tposition.x = camera.view.x + event.button.x; destroyer.Tposition.x = camera.view.x + event.button.x;
destroyer.Tposition.y = camera.view.y + event.button.y; destroyer.Tposition.y = camera.view.y + event.button.y;
destroyer.gotoT = true; destroyer.gotoT = true;
@ -90,16 +70,6 @@ int main() {
lastToggle = SDL_GetTicks(); lastToggle = SDL_GetTicks();
} }
//DEBUG
if (keystate[SDL_SCANCODE_B] && (SDL_GetTicks() - lastToggle > 200)) {
std::clog << "x: " << destroyer.position.x << " y: " << destroyer.position.y << "\n";
}
if (keystate[SDL_SCANCODE_O] && (SDL_GetTicks() - lastToggle > 200)) {
destroyer.position.x = 0;
destroyer.position.y = 0;
}
// Player movement // Player movement
int moveX = keystate[SDL_SCANCODE_RIGHT] - keystate[SDL_SCANCODE_LEFT]; int moveX = keystate[SDL_SCANCODE_RIGHT] - keystate[SDL_SCANCODE_LEFT];
int moveY = keystate[SDL_SCANCODE_DOWN] - keystate[SDL_SCANCODE_UP]; int moveY = keystate[SDL_SCANCODE_DOWN] - keystate[SDL_SCANCODE_UP];
@ -144,14 +114,15 @@ int main() {
float step = destroyer.speed * deltaTime; float step = destroyer.speed * deltaTime;
destroyer.position.x += (float)moveX * step; destroyer.position.x += (float)moveX * step;
destroyer.position.y += (float)moveY * step; destroyer.position.y += (float)moveY * step;
std::clog << "step: " << step << "\n";
if (destroyer.gotoT) { if (destroyer.gotoT) {
float dx = destroyer.Tposition.x - destroyer.Central_position().x; float dx = destroyer.Tposition.x - destroyer.Central_position().x;
float dy = destroyer.Tposition.y - destroyer.Central_position().y; float dy = destroyer.Tposition.y - destroyer.Central_position().y;
if(dx != 0 && dy != 0) { destroyer.angle = std::atan2(dy, dx) * 180 / M_PI + 90;
destroyer.angle = std::atan2(dy, dx) * 180 / M_PI + 90; std::clog << "angle: " << destroyer.angle
} << "\ncalc: " << std::atan2(dy, dx) * 180 / M_PI << "\n";
float distance = std::sqrt(dx * dx + dy * dy); float distance = std::sqrt(dx * dx + dy * dy);
if (distance > 0) { if (distance > 0) {
@ -172,10 +143,7 @@ int main() {
} }
} }
// Camera handling // Camera movement
camera.view.w = SCREEN_WIDTH / camera.zoom;
camera.view.h = SCREEN_HEIGHT / camera.zoom;
if (camera.followPlayer) { if (camera.followPlayer) {
float targetX = float targetX =
destroyer.position.x + destroyer.position.w / 2 - camera.view.w / 2; destroyer.position.x + destroyer.position.w / 2 - camera.view.w / 2;
@ -195,11 +163,9 @@ int main() {
WORLD_WIDTH - destroyer.position.w); WORLD_WIDTH - destroyer.position.w);
destroyer.position.y = SDL_clamp(destroyer.position.y, 0.0f, destroyer.position.y = SDL_clamp(destroyer.position.y, 0.0f,
WORLD_HEIGHT - destroyer.position.h); WORLD_HEIGHT - destroyer.position.h);
camera.view.w = SDL_clamp(camera.view.w, SCREEN_WIDTH * camera.zoom, SCREEN_WIDTH * 4.0f);
camera.view.h = SDL_clamp(camera.view.h,SCREEN_HEIGHT * camera.zoom,SCREEN_HEIGHT * 4.0f);
camera.view.x = SDL_clamp(camera.view.x, 0.0f, WORLD_WIDTH - camera.view.w); camera.view.x = SDL_clamp(camera.view.x, 0.0f, WORLD_WIDTH - camera.view.w);
camera.view.y = SDL_clamp(camera.view.y, 0.0f, WORLD_HEIGHT - camera.view.h); camera.view.y =
SDL_clamp(camera.view.y, 0.0f, WORLD_HEIGHT - camera.view.h);
// ANIMATION HERE // ANIMATION HERE

View File

@ -3,40 +3,39 @@
#include <SDL3_image/SDL_image.h> #include <SDL3_image/SDL_image.h>
#include <iostream> #include <iostream>
#include <optional> #include <optional>
#include <cmath>
#ifndef TYPES_NS #ifndef TYPES_NS
#define TYPES_NS #define TYPES_NS
struct Angle { struct Angle360 {
private: private:
float value{0}; int value{0};
void normalize() { void normalize() {
value = std::fmod(value, 360); value %= 360;
if (value < 0) if (value < 0)
value += 360; // Handle negative values value += 360; // Handle negative values
} }
public: public:
// Constructor // Constructor
Angle(int val = 0) : value(val) { normalize(); } Angle360(int val = 0) : value(val) { normalize(); }
// Assignment operator // Assignment operator
Angle &operator=(int val) { Angle360 &operator=(int val) {
value = val; value = val;
normalize(); normalize();
return *this; return *this;
} }
// Compound assignment // Compound assignment
Angle &operator+=(int rhs) { Angle360 &operator+=(int rhs) {
value += rhs; value += rhs;
normalize(); normalize();
return *this; return *this;
} }
Angle &operator-=(int rhs) { Angle360 &operator-=(int rhs) {
value -= rhs; value -= rhs;
normalize(); normalize();
return *this; return *this;
@ -46,7 +45,7 @@ public:
operator int() const { return value; } operator int() const { return value; }
// Stream output // Stream output
friend std::ostream &operator<<(std::ostream &os, const Angle &a) { friend std::ostream &operator<<(std::ostream &os, const Angle360 &a) {
return os << a.value; return os << a.value;
} }
}; };
@ -59,18 +58,18 @@ struct basic_cords {
struct Entity { struct Entity {
SDL_FRect position; SDL_FRect position;
SDL_FRect Tposition; SDL_FRect Tposition;
bool gotoT = false; bool gotoT;
SDL_Texture *texture; SDL_Texture *texture;
SDL_Rect srcRect; SDL_Rect srcRect;
float speed; float speed;
Angle angle = 0; Angle360 angle = 0;
basic_cords Central_position(std::optional<float> x = std::nullopt, basic_cords Central_position(std::optional<float> x = std::nullopt,
std::optional<float> y = std::nullopt) { std::optional<float> y = std::nullopt) {
if (x != std::nullopt) { if (x != NULL) {
position.x = *x - position.w / 2; position.x = *x - position.w / 2;
} }
if (y != std::nullopt) { if (y != NULL) {
position.y = *y - position.h / 2; position.y = *y - position.h / 2;
} }
@ -80,26 +79,9 @@ struct Entity {
struct Camera { struct Camera {
SDL_FRect view = {0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT}; SDL_FRect view = {0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT};
basic_cords Center_of_view(std::optional<float> x = std::nullopt,
std::optional<float> y = std::nullopt) {
if (x != std::nullopt) {
view.x = *x - view.w / 2;
}
if (y != std::nullopt) {
view.y = *y - view.h / 2;
}
return {view.x + view.w / 2, view.y + view.h / 2};
}
bool followPlayer = false; bool followPlayer = false;
float speed = 500.0f; float speed = 500.0f;
float smoothness = 0.1f; float smoothness = 0.1f;
float zoom = 1.0f;
float minZoom = 0.5f;
float maxZoom = 3.0f;
float zoomSpeed = 0.1f;
}; };
struct sdl_session { struct sdl_session {