Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 |
10
CHANGELOG
10
CHANGELOG
@@ -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.
|
|
||||||
27
CHANGELOG.md
Normal file
27
CHANGELOG.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Change Log
|
||||||
|
|
||||||
|
## 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.
|
||||||
@@ -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,11 @@
|
|||||||
# 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 C)
|
||||||
|
|
||||||
set(TARGET_NAME "spaceshipsim")
|
set(TARGET_NAME "spaceshipsim")
|
||||||
set(TARGET_VERSION "0.3")
|
set(TARGET_VERSION "0.5")
|
||||||
|
|
||||||
if(NOT CMAKE_BUILD_TYPE)
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
set(CMAKE_BUILD_TYPE "release")
|
set(CMAKE_BUILD_TYPE "release")
|
||||||
@@ -34,7 +34,7 @@ set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3 -ffast-math")
|
|||||||
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
|
set(CMAKE_C_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)
|
||||||
|
|
||||||
@@ -44,12 +44,12 @@ include_directories(
|
|||||||
set(SRCS
|
set(SRCS
|
||||||
src/event_manager.c
|
src/event_manager.c
|
||||||
src/main.c
|
src/main.c
|
||||||
src/ship.c)
|
src/ship.c
|
||||||
|
src/starfield.c)
|
||||||
|
|
||||||
add_definitions(-DVERSION="${TARGET_VERSION}")
|
add_definitions(-DVERSION="${TARGET_VERSION}")
|
||||||
|
|
||||||
if(${CMAKE_BUILD_TYPE} STREQUAL "debug" OR
|
if(${CMAKE_BUILD_TYPE} STREQUAL "debug" OR ${CMAKE_BUILD_TYPE} STREQUAL "relwithdebinfo")
|
||||||
${CMAKE_BUILD_TYPE} STREQUAL "relwithdebinfo")
|
|
||||||
add_definitions(-DDEBUG)
|
add_definitions(-DDEBUG)
|
||||||
else()
|
else()
|
||||||
add_definitions(-DNDEBUG)
|
add_definitions(-DNDEBUG)
|
||||||
|
|||||||
@@ -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/)
|
- A C compiler that supports C99 or higher
|
||||||
- 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:
|
||||||
|
|
||||||
|
```bash
|
||||||
cd build/
|
cd build/
|
||||||
cmake ..
|
cmake ..
|
||||||
make
|
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).
|
||||||
2
TODO
Normal file
2
TODO
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
TODO:
|
||||||
|
- Add presets with stellar masses (for gravity simulation).
|
||||||
@@ -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
|
||||||
@@ -101,6 +101,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,6 +120,8 @@ void handle_event() {
|
|||||||
ALLEGRO_EVENT evnt;
|
ALLEGRO_EVENT evnt;
|
||||||
al_wait_for_event(event_queue, &evnt);
|
al_wait_for_event(event_queue, &evnt);
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
switch(evnt.type)
|
switch(evnt.type)
|
||||||
{
|
{
|
||||||
case ALLEGRO_EVENT_TIMER:
|
case ALLEGRO_EVENT_TIMER:
|
||||||
@@ -133,6 +145,7 @@ void handle_event() {
|
|||||||
set_key(evnt.keyboard.keycode, 0);
|
set_key(evnt.keyboard.keycode, 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} while(al_get_next_event(event_queue, &evnt));
|
||||||
}
|
}
|
||||||
|
|
||||||
int key_is_down(int code) {
|
int key_is_down(int code) {
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -27,7 +27,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
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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,7 +26,7 @@
|
|||||||
# define FPS 60.0f
|
# define FPS 60.0f
|
||||||
#endif
|
#endif
|
||||||
#ifndef ACCEL
|
#ifndef ACCEL
|
||||||
# define ACCEL 0.5f
|
# define ACCEL 0.1f
|
||||||
#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,6 +39,16 @@
|
|||||||
# define WINDOW_HEIGHT 600
|
# define WINDOW_HEIGHT 600
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef ZOOM_MIN
|
||||||
|
# define ZOOM_MIN 0.25f
|
||||||
|
#endif
|
||||||
|
#ifndef ZOOM_MAX
|
||||||
|
# define ZOOM_MAX 4.0f
|
||||||
|
#endif
|
||||||
|
#ifndef ZOOM_STEP
|
||||||
|
# define ZOOM_STEP 0.1f
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#ifndef M_PI
|
#ifndef M_PI
|
||||||
# define M_PI 3.14159265f
|
# define M_PI 3.14159265f
|
||||||
|
|||||||
107
src/main.c
107
src/main.c
@@ -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
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "event_manager.h"
|
#include "event_manager.h"
|
||||||
#include "ship.h"
|
#include "ship.h"
|
||||||
|
#include "starfield.h"
|
||||||
|
|
||||||
int run;
|
int run;
|
||||||
int redraw;
|
int redraw;
|
||||||
@@ -33,10 +34,14 @@ int show_info;
|
|||||||
const char *help =
|
const char *help =
|
||||||
"HELP:\n"
|
"HELP:\n"
|
||||||
"LEFT/RIGHT - turn the ship\n"
|
"LEFT/RIGHT - turn the ship\n"
|
||||||
"UP/DOWN - accelerate/decelerate\n"
|
"UP/DOWN - accelerate/reverse accelerate\n"
|
||||||
"R - reset the simulation\n"
|
"R - reset the simulation\n"
|
||||||
|
"P - pause/unpause the simulation\n"
|
||||||
"I - show/hide simulation information\n"
|
"I - show/hide simulation information\n"
|
||||||
"H - show/hide this help 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";
|
"Q/ESC - quit";
|
||||||
|
|
||||||
const char *info_format =
|
const char *info_format =
|
||||||
@@ -44,11 +49,17 @@ const char *info_format =
|
|||||||
"x: %f\n"
|
"x: %f\n"
|
||||||
"y: %f\n"
|
"y: %f\n"
|
||||||
"angle: %f (%f degrees)\n"
|
"angle: %f (%f degrees)\n"
|
||||||
"velX: %f\n"
|
"velX: %f px/sec\n"
|
||||||
"velY: %f";
|
"velY: %f px/sec";
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
printf("SpaceShipSim v%s\n", VERSION);
|
char title[32];
|
||||||
|
float zoom = 1.0f;
|
||||||
|
float display_width = WINDOW_WIDTH;
|
||||||
|
float display_height = WINDOW_HEIGHT;
|
||||||
|
|
||||||
|
sprintf(title, "SpaceShipSim v%s", VERSION);
|
||||||
|
puts(title);
|
||||||
|
|
||||||
if(!al_init())
|
if(!al_init())
|
||||||
{
|
{
|
||||||
@@ -79,6 +90,7 @@ int main() {
|
|||||||
fprintf(stderr, "alleg5: failed to initialize display.\n");
|
fprintf(stderr, "alleg5: failed to initialize display.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
al_set_window_title(display, title);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
puts("Created display.");
|
puts("Created display.");
|
||||||
#endif
|
#endif
|
||||||
@@ -101,23 +113,88 @@ int main() {
|
|||||||
redraw = 1;
|
redraw = 1;
|
||||||
show_help = 1;
|
show_help = 1;
|
||||||
show_info = 1;
|
show_info = 1;
|
||||||
|
int paused = 0;
|
||||||
|
int old_paused = 0;
|
||||||
|
int just_toggled_fullscreen = 0;
|
||||||
|
int zoom_pressed = 0;
|
||||||
while(run)
|
while(run)
|
||||||
{
|
{
|
||||||
handle_event();
|
handle_event();
|
||||||
|
|
||||||
// only redraw or run simulation if the timer event has occurred
|
// only redraw or run simulation if the timer event has occurred
|
||||||
if(redraw)
|
if(redraw)
|
||||||
|
{
|
||||||
|
if(just_toggled_fullscreen)
|
||||||
|
{
|
||||||
|
display_width = al_get_display_width(display);
|
||||||
|
display_height = al_get_display_height(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))
|
if(key_is_down(KEY_RESET))
|
||||||
ship_init(&ship, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
|
ship_init(&ship, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
|
||||||
/*
|
else
|
||||||
* 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);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
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.x * zoom,
|
||||||
|
display_height / 2.0f - ship.y * zoom);
|
||||||
|
al_use_transform(&transform);
|
||||||
|
|
||||||
|
starfield_draw(ship.x, ship.y, zoom, display_width, display_height);
|
||||||
ship_draw(&ship);
|
ship_draw(&ship);
|
||||||
|
|
||||||
|
al_identity_transform(&transform);
|
||||||
|
al_use_transform(&transform);
|
||||||
|
|
||||||
if(show_info)
|
if(show_info)
|
||||||
{
|
{
|
||||||
char info[256];
|
char info[256];
|
||||||
@@ -125,10 +202,10 @@ int main() {
|
|||||||
ship.x, ship.y,
|
ship.x, ship.y,
|
||||||
ship.direction,
|
ship.direction,
|
||||||
RAD_TO_DEG(ship.direction),
|
RAD_TO_DEG(ship.direction),
|
||||||
ship.velX, ship.velY);
|
ship.velX * FPS, ship.velY * FPS);
|
||||||
al_draw_multiline_text(font,
|
al_draw_multiline_text(font,
|
||||||
al_map_rgb(0xFF, 0xFF, 0xFF),
|
al_map_rgb(0xFF, 0xFF, 0xFF),
|
||||||
5, 5, WINDOW_WIDTH, 10.0f,
|
5, 5, (int)display_width, 10.0f,
|
||||||
ALLEGRO_ALIGN_LEFT,
|
ALLEGRO_ALIGN_LEFT,
|
||||||
info);
|
info);
|
||||||
}
|
}
|
||||||
@@ -137,8 +214,8 @@ int main() {
|
|||||||
{
|
{
|
||||||
al_draw_multiline_text(font,
|
al_draw_multiline_text(font,
|
||||||
al_map_rgb(0xFF, 0xFF, 0xFF),
|
al_map_rgb(0xFF, 0xFF, 0xFF),
|
||||||
WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2,
|
display_width / 2, display_height / 2 - 50,
|
||||||
WINDOW_WIDTH, 10.0f,
|
(int)display_width, 10.0f,
|
||||||
ALLEGRO_ALIGN_CENTRE,
|
ALLEGRO_ALIGN_CENTRE,
|
||||||
help);
|
help);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -23,7 +23,6 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <allegro5/allegro_primitives.h>
|
#include <allegro5/allegro_primitives.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#define SHIP_RADIUS 10.0f // radius of the ship in pixels
|
#define SHIP_RADIUS 10.0f // radius of the ship in pixels
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
75
src/starfield.c
Normal file
75
src/starfield.c
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* 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 "globals.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/starfield.h
Normal file
30
src/starfield.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
Reference in New Issue
Block a user