diff --git a/Input/inputHandler.c b/Input/inputHandler.c index 2b5f428..5c63da3 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -131,7 +131,8 @@ void mouseInput(Game *game){ else if(inputHandler->cursorWorldPos.x > maxWidth){ printf("OutOfBoundsDestination Spawn\n");} else if(inputHandler->cursorWorldPos.y > maxHeight){ printf("OutOfBoundsDestination Spawn\n");} else { - ListInsertBack(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); + //ListInsertBack(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); + ListInsertSorted(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); } } diff --git a/List/list.c b/List/list.c index 5f0ca4e..53ea5d1 100644 --- a/List/list.c +++ b/List/list.c @@ -63,6 +63,37 @@ void ListInsertBack(List *list, Sprite *data){ } } +void ListInsertSorted(List *list, Sprite *data){ + Node *new = ListCreateNode(data); + + int inserted = 1; + Node *current = list->head; + while(inserted == 1){ + if(current == 0){ + free(new); + ListInsertBack(list, data); + inserted = 0; + continue; + } + else if(data->y <= current->data.y){ + if(current == list->head){ + free(new); + ListInsertFront(list, data); + inserted = 0; + } + else{ + new->prev = current->prev; + new->next = current; + current->prev->next = new; + current->prev = new; + inserted = 0; + } + continue; + } + current = current->next; + } +} + List * ListInit(){ List *newList = (List *) malloc(sizeof(List)); newList->head = 0; @@ -74,8 +105,31 @@ List * ListInit(){ void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera){ Node *current = list->head; + // Only drawing the Sprites which are within Camera view + Vector2 topleft = {0, 0}; + IsometricMapProject(map[0], camera, topleft.x, topleft.y, &topleft); + Vector2 topright = {GetScreenWidth(), 0}; + IsometricMapProject(map[0], camera, topright.x, topright.y, &topright); + Vector2 botleft = {0, GetScreenHeight()}; + IsometricMapProject(map[0], camera, botleft.x, botleft.y, &botleft); + Vector2 botright = {GetScreenWidth(), GetScreenHeight()}; + IsometricMapProject(map[0], camera, botright.x, botright.y, &botright); + int itmp, jtmp, maxI, maxJ; + int extraPixels = 0; while(current != 0){ - DrawSpriteToWorld(¤t->data, map, camera); + // drawing some extra corner pixels + // if extraPixels == 0 you can see flickering in the corners + itmp = (int)(topleft.x) - extraPixels; + jtmp = (int)(topright.y) - extraPixels; + maxI = (int)(botright.x) + extraPixels; + maxJ = (int)(botleft.y) + extraPixels; + // Only drawing the Sprites which are within Camera view + if( current->data.x > itmp && + current->data.y > jtmp && + current->data.x < maxI && + current->data.y < maxJ){ + DrawSpriteToWorld(¤t->data, map, camera); + } current = current->next; } } @@ -87,7 +141,9 @@ void ListActAllSprites(Game *game){ float movementSpeed = 150.0f * GetFrameTime(); Node *current = game->sprites->head; + int counter = 0; while (current != 0){ + counter ++; if(current->data.hasDestination == 1){ Vector2 movement = { current->data.destX - current->data.x, @@ -147,6 +203,60 @@ void ListActAllSprites(Game *game){ } } + // Sorting the sprites by y-coord, bruh + if(counter > 2 && current != game->sprites->tail){ + if(current->data.y < current->prev->data.y){ + Node *prepre = current->prev->prev; + Node *toBack = current->prev; + Node *toFront = current; + Node *back = current->next; + prepre->next = toFront; + toFront->next = toBack; + toFront->prev = prepre; + toBack->prev = toFront; + toBack->next = back; + back->prev = toBack; + } + } + else if(counter > 2 && current == game->sprites->tail){ + if(current->data.y < current->prev->data.y){ + Node *prepre = current->prev->prev; + Node *toBack = current->prev; + Node *toFront = current; + prepre->next = toFront; + toFront->next = toBack; + toFront->prev = prepre; + toBack->prev = toFront; + toBack->next = 0; + game->sprites->tail = toBack; + } + } + else if(counter == 2 && current != game->sprites->tail){ + if(current->data.y < current->prev->data.y){ + Node *toBack = current->prev; + Node *toFront = current; + Node *back = current->next; + toFront->next = toBack; + toBack->prev = toFront; + toBack->next = back; + back->prev = toBack; + toFront->prev = 0; + game->sprites->head = toFront; + } + } + else if(counter == 2 && current == game->sprites->tail){ + if(current->data.y < current->prev->data.y){ + Node *toBack = current->prev; + Node *toFront = current; + toBack->prev = toFront; + toBack->next = 0; + toFront->next = toBack; + toFront->prev = 0; + game->sprites->head = toFront; + game->sprites->tail = toBack; + } + } + SpriteUpdateAnimation(¤t->data); // updating z-position according to the tile the sprite stands on Tile *floorTile = IsometricMapGetTileFromWorldCoordinates(game->layers, current->data.x, current->data.y, 0); diff --git a/List/list.h b/List/list.h index a7e80f9..f808ca5 100644 --- a/List/list.h +++ b/List/list.h @@ -28,6 +28,7 @@ void ListPrintForward(List *list); void ListInsertFront(List *list, Sprite *data); void ListInsertBack(List *list, Sprite *data); +void ListInsertSorted(List *list, Sprite *data); List * ListInit(); void ListDrawAllSprites(List *list, IsometricMap **map, Camera2D *camera); void ListActAllSprites(Game *game); diff --git a/inputHandler.o b/inputHandler.o index ae05d6c..16524a7 100644 Binary files a/inputHandler.o and b/inputHandler.o differ diff --git a/isometricRenderer.o b/isometricRenderer.o index 12508ca..2261181 100644 Binary files a/isometricRenderer.o and b/isometricRenderer.o differ diff --git a/list.o b/list.o index 7533f36..75049e0 100644 Binary files a/list.o and b/list.o differ diff --git a/main.c b/main.c index ba59fd8..d1ed27a 100644 --- a/main.c +++ b/main.c @@ -36,20 +36,20 @@ int main(){ ListDrawAllSprites(game->sprites, game->layers, game->camera); /* - ListInsertBack(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); - ListInsertBack(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); - ListInsertBack(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); - ListInsertBack(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); - ListInsertBack(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); - ListInsertBack(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); - ListInsertBack(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); - ListInsertBack(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); - ListInsertBack(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); - ListInsertBack(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); + ListInsertSorted(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); + ListInsertSorted(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); + ListInsertSorted(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); + ListInsertSorted(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); + ListInsertSorted(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); + ListInsertSorted(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); + ListInsertSorted(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); + ListInsertSorted(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); + ListInsertSorted(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); + ListInsertSorted(game->sprites, SpriteCreate(game->textures, 1, 250 + c % 500, 250 + c % 500)); c+=10; printf("Sprites: %d\n", c); */ - + EndMode2D(); // Moving cursor Sprite to Mouse Pos and drawing it diff --git a/main.o b/main.o index cf7d598..c9cb433 100644 Binary files a/main.o and b/main.o differ diff --git a/spiel b/spiel index 5875750..ddac5a5 100755 Binary files a/spiel and b/spiel differ