You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
215 lines
5.9 KiB
215 lines
5.9 KiB
#include "sprite.h"
|
|
#include "raylib.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "../IsometricMap/isometricMap.h"
|
|
#include "../definitions.h"
|
|
#include "../Textures/animationHandler.h"
|
|
#include "../Textures/animation.h"
|
|
#include "../Textures/textureatlas.h"
|
|
#include "../IsometricMap/tile.h"
|
|
|
|
void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera){
|
|
|
|
Vector2 pos = {sprite->x - sprite->texture->width, sprite->y - sprite->texture->height/2};
|
|
|
|
IsometricMapUnprojectIgnoreCam( pos.x, pos.y, &pos);
|
|
|
|
DrawTexture(*sprite->texture, pos.x, pos.y, WHITE);
|
|
}
|
|
|
|
void DrawSpriteToScreen(Sprite *sprite){
|
|
DrawTexture(*sprite->texture, sprite->x, sprite->y, WHITE);
|
|
}
|
|
|
|
void SpriteUpdate(Sprite *sprite){
|
|
|
|
sprite->depth = sprite->x + sprite->y + sprite->z;
|
|
|
|
// sprite->texture = sprite->animationHandler->currentFrame->texture;
|
|
}
|
|
|
|
Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y){
|
|
Sprite *newSprite = malloc(sizeof(Sprite));
|
|
|
|
newSprite->texture = atlas->textures[textureID];
|
|
newSprite->x = x - (newSprite->texture->width/2);
|
|
newSprite->y = y - (newSprite->texture->height/2);
|
|
newSprite->z = 0;
|
|
newSprite->depth = newSprite->x + newSprite->y;
|
|
newSprite->next = 0;
|
|
newSprite->prev = 0;
|
|
|
|
return newSprite;
|
|
}
|
|
|
|
void SpriteListPrintForward(SpriteList *list){
|
|
Sprite *current = list->head;
|
|
|
|
printf("\n[\n");
|
|
while(current != 0){
|
|
printf("%f,\n", current->depth);
|
|
current = current->next;
|
|
}
|
|
printf("]\n");
|
|
}
|
|
|
|
void SpriteListInsertBefore(SpriteList *list, Sprite *new, Sprite *current){
|
|
new->next = current;
|
|
new->prev = current->prev;
|
|
current->prev = new;
|
|
|
|
if(current == list->head){
|
|
list->head = new;
|
|
}
|
|
else{
|
|
new->prev->next = new;
|
|
}
|
|
|
|
list->spriteAmount++;
|
|
}
|
|
|
|
void SpriteListInsertAfter(SpriteList *list, Sprite *new, Sprite *current){
|
|
new->prev = current;
|
|
new->next = current->next;
|
|
current->next = new;
|
|
|
|
if(current == list->tail){
|
|
list->tail = new;
|
|
}
|
|
else{
|
|
new->next->prev = new;
|
|
}
|
|
|
|
list->spriteAmount++;
|
|
}
|
|
|
|
void SpriteListInsert(SpriteList *list, Sprite *new){
|
|
|
|
if(list->head == 0){
|
|
list->head = new;
|
|
list->tail = new;
|
|
|
|
list->spriteAmount++;
|
|
}
|
|
else{
|
|
Sprite *current = list->head;
|
|
|
|
while(current != 0){
|
|
if(new->depth < current->depth){
|
|
SpriteListInsertBefore(list, new, current);
|
|
return;
|
|
}
|
|
current = current->next;
|
|
}
|
|
|
|
SpriteListInsertAfter(list, new, list->tail);
|
|
}
|
|
|
|
}
|
|
|
|
void SpriteListSpriteChanged(SpriteList *list, Sprite *changed){
|
|
if(changed != list->tail && changed->depth > changed->next->depth){
|
|
//Nach rechts
|
|
|
|
Sprite *current = changed->next;
|
|
SpriteListRemove(list, changed);
|
|
|
|
while(current != 0){
|
|
if(changed->depth < current->depth){
|
|
SpriteListInsertBefore(list, changed, current);
|
|
return;
|
|
}
|
|
current = current->next;
|
|
}
|
|
|
|
SpriteListInsertAfter(list, changed, list->tail);
|
|
}
|
|
else if(changed != list->head && changed->depth < changed->prev->depth){
|
|
//Nach links
|
|
|
|
Sprite *current = changed->prev;
|
|
SpriteListRemove(list, changed);
|
|
|
|
while(current != 0){
|
|
if(changed->depth > current->depth){
|
|
SpriteListInsertAfter(list, changed, current);
|
|
return;
|
|
}
|
|
current = current->prev;
|
|
}
|
|
|
|
SpriteListInsertBefore(list, changed, list->head);
|
|
}
|
|
}
|
|
|
|
void SpriteListRemove(SpriteList *list, Sprite *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;
|
|
list->spriteAmount--;
|
|
}
|
|
else if(list->head == remove){
|
|
remove->next->prev = 0;
|
|
list->head = remove->next;
|
|
list->spriteAmount--;
|
|
}
|
|
else if(list->tail == remove){
|
|
remove->prev->next = 0;
|
|
list->tail = remove->prev;
|
|
list->spriteAmount--;
|
|
}
|
|
else{
|
|
remove->prev->next = remove->next;
|
|
remove->next->prev = remove->prev;
|
|
list->spriteAmount--;
|
|
}
|
|
|
|
remove->next = 0;
|
|
remove->prev = 0;
|
|
}
|
|
|
|
SpriteList * SpriteListInit(){
|
|
SpriteList *newSpriteList = malloc(sizeof(SpriteList));
|
|
newSpriteList->head = 0;
|
|
newSpriteList->tail = 0;
|
|
newSpriteList->spriteAmount = 0;
|
|
return newSpriteList;
|
|
}
|
|
|
|
// iterates over all Sprites in the list and draws them to the world
|
|
void SpriteListDrawAllSprites(SpriteList *list, IsometricMap *map, Camera2D *camera){
|
|
Sprite *current = list->head;
|
|
|
|
// Only drawing the Sprites which are within Camera view
|
|
Vector2 topleft = {0, 0};
|
|
IsometricMapProject(map, camera, topleft.x, topleft.y, &topleft);
|
|
Vector2 topright = {GetScreenWidth(), 0};
|
|
IsometricMapProject(map, camera, topright.x, topright.y, &topright);
|
|
Vector2 botleft = {0, GetScreenHeight()};
|
|
IsometricMapProject(map, camera, botleft.x, botleft.y, &botleft);
|
|
Vector2 botright = {GetScreenWidth(), GetScreenHeight()};
|
|
IsometricMapProject(map, camera, botright.x, botright.y, &botright);
|
|
int extraPixels = 0;
|
|
int itmp, jtmp, maxI, maxJ;
|
|
itmp = (int)(topleft.x) - extraPixels;
|
|
jtmp = (int)(topright.y) - extraPixels;
|
|
maxI = (int)(botright.x) + extraPixels;
|
|
maxJ = (int)(botleft.y) + extraPixels;
|
|
while(current != 0){
|
|
// drawing some extra corner pixels
|
|
// if extraPixels == 0 you might be able to see flickering in the corners
|
|
// Only drawing the Sprites which are within Camera view
|
|
if( current->x > itmp &&
|
|
current->y > jtmp &&
|
|
current->x < maxI &&
|
|
current->y < maxJ){
|
|
DrawSpriteToWorld(current, map, camera);
|
|
}
|
|
current = current->next;
|
|
}
|
|
}
|