star-sim

Barnes-Hut gravity simulation.
git clone git://git.amin.space/star-sim.git
Log | Files | Refs | README | LICENSE

sim.h (2500B)


      1 #ifndef STAR_GARDEN_H
      2 
      3 #include "barnes_hut.h"
      4 #include "star.h"
      5 
      6 #include <math.h>
      7 #include <stdint.h>
      8 #include <stdlib.h>
      9 
     10 #define FULLSCREEN
     11 //#define RESPAWN_STARS
     12 #define USE_TEST_SEED
     13 
     14 #define TITLE "Stars"
     15 #define SCREEN_WIDTH 640
     16 #define SCREEN_HEIGHT 480
     17 #define BYTES_PER_PIXEL 4
     18 #define NUM_STARS 100
     19 #define DRAG 1
     20 
     21 #define SECOND 1000.0f
     22 #define FPS 60
     23 #define MS_PER_FRAME (SECOND / FPS)
     24 #define UPDATES_PER_SECOND 120
     25 #define MS_PER_UPDATE (SECOND / UPDATES_PER_SECOND)
     26 
     27 #ifndef M_PI
     28 #define M_PI (3.14159265358979323846264338327950288)
     29 #endif
     30 
     31 #define COLOR_WHITE      0xFFFFFF
     32 #define COLOR_BLACK      0x000000
     33 #define COLOR_SOL_BG     0x002B36
     34 #define COLOR_YELLOW     0xB58900
     35 #define COLOR_ORANGE     0xCB4B16
     36 #define COLOR_RED        0xDC322F
     37 #define COLOR_MAGENTA    0xD33682
     38 #define COLOR_VIOLET     0x6C71C4
     39 #define COLOR_BLUE       0x268bD2
     40 #define COLOR_CYAN       0x2AA198
     41 #define COLOR_GREEN      0x859900
     42 #define COLOR_BACKGROUND COLOR_BLACK
     43 
     44 typedef enum color_t
     45 {
     46     YELLOW,
     47     ORANGE,
     48     RED,
     49     MAGENTA,
     50     VIOLET,
     51     BLUE,
     52     CYAN,
     53     GREEN,
     54     NUM_COLORS,
     55 } color_t;
     56 
     57 struct SimBounds
     58 {
     59     float center_x;
     60     float center_y;
     61     float side_length_x;
     62     float side_length_y;
     63 };
     64 
     65 struct SimView
     66 {
     67     float dx;
     68     float dy;
     69     float zoom;
     70 };
     71 
     72 struct SimState
     73 {
     74     int num_stars;
     75     struct SimBounds bounding_box;
     76     struct SimView view;
     77     struct QuadTree *qt;
     78     struct Star stars[NUM_STARS];
     79 };
     80 
     81 struct OffscreenBuffer
     82 {
     83     // NOTE(amin): pixels are always 32-bits wide. Memory order: BB GG RR XX.
     84     void *memory;
     85     unsigned int width;
     86     unsigned int height;
     87     unsigned int pitch;
     88 };
     89 
     90 void sim_init(struct SimState *sim_state, int field_width, int field_height);
     91 void sim_update(struct SimState *sim_state, int field_width, int field_height);
     92 void sim_bounding_box_update(struct SimBounds *bounds, float min_x, float min_y, float max_x, float max_y);
     93 void sim_render(struct OffscreenBuffer *buffer, float dt, struct SimState *sim_state);
     94 void sim_bounding_box_render(struct OffscreenBuffer *buffer, struct SimBounds *bounding_box, uint32_t color, struct SimView *view);
     95 void sim_grid_render(struct OffscreenBuffer *buffer, struct QuadTreeNode *node, uint32_t color, struct SimView *view);
     96 float sim_calc_render_offset(float zoom, float delta, float pos, float center);
     97 void sim_set_pixel(struct OffscreenBuffer *buffer, uint32_t x, uint32_t y, uint32_t color);
     98 void sim_cleanup(struct SimState *sim_state);
     99 
    100 #define STAR_GARDEN_H
    101 #endif