Sorting der Sprites funktioniert

main
Jonathan Hager 3 years ago
parent 31ac3e9392
commit 27b7fa6d31
Signed by: JonathanHager
GPG Key ID: 34881E488569708C

@ -113,7 +113,8 @@ void mouseInput(Game *game){
else if(inputHandler->cursorWorldPos.x > maxWidth){ printf("OutOfBoundsDestination Spawn\n");} else if(inputHandler->cursorWorldPos.x > maxWidth){ printf("OutOfBoundsDestination Spawn\n");}
else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");} else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");}
else { else {
ListInsertBack(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); ListInsert(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y));
ListPrintForward(sprites);
//ListInsertSorted(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); //ListInsertSorted(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y));
} }
} else{ } else{

@ -19,50 +19,119 @@ void ListPrintForward(List *list){
printf("\n[\n"); printf("\n[\n");
while(current != 0){ while(current != 0){
//printf("(%f | %f),\n", current->data.x, current->data.y); printf("%f,\n", current->data->depth);
current = current->next; current = current->next;
} }
printf("]\n"); printf("]\n");
} }
void ListInsertFront(List *list, Sprite *data){ void ListInsertBefore(List *list, Node *new, Node *current){
Node *new = ListCreateNode(data); new->next = current;
new->prev = current->prev;
current->prev = new;
if(list->head == 0){ if(current == list->head){
list->head = new; list->head = new;
list->tail = new;
} }
else if(list->head == list->tail){ else{
list->head = new; new->prev->next = new;
list->head->next = list->tail; }
list->tail->prev = list->head; }
void ListInsertAfter(List *list, Node *new, Node *current){
new->prev = current;
new->next = current->next;
current->next = new;
if(current == list->tail){
list->tail = new;
} }
else{ else{
list->head->prev = new; new->next->prev = new;
new->next = list->head;
list->head = new;
} }
} }
void ListInsertBack(List *list, Sprite *data){ void ListInsert(List *list, Sprite *data){
Node *new = ListCreateNode(data); Node *new = ListCreateNode(data);
if(list->head == 0){ if(list->head == 0){
list->head = new; list->head = new;
list->tail = new; list->tail = new;
}
else if(list->head == list->tail){
list->tail = new;
list->head->next = list->tail;
list->tail->prev = list->head;
} }
else{ else{
list->tail->next = new; Node *current = list->head;
new->prev = list->tail;
list->tail = new; while(current != 0){
if(new->data->depth < current->data->depth){
ListInsertBefore(list, new, current);
return;
}
current = current->next;
}
ListInsertAfter(list, new, list->tail);
} }
} }
void ListSpriteChanged(List *list, Node *changed){
if(changed != list->tail && changed->data->depth > changed->next->data->depth){
//Nach rechts
Node *current = changed->next;
ListRemove(list, changed);
while(current != 0){
if(changed->data->depth < current->data->depth){
ListInsertBefore(list, changed, current);
return;
}
current = current->next;
}
ListInsertAfter(list, changed, list->tail);
}
else if(changed != list->head && changed->data->depth < changed->prev->data->depth){
//Nach links
Node *current = changed->prev;
ListRemove(list, changed);
while(current != 0){
if(changed->data->depth > current->data->depth){
ListInsertAfter(list, changed, current);
return;
}
current = current->prev;
}
ListInsertBefore(list, changed, list->head);
}
}
void ListRemove(List *list, Node *remove){
if(remove == 0){
printf("WARNING: TRIED TO REMOVE NULLPOINTER\n");
}
else if(list->head == remove && list->tail == remove){
list->head = 0;
list->tail = 0;
}
else if(list->head == remove){
remove->next->prev = 0;
list->head = remove->next;
}
else if(list->tail == remove){
remove->prev->next = 0;
list->tail = remove->prev;
}
else{
remove->prev->next = remove->next;
remove->next->prev = remove->prev;
}
remove->next = 0;
remove->prev = 0;
}
List * ListInit(){ List * ListInit(){
List *newList = malloc(sizeof(List)); List *newList = malloc(sizeof(List));
@ -106,13 +175,15 @@ void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera){
// iterates over all Sprites in the list and does their acting (moving etc) // iterates over all Sprites in the list and does their acting (moving etc)
void ListActAllSprites(Game *game){ void ListActAllSprites(Game *game){
ListPrintForward(game->sprites);
// Sprites move towards their destination // Sprites move towards their destination
float movementSpeed = 150.0f * GetFrameTime(); float movementSpeed = 150.0f * GetFrameTime();
Node *current = game->sprites->head; Node *current = game->sprites->head;
int counter = 0; int counter = 0;
while (current != 0){ while (current != 0){
current->data->depth = current->data->x + current->data->y + current->data->z;
ListSpriteChanged(game->sprites, current);
counter ++; counter ++;
if(current->data->hasDestination == 1){ if(current->data->hasDestination == 1){
Vector2 movement = { Vector2 movement = {

@ -26,8 +26,11 @@ Node * ListCreateNode(Sprite *data);
//Print the list in order //Print the list in order
void ListPrintForward(List *list); void ListPrintForward(List *list);
void ListInsertFront(List *list, Sprite *data); void ListInsertBefore(List *list, Node *new, Node *current);
void ListInsertBack(List *list, Sprite *data); void ListInsertAfter(List *list, Node *new, Node *current);
void ListInsert(List *list, Sprite *data);
void ListRemove(List *list, Node *remove);
void ListSpriteChanged(List *list, Node *changed);
List * ListInit(); List * ListInit();
void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera); void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera);
void ListActAllSprites(Game *game); void ListActAllSprites(Game *game);

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
game.o

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
list.o

Binary file not shown.

BIN
main.o

Binary file not shown.

BIN
spiel

Binary file not shown.

@ -64,10 +64,12 @@ Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y){
newSprite->texture = newSprite->animationHandler->currentFrame->texture; newSprite->texture = newSprite->animationHandler->currentFrame->texture;
newSprite->x = x - newSprite->texture->width / 2; newSprite->x = x - newSprite->texture->width / 2;
newSprite->y = y - newSprite->texture->height / 2; newSprite->y = y - newSprite->texture->height / 2;
newSprite->z = 0;
newSprite->destX = x; newSprite->destX = x;
newSprite->destY = y; newSprite->destY = y;
newSprite->hasDestination = 0; newSprite->hasDestination = 0;
newSprite->selected = 0; newSprite->selected = 0;
newSprite->depth = newSprite->x + newSprite->y;
newSprite->sortable = BucketInit(newSprite, SPRITE); newSprite->sortable = BucketInit(newSprite, SPRITE);

@ -16,6 +16,7 @@ typedef struct Sprite {
float destY; float destY;
int hasDestination; int hasDestination;
int selected; int selected;
float depth;
Bucket *sortable; Bucket *sortable;
} Sprite; } Sprite;

Binary file not shown.

Binary file not shown.
Loading…
Cancel
Save