removed trash
This commit is contained in:
parent
73a7be3f43
commit
99e637fc8b
34
src/init.cpp
34
src/init.cpp
@ -1,18 +1,18 @@
|
|||||||
#include "init.hpp"
|
#include "init.hpp"
|
||||||
|
#include "color.h"
|
||||||
|
#include "main.hpp"
|
||||||
|
#include <csignal>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <csignal>
|
|
||||||
#include "main.hpp"
|
|
||||||
#include <iostream>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include "color.h"
|
|
||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
void init_entity(Entity &entity, SDL_Renderer *renderer,std::string type, std::string name) {
|
void init_entity(Entity &entity, SDL_Renderer *renderer, std::string type,
|
||||||
|
std::string name) {
|
||||||
json entities;
|
json entities;
|
||||||
try {
|
try {
|
||||||
std::ifstream entitiesF("./assets/entities.json");
|
std::ifstream entitiesF("./assets/entities.json");
|
||||||
@ -29,26 +29,16 @@ void init_entity(Entity &entity, SDL_Renderer *renderer,std::string type, std::s
|
|||||||
loaded_entities.push_back(&entity);
|
loaded_entities.push_back(&entity);
|
||||||
std::clog << entities[type];
|
std::clog << entities[type];
|
||||||
|
|
||||||
entity.texture = IMG_LoadTexture(renderer, entities[type][name]["texture"].get<std::string>().c_str());
|
entity.texture = IMG_LoadTexture(
|
||||||
|
renderer, entities[type][name]["texture"].get<std::string>().c_str());
|
||||||
|
|
||||||
if(entity.texture == NULL) {
|
if (entity.texture == NULL) {
|
||||||
std::cerr << "\n" << RED"[ERROR]" << RESET" failed to load texture\n";
|
std::cerr << "\n" << RED "[ERROR]" << RESET " failed to load texture\n";
|
||||||
exit(ENOENT);
|
exit(ENOENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
entity.width = entities[type][name]["width"].get<unsigned int>();
|
entity.srcRect = {0, 0, entities[type][name]["width"].get<int>(),
|
||||||
entity.height = entities[type][name]["height"].get<unsigned int>();
|
entities[type][name]["height"].get<int>()};
|
||||||
|
|
||||||
entity.srcRect = {0, 0, static_cast<int>(entity.width),
|
|
||||||
static_cast<int>(entity.height)};
|
|
||||||
|
|
||||||
entity.set_central_offset(static_cast<float>(entity.width) / 2, static_cast<float>(entity.height) / 2);
|
|
||||||
|
|
||||||
entity.speed = entities[type][name]["speed"].get<float>();
|
entity.speed = entities[type][name]["speed"].get<float>();
|
||||||
|
|
||||||
std::clog << entity.speed;
|
|
||||||
|
|
||||||
entity.position().x +=100;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
58
src/main.cpp
58
src/main.cpp
@ -1,22 +1,23 @@
|
|||||||
#include "init.hpp"
|
|
||||||
#include "exitcleanup.hpp"
|
#include "exitcleanup.hpp"
|
||||||
|
#include "init.hpp"
|
||||||
#include "types.hpp"
|
#include "types.hpp"
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3_image/SDL_image.h>
|
#include <SDL3_image/SDL_image.h>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <csignal>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <csignal>
|
|
||||||
|
|
||||||
std::vector<Entity*> loaded_entities;
|
std::vector<Entity *> loaded_entities;
|
||||||
sdl_session main_sdl_session;
|
sdl_session main_sdl_session;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
|
|
||||||
main_sdl_session.window =
|
main_sdl_session.window =
|
||||||
SDL_CreateWindow("Naval Swarm", SCREEN_WIDTH, SCREEN_HEIGHT, 0);
|
SDL_CreateWindow("Naval Swarm", SCREEN_WIDTH, SCREEN_HEIGHT, 0);
|
||||||
SDL_Renderer *renderer = SDL_CreateRenderer( main_sdl_session.window, "gpu,vulcan");
|
SDL_Renderer *renderer =
|
||||||
|
SDL_CreateRenderer(main_sdl_session.window, "gpu,vulcan");
|
||||||
|
|
||||||
// Load textures
|
// Load textures
|
||||||
SDL_Texture *bgTexture = IMG_LoadTexture(renderer, "assets/background.png");
|
SDL_Texture *bgTexture = IMG_LoadTexture(renderer, "assets/background.png");
|
||||||
@ -25,10 +26,11 @@ int main() {
|
|||||||
Entity destroyer;
|
Entity destroyer;
|
||||||
|
|
||||||
init_entity(destroyer, renderer, "ship", "destroyer");
|
init_entity(destroyer, renderer, "ship", "destroyer");
|
||||||
|
|
||||||
|
destroyer.position = {WORLD_WIDTH / 2.0f, WORLD_HEIGHT / 2.0f,
|
||||||
destroyer.position() = {WORLD_WIDTH / 2.0f, WORLD_HEIGHT / 2.0f, static_cast<float>(destroyer.width),static_cast<float>(destroyer.height)};
|
static_cast<float>(destroyer.srcRect.w),
|
||||||
|
static_cast<float>(destroyer.srcRect.h)};
|
||||||
|
|
||||||
// Initialize camera
|
// Initialize camera
|
||||||
Camera camera;
|
Camera camera;
|
||||||
bool running = true;
|
bool running = true;
|
||||||
@ -110,8 +112,8 @@ 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";
|
std::clog << "step: " << step << "\n";
|
||||||
|
|
||||||
if (destroyer.gotoT) {
|
if (destroyer.gotoT) {
|
||||||
@ -119,7 +121,8 @@ int main() {
|
|||||||
float dy = destroyer.Tposition.y - destroyer.Central_position().y;
|
float dy = destroyer.Tposition.y - destroyer.Central_position().y;
|
||||||
|
|
||||||
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";
|
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) {
|
||||||
@ -127,11 +130,12 @@ int main() {
|
|||||||
dy /= distance;
|
dy /= distance;
|
||||||
|
|
||||||
if (distance <= step) {
|
if (distance <= step) {
|
||||||
destroyer.Central_position().x = destroyer.Tposition.x;
|
destroyer.Central_position(destroyer.Tposition.x,
|
||||||
destroyer.Central_position().y = destroyer.Tposition.y;
|
destroyer.Tposition.y);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
destroyer.Central_position().x += dx * step;
|
destroyer.position.x += dx * step;
|
||||||
destroyer.Central_position().y += dy * step;
|
destroyer.position.y += dy * step;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -141,10 +145,10 @@ int main() {
|
|||||||
|
|
||||||
// Camera movement
|
// Camera movement
|
||||||
if (camera.followPlayer) {
|
if (camera.followPlayer) {
|
||||||
float targetX = destroyer.position().x + destroyer.position().w / 2 -
|
float targetX =
|
||||||
camera.view.w / 2;
|
destroyer.position.x + destroyer.position.w / 2 - camera.view.w / 2;
|
||||||
float targetY = destroyer.position().y + destroyer.position().h / 2 -
|
float targetY =
|
||||||
camera.view.h / 2;
|
destroyer.position.y + destroyer.position.h / 2 - camera.view.h / 2;
|
||||||
camera.view.x += (targetX - camera.view.x) * camera.smoothness;
|
camera.view.x += (targetX - camera.view.x) * camera.smoothness;
|
||||||
camera.view.y += (targetY - camera.view.y) * camera.smoothness;
|
camera.view.y += (targetY - camera.view.y) * camera.smoothness;
|
||||||
} else {
|
} else {
|
||||||
@ -155,10 +159,10 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// World bounds
|
// World bounds
|
||||||
destroyer.position().x = SDL_clamp(destroyer.position().x, 0.0f,
|
destroyer.position.x = SDL_clamp(destroyer.position.x, 0.0f,
|
||||||
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.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 =
|
camera.view.y =
|
||||||
SDL_clamp(camera.view.y, 0.0f, WORLD_HEIGHT - camera.view.h);
|
SDL_clamp(camera.view.y, 0.0f, WORLD_HEIGHT - camera.view.h);
|
||||||
@ -179,9 +183,9 @@ int main() {
|
|||||||
// Draw player (simplified without animation)
|
// Draw player (simplified without animation)
|
||||||
SDL_FRect srcFRect;
|
SDL_FRect srcFRect;
|
||||||
SDL_RectToFRect(&destroyer.srcRect, &srcFRect);
|
SDL_RectToFRect(&destroyer.srcRect, &srcFRect);
|
||||||
SDL_FRect destRect = {destroyer.position().x - camera.view.x,
|
SDL_FRect destRect = {destroyer.position.x - camera.view.x,
|
||||||
destroyer.position().y - camera.view.y,
|
destroyer.position.y - camera.view.y,
|
||||||
destroyer.position().w, destroyer.position().h};
|
destroyer.position.w, destroyer.position.h};
|
||||||
SDL_RenderTextureRotated(renderer, destroyer.texture, &srcFRect, &destRect,
|
SDL_RenderTextureRotated(renderer, destroyer.texture, &srcFRect, &destRect,
|
||||||
destroyer.angle, nullptr, SDL_FLIP_NONE);
|
destroyer.angle, nullptr, SDL_FLIP_NONE);
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3_image/SDL_image.h>
|
#include <SDL3_image/SDL_image.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#ifndef TYPES_NS
|
#ifndef TYPES_NS
|
||||||
#define TYPES_NS
|
#define TYPES_NS
|
||||||
@ -49,50 +50,32 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct basic_cords {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
};
|
||||||
|
|
||||||
struct Entity {
|
struct Entity {
|
||||||
private:
|
SDL_FRect position;
|
||||||
SDL_FRect m_position;
|
SDL_FRect Tposition;
|
||||||
SDL_FPoint m_offset{0, 0};
|
bool gotoT;
|
||||||
SDL_FRect m_central_cache; // New cached position
|
SDL_Texture *texture;
|
||||||
|
SDL_Rect srcRect;
|
||||||
public:
|
float speed;
|
||||||
// Direct access reference with auto-sync
|
Angle360 angle = 0;
|
||||||
SDL_FRect& position() {
|
|
||||||
// Update cache when position changes
|
basic_cords Central_position(std::optional<float> x = std::nullopt,
|
||||||
m_central_cache = {
|
std::optional<float> y = std::nullopt) {
|
||||||
m_position.x + m_offset.x,
|
if (x != NULL) {
|
||||||
m_position.y + m_offset.y,
|
position.x = *x - position.w / 2;
|
||||||
m_position.w,
|
}
|
||||||
m_position.h
|
if (y != NULL) {
|
||||||
};
|
position.y = *y - position.h / 2;
|
||||||
return m_position;
|
}
|
||||||
}
|
|
||||||
|
return {position.x + position.w / 2, position.y + position.h / 2};
|
||||||
// Return reference to cached central position
|
}
|
||||||
SDL_FRect& Central_position() {
|
};
|
||||||
// Auto-update cache before return
|
|
||||||
m_central_cache.x = m_position.x + m_offset.x;
|
|
||||||
m_central_cache.y = m_position.y + m_offset.y;
|
|
||||||
return m_central_cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set offset values with cache invalidation
|
|
||||||
void set_central_offset(float x, float y) {
|
|
||||||
m_offset = {x, y};
|
|
||||||
Central_position(); // Update cache
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Existing members ---
|
|
||||||
SDL_FRect Tposition;
|
|
||||||
bool gotoT;
|
|
||||||
SDL_Texture* texture;
|
|
||||||
SDL_Rect srcRect;
|
|
||||||
float speed;
|
|
||||||
Angle360 angle = 0;
|
|
||||||
unsigned int width;
|
|
||||||
unsigned int height;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
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};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user