Advertisement
Le_BuG63

Untitled

Feb 16th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. #include "monster.h"
  2.  
  3. #include <assert.h>
  4. #include <string.h>
  5.  
  6. #include <SDL\SDL_image.h>
  7.  
  8. #include "map.h"
  9.  
  10. void    Monster_setArray(MONSTERTYPE monsterType, const char *monsterPath)
  11. {
  12.     if (strlen(monsterPath) > 0)
  13.     {
  14.         FILE    *monsterFile = NULL;
  15.  
  16.         monsterFile = fopen(monsterPath, "r");
  17.  
  18.         if (monsterFile)
  19.         {
  20.             MONSTER *monster = malloc(sizeof(MONSTER));
  21.             char    spritesPath[128];
  22.             char    animPath[128];
  23.  
  24.             fscanf(monsterFile, "%s %s", spritesPath,
  25.                                          animPath);
  26.  
  27.             monster_addsprite(spritesPath, monster);
  28.             Anim_load(animPath, &monster->animation);
  29.  
  30.             monster->position.x = rand() % 1280;
  31.             monster->position.y = rand() % 720;
  32.             monster->frame = 0;
  33.  
  34.             memcpy(&sMonster_array[monsterType], monster, sizeof(MONSTER));
  35.         }
  36.         fclose(monsterFile);
  37.     }
  38. }
  39.  
  40. void    Monster_addList(MONSTERTYPE monsterType)
  41. {
  42.     MONSTER     *monster = malloc(sizeof(MONSTER));
  43.     memcpy(monster, &sMonster_array[monsterType], sizeof(MONSTER));
  44.  
  45.     monster->position.x = rand() % 1280;
  46.     monster->position.y = rand() % 720;
  47.  
  48.     if (sList_monster == NULL)
  49.         sList_monster = list_create(monster);
  50.     else
  51.         list_insert_end(sList_monster, monster);
  52.  
  53.     free(monster);
  54. }
  55.  
  56. void    Monster_freeList(void)
  57. {
  58.     list_node   *tmp = NULL;
  59.  
  60.     tmp = sList_monster;
  61.  
  62.     while (tmp->next != NULL)
  63.     {
  64.         //Monster_free(tmp->data);
  65.         free(tmp);
  66.  
  67.         tmp = tmp->next;
  68.     }
  69. }
  70.  
  71. void    Monster_free(MONSTER *monster)
  72. {
  73.     assert(monster != NULL);
  74.  
  75.     for (int i = 0; i < (sizeof(monster->sprites) / sizeof(SDL_Surface*)); ++i)
  76.     {
  77.         SDL_FreeSurface(monster->sprites[i]);
  78.     }
  79.  
  80.     free(monster);
  81. }
  82.  
  83. void    Monster_blitsWave(SDL_Surface *screen)
  84. {
  85.     list_node *it = sList_monster;
  86.  
  87.     while (it ->next != NULL)
  88.     {
  89.         MONSTER     monster;
  90.  
  91.         memcpy(&monster, it->data, sizeof(MONSTER));
  92.  
  93.         SDL_Rect    rect = { .x = monster.position.x, .y = monster.position.y };
  94.  
  95.         SDL_BlitSurface(monster.sprites[monster.frame], NULL, screen, &rect);
  96.  
  97.         it = it->next;
  98.     }
  99. }
  100.  
  101. void    Monster_handleMovement(MAP  *map)
  102. {
  103.     list_node *it = sList_monster;
  104.  
  105.     while (it->next != NULL)
  106.     {
  107.         MONSTER     monster;
  108.  
  109.         memcpy(&monster, it->data, sizeof(MONSTER));
  110.         Anim_play(&monster.animation, &monster.frame);
  111.         memcpy(it->data, &monster, sizeof(MONSTER));
  112.    
  113.         it = it->next;
  114.     }
  115. }
  116.  
  117. static void monster_addsprite(const char *spritesPath, MONSTER *monster)
  118. {
  119.     if (strlen(spritesPath) > 0)
  120.     {
  121.         FILE    *spritesFile = NULL;
  122.  
  123.         spritesFile = fopen(spritesPath, "r");
  124.  
  125.         if (spritesFile)
  126.         {
  127.             int nSprites = 0;
  128.  
  129.             fscanf(spritesFile, "%d", &nSprites);
  130.  
  131.             monster->sprites = malloc(nSprites * sizeof(SDL_Surface));
  132.  
  133.             for (int i = 0; i < nSprites; ++i)
  134.             {
  135.                 char    path[256];
  136.  
  137.                 fscanf(spritesFile, "%s", path);
  138.  
  139.                 monster->sprites[i] = IMG_Load(path);
  140.             }
  141.         }
  142.  
  143.         fclose(spritesFile);
  144.     }
  145. }
  146.  
  147. static void monster_findPath(MAP *map)
  148. {
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement