faundation for json loading
This commit is contained in:
parent
b6551d9d6d
commit
87c6b5abdf
11
assets/entities.json
Normal file
11
assets/entities.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"ships": [
|
||||||
|
{
|
||||||
|
"destroyer": {
|
||||||
|
"texture": "assets/entities/ships/destroyer.svg",
|
||||||
|
"width": 58,
|
||||||
|
"height": 512
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
23
src/helper_funcs.cpp
Normal file
23
src/helper_funcs.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include "types.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
void load_entity(Entity &entity, std::string type, std::string name) {
|
||||||
|
try {
|
||||||
|
std::ifstream entitiesF("assets/entities.json");
|
||||||
|
json entities = json::parse(entitiesF);
|
||||||
|
|
||||||
|
} catch (const json::parse_error &e) {
|
||||||
|
std::cerr << "Parse error: " << e.what() << "\n";
|
||||||
|
exit(1);
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
std::cerr << "Error: " << e.what() << "\n";
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
88
src/main.cpp
88
src/main.cpp
@ -1,13 +1,15 @@
|
|||||||
|
#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 "types.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
|
#define destroyer_width 58
|
||||||
|
#define destroyer_height 512
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
SDL_Window* window = SDL_CreateWindow("Naval Swarm", SCREEN_WIDTH, SCREEN_HEIGHT, 0);
|
SDL_Window *window =
|
||||||
|
SDL_CreateWindow("Naval Swarm", SCREEN_WIDTH, SCREEN_HEIGHT, 0);
|
||||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, "gpu,vulcan");
|
SDL_Renderer *renderer = SDL_CreateRenderer(window, "gpu,vulcan");
|
||||||
|
|
||||||
// Load textures
|
// Load textures
|
||||||
@ -17,8 +19,11 @@ int main() {
|
|||||||
// Initialize player
|
// Initialize player
|
||||||
Entity destroyer;
|
Entity destroyer;
|
||||||
destroyer.texture = spriteSheet;
|
destroyer.texture = spriteSheet;
|
||||||
destroyer.position = {WORLD_WIDTH/2.0f, WORLD_HEIGHT/2.0f, 58.0f, 512.0f};
|
destroyer.set_offset(destroyer_width / 2, destroyer_height / 2);
|
||||||
destroyer.srcRect = {0, 0, 58, 512}; // Static source rectangle
|
destroyer.position() = {WORLD_WIDTH / 2.0f, WORLD_HEIGHT / 2.0f, 58.0f,
|
||||||
|
512.0f};
|
||||||
|
destroyer.srcRect = {0, 0, destroyer_width,
|
||||||
|
destroyer_height}; // Static source rectangle
|
||||||
destroyer.angle = 0;
|
destroyer.angle = 0;
|
||||||
|
|
||||||
// Initialize camera
|
// Initialize camera
|
||||||
@ -33,12 +38,16 @@ int main() {
|
|||||||
// Event handling
|
// Event handling
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event)) {
|
||||||
if (event.type == SDL_EVENT_QUIT) running = false;
|
if (event.type == SDL_EVENT_QUIT)
|
||||||
|
running = false;
|
||||||
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) {
|
||||||
printf("Left mouse clicked at (%.1f, %.1f)\nCamera position: (%.1f, %.1f)\nTarget position: (%.1f, %.1f)\n",
|
printf("Left mouse clicked at (%.1f, %.1f)\nCamera position: (%.1f, "
|
||||||
event.button.x, event.button.y, camera.view.x, camera.view.y, camera.view.x+event.button.x, camera.view.y+event.button.y);
|
"%.1f)\nTarget position: (%.1f, %.1f)\n",
|
||||||
|
event.button.x, event.button.y, camera.view.x, camera.view.y,
|
||||||
|
camera.view.x + event.button.x,
|
||||||
|
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;
|
||||||
@ -46,9 +55,6 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Input handling
|
// Input handling
|
||||||
const bool *keystate = SDL_GetKeyboardState(NULL);
|
const bool *keystate = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
@ -100,17 +106,40 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
destroyer.position.x += (float)moveX * destroyer.speed * deltaTime;
|
float step = destroyer.speed * deltaTime;
|
||||||
destroyer.position.y += (float)moveY * destroyer.speed * deltaTime;
|
destroyer.position().x += (float)moveX * step;
|
||||||
|
destroyer.position().y += (float)moveY * step;
|
||||||
|
|
||||||
if (destroyer.gotoT) {
|
if (destroyer.gotoT) {
|
||||||
destroyer.angle = std::atan2(destroyer.Tposition.y - destroyer.position.y, destroyer.Tposition.x - destroyer.position.x) * 180 / M_PI;
|
float dx = destroyer.Tposition.x - destroyer.position().x;
|
||||||
|
float dy = destroyer.Tposition.y - destroyer.position().y;
|
||||||
|
|
||||||
|
destroyer.angle = std::atan2(dy, dx) * 180 / M_PI;
|
||||||
|
|
||||||
|
float distance = std::sqrt(dx * dx + dy * dy);
|
||||||
|
if (distance > 0) {
|
||||||
|
dx /= distance;
|
||||||
|
dy /= distance;
|
||||||
|
|
||||||
|
if (distance <= step) {
|
||||||
|
destroyer.position().x = destroyer.Tposition.x;
|
||||||
|
destroyer.position().y = destroyer.Tposition.y;
|
||||||
|
} else {
|
||||||
|
destroyer.position().x += dx * step;
|
||||||
|
destroyer.position().y += dy * step;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
destroyer.gotoT = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Camera movement
|
// Camera movement
|
||||||
if (camera.followPlayer) {
|
if (camera.followPlayer) {
|
||||||
float targetX = destroyer.position.x + destroyer.position.w/2 - camera.view.w/2;
|
float targetX = destroyer.position().x + destroyer.position().w / 2 -
|
||||||
float targetY = destroyer.position.y + destroyer.position.h/2 - camera.view.h/2;
|
camera.view.w / 2;
|
||||||
|
float targetY = 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 {
|
||||||
@ -121,10 +150,13 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// World bounds
|
// World bounds
|
||||||
destroyer.position.x = SDL_clamp(destroyer.position.x, 0.0f, WORLD_WIDTH - destroyer.position.w);
|
destroyer.position().x = SDL_clamp(destroyer.position().x, 0.0f,
|
||||||
destroyer.position.y = SDL_clamp(destroyer.position.y, 0.0f, WORLD_HEIGHT - destroyer.position.h);
|
WORLD_WIDTH - destroyer.position().w);
|
||||||
|
destroyer.position().y = SDL_clamp(destroyer.position().y, 0.0f,
|
||||||
|
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 = 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
|
||||||
|
|
||||||
@ -134,20 +166,19 @@ int main() {
|
|||||||
|
|
||||||
// Draw background
|
// Draw background
|
||||||
if (bgTexture) {
|
if (bgTexture) {
|
||||||
SDL_FRect bgDest = { -camera.view.x, -camera.view.y, WORLD_WIDTH, WORLD_HEIGHT };
|
SDL_FRect bgDest = {-camera.view.x, -camera.view.y, WORLD_WIDTH,
|
||||||
|
WORLD_HEIGHT};
|
||||||
SDL_RenderTexture(renderer, bgTexture, NULL, &bgDest);
|
SDL_RenderTexture(renderer, bgTexture, NULL, &bgDest);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 = {
|
SDL_FRect destRect = {destroyer.position().x - camera.view.x,
|
||||||
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,
|
SDL_RenderTextureRotated(renderer, destroyer.texture, &srcFRect, &destRect,
|
||||||
destroyer.position.h
|
destroyer.angle, nullptr, SDL_FLIP_NONE);
|
||||||
};
|
|
||||||
SDL_RenderTextureRotated(renderer, destroyer.texture, &srcFRect, &destRect, destroyer.angle, nullptr, SDL_FLIP_NONE);
|
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
@ -156,7 +187,8 @@ int main() {
|
|||||||
if (frameTime < TARGET_FRAME_TIME_NS) {
|
if (frameTime < TARGET_FRAME_TIME_NS) {
|
||||||
const Uint64 sleepTime = TARGET_FRAME_TIME_NS - frameTime;
|
const Uint64 sleepTime = TARGET_FRAME_TIME_NS - frameTime;
|
||||||
SDL_DelayNS(sleepTime - 2'000'000);
|
SDL_DelayNS(sleepTime - 2'000'000);
|
||||||
while (SDL_GetTicksNS() - frameStart < TARGET_FRAME_TIME_NS) {}
|
while (SDL_GetTicksNS() - frameStart < TARGET_FRAME_TIME_NS) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
deltaTime = (SDL_GetTicksNS() - lastFrameTime) / 1e9f;
|
deltaTime = (SDL_GetTicksNS() - lastFrameTime) / 1e9f;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
#include "const.hpp"
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3_image/SDL_image.h>
|
#include <SDL3_image/SDL_image.h>
|
||||||
#include "const.hpp"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
struct Angle360 {
|
struct Angle360 {
|
||||||
@ -9,7 +9,8 @@ struct Angle360 {
|
|||||||
|
|
||||||
void normalize() {
|
void normalize() {
|
||||||
value %= 360;
|
value %= 360;
|
||||||
if (value < 0) value += 360; // Handle negative values
|
if (value < 0)
|
||||||
|
value += 360; // Handle negative values
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -46,7 +47,25 @@ struct Angle360 {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Entity {
|
struct Entity {
|
||||||
SDL_FRect position;
|
private:
|
||||||
|
SDL_FRect m_position;
|
||||||
|
SDL_FPoint m_offset{0, 0}; // Default offset
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Direct access reference with auto-sync
|
||||||
|
SDL_FRect &position() { return m_position; }
|
||||||
|
const SDL_FRect &position() const { return m_position; }
|
||||||
|
|
||||||
|
// Computed central position (always fresh)
|
||||||
|
SDL_FRect Central_position() const {
|
||||||
|
return {m_position.x + m_offset.x, m_position.y + m_offset.y, m_position.w,
|
||||||
|
m_position.h};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set offset values
|
||||||
|
void set_offset(float x, float y) { m_offset = {x, y}; }
|
||||||
|
|
||||||
|
// --- Existing members ---
|
||||||
SDL_FRect Tposition;
|
SDL_FRect Tposition;
|
||||||
bool gotoT;
|
bool gotoT;
|
||||||
SDL_Texture *texture;
|
SDL_Texture *texture;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user