Compare commits
23 Commits
v0.3
...
4e7e37821e
| Author | SHA1 | Date | |
|---|---|---|---|
| 4e7e37821e | |||
| f520790f9c | |||
| 44531ab24e | |||
| c1139ddb81 | |||
| da8d83de92 | |||
| bc6881f851 | |||
| b842bd0095 | |||
| bb28285794 | |||
| 6621decc09 | |||
| fd9fd453b3 | |||
| b0f4f6849d | |||
| 8924741937 | |||
| 63f950e26b | |||
| 349848ebf6 | |||
| 395c3c854f | |||
| a93e9a7b73 | |||
| 29219bbc2c | |||
| d343c5e5a0 | |||
| 17777e2178 | |||
| 609d197227 | |||
| e29a0bc7a3 | |||
| 92e406bc74 | |||
| 2d64adf476 |
@@ -1,5 +1,5 @@
|
||||
# Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
# Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
|
||||
# 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
|
||||
@@ -14,7 +14,7 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# 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)
|
||||
|
||||
set(TARGET_NAME "spaceshipsim")
|
||||
@@ -34,7 +34,7 @@ set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3 -ffast-math")
|
||||
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
|
||||
|
||||
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-FONT REQUIRED allegro_font-5)
|
||||
|
||||
|
||||
@@ -1,39 +1,40 @@
|
||||
====================
|
||||
*** SpaceShipSim ***
|
||||
====================
|
||||
# SpaceShipSim
|
||||
|
||||
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
|
||||
purposes.
|
||||
|
||||
# Compiling
|
||||
-----------
|
||||
## Compiling
|
||||
|
||||
In order to build this program you will require the following dependencies:
|
||||
|
||||
- GNU GCC (https://gcc.gnu.org/)
|
||||
- CMake (https://cmake.org/)
|
||||
- Allegro 5 (http://liballeg.org/)
|
||||
- A C compiler that supports C99 or higher
|
||||
- CMake 3.10 or higher
|
||||
- Allegro 5
|
||||
|
||||
You can then compile the program via the following commands:
|
||||
|
||||
```bash
|
||||
cd build/
|
||||
cmake ..
|
||||
make
|
||||
```
|
||||
|
||||
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'
|
||||
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
|
||||
you may want to set the installation prefix to `/usr/local/', and if you're
|
||||
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
|
||||
`-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
|
||||
packaging then please consult your distribution's policies. With this you
|
||||
should be able to run the `make install' target and install the binary.
|
||||
|
||||
# Contributing
|
||||
--------------
|
||||
If you would like to contribute to the project, send a patch file to my e-mail
|
||||
address: <nortega@themusicinnoise.net>.
|
||||
## Contributing
|
||||
|
||||
If you would like to contribute to the project, send a patch file to my e-mail
|
||||
address: [nicolas@ortegas.org](mailto:nicolas@ortegas.org).
|
||||
|
||||
## License
|
||||
|
||||
# License
|
||||
---------
|
||||
As educational software, unless otherwise noted, all files are licensed under
|
||||
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).
|
||||
2
TODO
Normal file
2
TODO
Normal file
@@ -0,0 +1,2 @@
|
||||
TODO:
|
||||
- Add presets with stellar masses (for gravity simulation).
|
||||
@@ -101,6 +101,9 @@ void set_key(int keycode, int value) {
|
||||
case ALLEGRO_KEY_F:
|
||||
keys[KEY_FULLSCREEN] = value;
|
||||
break;
|
||||
case ALLEGRO_KEY_P:
|
||||
keys[KEY_PAUSE] = value;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -110,6 +113,8 @@ void handle_event() {
|
||||
ALLEGRO_EVENT evnt;
|
||||
al_wait_for_event(event_queue, &evnt);
|
||||
|
||||
do
|
||||
{
|
||||
switch(evnt.type)
|
||||
{
|
||||
case ALLEGRO_EVENT_TIMER:
|
||||
@@ -133,6 +138,7 @@ void handle_event() {
|
||||
set_key(evnt.keyboard.keycode, 0);
|
||||
break;
|
||||
}
|
||||
} while(al_get_next_event(event_queue, &evnt));
|
||||
}
|
||||
|
||||
int key_is_down(int code) {
|
||||
|
||||
@@ -27,7 +27,8 @@ enum {
|
||||
KEY_RIGHT = 0x3,
|
||||
KEY_RESET = 0x4,
|
||||
KEY_FULLSCREEN = 0x5,
|
||||
KEY_MAX = 0x6
|
||||
KEY_PAUSE = 0x6,
|
||||
KEY_MAX = 0x7
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
# define FPS 60.0f
|
||||
#endif
|
||||
#ifndef ACCEL
|
||||
# define ACCEL 0.5f
|
||||
# define ACCEL 0.1f
|
||||
#endif
|
||||
#ifndef TURN_ACCEL
|
||||
# define TURN_ACCEL (M_PI / FPS) // turn at pi radians / sec
|
||||
|
||||
52
src/main.c
52
src/main.c
@@ -33,10 +33,12 @@ int show_info;
|
||||
const char *help =
|
||||
"HELP:\n"
|
||||
"LEFT/RIGHT - turn the ship\n"
|
||||
"UP/DOWN - accelerate/decelerate\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"
|
||||
"Q/ESC - quit";
|
||||
|
||||
const char *info_format =
|
||||
@@ -44,11 +46,13 @@ const char *info_format =
|
||||
"x: %f\n"
|
||||
"y: %f\n"
|
||||
"angle: %f (%f degrees)\n"
|
||||
"velX: %f\n"
|
||||
"velY: %f";
|
||||
"velX: %f px/sec\n"
|
||||
"velY: %f px/sec";
|
||||
|
||||
int main() {
|
||||
printf("SpaceShipSim v%s\n", VERSION);
|
||||
char title[32];
|
||||
sprintf(title, "SpaceShipSim v%s", VERSION);
|
||||
puts(title);
|
||||
|
||||
if(!al_init())
|
||||
{
|
||||
@@ -79,6 +83,7 @@ int main() {
|
||||
fprintf(stderr, "alleg5: failed to initialize display.\n");
|
||||
return 1;
|
||||
}
|
||||
al_set_window_title(display, title);
|
||||
#ifdef DEBUG
|
||||
puts("Created display.");
|
||||
#endif
|
||||
@@ -101,21 +106,46 @@ int main() {
|
||||
redraw = 1;
|
||||
show_help = 1;
|
||||
show_info = 1;
|
||||
int paused = 0;
|
||||
int old_paused = 0;
|
||||
int just_toggled_fullscreen = 0;
|
||||
while(run)
|
||||
{
|
||||
handle_event();
|
||||
|
||||
// only redraw or run simulation if the timer event has occurred
|
||||
if(redraw)
|
||||
{
|
||||
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_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.
|
||||
*/
|
||||
else
|
||||
ship_update(&ship);
|
||||
}
|
||||
|
||||
if (key_is_down(KEY_FULLSCREEN) && !just_toggled_fullscreen)
|
||||
{
|
||||
if(al_get_display_flags(display) & ALLEGRO_FULLSCREEN_WINDOW)
|
||||
{
|
||||
al_set_display_flag(display, ALLEGRO_FULLSCREEN_WINDOW, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
al_set_display_flag(display, ALLEGRO_FULLSCREEN_WINDOW, 1);
|
||||
}
|
||||
just_toggled_fullscreen = 1;
|
||||
}
|
||||
else if (!key_is_down(KEY_FULLSCREEN) && just_toggled_fullscreen)
|
||||
{
|
||||
just_toggled_fullscreen = 0;
|
||||
}
|
||||
|
||||
al_clear_to_color(al_map_rgb(0, 0, 0));
|
||||
ship_draw(&ship);
|
||||
if(show_info)
|
||||
@@ -125,7 +155,7 @@ int main() {
|
||||
ship.x, ship.y,
|
||||
ship.direction,
|
||||
RAD_TO_DEG(ship.direction),
|
||||
ship.velX, ship.velY);
|
||||
ship.velX * FPS, ship.velY * FPS);
|
||||
al_draw_multiline_text(font,
|
||||
al_map_rgb(0xFF, 0xFF, 0xFF),
|
||||
5, 5, WINDOW_WIDTH, 10.0f,
|
||||
@@ -137,7 +167,7 @@ int main() {
|
||||
{
|
||||
al_draw_multiline_text(font,
|
||||
al_map_rgb(0xFF, 0xFF, 0xFF),
|
||||
WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2,
|
||||
(float)WINDOW_WIDTH / 2, (float)WINDOW_HEIGHT / 2 - 50,
|
||||
WINDOW_WIDTH, 10.0f,
|
||||
ALLEGRO_ALIGN_CENTRE,
|
||||
help);
|
||||
|
||||
Reference in New Issue
Block a user