Ejemplo n.º 1
0
jnx_list *cartographer_get_at(sfView *view)
{
  /*-----------------------------------------------------------------------------
   *  Again a temporary solution to return the entire list
   *-----------------------------------------------------------------------------*/
  if(object_list == NULL)
  {
    return NULL;
  }
  sfVector2f position = sfView_getCenter(view);
  int view_max_size = sfView_getSize(view).x;
  jnx_node *head = object_list->head;
  jnx_node *head_reset = head;
  jnx_list *temp_list = jnx_list_create();	
  int count = 0;
  while(head)
  {
    game_object *obj = head->_data;
    sfVector2f obj_pos = sfSprite_getPosition(obj->sprite);
    if(obj_pos.x < position.x + view_max_size && obj_pos.x > position.x - view_max_size && obj_pos.y < position.y + view_max_size && obj_pos.y > position.y - view_max_size)
    {
      jnx_list_add(temp_list,obj);
      ++count;
    }
    head = head->next_node;
  }
  head = head_reset;
  /*-----------------------------------------------------------------------------
   *  Note, that you probably don't want to return the original list ever as any manipulation outside of this functionality could cause error
   *-----------------------------------------------------------------------------*/
  return temp_list;
}
Ejemplo n.º 2
0
session_state session_service_destroy_session(session_service *service,\
        jnx_guid *g) {
    JNXCHECK(service);
    session_state e = SESSION_STATE_NOT_FOUND;
    jnx_node *h = service->session_list->head,
              *r = service->session_list->head;
    jnx_list *cl = jnx_list_create();
    session *retrieved_session = NULL;
    while(h) {
        session *s = h->_data;
        if(jnx_guid_compare(g,&s->session_guid) == JNX_GUID_STATE_SUCCESS) {
            retrieved_session = s;
            e = SESSION_STATE_OKAY;
        } else {
            jnx_list_add_ts(cl,s);
        }
        h = h->next_node;
    }
    service->session_list->head = r;
    jnx_list_destroy(&service->session_list);
    if(cl) {
        destroy_session(retrieved_session);
    }
    service->session_list = cl;
    return e;
}
Ejemplo n.º 3
0
static void test_removal_from() {
  JNXLOG(LDEBUG,"- test_removal_from\n");
  jnx_list *list = jnx_list_create();

  // Test the empty list removal should succeed without blowing up
  jnx_list_remove_from(&list, 5, compare_ints);

  int i;
  for (i = 1; i < 10; i++)
    jnx_list_add(list, (void *)i);

  // Remove from the middle
  jnx_list_remove_from(&list, 3, compare_ints);
  // Remove the head
  jnx_list_remove_from(&list, 1, compare_ints);
  // Remove the tail
  jnx_list_remove_from(&list, 9, compare_ints);

  // Check the remaining elements are 245678
  char digit[2];
  char leftover[10];
  leftover[0] = '\0';
  jnx_node *current = list->head;
  while (current != NULL) {
    sprintf(digit, "%d", (int) current->_data);
    strcat(leftover, digit);
    current = current->next_node;		
  }

  JNXCHECK(strcmp(leftover, "245678") == 0);
}
Ejemplo n.º 4
0
void cartographer_add(game_object *obj)
{
  if(object_list == NULL) { object_list = jnx_list_create(); }
  /*-----------------------------------------------------------------------------
   *  Temporary solution whilst I create an algorithm for search 
   *-----------------------------------------------------------------------------*/
  jnx_list_add(object_list,obj);
}
Ejemplo n.º 5
0
session_service *session_service_create(session_linking_service_func linking_func,
                                        session_unlinking_service_func unlinking_func) {
    session_service *s = malloc(sizeof(session_service));
    s->session_list = jnx_list_create();
    s->unlinking_func = unlinking_func;
    s->linking_func = linking_func;
    return s;
}
Ejemplo n.º 6
0
static void test_contains() {
  jnx_list *l = jnx_list_create();

  char *text = "Text";
  char *another = "Another";
  char *fake = "Fake";
  jnx_list_add(l,text);
  jnx_list_add(l,another);

  JNXCHECK(jnx_list_contains(l,text,test_contains_comparator) == 1);
  JNXCHECK(jnx_list_contains(l,another,test_contains_comparator) == 1);
  JNXCHECK(jnx_list_contains(l,fake,test_contains_comparator) == 0);

}
Ejemplo n.º 7
0
jnx_int32 jnx_hash_put(jnx_hashmap* hashmap, const jnx_char* key, void* value) {
  JNXCHECK(hashmap);
  JNXCHECK(key);
  JNXCHECK(value);
  jnx_int32 index = jnx_hash_string(key, hashmap->size);
  if (hashmap->data[index].used == 0) {
    // we need to setup the bucket
    hashmap->data[index].bucket = jnx_list_create();
    hashmap->data[index].bucket_len = 0;
    //okay so bucket is ready to get given a KVP entry, so we'll use our bucket struct
    jnx_hash_bucket_el* current_bucket_el = malloc(sizeof(jnx_hash_bucket_el));
    current_bucket_el->origin_key = malloc(strlen(key) + 1);
    strncpy(current_bucket_el->origin_key,key,strlen(key) + 1);
    current_bucket_el->origin_value = value;//insert our bucket element...
    jnx_list_add(hashmap->data[index].bucket, current_bucket_el);
    hashmap->data[index].bucket_len++;
    hashmap->data[index].used++;
    hashmap->used_up++;
  } else {
    // So we are here assuming that the bucket exists so we're going to append to the existing bucket...
    //check to see if it already exists!
    jnx_node* head = hashmap->data[index].bucket->head;
    jnx_node* rewind_head = head;
    while (head) {
      jnx_hash_bucket_el* bucketel = head->_data;
      if (strcmp(bucketel->origin_key, key) == 0) {
        bucketel->origin_value = value;
        hashmap->data[index].bucket->head = rewind_head;
        return 0;
      }
      head = head->next_node;
    }
    hashmap->data[index].bucket->head = rewind_head;
    //If the list has been searched and the entry does not exist, we'll do a new insert
    jnx_hash_bucket_el* current_bucket_el = malloc(sizeof(jnx_hash_bucket_el));
    current_bucket_el->origin_key = malloc(strlen(key) + 1);
    strncpy(current_bucket_el->origin_key,key,strlen(key) + 1);
    current_bucket_el->origin_value = value;
    jnx_list_add(hashmap->data[index].bucket, current_bucket_el);
    hashmap->data[index].bucket_len++;
    hashmap->data[index].used = 1;
    hashmap->used_up++;
    //so the linked list length should be incremented....
  }

  return 0;
}
Ejemplo n.º 8
0
static void test_removal_returns_correct_data() {
  JNXLOG(LDEBUG,"- test_removal_returns_correct_data\n");

  char *data = malloc(strlen("data item") + 1);
  strcpy(data, "data item");

  char *lookup = malloc(strlen("data item") + 1);
  strcpy(lookup, "data item");

  jnx_list *ls = jnx_list_create();
  jnx_list_add(ls, (void *) data);

  void *result = jnx_list_remove_from(&ls, (void *) lookup, (ordering) strcmp);
  JNXCHECK(result == data);
  JNXCHECK(result != lookup);
  JNXCHECK(ls->head == NULL);
}
Ejemplo n.º 9
0
session_state session_service_fetch_all_sessions(session_service *service,
        jnx_list **olist) {
    *olist = NULL;
    if(service->session_list->counter == 0) {
        return SESSION_STATE_NOT_FOUND;
    }
    jnx_node *h = service->session_list->head,
              *r = service->session_list->head;
    *olist = jnx_list_create();
    while(h) {
        session *s = h->_data;
        jnx_list_add_ts(*olist,s);
        h = h->next_node;
    }
    service->session_list->head = r;
    return SESSION_STATE_OKAY;
}
Ejemplo n.º 10
0
static void test_data_removal() {
  JNXLOG(LDEBUG,"- test_data_removal");
  char *test_string = "AABBCC";
  jnx_list *list = jnx_list_create();
  int count = 10;
  int x;
  for(x = 0; x < count; ++x) {
    char *dynamic_string = malloc(strlen(test_string));
    jnx_list_add(list,dynamic_string);
  }
  char *ret;
  while((ret = jnx_list_remove(&list)) != NULL) {
    free(ret);
  }
  jnx_list_destroy(&list);
  JNXCHECK(list == NULL);
  JNXLOG(LDEBUG,"OK");
}
Ejemplo n.º 11
0
static void test_list_index() {
  JNXLOG(LDEBUG,"- test_list_sequence");
  jnx_list *j = jnx_list_create();
  int count = 6;
  char *ar[] = { "A", "B", "C", "D", "E", "F" };
  int y;
  for(y = 0; y < count; ++y) {
    jnx_list_add(j,ar[y]);
  }
  int x;
  for(x = count - 1; x >= 0; x--) {
    char *current = jnx_list_remove(&j);
    JNXCHECK(strcmp(current,ar[x]) == 0);
  }
  JNXLOG(LDEBUG,"OK");
  jnx_list_destroy(&j);
  JNXCHECK(j == NULL);
}
Ejemplo n.º 12
0
static void test_list_tail() {
  jnx_list *l = jnx_list_create();

  int count = 3;
  char *ar[] = { "A", "B", "C" };
  int x;
  for(x = 0; x < count; ++x) {
    jnx_list_add(l,ar[x]);
  }
  int c=count;
  while(l->tail != NULL) {

    JNXCHECK(strcmp((char*)l->tail->_data,ar[c-1]) == 0);
    --c;
    l->tail = l->tail->prev_node;
  }

  jnx_list_destroy(&l);
}
Ejemplo n.º 13
0
static void test_list_creation() {
  JNXLOG(LDEBUG,"- test_list_creation");
  jnx_list *secondlist = jnx_list_create();
  JNXCHECK(secondlist != NULL);
  JNXLOG(LDEBUG,"OK");
  struct foo *f = malloc(sizeof(foo));
  f->number = 10;
  JNXLOG(LDEBUG,"- test_list_insertion");
  jnx_list_add(secondlist,(void*)f);
  JNXLOG(LDEBUG,"OK");
  JNXLOG(LDEBUG,"- test_list_removal");
  struct foo *output = (struct foo*)jnx_list_remove(&secondlist);
  JNXCHECK(output->number == 10);
  free(output);
  JNXLOG(LDEBUG,"OK");
  JNXLOG(LDEBUG,"- test_list_deletion");
  jnx_list_destroy(&secondlist);
  JNXLOG(LDEBUG,"OK");
}
Ejemplo n.º 14
0
void cartographer_update()
{

  /*-----------------------------------------------------------------------------
   *  Warning, may slow down game loop if there are alot of objects here
   *-----------------------------------------------------------------------------*/
  jnx_node *head = object_list->head;
  jnx_list *new_draw = jnx_list_create();
  while(head)
  {
    game_object *current = head->_data;
    if(current->health > 0)
    {
      //still an active object
      jnx_list_add(new_draw,current);
    }
    else
    {
      if(strcmp(current->object_type,"player")  == 0)
      {
        /*-----------------------------------------------------------------------------
         *  If the player has been killed we'll call a clean up function to end the game
         *-----------------------------------------------------------------------------*/
        game_end();	
      }
      JNX_LOG(NULL,"Object %s at %g %g has been removed as health was %d\n", current->object_type,current->position.x, current->position.y, current->health);
      //remove the object
      play_sound(sound_lexplosion);

      /*-----------------------------------------------------------------------------
       *  Add our soon to be lost object to the score board
       *-----------------------------------------------------------------------------*/
      printf("adding object for destruction %s with health %d\n",current->object_type,current->health);
      //Possible bug here if the player starts in reach of enemy ships and they start blowing up before game time	
      score_add_destroyed_obj(current);
      sfSprite_destroy(current->sprite);
      free(current);
    }
    head = head->next_node;
  }
  object_list = new_draw;
}
Ejemplo n.º 15
0
static void test_removal_front() {
  JNXLOG(LDEBUG,"- test_removal_front\n");
  jnx_list *l = jnx_list_create();
  int count = 3;
  char *ar[] = { "A", "B", "C" };
  int x;
  for(x = 0; x < count; x++) {
    jnx_list_add(l,ar[x]);
  }
  JNXCHECK(l->counter == 3);
  void *data = jnx_list_remove_front(&l);
  JNXCHECK(strcmp(data,"A") == 0);
  JNXCHECK(l->counter == 2);
  data = jnx_list_remove_front(&l);
  JNXCHECK(strcmp(data, "B") == 0);
  JNXCHECK(l->counter == 1);
  data = jnx_list_remove_front(&l);
  JNXCHECK(strcmp(data, "C") == 0);
  JNXCHECK(l->counter == 0);
}