PURE EFFIZIENZ und sortierung ist bruh

main
JanEhehalt 3 years ago
parent 0732ddedeb
commit 88220d6a7d

@ -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));
}
}

@ -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){
// 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(&current->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(&current->data);
// updating z-position according to the tile the sprite stands on
Tile *floorTile = IsometricMapGetTileFromWorldCoordinates(game->layers, current->data.x, current->data.y, 0);

@ -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);

Binary file not shown.

Binary file not shown.

BIN
list.o

Binary file not shown.

@ -36,16 +36,16 @@ 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);
*/

BIN
main.o

Binary file not shown.

BIN
spiel

Binary file not shown.
Loading…
Cancel
Save