7 Commits
v0.4 ... v0.5

Author SHA1 Message Date
1f3674b266 Bump version to v0.5. 2026-03-16 21:55:33 +01:00
2e5e37b0a5 Fix display transformation and star generation for fullscreen mode. 2026-03-16 21:49:02 +01:00
7cc8524811 Remove unnecessary header include. 2026-03-16 21:33:55 +01:00
55402c0c66 Update copyright headers. 2026-03-16 21:33:30 +01:00
f1cdffba2b Add a starfield in the background.
This should help to better visualize the velocity of the ship.
2026-03-16 21:29:39 +01:00
0e5a4c89e5 Follow the ship. 2026-03-16 21:21:42 +01:00
bb47dcc9f9 Implement zoom. 2026-03-16 21:17:23 +01:00
10 changed files with 196 additions and 19 deletions

View File

@@ -1,5 +1,11 @@
# 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.

View File

@@ -18,7 +18,7 @@ cmake_minimum_required(VERSION 3.10)
project(SpaceShipSim C)
set(TARGET_NAME "spaceshipsim")
set(TARGET_VERSION "0.4")
set(TARGET_VERSION "0.5")
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "release")
@@ -44,7 +44,8 @@ include_directories(
set(SRCS
src/event_manager.c
src/main.c
src/ship.c)
src/ship.c
src/starfield.c)
add_definitions(-DVERSION="${TARGET_VERSION}")

View File

@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* 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
@@ -104,6 +104,13 @@ void set_key(int keycode, int value) {
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:
break;
}

View File

@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* 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
@@ -28,7 +28,9 @@ enum {
KEY_RESET = 0x4,
KEY_FULLSCREEN = 0x5,
KEY_PAUSE = 0x6,
KEY_MAX = 0x7
KEY_ZOOM = 0x7,
KEY_SHIFT = 0x8,
KEY_MAX = 0x9
};
/**

View File

@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* 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
@@ -39,6 +39,16 @@
# define WINDOW_HEIGHT 600
#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>
#ifndef M_PI
# define M_PI 3.14159265f

View File

@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* 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
@@ -19,6 +19,7 @@
#include "globals.h"
#include "event_manager.h"
#include "ship.h"
#include "starfield.h"
int run;
int redraw;
@@ -39,6 +40,8 @@ const char *help =
"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 =
@@ -51,6 +54,10 @@ const char *info_format =
int main() {
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);
@@ -109,6 +116,7 @@ int main() {
int paused = 0;
int old_paused = 0;
int just_toggled_fullscreen = 0;
int zoom_pressed = 0;
while(run)
{
handle_event();
@@ -116,6 +124,12 @@ int main() {
// only redraw or run simulation if the timer event has occurred
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))
@@ -146,8 +160,41 @@ int main() {
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.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);
al_identity_transform(&transform);
al_use_transform(&transform);
if(show_info)
{
char info[256];
@@ -158,7 +205,7 @@ int main() {
ship.velX * FPS, ship.velY * FPS);
al_draw_multiline_text(font,
al_map_rgb(0xFF, 0xFF, 0xFF),
5, 5, WINDOW_WIDTH, 10.0f,
5, 5, (int)display_width, 10.0f,
ALLEGRO_ALIGN_LEFT,
info);
}
@@ -167,8 +214,8 @@ int main() {
{
al_draw_multiline_text(font,
al_map_rgb(0xFF, 0xFF, 0xFF),
(float)WINDOW_WIDTH / 2, (float)WINDOW_HEIGHT / 2 - 50,
WINDOW_WIDTH, 10.0f,
display_width / 2, display_height / 2 - 50,
(int)display_width, 10.0f,
ALLEGRO_ALIGN_CENTRE,
help);
}

View File

@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* 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
@@ -23,7 +23,6 @@
#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

View File

@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* 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

75
src/starfield.c Normal file
View 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
View 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);