diff --git a/CMakeLists.txt b/CMakeLists.txt index 303bf3a..3677a16 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}") diff --git a/src/main.c b/src/main.c index db5311b..63e70d1 100644 --- a/src/main.c +++ b/src/main.c @@ -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); diff --git a/src/starfield.c b/src/starfield.c new file mode 100644 index 0000000..4d99269 --- /dev/null +++ b/src/starfield.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2026 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * 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 . + */ + +#include "starfield.h" +#include "globals.h" + +#include +#include +#include +#include + +#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); + } + } +} diff --git a/src/starfield.h b/src/starfield.h new file mode 100644 index 0000000..1ba6a2e --- /dev/null +++ b/src/starfield.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2026 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * 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 . + */ + +#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);