| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- //
- // sim.c
- // Life
- //
- // Created by Benoit Sida on 2014-06-13.
- // Copyright (c) 2014 Benoit Sida. All rights reserved.
- //
-
- #include <stdio.h>
- #include "constante.h"
- #include "sim.h"
-
- static void randmap(Cell map[]);
- static void drawmap(Sprites *Sprites, SDL_Surface *screen, Cell map[]);
- static void eventWatch();
- static void next(Cell map[]);
- static void drawCell(Sprites *Sprites, SDL_Surface *screen, SDL_Rect position, Cell Cell);
- static void refreshCells(Cell map[]);
-
- void sim(Sprites *Sprites, SDL_Window *ecran, SDL_Surface *screen, int cycles)
- {
- Cell map[MAXMAP];
- randmap(map);
- int remaining = cycles;
- while (remaining != 0){
- refreshCells(map);
- drawmap(Sprites, screen, map);
- SDL_UpdateWindowSurface(ecran);
- eventWatch(&remaining);
- SDL_Delay(WAIT);
- next(map);
- remaining--;
- }
- }
-
- static void randmap(Cell map[])
- {
- for (int i = 0 ; i < MAXMAP ; i++)
- {
- map[i].x = i%NB_BLOCS;
- map[i].y = i/NB_BLOCS;
- map[i].type = rand()%2;
- }
- }
-
- static void drawmap(Sprites *Sprites, SDL_Surface *screen, Cell map[])
- {
- SDL_Rect position = {0,0};
- SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
- for (int i = 0 ; i < MAXMAP ; i++)
- {
- position.x = map[i].x * TAILLE_BLOC;
- position.y = map[i].y * TAILLE_BLOC;
- drawCell(Sprites, screen, position, map[i]);
- }
- }
-
- static void eventWatch()
- {
- SDL_Event event;
- int pause = 0;
- SDL_PollEvent(&event);
- switch(event.type)
- {
- case SDL_QUIT:
- exit(EXIT_SUCCESS);
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- exit(EXIT_SUCCESS);
- break;
- case SDLK_SPACE:
- pause = 1;
- while (pause) {
- SDL_WaitEvent(&event);
- switch(event.type)
- {
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_SPACE:
- pause = 0;
- break;
- }
- }
- }
- break;
- }
- break;
- }
- }
-
- static void next(Cell map[])
- {
- for (int i = 0 ; i < MAXMAP ; i++)
- {
- map[i].type = map[i].type == 1 ? (map[i].voisines == 2 || map[i].voisines == 3 ? 1 : 2) : (map[i].voisines == 3 ? 1 : 0);
- }
- }
-
- static void drawCell(Sprites *Sprites, SDL_Surface *screen, SDL_Rect position, Cell Cell)
- {
- if (Cell.type == 0)
- SDL_BlitSurface(Sprites->cell, NULL, screen, &position);
- else if (Cell.type == 2)
- SDL_BlitSurface(Sprites->deadcell, NULL, screen, &position);
- else {
- //SDL_BlitSurface(Sprites->alivecell, NULL, screen, &position);
- SDL_BlitSurface(Sprites->cellsheet, &Sprites->clip[Cell.clip], screen, &position);
- }
- }
-
- static void refreshCells(Cell map[])
- {
- for (int i = 0; i < MAXMAP; i++)
- {
-
- // CELL.near
- for (int j = 0; j < 4; j++)
- {
- map[i].near[j] = false;
- }
- map[i].near[TOP] = i-NB_BLOCS < 0 ? false : (map[i-NB_BLOCS].type == 1 ? true : false);
- map[i].near[LEFT] = i%NB_BLOCS == 0 ? false : (map[i-1].type == 1 ? true : false);
- map[i].near[RIGHT] = i%NB_BLOCS == 11 ? false : (map[i+1].type == 1 ? true : false);
- map[i].near[LOW] = i+NB_BLOCS > MAXMAP ? false : (map[i+NB_BLOCS].type == 1 ? true : false);
-
- //CELL.voisines
- map[i].voisines = 0;
- for (int j = 0; j < 4; j ++)
- {
- map[i].voisines = map[i].near[j] == true ? ++map[i].voisines : map[i].voisines;
- }
-
- // CELL.clip
- map[i].clip = 0;
- switch (map[i].voisines)
- {
- case 1:
- map[i].clip = map[i].near[TOP] ? 10 : (map[i].near[RIGHT] ? 11 : (map[i].near[LOW] ? 12 : 13));
- break;
- case 2:
- if (map[i].near[TOP])
- map[i].clip = map[i].near[RIGHT] ? 9 : (map[i].near[LOW] ? 8 : 7);
- else if (map[i].near[RIGHT])
- map[i].clip = map[i].near[LOW] ? 6 : 5;
- else
- map[i].clip = 4;
- break;
- case 3:
- if (!map[i].near[TOP])
- map[i].clip = 0;
- else if (!map[i].near[RIGHT])
- map[i].clip = 1;
- else if (!map[i].near[LOW])
- map[i].clip = 2;
- else
- map[i].clip = 3;
- break;
- case 4:
- map[i].clip = 14;
- break;
- case 0:
- map[i].clip = 15;
- break;
- }
- }
- }
|