add more rubust structure
This commit is contained in:
parent
1f8aa8e55c
commit
b6551d9d6d
7
src/const.hpp
Normal file
7
src/const.hpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include <SDL3/SDL_stdinc.h>
|
||||
constexpr int SCREEN_WIDTH = 800;
|
||||
constexpr int SCREEN_HEIGHT = 600;
|
||||
constexpr int WORLD_WIDTH = 1600;
|
||||
constexpr int WORLD_HEIGHT = 1200;
|
||||
constexpr int TARGET_FPS = 60;
|
||||
constexpr Uint64 TARGET_FRAME_TIME_NS = 1'000'000'000 / TARGET_FPS;
|
62
src/main.cpp
62
src/main.cpp
@ -1,28 +1,9 @@
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
#include <cmath>
|
||||
#include "types.hpp"
|
||||
|
||||
constexpr int SCREEN_WIDTH = 800;
|
||||
constexpr int SCREEN_HEIGHT = 600;
|
||||
constexpr int WORLD_WIDTH = 1600;
|
||||
constexpr int WORLD_HEIGHT = 1200;
|
||||
constexpr int TARGET_FPS = 60;
|
||||
constexpr Uint64 TARGET_FRAME_TIME_NS = 1'000'000'000 / TARGET_FPS;
|
||||
|
||||
struct Entity {
|
||||
SDL_FRect position;
|
||||
SDL_Texture* texture;
|
||||
SDL_Rect srcRect;
|
||||
float speed = 250.0f;
|
||||
Angle360 Angle;
|
||||
};
|
||||
|
||||
struct Camera {
|
||||
SDL_FRect view = {0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||
bool followPlayer = true;
|
||||
float speed = 500.0f;
|
||||
float smoothness = 0.1f;
|
||||
};
|
||||
|
||||
int main() {
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
@ -38,7 +19,7 @@ int main() {
|
||||
destroyer.texture = spriteSheet;
|
||||
destroyer.position = {WORLD_WIDTH/2.0f, WORLD_HEIGHT/2.0f, 58.0f, 512.0f};
|
||||
destroyer.srcRect = {0, 0, 58, 512}; // Static source rectangle
|
||||
destroyer.Angle = 0;
|
||||
destroyer.angle = 0;
|
||||
|
||||
// Initialize camera
|
||||
Camera camera;
|
||||
@ -53,8 +34,21 @@ int main() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_EVENT_QUIT) running = false;
|
||||
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||
// Check specific button (e.g., left mouse button)
|
||||
if (event.button.button == SDL_BUTTON_LEFT) {
|
||||
printf("Left mouse clicked at (%.1f, %.1f)\nCamera position: (%.1f, %.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.y = camera.view.y+event.button.y;
|
||||
destroyer.gotoT = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Input handling
|
||||
const bool* keystate = SDL_GetKeyboardState(NULL);
|
||||
|
||||
@ -74,34 +68,34 @@ int main() {
|
||||
case 1:
|
||||
switch (moveY) {
|
||||
case 1:
|
||||
destroyer.Angle = 135;
|
||||
destroyer.angle = 135;
|
||||
break;
|
||||
case -1:
|
||||
destroyer.Angle = 45;
|
||||
destroyer.angle = 45;
|
||||
break;
|
||||
default:
|
||||
destroyer.Angle = 90;
|
||||
destroyer.angle = 90;
|
||||
}
|
||||
break;
|
||||
case -1:
|
||||
switch (moveY) {
|
||||
case 1:
|
||||
destroyer.Angle = 225;
|
||||
destroyer.angle = 225;
|
||||
break;
|
||||
case -1:
|
||||
destroyer.Angle = 315;
|
||||
destroyer.angle = 315;
|
||||
break;
|
||||
default:
|
||||
destroyer.Angle = 270;
|
||||
destroyer.angle = 270;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
switch (moveY) {
|
||||
case 1:
|
||||
destroyer.Angle = 180;
|
||||
destroyer.angle = 180;
|
||||
break;
|
||||
case -1:
|
||||
destroyer.Angle = 0;
|
||||
destroyer.angle = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -109,6 +103,10 @@ int main() {
|
||||
destroyer.position.x += (float)moveX * destroyer.speed * deltaTime;
|
||||
destroyer.position.y += (float)moveY * destroyer.speed * deltaTime;
|
||||
|
||||
if(destroyer.gotoT) {
|
||||
destroyer.angle = std::atan2(destroyer.Tposition.y - destroyer.position.y, destroyer.Tposition.x - destroyer.position.x) * 180 / M_PI;
|
||||
}
|
||||
|
||||
// Camera movement
|
||||
if (camera.followPlayer) {
|
||||
float targetX = destroyer.position.x + destroyer.position.w/2 - camera.view.w/2;
|
||||
@ -116,8 +114,8 @@ int main() {
|
||||
camera.view.x += (targetX - camera.view.x) * camera.smoothness;
|
||||
camera.view.y += (targetY - camera.view.y) * camera.smoothness;
|
||||
} else {
|
||||
float camMoveX = keystate[SDL_SCANCODE_L] - keystate[SDL_SCANCODE_J];
|
||||
float camMoveY = keystate[SDL_SCANCODE_K] - keystate[SDL_SCANCODE_I];
|
||||
float camMoveX = keystate[SDL_SCANCODE_D] - keystate[SDL_SCANCODE_A];
|
||||
float camMoveY = keystate[SDL_SCANCODE_S] - keystate[SDL_SCANCODE_W];
|
||||
camera.view.x += camMoveX * camera.speed * deltaTime;
|
||||
camera.view.y += camMoveY * camera.speed * deltaTime;
|
||||
}
|
||||
@ -149,7 +147,7 @@ int main() {
|
||||
destroyer.position.w,
|
||||
destroyer.position.h
|
||||
};
|
||||
SDL_RenderTextureRotated(renderer, destroyer.texture, &srcFRect, &destRect, destroyer.Angle, nullptr, SDL_FLIP_NONE);
|
||||
SDL_RenderTextureRotated(renderer, destroyer.texture, &srcFRect, &destRect, destroyer.angle, nullptr, SDL_FLIP_NONE);
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
#include "const.hpp"
|
||||
#include <iostream>
|
||||
|
||||
struct Angle360 {
|
||||
@ -42,5 +43,21 @@ struct Angle360 {
|
||||
friend std::ostream& operator<<(std::ostream& os, const Angle360& a) {
|
||||
return os << a.value;
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
struct Entity {
|
||||
SDL_FRect position;
|
||||
SDL_FRect Tposition;
|
||||
bool gotoT;
|
||||
SDL_Texture* texture;
|
||||
SDL_Rect srcRect;
|
||||
float speed = 250.0f;
|
||||
Angle360 angle;
|
||||
};
|
||||
|
||||
struct Camera {
|
||||
SDL_FRect view = {0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||
bool followPlayer = false;
|
||||
float speed = 500.0f;
|
||||
float smoothness = 0.1f;
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user