Add a starfield in the background.
This should help to better visualize the velocity of the ship.
This commit is contained in:
@@ -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}")
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "globals.h"
|
||||
#include "event_manager.h"
|
||||
#include "ship.h"
|
||||
#include "starfield.h"
|
||||
|
||||
int run;
|
||||
int redraw;
|
||||
@@ -179,6 +180,7 @@ int main() {
|
||||
WINDOW_HEIGHT / 2.0f - ship.y * zoom);
|
||||
al_use_transform(&transform);
|
||||
|
||||
starfield_draw(ship.x, ship.y, zoom);
|
||||
ship_draw(&ship);
|
||||
|
||||
al_identity_transform(&transform);
|
||||
|
||||
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 view_width = WINDOW_WIDTH / zoom;
|
||||
float view_height = WINDOW_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);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
src/starfield.h
Normal file
28
src/starfield.h
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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).
|
||||
*/
|
||||
void starfield_draw(float camera_x, float camera_y, float zoom);
|
||||
Reference in New Issue
Block a user