| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- //
- // 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(SDL_Surface *screen, Cell map[]);
- static void eventWatch();
- static void next(Cell map[]);
- static void drawCell(SDL_Surface *screen, SDL_Rect position, Cell Cell);
- //static void blitSprite(int orig, SDL_Surface *screen, SDL_Rect position);
-
- void sim(SDL_Window *ecran, SDL_Surface *screen, int cycles)
- {
- Cell map[MAXMAP];
- randmap(map);
- int remaining = cycles;
- while (remaining != 0){
- drawmap(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;
- map[i].voisines = 0;
- }
- }
-
- static void drawmap(SDL_Surface *screen, Cell map[])
- {
- SDL_Rect position;
- 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(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].voisines = 0;
- map[i].voisines = i%NB_BLOCS == 0 ? map[i].voisines : (map[i-1].type == 1 ? ++map[i].voisines : map[i].voisines);
- map[i].voisines = i%(NB_BLOCS) == 11 ? map[i].voisines : (map[i+1].type == 1 ? ++map[i].voisines : map[i].voisines);
- map[i].voisines = i-NB_BLOCS < 0 ? map[i].voisines : (map[i-NB_BLOCS].type == 1 ? ++map[i].voisines : map[i].voisines);
- map[i].voisines = i+NB_BLOCS > MAXMAP ? map[i].voisines : (map[i+NB_BLOCS].type == 1 ? ++map[i].voisines : map[i].voisines);
- }
-
- 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(SDL_Surface *screen, SDL_Rect position, Cell Cell)
- {
- SDL_Surface *cell = NULL, *deadcell = NULL, *alivecell = NULL;
- cell = IMG_Load("sprites/cell.png");
- alivecell = IMG_Load("sprites/alivecell.png");
- deadcell = IMG_Load("sprites/deadcell.png");
-
- if (Cell.type == 0)
- SDL_BlitSurface(cell, NULL, screen, &position);
- else if (Cell.type == 2)
- SDL_BlitSurface(deadcell, NULL, screen, &position);
- else {
- SDL_BlitSurface(alivecell, NULL, screen, &position);
- }
- }
-
- /*static void blitSprite(int orig, SDL_Surface *screen, SDL_Rect position)
- {
- SDL_Surface *cellsheet;
- cellsheet = IMG_Load("sprites/cellsheet.png");
-
- SDL_Rect clip[9];
- clip[0].x = clip[0].y = 0;
- clip[1].x = 34;clip[1].y = 0;
- clip[2].x = 68;clip[2].y = 0;
- clip[3].x = 0;clip[3].y = 34;
- clip[4].x = clip[4].y = 34;
- clip[5].x = 68;clip[5].y = 34;
- clip[6].x = 0;clip[6].y = 68;
- clip[7].x = 34;clip[7].y = 68;
- clip[8].x = clip[8].y = 68;
- for (int i = 0; i > 9; i++)
- clip[i].h = clip[i].w = 34;
-
- SDL_BlitSurface(cellsheet, &clip[orig], screen, &position);
- }
-
- static void switchType(Cell map[])
- {
- }*/
|