Game programming in C, separation of concerns -


this example code taken book , coded 1 file. simple instance of simulating ants , rocks in mode13, makes sense. colors in mode13 green (value 10) ant , gray (value 7) rock.

but, how should code separated multiple files later use when things complicated,

ex: game.c, game.h, ant.c, ant.h...

so ant type not know of (vga) graphics subsystem.

i thinking of moving ant type, ant initiation, , ant movement ant.c , ant.h. ants array, it's drawing , calls other systems main.c , main.h

i don't want ant.c code call outside subroutines of vga subsystem. include file in ant.c should ant.h, outside information ant.c should passed via function parameters.

how do since after movement of ant check if there rock under can change direction?

any ideas highly appreciated.

sample code:

#define num_ants 250 #define num_rocks 400  enum direction {ant_north, ant_east, ant_south, ant_west}; struct ant {     int x, y;     enum direction state;     unsigned char color;     unsigned char back_color; };  struct ant ants[num_ants];  void draw_ground(void) {     (int = 0; < num_rocks; i++) put_pixel(rand() % 320, rand() % 200, 7); }  void initialize_ants(void) {     (int = 0; < num_ants; i++)     {         ants[i].x = rand() % 320;         ants[i].y = rand() % 200;         ants[i].state = rand() % 4;         ants[i].color = 10;         ants[i].back_color = get_pixel(ants[i].x, ants[i].y);     } }  void erase_ants(void) {     (int = 0; < num_ants; i++) put_pixel(ants[i].x, ants[i].y, ants[i].back_color); }  void move_ants(void) {     (int = 0; < num_ants; i++)     {         switch (ants[i].state)         {             case ant_north:                 ants[i].y--;                 break;             case ant_south:                 ants[i].y++;                 break;             case ant_east:                 ants[i].x++;                 break;             case ant_west:                 ants[i].x--;         }          ants[i].x = ants[i].x < 0 ? 319 : ants[i].x;         ants[i].x = ants[i].x > 319 ? 0 : ants[i].x;         ants[i].y = ants[i].y < 0 ? 199 : ants[i].y;         ants[i].y = ants[i].y > 199 ? 0 : ants[i].y;          if (7 == get_pixel(ants[i].x, ants[i].y)) ants[i].state = rand() % 4;     } }  void behind_ants(void) {     (int = 0; < num_ants; i++) ants[i].back_color = get_pixel(ants[i].x, ants[i].y); }  void draw_ants(void) {     (int = 0; < num_ants; i++) put_pixel(ants[i].x, ants[i].y, ants[i].color); }  int main (int argc, char* argv[]) {     draw_ground();     initialize_ants();     while (!key_press())     {         erase_ants();         move_ants();         behind_ants();         draw_ants();         delay(5);     }     return 0; } 

one way may create world.c/world.h contain drawing methods. if change vga else file modified.

ant.c call world_draw_ant(coordinates) , world_is_vacant(coordinates). these vga calls get_pixel() , put_pixel().

as stands positions of rocks recorded on screen. instead store list of rock locations , search list rocks see if ant can move new location. slower get_pixel(), give freedom of running simulation without needing update display @ every step.


Comments