|
| 1 | +#include "lTexture.h" |
| 2 | + |
| 3 | +void lTexture::free() |
| 4 | +{ |
| 5 | + if (mTexture != NULL) |
| 6 | + { |
| 7 | + SDL_DestroyTexture(mTexture); |
| 8 | + mTexture=NULL; |
| 9 | + mHeight=0; |
| 10 | + mWidth=0; |
| 11 | + } |
| 12 | +} |
| 13 | +lTexture::lTexture(SDL_Renderer* Renderer) |
| 14 | +{ |
| 15 | + mRenderer=Renderer; |
| 16 | + mTexture = NULL; |
| 17 | + mWidth = 0; |
| 18 | + mHeight = 0; |
| 19 | +} |
| 20 | + |
| 21 | +lTexture::~lTexture() |
| 22 | +{ |
| 23 | + free(); |
| 24 | + mRenderer=NULL; |
| 25 | +} |
| 26 | + |
| 27 | +bool lTexture::setFont(TTF_Font* Font) |
| 28 | +{ |
| 29 | + mFont=NULL; |
| 30 | + mFont=Font; |
| 31 | + return (mFont != NULL); |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +bool lTexture::setRenderer(SDL_Renderer* Renderer) |
| 36 | +{ |
| 37 | + mRenderer=NULL; |
| 38 | + mRenderer=Renderer; |
| 39 | + return (mRenderer != NULL); |
| 40 | +} |
| 41 | +bool lTexture::loadfromFile(std::string path) |
| 42 | +{ |
| 43 | + SDL_Texture* newTexture = NULL; |
| 44 | + SDL_Surface* loadedSurface = IMG_Load(path.c_str()); |
| 45 | + if (loadedSurface == NULL) |
| 46 | + { |
| 47 | + printf("Bild konnte nicht geladen werden. IMG_Error: %s\n",IMG_GetError()); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + newTexture = SDL_CreateTextureFromSurface(mRenderer,loadedSurface); |
| 52 | + if (newTexture == NULL) |
| 53 | + { |
| 54 | + printf("Konnte keine Textur aus %s erstellen. SDL_Error: %s\n",path.c_str(),SDL_GetError()); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + mHeight = loadedSurface->h; |
| 59 | + mWidth = loadedSurface->w; |
| 60 | + } |
| 61 | + } |
| 62 | + SDL_FreeSurface(loadedSurface); |
| 63 | + mTexture=newTexture; |
| 64 | + return (mTexture != NULL); |
| 65 | +} |
| 66 | +// #ifdef _SLD_TTF_H |
| 67 | +bool lTexture::loadfromText(std::string text,SDL_Color textcolor, TTF_Font* font) |
| 68 | +{ |
| 69 | + free(); |
| 70 | + if (font != NULL) |
| 71 | + {mFont=font; |
| 72 | + } |
| 73 | + if (mFont != NULL) |
| 74 | + { |
| 75 | + SDL_Surface* textSurface= TTF_RenderText_Solid(mFont,text.c_str(),textcolor); |
| 76 | + if (textSurface==NULL) |
| 77 | + { |
| 78 | + printf("Text konnte nicht gerendert werden. TTF_Error= %s\n",TTF_GetError()); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + mTexture = SDL_CreateTextureFromSurface(mRenderer,textSurface); |
| 83 | + if (mTexture==NULL) |
| 84 | + { |
| 85 | + printf("Konnte keine Textur aus gerendertem Text erstellen. SDL_Error: %s\n",SDL_GetError()); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + mHeight=textSurface->h; |
| 90 | + mWidth=textSurface->w; |
| 91 | + } |
| 92 | + SDL_FreeSurface(textSurface); |
| 93 | + } |
| 94 | + } |
| 95 | + else |
| 96 | + {printf("Keine Schriftart angegeben!"); |
| 97 | + } |
| 98 | + return (mTexture != NULL); |
| 99 | +} |
| 100 | +// #endif |
| 101 | +lTexture::lTexture(std::string path,SDL_Renderer* Renderer) |
| 102 | +{ |
| 103 | + mRenderer=Renderer; |
| 104 | + loadfromFile(path); |
| 105 | +} |
| 106 | + |
| 107 | +int lTexture::getWidth() |
| 108 | +{ |
| 109 | + return mWidth; |
| 110 | +} |
| 111 | +int lTexture::getHeight() |
| 112 | +{ |
| 113 | + return mHeight; |
| 114 | +} |
| 115 | + |
| 116 | +void lTexture::render(SDL_Rect* space, SDL_Rect* clip,double angle,SDL_Point* center,SDL_RendererFlip flip) |
| 117 | +{ |
| 118 | + SDL_RenderCopyEx(mRenderer,mTexture,clip,space,angle,center,flip); |
| 119 | +} |
| 120 | + |
| 121 | +void lTexture::render(int x, int y, SDL_Rect* clip,double angle,SDL_Point* center,SDL_RendererFlip flip) |
| 122 | +{ |
| 123 | + SDL_Rect renderRect = {x,y,mWidth,mHeight}; |
| 124 | + if (clip != NULL) |
| 125 | + { |
| 126 | + renderRect.w=clip->w; |
| 127 | + renderRect.h=clip->h; |
| 128 | + } |
| 129 | + render(&renderRect,clip,angle,center,flip); |
| 130 | +} |
| 131 | +void lTexture::setColor(Uint8 red,Uint8 green,Uint8 blue) |
| 132 | +{ |
| 133 | + SDL_SetTextureColorMod(mTexture,red, green, blue); |
| 134 | + } |
| 135 | +void lTexture::setAlpha(Uint8 alpha) |
| 136 | +{ |
| 137 | + SDL_SetTextureAlphaMod(mTexture,alpha); |
| 138 | +} |
| 139 | + |
| 140 | +void lTexture::setBlendMode(SDL_BlendMode blending) |
| 141 | +{ |
| 142 | + SDL_SetTextureBlendMode(mTexture,blending); |
| 143 | +} |
0 commit comments