Compare commits
No commits in common. "73a7be3f439e252346edfa651d1ae92d40fa512d" and "87c6b5abdfc4b0ebc77f31d3d161f3c952b9dcd1" have entirely different histories.
73a7be3f43
...
87c6b5abdf
Before Width: | Height: | Size: 69 KiB After Width: | Height: | Size: 69 KiB |
@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"ship": {
|
"ships": [
|
||||||
|
{
|
||||||
"destroyer": {
|
"destroyer": {
|
||||||
"texture": "assets/entities/ships/destroyer.svg",
|
"texture": "assets/entities/ships/destroyer.svg",
|
||||||
"width": 58,
|
"width": 58,
|
||||||
"height": 512,
|
"height": 512
|
||||||
"speed": 250
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
22
src/color.h
22
src/color.h
@ -1,22 +0,0 @@
|
|||||||
|
|
||||||
#ifndef RESET
|
|
||||||
|
|
||||||
#define RESET "\033[0m"
|
|
||||||
#define BLACK "\033[30m" /* Black */
|
|
||||||
#define RED "\033[31m" /* Red */
|
|
||||||
#define GREEN "\033[32m" /* Green */
|
|
||||||
#define YELLOW "\033[33m" /* Yellow */
|
|
||||||
#define BLUE "\033[34m" /* Blue */
|
|
||||||
#define MAGENTA "\033[35m" /* Magenta */
|
|
||||||
#define CYAN "\033[36m" /* Cyan */
|
|
||||||
#define WHITE "\033[37m" /* White */
|
|
||||||
#define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
|
|
||||||
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
|
|
||||||
#define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
|
|
||||||
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
|
|
||||||
#define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
|
|
||||||
#define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
|
|
||||||
#define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
|
|
||||||
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,13 +1,7 @@
|
|||||||
#include <SDL3/SDL_stdinc.h>
|
#include <SDL3/SDL_stdinc.h>
|
||||||
|
|
||||||
#ifndef CONST_NS
|
|
||||||
#define CONST_NS
|
|
||||||
|
|
||||||
constexpr int SCREEN_WIDTH = 800;
|
constexpr int SCREEN_WIDTH = 800;
|
||||||
constexpr int SCREEN_HEIGHT = 600;
|
constexpr int SCREEN_HEIGHT = 600;
|
||||||
constexpr int WORLD_WIDTH = 1600;
|
constexpr int WORLD_WIDTH = 1600;
|
||||||
constexpr int WORLD_HEIGHT = 1200;
|
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;
|
||||||
|
|
||||||
#endif
|
|
@ -1,63 +0,0 @@
|
|||||||
#include <errno.h>
|
|
||||||
#include <csignal>
|
|
||||||
#include "color.h"
|
|
||||||
#include <iostream>
|
|
||||||
#include "exitcleanup.hpp"
|
|
||||||
#include "types.hpp"
|
|
||||||
#include "main.hpp"
|
|
||||||
|
|
||||||
void SignalHandler(int code) {
|
|
||||||
switch (code) {
|
|
||||||
case SIGTERM:
|
|
||||||
std::cerr << "\nreceived SIGTERM exiting...\n";
|
|
||||||
break;
|
|
||||||
case SIGINT:
|
|
||||||
std::cerr << "\nreceived SIGINT exiting...\n";
|
|
||||||
break;
|
|
||||||
case SIGQUIT:
|
|
||||||
std::cerr << "\nreceived SIGQUIT exiting...\n";
|
|
||||||
break;
|
|
||||||
case SIGHUP:
|
|
||||||
std::cerr << "\nreceived SIGHUP exiting...\n";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SIGSEGV:
|
|
||||||
std::cerr << "\nreceived SIGSEGV(segmentaiton fault) exiting...\nIf this "
|
|
||||||
"repeats please report it as a bug\n";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
GeneralCleanUp(code);
|
|
||||||
} catch(...) {
|
|
||||||
std::cerr << RED"[ERROR]" << RESET" general cleanup error\n";
|
|
||||||
exit(errno);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void GeneralCleanUp(int code) {
|
|
||||||
|
|
||||||
for(Entity *entity : loaded_entities) {
|
|
||||||
try {
|
|
||||||
SDL_DestroyTexture(entity->texture);
|
|
||||||
} catch(...) {
|
|
||||||
std::cerr << RED"[ERROR]" << RESET" failed to destroy texture of entity\nentity address:" << YELLOW << entity << RESET"\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
SDL_DestroyWindow(main_sdl_session.window);
|
|
||||||
} catch(...) {
|
|
||||||
std::cerr << RED"[ERROR]" << RESET" failed to destroy window\n";
|
|
||||||
}
|
|
||||||
try{
|
|
||||||
SDL_DestroyRenderer(main_sdl_session.renderer);
|
|
||||||
} catch(...) {
|
|
||||||
std::cerr << RED"[ERROR]" << RESET" failed to destroy renderer\n";
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
SDL_Quit();
|
|
||||||
} catch (...) {
|
|
||||||
std::cerr << RED"[ERROR]" << RESET" failed to quit sdl\n";
|
|
||||||
}
|
|
||||||
exit(code);
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
#ifndef EC_NS
|
|
||||||
#define EC_NS
|
|
||||||
void GeneralCleanUp(int code = 0);
|
|
||||||
#endif
|
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
54
src/init.cpp
54
src/init.cpp
@ -1,54 +0,0 @@
|
|||||||
#include "init.hpp"
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <fstream>
|
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include <ostream>
|
|
||||||
#include <string>
|
|
||||||
#include <csignal>
|
|
||||||
#include "main.hpp"
|
|
||||||
#include <iostream>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include "color.h"
|
|
||||||
|
|
||||||
using json = nlohmann::json;
|
|
||||||
|
|
||||||
void init_entity(Entity &entity, SDL_Renderer *renderer,std::string type, std::string name) {
|
|
||||||
json entities;
|
|
||||||
try {
|
|
||||||
std::ifstream entitiesF("./assets/entities.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);
|
|
||||||
}
|
|
||||||
|
|
||||||
loaded_entities.push_back(&entity);
|
|
||||||
std::clog << entities[type];
|
|
||||||
|
|
||||||
entity.texture = IMG_LoadTexture(renderer, entities[type][name]["texture"].get<std::string>().c_str());
|
|
||||||
|
|
||||||
if(entity.texture == NULL) {
|
|
||||||
std::cerr << "\n" << RED"[ERROR]" << RESET" failed to load texture\n";
|
|
||||||
exit(ENOENT);
|
|
||||||
}
|
|
||||||
|
|
||||||
entity.width = entities[type][name]["width"].get<unsigned int>();
|
|
||||||
entity.height = entities[type][name]["height"].get<unsigned 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>();
|
|
||||||
|
|
||||||
std::clog << entity.speed;
|
|
||||||
|
|
||||||
entity.position().x +=100;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
|||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
#ifndef IN_NS
|
|
||||||
#define IN_NS
|
|
||||||
|
|
||||||
void init_entity(Entity &entity, SDL_Renderer *renderer,std::string type, std::string name);
|
|
||||||
|
|
||||||
#endif
|
|
47
src/main.cpp
47
src/main.cpp
@ -1,33 +1,30 @@
|
|||||||
#include "init.hpp"
|
|
||||||
#include "exitcleanup.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 <iostream>
|
|
||||||
#include <vector>
|
|
||||||
#include <csignal>
|
|
||||||
|
|
||||||
std::vector<Entity*> loaded_entities;
|
#define destroyer_width 58
|
||||||
sdl_session main_sdl_session;
|
#define destroyer_height 512
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
|
SDL_Window *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(window, "gpu,vulcan");
|
||||||
|
|
||||||
// Load textures
|
// Load textures
|
||||||
|
SDL_Texture *spriteSheet = IMG_LoadTexture(renderer, "assets/destroyer.svg");
|
||||||
SDL_Texture *bgTexture = IMG_LoadTexture(renderer, "assets/background.png");
|
SDL_Texture *bgTexture = IMG_LoadTexture(renderer, "assets/background.png");
|
||||||
|
|
||||||
// Initialize player
|
// Initialize player
|
||||||
Entity destroyer;
|
Entity destroyer;
|
||||||
|
destroyer.texture = spriteSheet;
|
||||||
init_entity(destroyer, renderer, "ship", "destroyer");
|
destroyer.set_offset(destroyer_width / 2, destroyer_height / 2);
|
||||||
|
destroyer.position() = {WORLD_WIDTH / 2.0f, WORLD_HEIGHT / 2.0f, 58.0f,
|
||||||
|
512.0f};
|
||||||
destroyer.position() = {WORLD_WIDTH / 2.0f, WORLD_HEIGHT / 2.0f, static_cast<float>(destroyer.width),static_cast<float>(destroyer.height)};
|
destroyer.srcRect = {0, 0, destroyer_width,
|
||||||
|
destroyer_height}; // Static source rectangle
|
||||||
|
destroyer.angle = 0;
|
||||||
|
|
||||||
// Initialize camera
|
// Initialize camera
|
||||||
Camera camera;
|
Camera camera;
|
||||||
@ -112,14 +109,12 @@ 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.position().x;
|
||||||
float dy = destroyer.Tposition.y - destroyer.Central_position().y;
|
float dy = destroyer.Tposition.y - destroyer.position().y;
|
||||||
|
|
||||||
destroyer.angle = std::atan2(dy, dx) * 180 / M_PI + 90;
|
destroyer.angle = std::atan2(dy, dx) * 180 / M_PI;
|
||||||
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 +122,11 @@ int main() {
|
|||||||
dy /= distance;
|
dy /= distance;
|
||||||
|
|
||||||
if (distance <= step) {
|
if (distance <= step) {
|
||||||
destroyer.Central_position().x = destroyer.Tposition.x;
|
destroyer.position().x = destroyer.Tposition.x;
|
||||||
destroyer.Central_position().y = destroyer.Tposition.y;
|
destroyer.position().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 {
|
||||||
@ -201,8 +196,10 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
|
SDL_DestroyTexture(spriteSheet);
|
||||||
SDL_DestroyTexture(bgTexture);
|
SDL_DestroyTexture(bgTexture);
|
||||||
GeneralCleanUp();
|
SDL_DestroyRenderer(renderer);
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
10
src/main.hpp
10
src/main.hpp
@ -1,10 +0,0 @@
|
|||||||
#include <vector>
|
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
#ifndef MAIN_NS
|
|
||||||
#define MAIN_NS
|
|
||||||
|
|
||||||
extern std::vector<Entity*> loaded_entities;
|
|
||||||
extern sdl_session main_sdl_session;
|
|
||||||
|
|
||||||
#endif
|
|
@ -3,9 +3,6 @@
|
|||||||
#include <SDL3_image/SDL_image.h>
|
#include <SDL3_image/SDL_image.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#ifndef TYPES_NS
|
|
||||||
#define TYPES_NS
|
|
||||||
|
|
||||||
struct Angle360 {
|
struct Angle360 {
|
||||||
private:
|
private:
|
||||||
int value{0};
|
int value{0};
|
||||||
@ -52,57 +49,34 @@ public:
|
|||||||
struct Entity {
|
struct Entity {
|
||||||
private:
|
private:
|
||||||
SDL_FRect m_position;
|
SDL_FRect m_position;
|
||||||
SDL_FPoint m_offset{0, 0};
|
SDL_FPoint m_offset{0, 0}; // Default offset
|
||||||
SDL_FRect m_central_cache; // New cached position
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Direct access reference with auto-sync
|
// Direct access reference with auto-sync
|
||||||
SDL_FRect& position() {
|
SDL_FRect &position() { return m_position; }
|
||||||
// Update cache when position changes
|
const SDL_FRect &position() const { return m_position; }
|
||||||
m_central_cache = {
|
|
||||||
m_position.x + m_offset.x,
|
// Computed central position (always fresh)
|
||||||
m_position.y + m_offset.y,
|
SDL_FRect Central_position() const {
|
||||||
m_position.w,
|
return {m_position.x + m_offset.x, m_position.y + m_offset.y, m_position.w,
|
||||||
m_position.h
|
m_position.h};
|
||||||
};
|
|
||||||
return m_position;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return reference to cached central position
|
// Set offset values
|
||||||
SDL_FRect& Central_position() {
|
void set_offset(float x, float y) { m_offset = {x, y}; }
|
||||||
// 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 ---
|
// --- Existing members ---
|
||||||
SDL_FRect Tposition;
|
SDL_FRect Tposition;
|
||||||
bool gotoT;
|
bool gotoT;
|
||||||
SDL_Texture *texture;
|
SDL_Texture *texture;
|
||||||
SDL_Rect srcRect;
|
SDL_Rect srcRect;
|
||||||
float speed;
|
float speed = 250.0f;
|
||||||
Angle360 angle = 0;
|
Angle360 angle;
|
||||||
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};
|
||||||
bool followPlayer = false;
|
bool followPlayer = false;
|
||||||
float speed = 500.0f;
|
float speed = 500.0f;
|
||||||
float smoothness = 0.1f;
|
float smoothness = 0.1f;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sdl_session {
|
|
||||||
SDL_Window *window;
|
|
||||||
SDL_Renderer *renderer;
|
|
||||||
};
|
|
||||||
#endif
|
|
Loading…
x
Reference in New Issue
Block a user