EStatus StationSendPacket(Station* pThis, Packet* pPacket) { PacketEntry* pEntry; VALIDATE_ARGUMENTS(pThis && pPacket); if (pPacket->header.type != ePKT_TYPE_ACK) pPacket->header.sequenceNum = pThis->sequenceNum++; StationNewPacketEntry(pThis, pPacket, &pEntry); if (!pEntry) { PacketDelete(&pPacket); return eSTATUS_COMMON_OK; } if (pThis->outboxHandler.callback) { pThis->outboxHandler.callback(pThis, pEntry->pPacket, ePKT_EVENT_ADDED, pEntry->retriesCount, pEntry->nextRetryTime, pThis->outboxHandler.pArg); } if (StationIsPacketPrioritized(pThis, pPacket)) CHECK(ListPushFront(pThis->pOutbox, pEntry)); else CHECK(ListPushBack(pThis->pOutbox, pEntry)); // Don't check the result: it can fail if silentTime is in the past - just ignore this case // TimeLineEvent(pThis->pTimeLine, pThis->silentTime, pPacket); return eSTATUS_COMMON_OK; }
act_obj* actTestballSpawn(linked_list* act_list, long x, long y) { SDL_Rect bbox = {0,0,16,16}; SDL_Rect arect = {0,0,16,16}; gfx_anim* anim = gfxAnimCreate(NULL, 4, 0, gfxLoadFile("derp.bmp"), arect); gfx_spr* obj = gfxSpriteCreate(bbox, true, anim, 1); act_obj* aobj = ActorCreate(obj, (float)x, (float)y, NULL, TestballThinkProc); aobj->xvel = rand() % 300; aobj->yvel = -(rand() % 300); if((rand() % 100) > 50) aobj->xvel = -aobj->xvel; ListPushFront(act_list, aobj); return aobj; }
// 用来移动一步, // 移动以后,如果吃到了食物,则生长。 // 如果碰到了墙或者自己,则死亡,并返回是否死亡的状态。 int SnakeMove() { // 头和尾的坐标 PGAME_COORD posHead; PGAME_COORD posTail; // 把尾从链表中取出,按照当前方向放置到头的位置。 posHead = (PGAME_COORD)ListGetAt(snake_list, 0); posTail = (PGAME_COORD)ListPopBack(snake_list); // 根据当前方向来设定新的头坐标。 switch (snake_dir) { case SNAKE_UP: posTail->y = posHead->y - 1; posTail->x = posHead->x; break; case SNAKE_DOWN: posTail->y = posHead->y + 1; posTail->x = posHead->x; break; case SNAKE_LEFT: posTail->x = posHead->x - 1; posTail->y = posHead->y; break; case SNAKE_RIGHT: posTail->x = posHead->x + 1; posTail->y = posHead->y; break; } ListPushFront(snake_list, posTail); // 判断是否吃到了食物 if (CoordEqual(posHead, &food)) { return SNAKE_EATEN_FOOD; } // 如果没有吃到食物判断是否撞到障碍,然后返回状态。 return IsSnakeDead(); }
// 创建蛇 void CreateSnake(dirction dir, int head_x, int head_y, int init_len) { PGAME_COORD p; SetDirction(dir); snake_list = ListCreate(NULL); p = (PGAME_COORD)malloc(sizeof(GAME_COORD)); // 蛇头部的初始位置; p->x = head_x; p->y = head_y; ListPushFront(snake_list, p); for (int i = 1; i < init_len; i++) { //p = (PGAME_COORD)malloc(sizeof(GAME_COORD)); SnakeGorwup(); } return; }
void ManipulateNumerics() { /* We should initialize the container before any operations. */ List* list = ListInit(); /* Push the integer elements. */ ListPushFront(list, (void*)(intptr_t)20); ListPushBack(list, (void*)(intptr_t)40); /* Insert the elements with the specified indexes. */ ListInsert(list, 0, (void*)(intptr_t)10); ListInsert(list, 3, (void*)(intptr_t)50); ListInsert(list, 2, (void*)(intptr_t)30); /*---------------------------------------------------------------* * Now the list should be: (10)<-->(20)<-->(30)<-->(40)<-->(50) * *---------------------------------------------------------------*/ /* Iterate through the list. */ void* element; int num = 10; ListFirst(list, false); while (ListNext(list, &element)) { assert((int)(intptr_t)element == num); num += 10; } /* Iterate through the list in the reversed order. */ num = 50; ListFirst(list, true); while (ListReverseNext(list, &element)) { assert((int)(intptr_t)element == num); num -= 10; } /* Get the element from the list head. */ ListGetFront(list, &element); assert((int)(intptr_t)element == 10); /* Get the element from the list tail. */ ListGetBack(list, &element); assert((int)(intptr_t)element == 50); /* Get the elements from the specified indexes. */ ListGetAt(list, 2, &element); assert((int)(intptr_t)element == 30); ListGetAt(list, 3, &element); assert((int)(intptr_t)element == 40); /* Replace the element residing at the list head. */ ListSetFront(list, (void*)(intptr_t)-1); /* Replace the element residing at the list tail. */ ListSetBack(list, (void*)(intptr_t)-5); /* Replace the elements residing at the specified indexes. */ ListSetAt(list, 1, (void*)(intptr_t)-2); ListSetAt(list, 2, (void*)(intptr_t)-3); ListSetAt(list, 3, (void*)(intptr_t)-4); /* Reverse the list. */ ListReverse(list); /*---------------------------------------------------------------* * Now the list should be: (-5)<-->(-4)<-->(-3)<-->(-2)<-->(-1) * *---------------------------------------------------------------*/ /* Remove the element from the list head. */ ListPopFront(list); /* Remove the element from the list tail. */ ListPopBack(list); /* Remove the elements from the specified indexes. */ ListRemove(list, 1); ListRemove(list, 1); /* Get the list size. And the remaining element should be (-4). */ unsigned size = ListSize(list); assert(size == 1); ListGetFront(list, &element); assert((int)(intptr_t)element == -4); ListDeinit(list); }
void ManipulateObjects() { /* We should initialize the container before any operations. */ List* list = ListInit(); ListSetClean(list, CleanObject); /* Push the object elements. */ Tuple* tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 20; tuple->second = -20; ListPushFront(list, tuple); tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 40; tuple->second = -40; ListPushBack(list, tuple); /* Insert the elements with the specified indexes. */ tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 10; tuple->second = -10; ListInsert(list, 0, tuple); tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 50; tuple->second = -50; ListInsert(list, 3, tuple); tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = 30; tuple->second = -30; ListInsert(list, 2, tuple); /*---------------------------------------------------------------* * Now the list should be: (10)<-->(20)<-->(30)<-->(40)<-->(50) * *---------------------------------------------------------------*/ /* Iterate through the list. */ void* element; int num = 10; ListFirst(list, false); while (ListNext(list, &element)) { assert(((Tuple*)element)->first == num); num += 10; } /* Iterate through the list in the reversed order. */ num = 50; ListFirst(list, true); while (ListReverseNext(list, &element)) { assert(((Tuple*)element)->first == num); num -= 10; } /* Get the element from the list head. */ ListGetFront(list, &element); assert(((Tuple*)element)->first == 10); /* Get the element from the list tail. */ ListGetBack(list, &element); assert(((Tuple*)element)->first == 50); /* Get the elements from the specified indexes. */ ListGetAt(list, 2, &element); assert(((Tuple*)element)->first == 30); ListGetAt(list, 3, &element); assert(((Tuple*)element)->first == 40); /* Replace the element residing at the list head. */ tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = -1; tuple->second = 1; ListSetFront(list, tuple); /* Replace the element residing at the list tail. */ tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = -5; tuple->second = 5; ListSetBack(list, tuple); /* Replace the elements residing at the specified indexes. */ tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = -2; tuple->second = 2; ListSetAt(list, 1, tuple); tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = -3; tuple->second = 3; ListSetAt(list, 2, tuple); tuple = (Tuple*)malloc(sizeof(Tuple)); tuple->first = -4; tuple->second = 4; ListSetAt(list, 3, tuple); /* Reverse the list. */ ListReverse(list); /*---------------------------------------------------------------* * Now the list should be: (-5)<-->(-4)<-->(-3)<-->(-2)<-->(-1) * *---------------------------------------------------------------*/ /* Remove the element from the list head. */ ListPopFront(list); /* Remove the element from the list tail. */ ListPopBack(list); /* Remove the elements from the specified indexes. */ ListRemove(list, 1); ListRemove(list, 1); /* Get the list size. And the remaining element should be (-4). */ unsigned size = ListSize(list); assert(size == 1); ListGetFront(list, &element); assert(((Tuple*)element)->first == -4); ListDeinit(list); }