Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
724c575c4e | ||
|
|
28ed600954 | ||
|
|
978bf41ab0 | ||
|
|
4046f6dfd5 | ||
|
|
c038867810 | ||
|
|
abe90847ea | ||
|
|
1f3674b266 | ||
|
|
2e5e37b0a5 | ||
|
|
7cc8524811 | ||
|
|
55402c0c66 | ||
|
|
f1cdffba2b | ||
|
|
0e5a4c89e5 | ||
|
|
bb47dcc9f9 | ||
|
|
3d9c211737 | ||
|
|
4e7e37821e | ||
|
|
f520790f9c | ||
|
|
44531ab24e | ||
|
|
c1139ddb81 | ||
|
|
da8d83de92 | ||
|
|
bc6881f851 | ||
|
|
b842bd0095 | ||
|
|
bb28285794 | ||
|
|
6621decc09 | ||
|
|
fd9fd453b3 | ||
|
|
b0f4f6849d | ||
|
|
8924741937 | ||
|
|
63f950e26b | ||
|
|
349848ebf6 | ||
|
|
395c3c854f | ||
|
|
a93e9a7b73 | ||
|
|
29219bbc2c | ||
|
|
d343c5e5a0 | ||
|
|
17777e2178 | ||
|
|
609d197227 | ||
|
|
e29a0bc7a3 | ||
|
|
92e406bc74 | ||
|
|
2d64adf476 |
@@ -1,10 +0,0 @@
|
|||||||
v0.1: First Release
|
|
||||||
- Basic simulation.
|
|
||||||
- Information printing.
|
|
||||||
|
|
||||||
v0.2: Menu Bar
|
|
||||||
- Added a menu bar.
|
|
||||||
- Can reset the simulation.
|
|
||||||
|
|
||||||
v0.3: Port to C
|
|
||||||
- Codebase ported to C for better performance.
|
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# Change Log
|
||||||
|
|
||||||
|
## v1.0: Port to C++
|
||||||
|
|
||||||
|
- Port the bulk of the codebase to C++ for simplicity.
|
||||||
|
|
||||||
|
## v0.6: Planetary Masses
|
||||||
|
|
||||||
|
- Add planetary masses.
|
||||||
|
- Modify some of the constants.
|
||||||
|
|
||||||
|
## v0.5: Effects
|
||||||
|
|
||||||
|
- Added stars in background for a better sense of velocity.
|
||||||
|
- Viewport follows ship at center.
|
||||||
|
- Zoom functionality.
|
||||||
|
|
||||||
|
## v0.4: More functionality
|
||||||
|
|
||||||
|
- Fix acceleration.
|
||||||
|
- Add fullscreen support.
|
||||||
|
- Pause functionality.
|
||||||
|
|
||||||
|
## v0.3: Port to C
|
||||||
|
|
||||||
|
- Codebase ported to C for better performance.
|
||||||
|
|
||||||
|
## v0.2: Menu Bar
|
||||||
|
|
||||||
|
- Added a menu bar.
|
||||||
|
- Can reset the simulation.
|
||||||
|
|
||||||
|
## v1.0: First Release
|
||||||
|
|
||||||
|
- Basic simulation.
|
||||||
|
- Information printing.
|
||||||
+36
-17
@@ -1,5 +1,5 @@
|
|||||||
# Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
# Copyright (C) 2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
# Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
# Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
@@ -14,11 +14,13 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 2.8)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
project(SpaceShipSim C)
|
project(spaceshipsim
|
||||||
|
VERSION 1.0
|
||||||
set(TARGET_NAME "spaceshipsim")
|
SPDX_LICENSE "GPL-3.0-or-later"
|
||||||
set(TARGET_VERSION "0.3")
|
DESCRIPTION "A simulation of a 2D space ship in the frictionless environment of space."
|
||||||
|
HOMEPAGE_URL "https://code.ortegas.org/nortega/spaceshipsim"
|
||||||
|
LANGUAGES C CXX)
|
||||||
|
|
||||||
if(NOT CMAKE_BUILD_TYPE)
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
set(CMAKE_BUILD_TYPE "release")
|
set(CMAKE_BUILD_TYPE "release")
|
||||||
@@ -33,8 +35,14 @@ set(CMAKE_C_FLAGS_RELEASE "-O3 -ffast-math")
|
|||||||
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3 -ffast-math")
|
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3 -ffast-math")
|
||||||
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
|
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
|
||||||
|
|
||||||
|
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Wextra -Werror")
|
||||||
|
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
|
||||||
|
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -ffast-math")
|
||||||
|
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O3 -ffast-math")
|
||||||
|
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os")
|
||||||
|
|
||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
pkg_check_modules(ALLEG5 REQUIRED allegro-5)
|
pkg_check_modules(ALLEG5 REQUIRED allegro-5>=5.1.9)
|
||||||
pkg_check_modules(ALLEG5-PRIM REQUIRED allegro_primitives-5)
|
pkg_check_modules(ALLEG5-PRIM REQUIRED allegro_primitives-5)
|
||||||
pkg_check_modules(ALLEG5-FONT REQUIRED allegro_font-5)
|
pkg_check_modules(ALLEG5-FONT REQUIRED allegro_font-5)
|
||||||
|
|
||||||
@@ -42,27 +50,38 @@ include_directories(
|
|||||||
SYSTEM ${ALLEG5_INCLUDE_DIRS})
|
SYSTEM ${ALLEG5_INCLUDE_DIRS})
|
||||||
|
|
||||||
set(SRCS
|
set(SRCS
|
||||||
src/event_manager.c
|
"src/event_manager.c"
|
||||||
src/main.c
|
"src/main.cpp"
|
||||||
src/ship.c)
|
"src/planet.cpp"
|
||||||
|
"src/ship.cpp"
|
||||||
|
"src/starfield.c"
|
||||||
|
)
|
||||||
|
|
||||||
add_definitions(-DVERSION="${TARGET_VERSION}")
|
set(HDRS
|
||||||
|
"src/event_manager.h"
|
||||||
|
"src/globals.h"
|
||||||
|
"src/planet.h"
|
||||||
|
"src/ship.h"
|
||||||
|
"src/starfield.h"
|
||||||
|
"src/vec.h"
|
||||||
|
)
|
||||||
|
|
||||||
if(${CMAKE_BUILD_TYPE} STREQUAL "debug" OR
|
add_definitions(-DVERSION="${PROJECT_VERSION}")
|
||||||
${CMAKE_BUILD_TYPE} STREQUAL "relwithdebinfo")
|
|
||||||
|
if(${CMAKE_BUILD_TYPE} STREQUAL "debug" OR ${CMAKE_BUILD_TYPE} STREQUAL "relwithdebinfo")
|
||||||
add_definitions(-DDEBUG)
|
add_definitions(-DDEBUG)
|
||||||
else()
|
else()
|
||||||
add_definitions(-DNDEBUG)
|
add_definitions(-DNDEBUG)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_executable(${TARGET_NAME} ${SRCS})
|
add_executable(${PROJECT_NAME} ${SRCS} ${HDRS})
|
||||||
|
|
||||||
target_link_libraries(${TARGET_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
m # math library
|
m # math library
|
||||||
${ALLEG5_LIBRARIES}
|
${ALLEG5_LIBRARIES}
|
||||||
${ALLEG5-PRIM_LIBRARIES}
|
${ALLEG5-PRIM_LIBRARIES}
|
||||||
${ALLEG5-FONT_LIBRARIES})
|
${ALLEG5-FONT_LIBRARIES})
|
||||||
|
|
||||||
install(TARGETS ${TARGET_NAME}
|
install(TARGETS ${PROJECT_NAME}
|
||||||
RUNTIME DESTINATION bin/
|
RUNTIME DESTINATION bin/
|
||||||
CONFIGURATIONS release minsizerel)
|
CONFIGURATIONS release minsizerel)
|
||||||
|
|||||||
+23
-22
@@ -1,39 +1,40 @@
|
|||||||
====================
|
# SpaceShipSim
|
||||||
*** SpaceShipSim ***
|
|
||||||
====================
|
|
||||||
This is a small simulation program of a space ship (with infinite fuel) in a
|
This is a small simulation program of a space ship (with infinite fuel) in a
|
||||||
frictionless environment (space). The idea is to use it for educational
|
frictionless environment (space). The idea is to use it for educational
|
||||||
purposes.
|
purposes.
|
||||||
|
|
||||||
# Compiling
|
## Compiling
|
||||||
-----------
|
|
||||||
In order to build this program you will require the following dependencies:
|
In order to build this program you will require the following dependencies:
|
||||||
|
|
||||||
- GNU GCC (https://gcc.gnu.org/)
|
- GNU C and C++ compiler that supports C++11
|
||||||
- CMake (https://cmake.org/)
|
- CMake 3.10 or higher
|
||||||
- Allegro 5 (http://liballeg.org/)
|
- Allegro 5
|
||||||
|
|
||||||
You can then compile the program via the following commands:
|
You can then compile the program via the following commands:
|
||||||
|
|
||||||
cd build/
|
```bash
|
||||||
cmake ..
|
cd build/
|
||||||
make
|
cmake ..
|
||||||
|
make
|
||||||
|
```
|
||||||
|
|
||||||
This will create a release build with compiler optimizations. If you would like
|
This will create a release build with compiler optimizations. If you would like
|
||||||
a debug build then pass the `-DCMAKE_BUILD_TYPE=debug' flag to the `cmake'
|
a debug build then pass the `-DCMAKE_BUILD_TYPE=debug` flag to the `cmake`
|
||||||
command. If you would like to install then run `cmake' with the additional flag
|
command. If you would like to install then run `cmake` with the additional flag
|
||||||
`-DCMAKE_INSTALL_PREFIX=<install_dir>'. If you are installing as a user then
|
`-DCMAKE_INSTALL_PREFIX=<install_dir>`. If you are installing as a user then
|
||||||
you may want to set the installation prefix to `/usr/local/', and if you're
|
you may want to set the installation prefix to `/usr/local/`, and if you're
|
||||||
packaging then please consult your distribution's policies. With this you
|
packaging then please consult your distribution's policies. With this you
|
||||||
should be able to run the `make install' target and install the binary.
|
should be able to run the `make install' target and install the binary.
|
||||||
|
|
||||||
# Contributing
|
## Contributing
|
||||||
--------------
|
|
||||||
If you would like to contribute to the project, send a patch file to my e-mail
|
If you would like to contribute to the project, send a patch file to my e-mail
|
||||||
address: <nortega@themusicinnoise.net>.
|
address: [nicolas@ortegas.org](mailto:nicolas@ortegas.org).
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
# License
|
|
||||||
---------
|
|
||||||
As educational software, unless otherwise noted, all files are licensed under
|
As educational software, unless otherwise noted, all files are licensed under
|
||||||
the terms & conditions of the GNU General Public License version 3 or greater
|
the terms & conditions of the GNU General Public License version 3 or greater
|
||||||
(see `LICENSE' file for more information).
|
(see the [license file](LICENSE) for more information).
|
||||||
+56
-37
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
* Copyright (C) 2018,2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -26,8 +26,8 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <allegro5/allegro.h>
|
#include <allegro5/allegro.h>
|
||||||
|
|
||||||
static ALLEGRO_EVENT_QUEUE *event_queue;
|
static ALLEGRO_EVENT_QUEUE *event_queue = NULL;
|
||||||
static ALLEGRO_TIMER *timer;
|
static ALLEGRO_TIMER *timer = NULL;
|
||||||
static int keys[KEY_MAX];
|
static int keys[KEY_MAX];
|
||||||
|
|
||||||
int evnt_mngr_init(ALLEGRO_DISPLAY *display) {
|
int evnt_mngr_init(ALLEGRO_DISPLAY *display) {
|
||||||
@@ -35,24 +35,23 @@ int evnt_mngr_init(ALLEGRO_DISPLAY *display) {
|
|||||||
if(!al_install_keyboard())
|
if(!al_install_keyboard())
|
||||||
return 0;
|
return 0;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
puts("Initialized keyboard.");
|
puts("[DEBUG] Initialized keyboard.");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
timer = al_create_timer(1.0f / FPS);
|
timer = al_create_timer(1.0f / FPS);
|
||||||
if(!timer)
|
if(!timer)
|
||||||
return 0;
|
return 0;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
puts("Initialized timer.");
|
puts("[DEBUG] Initialized timer.");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
event_queue = al_create_event_queue();
|
event_queue = al_create_event_queue();
|
||||||
if(!event_queue)
|
if(!event_queue) {
|
||||||
{
|
|
||||||
al_destroy_timer(timer);
|
al_destroy_timer(timer);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
puts("Initialized event queue.");
|
puts("[DEBUG] Initialized event queue.");
|
||||||
#endif
|
#endif
|
||||||
al_register_event_source(event_queue,
|
al_register_event_source(event_queue,
|
||||||
al_get_display_event_source(display));
|
al_get_display_event_source(display));
|
||||||
@@ -70,19 +69,28 @@ int evnt_mngr_init(ALLEGRO_DISPLAY *display) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void evnt_mngr_deinit() {
|
void evnt_mngr_deinit() {
|
||||||
al_destroy_timer(timer);
|
if(al_is_keyboard_installed()) {
|
||||||
|
al_uninstall_keyboard();
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
puts("Destroyed timer.");
|
puts("[DEBUG] Uninstalled keyboard.");
|
||||||
#endif
|
#endif
|
||||||
al_destroy_event_queue(event_queue);
|
}
|
||||||
|
if(timer) {
|
||||||
|
al_destroy_timer(timer);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
puts("Destroyed event queue.");
|
puts("[DEBUG] Destroyed timer.");
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
if(event_queue) {
|
||||||
|
al_destroy_event_queue(event_queue);
|
||||||
|
#ifdef DEBUG
|
||||||
|
puts("[DEBUG] Destroyed event queue.");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_key(int keycode, int value) {
|
void set_key(int keycode, int value) {
|
||||||
switch(keycode)
|
switch(keycode) {
|
||||||
{
|
|
||||||
case ALLEGRO_KEY_UP:
|
case ALLEGRO_KEY_UP:
|
||||||
keys[KEY_UP] = value;
|
keys[KEY_UP] = value;
|
||||||
break;
|
break;
|
||||||
@@ -101,6 +109,16 @@ void set_key(int keycode, int value) {
|
|||||||
case ALLEGRO_KEY_F:
|
case ALLEGRO_KEY_F:
|
||||||
keys[KEY_FULLSCREEN] = value;
|
keys[KEY_FULLSCREEN] = value;
|
||||||
break;
|
break;
|
||||||
|
case ALLEGRO_KEY_P:
|
||||||
|
keys[KEY_PAUSE] = value;
|
||||||
|
break;
|
||||||
|
case ALLEGRO_KEY_LSHIFT:
|
||||||
|
case ALLEGRO_KEY_RSHIFT:
|
||||||
|
keys[KEY_SHIFT] = value;
|
||||||
|
break;
|
||||||
|
case ALLEGRO_KEY_Z:
|
||||||
|
keys[KEY_ZOOM] = value;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -110,29 +128,30 @@ void handle_event() {
|
|||||||
ALLEGRO_EVENT evnt;
|
ALLEGRO_EVENT evnt;
|
||||||
al_wait_for_event(event_queue, &evnt);
|
al_wait_for_event(event_queue, &evnt);
|
||||||
|
|
||||||
switch(evnt.type)
|
do {
|
||||||
{
|
switch(evnt.type) {
|
||||||
case ALLEGRO_EVENT_TIMER:
|
case ALLEGRO_EVENT_TIMER:
|
||||||
redraw = 1;
|
redraw = 1;
|
||||||
break;
|
break;
|
||||||
case ALLEGRO_EVENT_DISPLAY_CLOSE:
|
case ALLEGRO_EVENT_DISPLAY_CLOSE:
|
||||||
run = 0;
|
|
||||||
break;
|
|
||||||
case ALLEGRO_EVENT_KEY_DOWN:
|
|
||||||
if(evnt.keyboard.keycode == ALLEGRO_KEY_H)
|
|
||||||
show_help = !show_help;
|
|
||||||
else if(evnt.keyboard.keycode == ALLEGRO_KEY_I)
|
|
||||||
show_info = !show_info;
|
|
||||||
else if(evnt.keyboard.keycode == ALLEGRO_KEY_Q ||
|
|
||||||
evnt.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
|
|
||||||
run = 0;
|
run = 0;
|
||||||
else
|
break;
|
||||||
set_key(evnt.keyboard.keycode, 1);
|
case ALLEGRO_EVENT_KEY_DOWN:
|
||||||
break;
|
if(evnt.keyboard.keycode == ALLEGRO_KEY_H)
|
||||||
case ALLEGRO_EVENT_KEY_UP:
|
show_help = !show_help;
|
||||||
set_key(evnt.keyboard.keycode, 0);
|
else if(evnt.keyboard.keycode == ALLEGRO_KEY_I)
|
||||||
break;
|
show_info = !show_info;
|
||||||
}
|
else if(evnt.keyboard.keycode == ALLEGRO_KEY_Q ||
|
||||||
|
evnt.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
|
||||||
|
run = 0;
|
||||||
|
else
|
||||||
|
set_key(evnt.keyboard.keycode, 1);
|
||||||
|
break;
|
||||||
|
case ALLEGRO_EVENT_KEY_UP:
|
||||||
|
set_key(evnt.keyboard.keycode, 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while(al_get_next_event(event_queue, &evnt));
|
||||||
}
|
}
|
||||||
|
|
||||||
int key_is_down(int code) {
|
int key_is_down(int code) {
|
||||||
|
|||||||
+14
-3
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
* Copyright (C) 2018,2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -20,6 +20,10 @@
|
|||||||
|
|
||||||
#include <allegro5/allegro.h>
|
#include <allegro5/allegro.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
KEY_UP = 0x0,
|
KEY_UP = 0x0,
|
||||||
KEY_DOWN = 0x1,
|
KEY_DOWN = 0x1,
|
||||||
@@ -27,7 +31,10 @@ enum {
|
|||||||
KEY_RIGHT = 0x3,
|
KEY_RIGHT = 0x3,
|
||||||
KEY_RESET = 0x4,
|
KEY_RESET = 0x4,
|
||||||
KEY_FULLSCREEN = 0x5,
|
KEY_FULLSCREEN = 0x5,
|
||||||
KEY_MAX = 0x6
|
KEY_PAUSE = 0x6,
|
||||||
|
KEY_ZOOM = 0x7,
|
||||||
|
KEY_SHIFT = 0x8,
|
||||||
|
KEY_MAX = 0x9
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,3 +66,7 @@ void handle_event();
|
|||||||
* @return If down 1 will be returned, else 0.
|
* @return If down 1 will be returned, else 0.
|
||||||
*/
|
*/
|
||||||
int key_is_down(int code);
|
int key_is_down(int code);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|||||||
+24
-6
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
* Copyright (C) 2018,2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -18,6 +18,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#ifndef M_PI
|
||||||
|
# define M_PI 3.14159265f
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef VERSION
|
#ifndef VERSION
|
||||||
# define VERSION "[version]"
|
# define VERSION "[version]"
|
||||||
#endif
|
#endif
|
||||||
@@ -26,7 +31,7 @@
|
|||||||
# define FPS 60.0f
|
# define FPS 60.0f
|
||||||
#endif
|
#endif
|
||||||
#ifndef ACCEL
|
#ifndef ACCEL
|
||||||
# define ACCEL 0.5f
|
# define ACCEL 0.3f
|
||||||
#endif
|
#endif
|
||||||
#ifndef TURN_ACCEL
|
#ifndef TURN_ACCEL
|
||||||
# define TURN_ACCEL (M_PI / FPS) // turn at pi radians / sec
|
# define TURN_ACCEL (M_PI / FPS) // turn at pi radians / sec
|
||||||
@@ -39,16 +44,29 @@
|
|||||||
# define WINDOW_HEIGHT 600
|
# define WINDOW_HEIGHT 600
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <math.h>
|
#ifndef ZOOM_MIN
|
||||||
#ifndef M_PI
|
# define ZOOM_MIN 0.25f
|
||||||
# define M_PI 3.14159265f
|
#endif
|
||||||
|
#ifndef ZOOM_MAX
|
||||||
|
# define ZOOM_MAX 4.0f
|
||||||
|
#endif
|
||||||
|
#ifndef ZOOM_STEP
|
||||||
|
# define ZOOM_STEP 0.1f
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define RAD_TO_DEG(x) (x * 180 / M_PI)
|
#define RAD_TO_DEG(x) (x * 180 / M_PI)
|
||||||
|
|
||||||
#include <allegro5/allegro.h>
|
#include <allegro5/allegro.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
extern int redraw; ///< Whether or not to redraw the screen.
|
extern int redraw; ///< Whether or not to redraw the screen.
|
||||||
extern int run; ///< Whether or not to continue running the simulation.
|
extern int run; ///< Whether or not to continue running the simulation.
|
||||||
extern int show_help; ///< Whether or not to show the help info.
|
extern int show_help; ///< Whether or not to show the help info.
|
||||||
extern int show_info; ///< Whether or not to show simulation info.
|
extern int show_info; ///< Whether or not to show simulation info.
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|||||||
-166
@@ -1,166 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
|
||||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "globals.h"
|
|
||||||
#include "event_manager.h"
|
|
||||||
#include "ship.h"
|
|
||||||
|
|
||||||
int run;
|
|
||||||
int redraw;
|
|
||||||
int show_help;
|
|
||||||
int show_info;
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <allegro5/allegro.h>
|
|
||||||
#include <allegro5/allegro_primitives.h>
|
|
||||||
#include <allegro5/allegro_font.h>
|
|
||||||
|
|
||||||
const char *help =
|
|
||||||
"HELP:\n"
|
|
||||||
"LEFT/RIGHT - turn the ship\n"
|
|
||||||
"UP/DOWN - accelerate/decelerate\n"
|
|
||||||
"R - reset the simulation\n"
|
|
||||||
"I - show/hide simulation information\n"
|
|
||||||
"H - show/hide this help information\n"
|
|
||||||
"Q/ESC - quit";
|
|
||||||
|
|
||||||
const char *info_format =
|
|
||||||
"INFO:\n"
|
|
||||||
"x: %f\n"
|
|
||||||
"y: %f\n"
|
|
||||||
"angle: %f (%f degrees)\n"
|
|
||||||
"velX: %f\n"
|
|
||||||
"velY: %f";
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
printf("SpaceShipSim v%s\n", VERSION);
|
|
||||||
|
|
||||||
if(!al_init())
|
|
||||||
{
|
|
||||||
fprintf(stderr, "alleg5: failed to initialize Allegro.\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
|
||||||
puts("Initialized allegro system.");
|
|
||||||
#endif
|
|
||||||
if(!al_init_primitives_addon())
|
|
||||||
{
|
|
||||||
fprintf(stderr, "alleg5: failed to initialize primitives addon.\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
|
||||||
puts("Initialized primitives addon.");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(!al_init_font_addon())
|
|
||||||
{
|
|
||||||
fprintf(stderr, "alleg5: failed to initialize font addon.\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ALLEGRO_DISPLAY *display = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT);
|
|
||||||
if(!display)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "alleg5: failed to initialize display.\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
|
||||||
puts("Created display.");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(!evnt_mngr_init(display))
|
|
||||||
{
|
|
||||||
fprintf(stderr, "alleg5: failed to initialize event queue.\n");
|
|
||||||
al_destroy_display(display);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize the spaceship at the center of the screen
|
|
||||||
struct ship ship;
|
|
||||||
ship_init(&ship, 400, 300);
|
|
||||||
|
|
||||||
ALLEGRO_FONT *font = al_create_builtin_font();
|
|
||||||
|
|
||||||
// begin running the simulation
|
|
||||||
run = 1;
|
|
||||||
redraw = 1;
|
|
||||||
show_help = 1;
|
|
||||||
show_info = 1;
|
|
||||||
while(run)
|
|
||||||
{
|
|
||||||
handle_event();
|
|
||||||
|
|
||||||
// only redraw or run simulation if the timer event has occurred
|
|
||||||
if(redraw)
|
|
||||||
{
|
|
||||||
if(key_is_down(KEY_RESET))
|
|
||||||
ship_init(&ship, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
|
|
||||||
/*
|
|
||||||
* We only run the simulation when the timer goes off so it's
|
|
||||||
* running at a consistent rate, rather than dependent on random
|
|
||||||
* events.
|
|
||||||
*/
|
|
||||||
ship_update(&ship);
|
|
||||||
al_clear_to_color(al_map_rgb(0, 0, 0));
|
|
||||||
ship_draw(&ship);
|
|
||||||
if(show_info)
|
|
||||||
{
|
|
||||||
char info[256];
|
|
||||||
sprintf(info, info_format,
|
|
||||||
ship.x, ship.y,
|
|
||||||
ship.direction,
|
|
||||||
RAD_TO_DEG(ship.direction),
|
|
||||||
ship.velX, ship.velY);
|
|
||||||
al_draw_multiline_text(font,
|
|
||||||
al_map_rgb(0xFF, 0xFF, 0xFF),
|
|
||||||
5, 5, WINDOW_WIDTH, 10.0f,
|
|
||||||
ALLEGRO_ALIGN_LEFT,
|
|
||||||
info);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(show_help)
|
|
||||||
{
|
|
||||||
al_draw_multiline_text(font,
|
|
||||||
al_map_rgb(0xFF, 0xFF, 0xFF),
|
|
||||||
WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2,
|
|
||||||
WINDOW_WIDTH, 10.0f,
|
|
||||||
ALLEGRO_ALIGN_CENTRE,
|
|
||||||
help);
|
|
||||||
}
|
|
||||||
al_flip_display();
|
|
||||||
redraw = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
al_destroy_font(font);
|
|
||||||
evnt_mngr_deinit();
|
|
||||||
al_destroy_display(display);
|
|
||||||
#ifdef DEBUG
|
|
||||||
puts("Destroyed display.");
|
|
||||||
#endif
|
|
||||||
al_shutdown_font_addon();
|
|
||||||
#ifdef DEBUG
|
|
||||||
puts("Shutdown font addon.");
|
|
||||||
#endif
|
|
||||||
al_shutdown_primitives_addon();
|
|
||||||
#ifdef DEBUG
|
|
||||||
puts("Shutdown primitives addon.");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
+270
@@ -0,0 +1,270 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018,2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "globals.h"
|
||||||
|
#include "event_manager.h"
|
||||||
|
#include "ship.h"
|
||||||
|
#include "starfield.h"
|
||||||
|
#include "planet.h"
|
||||||
|
#include "vec.h"
|
||||||
|
|
||||||
|
int run;
|
||||||
|
int redraw;
|
||||||
|
int show_help;
|
||||||
|
int show_info;
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <allegro5/allegro.h>
|
||||||
|
#include <allegro5/allegro_primitives.h>
|
||||||
|
#include <allegro5/allegro_font.h>
|
||||||
|
|
||||||
|
const char *help =
|
||||||
|
"HELP:\n"
|
||||||
|
"LEFT/RIGHT - turn the ship\n"
|
||||||
|
"UP/DOWN - accelerate/reverse accelerate\n"
|
||||||
|
"R - reset the simulation\n"
|
||||||
|
"P - pause/unpause the simulation\n"
|
||||||
|
"I - show/hide simulation information\n"
|
||||||
|
"H - show/hide this help information\n"
|
||||||
|
"F - toggle fullscreen\n"
|
||||||
|
"Z - zoom in\n"
|
||||||
|
"SHIFT+Z - zoom out\n"
|
||||||
|
"Q/ESC - quit";
|
||||||
|
|
||||||
|
const char *info_format =
|
||||||
|
"INFO:\n"
|
||||||
|
"x: %f\n"
|
||||||
|
"y: %f\n"
|
||||||
|
"angle: %f (%f degrees)\n"
|
||||||
|
"velX: %f px/sec\n"
|
||||||
|
"velY: %f px/sec";
|
||||||
|
|
||||||
|
struct allegro_components {
|
||||||
|
int primitives_addon;
|
||||||
|
int font_addon;
|
||||||
|
ALLEGRO_DISPLAY *display;
|
||||||
|
int evnt_system;
|
||||||
|
ALLEGRO_FONT *font;
|
||||||
|
};
|
||||||
|
|
||||||
|
void cleanup_allegro(struct allegro_components *allegro) {
|
||||||
|
if(allegro->font) {
|
||||||
|
al_destroy_font(allegro->font);
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "[DEBUG] Destroyed font." << std::endl;
|
||||||
|
#endif
|
||||||
|
allegro->font = nullptr;
|
||||||
|
}
|
||||||
|
if(allegro->evnt_system) {
|
||||||
|
evnt_mngr_deinit();
|
||||||
|
allegro->evnt_system = 0;
|
||||||
|
}
|
||||||
|
if(allegro->display) {
|
||||||
|
al_destroy_display(allegro->display);
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "[DEBUG] Destroyed display." << std::endl;
|
||||||
|
#endif
|
||||||
|
allegro->display = nullptr;
|
||||||
|
}
|
||||||
|
if(allegro->font_addon) {
|
||||||
|
al_shutdown_font_addon();
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "[DEBUG] Shutdown font addon." << std::endl;
|
||||||
|
#endif
|
||||||
|
allegro->font_addon = 0;
|
||||||
|
}
|
||||||
|
if(allegro->primitives_addon) {
|
||||||
|
al_shutdown_primitives_addon();
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "[DEBUG] Shutdown primitives addon." << std::endl;
|
||||||
|
#endif
|
||||||
|
allegro->primitives_addon = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
const std::string title = "SpaceShipSim v" + std::string(VERSION);
|
||||||
|
float zoom = 1.0f;
|
||||||
|
float display_width = WINDOW_WIDTH;
|
||||||
|
float display_height = WINDOW_HEIGHT;
|
||||||
|
struct allegro_components allegro __attribute__((cleanup(cleanup_allegro))) = {
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
nullptr,
|
||||||
|
0,
|
||||||
|
nullptr
|
||||||
|
};
|
||||||
|
|
||||||
|
std::cout << title << std::endl;
|
||||||
|
|
||||||
|
if(!al_init()) {
|
||||||
|
fprintf(stderr, "alleg5: failed to initialize Allegro.\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "[DEBUG] Initialized allegro system." << std::endl;
|
||||||
|
#endif
|
||||||
|
if(!(allegro.primitives_addon = al_init_primitives_addon())) {
|
||||||
|
fprintf(stderr, "alleg5: failed to initialize primitives addon.\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "[DEBUG] Initialized primitives addon." << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if(!(allegro.font_addon = al_init_font_addon())) {
|
||||||
|
fprintf(stderr, "alleg5: failed to initialize font addon.\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "[DEBUG] Initialized font addon." << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
allegro.display = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||||
|
if(!allegro.display) {
|
||||||
|
fprintf(stderr, "alleg5: failed to initialize display.\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
al_set_window_title(allegro.display, title.c_str());
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "[DEBUG] Created display." << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if(!(allegro.evnt_system = evnt_mngr_init(allegro.display))) {
|
||||||
|
fprintf(stderr, "alleg5: failed to initialize event queue.\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize the spaceship at the center of the screen
|
||||||
|
Ship ship(400, 300);
|
||||||
|
|
||||||
|
// initialize the planet
|
||||||
|
Planet planet(400.0f, 300.0f, 250.0f);
|
||||||
|
|
||||||
|
allegro.font = al_create_builtin_font();
|
||||||
|
#ifdef DEBUG
|
||||||
|
std::cout << "[DEBUG] Created font." << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// begin running the simulation
|
||||||
|
run = 1;
|
||||||
|
redraw = 1;
|
||||||
|
show_help = 1;
|
||||||
|
show_info = 1;
|
||||||
|
int paused = 0;
|
||||||
|
int old_paused = 0;
|
||||||
|
int just_toggled_fullscreen = 0;
|
||||||
|
int zoom_pressed = 0;
|
||||||
|
|
||||||
|
while(run) {
|
||||||
|
handle_event();
|
||||||
|
|
||||||
|
// only redraw or run simulation if the timer event has occurred
|
||||||
|
if(redraw) {
|
||||||
|
if(just_toggled_fullscreen) {
|
||||||
|
display_width = al_get_display_width(allegro.display);
|
||||||
|
display_height = al_get_display_height(allegro.display);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(paused == old_paused && key_is_down(KEY_PAUSE))
|
||||||
|
paused = !paused;
|
||||||
|
else if(paused != old_paused && !key_is_down(KEY_PAUSE))
|
||||||
|
old_paused = paused;
|
||||||
|
|
||||||
|
if(!paused) {
|
||||||
|
if(key_is_down(KEY_RESET))
|
||||||
|
ship.reset((float)WINDOW_WIDTH / 2, (float)WINDOW_HEIGHT / 2);
|
||||||
|
else {
|
||||||
|
ship.update(planet.getGravity(ship));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key_is_down(KEY_FULLSCREEN) && !just_toggled_fullscreen) {
|
||||||
|
if(al_get_display_flags(allegro.display) & ALLEGRO_FULLSCREEN_WINDOW)
|
||||||
|
al_set_display_flag(allegro.display, ALLEGRO_FULLSCREEN_WINDOW, 0);
|
||||||
|
else
|
||||||
|
al_set_display_flag(allegro.display, ALLEGRO_FULLSCREEN_WINDOW, 1);
|
||||||
|
|
||||||
|
just_toggled_fullscreen = 1;
|
||||||
|
} else if (!key_is_down(KEY_FULLSCREEN) && just_toggled_fullscreen) {
|
||||||
|
just_toggled_fullscreen = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(key_is_down(KEY_SHIFT) && key_is_down(KEY_ZOOM) && !zoom_pressed) {
|
||||||
|
zoom -= ZOOM_STEP;
|
||||||
|
if(zoom < ZOOM_MIN)
|
||||||
|
zoom = ZOOM_MIN;
|
||||||
|
zoom_pressed = 1;
|
||||||
|
} else if(key_is_down(KEY_ZOOM) && !zoom_pressed) {
|
||||||
|
zoom += ZOOM_STEP;
|
||||||
|
if(zoom > ZOOM_MAX)
|
||||||
|
zoom = ZOOM_MAX;
|
||||||
|
zoom_pressed = 1;
|
||||||
|
} else if(!key_is_down(KEY_ZOOM) && zoom_pressed) {
|
||||||
|
zoom_pressed = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
al_clear_to_color(al_map_rgb(0, 0, 0));
|
||||||
|
|
||||||
|
ALLEGRO_TRANSFORM transform;
|
||||||
|
al_identity_transform(&transform);
|
||||||
|
al_scale_transform(&transform, zoom, zoom);
|
||||||
|
al_translate_transform(&transform,
|
||||||
|
display_width / 2.0f - ship.getPos().x * zoom,
|
||||||
|
display_height / 2.0f - ship.getPos().y * zoom);
|
||||||
|
al_use_transform(&transform);
|
||||||
|
|
||||||
|
starfield_draw(ship.getPos().x, ship.getPos().y, zoom,
|
||||||
|
display_width, display_height);
|
||||||
|
planet.draw();
|
||||||
|
ship.draw();
|
||||||
|
|
||||||
|
al_identity_transform(&transform);
|
||||||
|
al_use_transform(&transform);
|
||||||
|
|
||||||
|
if(show_info) {
|
||||||
|
char info[256];
|
||||||
|
sprintf(info, info_format,
|
||||||
|
ship.getPos().x, ship.getPos().y,
|
||||||
|
ship.getDirection(),
|
||||||
|
RAD_TO_DEG(ship.getDirection()),
|
||||||
|
ship.getVel().x * FPS, ship.getVel().y * FPS);
|
||||||
|
al_draw_multiline_text(allegro.font,
|
||||||
|
al_map_rgb(0xFF, 0xFF, 0xFF),
|
||||||
|
5, 5, (int)display_width, 10.0f,
|
||||||
|
ALLEGRO_ALIGN_LEFT,
|
||||||
|
info);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(show_help) {
|
||||||
|
al_draw_multiline_text(allegro.font,
|
||||||
|
al_map_rgb(0xFF, 0xFF, 0xFF),
|
||||||
|
display_width / 2, display_height / 2 - 50,
|
||||||
|
(int)display_width, 10.0f,
|
||||||
|
ALLEGRO_ALIGN_CENTRE,
|
||||||
|
help);
|
||||||
|
}
|
||||||
|
al_flip_display();
|
||||||
|
redraw = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "planet.h"
|
||||||
|
#include <cmath>
|
||||||
|
#include <cassert>
|
||||||
|
#include <allegro5/allegro_primitives.h>
|
||||||
|
|
||||||
|
#define GRAVITY_CONSTANT 1.0f // Gravitational constant scaled for simulation
|
||||||
|
#define SHIP_MASS 1.0f // Assumed constant ship mass
|
||||||
|
#define MASS_TO_RADIUS 1.5f // Radius scale factor: radius = MASS_TO_RADIUS * mass^(1/3)
|
||||||
|
|
||||||
|
Planet::Planet(const float x, const float y, const float mass) :
|
||||||
|
pos(x, y), mass(mass), radius(MASS_TO_RADIUS * cbrtf(mass))
|
||||||
|
{}
|
||||||
|
|
||||||
|
Vec<float> Planet::getGravity(const Ship &ship) const {
|
||||||
|
const Vec<float> distance = this->pos - ship.getPos();
|
||||||
|
|
||||||
|
if(distance.length() < this->radius)
|
||||||
|
return Vec<float>(0, 0);
|
||||||
|
|
||||||
|
// F = G * m1 * m2 / r^2
|
||||||
|
const float force = GRAVITY_CONSTANT * SHIP_MASS * this->mass / (distance.length() * distance.length());
|
||||||
|
|
||||||
|
// a = F / m_ship = G * m_this / r^2
|
||||||
|
const float acceleration = force / SHIP_MASS;
|
||||||
|
|
||||||
|
// Normalize direction and apply acceleration
|
||||||
|
return distance.normalized() * acceleration;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Planet::draw() {
|
||||||
|
// Color intensity based on mass (brighter = more massive)
|
||||||
|
float color_scale = fminf(1.0f, this->mass / 1000.0f);
|
||||||
|
int r = (int)(100 + 155 * color_scale);
|
||||||
|
int g = (int)(100 + 50 * color_scale);
|
||||||
|
int b = (int)(150 - 100 * color_scale);
|
||||||
|
|
||||||
|
al_draw_filled_circle(this->pos.x, this->pos.y, this->radius,
|
||||||
|
al_map_rgb(r, g, b));
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ship.h"
|
||||||
|
#include "vec.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function pointer type for calculating planet mass.
|
||||||
|
* Takes no parameters and returns the mass value.
|
||||||
|
*/
|
||||||
|
typedef float (*planet_mass_func)(void);
|
||||||
|
|
||||||
|
class Planet {
|
||||||
|
private:
|
||||||
|
Vec<float> pos; ///< The x and y coordinates of the planet center.
|
||||||
|
const float mass; ///< The mass of the planet.
|
||||||
|
const float radius; ///< The radius of the planet (derived from mass).
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Initialize a planet at a position with a specified mass.
|
||||||
|
*
|
||||||
|
* @param x Initial x position of the planet center.
|
||||||
|
* @param y Initial y position of the planet center.
|
||||||
|
* @param mass The mass of the planet.
|
||||||
|
*/
|
||||||
|
Planet(const float x, const float y, const float mass);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Calculate the gravitational acceleration on a ship at a given position.
|
||||||
|
*
|
||||||
|
* Returns the acceleration vector components due to the planet's gravity.
|
||||||
|
*
|
||||||
|
* @param ship Reference to ship.
|
||||||
|
*
|
||||||
|
* @returns A Vec<float> representing the gravitational acceleration (ax, ay).
|
||||||
|
*/
|
||||||
|
Vec<float> getGravity(const Ship &ship) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Draw the planet.
|
||||||
|
*/
|
||||||
|
void draw();
|
||||||
|
};
|
||||||
-84
@@ -1,84 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
|
||||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "ship.h"
|
|
||||||
#include "event_manager.h"
|
|
||||||
#include "globals.h"
|
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <allegro5/allegro_primitives.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#define SHIP_RADIUS 10.0f // radius of the ship in pixels
|
|
||||||
|
|
||||||
void ship_init(struct ship *ship, int x, int y) {
|
|
||||||
assert(ship);
|
|
||||||
ship->x = x;
|
|
||||||
ship->y = y;
|
|
||||||
ship->velX = ship->velY = 0;
|
|
||||||
ship->direction = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ship_update(struct ship *ship) {
|
|
||||||
assert(ship);
|
|
||||||
|
|
||||||
if(key_is_down(KEY_RIGHT))
|
|
||||||
ship->direction += TURN_ACCEL;
|
|
||||||
if(key_is_down(KEY_LEFT))
|
|
||||||
ship->direction -= TURN_ACCEL;
|
|
||||||
|
|
||||||
// keep direction within bounds
|
|
||||||
if(ship->direction >= M_PI * 2)
|
|
||||||
ship->direction -= M_PI * 2;
|
|
||||||
else if(ship->direction < 0)
|
|
||||||
ship->direction += M_PI * 2;
|
|
||||||
|
|
||||||
if(key_is_down(KEY_UP))
|
|
||||||
{
|
|
||||||
ship->velX += cos(ship->direction) * ACCEL;
|
|
||||||
ship->velY += sin(ship->direction) * ACCEL;
|
|
||||||
}
|
|
||||||
if(key_is_down(KEY_DOWN))
|
|
||||||
{
|
|
||||||
// moving backwards is slower than moving forward
|
|
||||||
ship->velX -= cos(ship->direction) * (ACCEL / 2);
|
|
||||||
ship->velY -= sin(ship->direction) * (ACCEL / 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
ship->x += ship->velX;
|
|
||||||
ship->y += ship->velY;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ship_draw(struct ship *ship) {
|
|
||||||
assert(ship);
|
|
||||||
const float x0 = ship->x + (cos(ship->direction) *
|
|
||||||
SHIP_RADIUS);
|
|
||||||
const float y0 = ship->y + (sin(ship->direction) *
|
|
||||||
SHIP_RADIUS);
|
|
||||||
const float x1 = ship->x + (cos(ship->direction +
|
|
||||||
M_PI * 0.8f) * SHIP_RADIUS);
|
|
||||||
const float y1 = ship->y + (sin(ship->direction +
|
|
||||||
M_PI * 0.8f) * SHIP_RADIUS);
|
|
||||||
const float x2 = ship->x + (cos(ship->direction +
|
|
||||||
M_PI * 1.2f) * SHIP_RADIUS);
|
|
||||||
const float y2 = ship->y + (sin(ship->direction +
|
|
||||||
M_PI * 1.2f) * SHIP_RADIUS);
|
|
||||||
al_draw_filled_triangle(x0, y0, x1, y1, x2, y2,
|
|
||||||
al_map_rgb(0xFF, 0x0, 0x0));
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018,2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "ship.h"
|
||||||
|
#include "event_manager.h"
|
||||||
|
#include "globals.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <cassert>
|
||||||
|
#include <allegro5/allegro_primitives.h>
|
||||||
|
|
||||||
|
#define SHIP_RADIUS 10.0f // radius of the ship in pixels
|
||||||
|
|
||||||
|
Ship::Ship(const float x, const float y) :
|
||||||
|
pos(x, y), vel(0, 0), direction(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void Ship::update(const Vec<float> &grav) {
|
||||||
|
if(key_is_down(KEY_RIGHT))
|
||||||
|
this->direction += TURN_ACCEL;
|
||||||
|
if(key_is_down(KEY_LEFT))
|
||||||
|
this->direction -= TURN_ACCEL;
|
||||||
|
|
||||||
|
// keep direction within bounds
|
||||||
|
if(this->direction >= M_PI * 2)
|
||||||
|
this->direction -= M_PI * 2;
|
||||||
|
else if(this->direction < 0)
|
||||||
|
this->direction += M_PI * 2;
|
||||||
|
|
||||||
|
if(key_is_down(KEY_UP)) {
|
||||||
|
this->vel.x += cos(this->direction) * ACCEL;
|
||||||
|
this->vel.y += sin(this->direction) * ACCEL;
|
||||||
|
}
|
||||||
|
if(key_is_down(KEY_DOWN)) {
|
||||||
|
// moving backwards is slower than moving forward
|
||||||
|
this->vel.x -= cos(this->direction) * (ACCEL / 2);
|
||||||
|
this->vel.y -= sin(this->direction) * (ACCEL / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply gravitational acceleration
|
||||||
|
this->vel += grav;
|
||||||
|
this->pos += this->vel;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ship::draw() const {
|
||||||
|
const float x0 = this->pos.x + (cos(this->direction) * SHIP_RADIUS);
|
||||||
|
const float y0 = this->pos.y + (sin(this->direction) * SHIP_RADIUS);
|
||||||
|
const float x1 = this->pos.x + (cos(this->direction + M_PI * 0.8f) * SHIP_RADIUS);
|
||||||
|
const float y1 = this->pos.y + (sin(this->direction + M_PI * 0.8f) * SHIP_RADIUS);
|
||||||
|
const float x2 = this->pos.x + (cos(this->direction + M_PI * 1.2f) * SHIP_RADIUS);
|
||||||
|
const float y2 = this->pos.y + (sin(this->direction + M_PI * 1.2f) * SHIP_RADIUS);
|
||||||
|
al_draw_filled_triangle(x0, y0, x1, y1, x2, y2,
|
||||||
|
al_map_rgb(0xFF, 0x0, 0x0));
|
||||||
|
}
|
||||||
+47
-29
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
* Copyright (C) 2018,2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -18,38 +18,56 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "vec.h"
|
||||||
#include <allegro5/allegro.h>
|
#include <allegro5/allegro.h>
|
||||||
|
|
||||||
struct ship {
|
class Ship {
|
||||||
float x, y; ///< The x and y coordinates of the ship.
|
private:
|
||||||
float velX, velY; ///< The x and y velocities of the ship.
|
/// Position of the ship in pixels.
|
||||||
|
Vec<float> pos;
|
||||||
|
/// Velocity of the ship in pixels per frame.
|
||||||
|
Vec<float> vel;
|
||||||
/**
|
/**
|
||||||
* The direction that the ship is facing in radians, where
|
* The direction that the ship is facing in radians, where
|
||||||
* 0 is right facing.
|
* 0 is right facing.
|
||||||
*/
|
*/
|
||||||
float direction;
|
float direction;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Initialize ship.
|
||||||
|
*
|
||||||
|
* @param x Initial X position.
|
||||||
|
* @param y Initial Y position.
|
||||||
|
*/
|
||||||
|
Ship(const float x, const float y);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Updates the ship's variables according to keyboard
|
||||||
|
* input and gravitational forces.
|
||||||
|
*
|
||||||
|
* @param grav Vector of gravitational acceleration in pixels per frame squared.
|
||||||
|
*/
|
||||||
|
void update(const Vec<float> &grav);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reset ship to position.
|
||||||
|
*
|
||||||
|
* @param x X position to reset to.
|
||||||
|
* @param y Y position to reset to.
|
||||||
|
*/
|
||||||
|
inline void reset(float x, float y) {
|
||||||
|
this->pos.set(x, y);
|
||||||
|
this->vel.set(0, 0);
|
||||||
|
this->direction = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Draw the ship.
|
||||||
|
*/
|
||||||
|
void draw() const;
|
||||||
|
|
||||||
|
inline Vec<float> getPos() const { return pos; }
|
||||||
|
inline Vec<float> getVel() const { return vel; }
|
||||||
|
inline float getDirection() const { return direction; }
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Initialize the ship at a position.
|
|
||||||
*
|
|
||||||
* @param ship A pointer to the ship object.
|
|
||||||
* @param x Initial x position of the ship.
|
|
||||||
* @param y Initial y position of the ship.
|
|
||||||
*/
|
|
||||||
void ship_init(struct ship *ship, int x, int y);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Updates the ship's variables according to keyboard
|
|
||||||
* input.
|
|
||||||
*
|
|
||||||
* @param ship A pointer to the ship object.
|
|
||||||
*/
|
|
||||||
void ship_update(struct ship *ship);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Draw the ship.
|
|
||||||
*
|
|
||||||
* @param ship Ship object to draw.
|
|
||||||
*/
|
|
||||||
void ship_draw(struct ship *ship);
|
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "starfield.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <allegro5/allegro.h>
|
||||||
|
#include <allegro5/allegro_primitives.h>
|
||||||
|
|
||||||
|
#define CHUNK_SIZE 256
|
||||||
|
#define STARS_PER_CHUNK 50
|
||||||
|
#define STAR_COLOR al_map_rgb(255, 255, 255)
|
||||||
|
|
||||||
|
static unsigned int hash_seed(int chunk_x, int chunk_y) {
|
||||||
|
unsigned int h = 5381;
|
||||||
|
h = ((h << 5) + h) ^ chunk_x;
|
||||||
|
h = ((h << 5) + h) ^ chunk_y;
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void generate_chunk_stars(int chunk_x, int chunk_y) {
|
||||||
|
unsigned int seed = hash_seed(chunk_x, chunk_y);
|
||||||
|
srand(seed);
|
||||||
|
|
||||||
|
float base_x = chunk_x * CHUNK_SIZE;
|
||||||
|
float base_y = chunk_y * CHUNK_SIZE;
|
||||||
|
|
||||||
|
for(int i = 0; i < STARS_PER_CHUNK; ++i) {
|
||||||
|
float star_x = base_x + (float)(rand() % CHUNK_SIZE);
|
||||||
|
float star_y = base_y + (float)(rand() % CHUNK_SIZE);
|
||||||
|
|
||||||
|
al_draw_pixel(star_x, star_y, STAR_COLOR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void starfield_draw(float camera_x, float camera_y, float zoom, float width, float height) {
|
||||||
|
float view_width = width / zoom;
|
||||||
|
float view_height = height / zoom;
|
||||||
|
|
||||||
|
float left = camera_x - view_width / 2.0f;
|
||||||
|
float right = camera_x + view_width / 2.0f;
|
||||||
|
float top = camera_y - view_height / 2.0f;
|
||||||
|
float bottom = camera_y + view_height / 2.0f;
|
||||||
|
|
||||||
|
int chunk_left = (int)floor(left / CHUNK_SIZE);
|
||||||
|
int chunk_right = (int)floor(right / CHUNK_SIZE);
|
||||||
|
int chunk_top = (int)floor(top / CHUNK_SIZE);
|
||||||
|
int chunk_bottom = (int)floor(bottom / CHUNK_SIZE);
|
||||||
|
|
||||||
|
for(int cy = chunk_top; cy <= chunk_bottom; ++cy) {
|
||||||
|
for(int cx = chunk_left; cx <= chunk_right; ++cx) {
|
||||||
|
generate_chunk_stars(cx, cy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Draw stars in the visible area of the starfield.
|
||||||
|
*
|
||||||
|
* @param camera_x The x position of the camera center.
|
||||||
|
* @param camera_y The y position of the camera center.
|
||||||
|
* @param zoom The zoom level (scale factor).
|
||||||
|
* @param width The display width in pixels.
|
||||||
|
* @param height The display height in pixels.
|
||||||
|
*/
|
||||||
|
void starfield_draw(float camera_x, float camera_y, float zoom, float width, float height);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2026 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class Vec {
|
||||||
|
public:
|
||||||
|
T x, y;
|
||||||
|
|
||||||
|
Vec() : x(0), y(0) {}
|
||||||
|
Vec(T x, T y) : x(x), y(y) {}
|
||||||
|
|
||||||
|
inline void set(T x, T y) {
|
||||||
|
this->x = x;
|
||||||
|
this->y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline T length() const {
|
||||||
|
return std::sqrt(static_cast<double>(x * x + y * y));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Vec<T> normalized() const {
|
||||||
|
const T len = length();
|
||||||
|
|
||||||
|
if (len == 0)
|
||||||
|
return Vec<T>(0, 0);
|
||||||
|
|
||||||
|
return Vec<T>(x / len, y / len);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Vec<T>& operator+=(const Vec<T> &other) {
|
||||||
|
this->x += other.x;
|
||||||
|
this->y += other.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
inline Vec<T>& operator*=(const T scalar) {
|
||||||
|
this->x *= scalar;
|
||||||
|
this->y *= scalar;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Vec<T> operator+(const Vec<T> &other) const {
|
||||||
|
return Vec<T>(this->x + other.x, this->y + other.y);
|
||||||
|
}
|
||||||
|
inline Vec<T> operator+(const T scalar) const {
|
||||||
|
return Vec<T>(this->x + scalar, this->y + scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Vec<T>& operator-=(const Vec<T> &other) {
|
||||||
|
this->x -= other.x;
|
||||||
|
this->y -= other.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
inline Vec<T>& operator-=(const T scalar) {
|
||||||
|
this->x -= scalar;
|
||||||
|
this->y -= scalar;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Vec<T> operator-(const Vec<T> &other) const {
|
||||||
|
return Vec<T>(this->x - other.x, this->y - other.y);
|
||||||
|
}
|
||||||
|
inline Vec<T> operator-(const T scalar) const {
|
||||||
|
return Vec<T>(this->x - scalar, this->y - scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Vec<T> operator*(const T scalar) const {
|
||||||
|
return Vec<T>(this->x * scalar, this->y * scalar);
|
||||||
|
}
|
||||||
|
inline Vec<T> operator/(const T scalar) const {
|
||||||
|
return Vec<T>(this->x / scalar, this->y / scalar);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user