Skip to content

Commit 686fa3f

Browse files
author
Finn Krein
committed
adjustet .gitignore added Texture wrapper class
1 parent 96a88ca commit 686fa3f

File tree

4 files changed

+192
-2
lines changed

4 files changed

+192
-2
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@
2727
*.exe
2828
*.out
2929
*.app
30+
31+
# vim buffers
32+
*.swp
33+
*.un~
34+
*.h~
35+
*.cc~

bezier.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
#ifdef BEZIER
1+
#ifndef BEZIER
22
#define BEZIER
33

44
//awesome comment
55

66
#include <SDL2/SDL.h>
77
#include <cmath>
88
#include <iostream>
9+
class bezierpath
10+
{
11+
public:
12+
int get_nodecount();
13+
private:
14+
int nodecount;
915

10-
16+
};
1117

1218

1319

lTexture.cc

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
}

lTexture.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef lTEXTURE
2+
#define lTEXTURE
3+
#include <SDL2/SDL.h>
4+
#include <SDL2/SDL_image.h>
5+
#include <stdio.h>
6+
#include <string>
7+
#include <SDL2/SDL_ttf.h>
8+
class lTexture
9+
{
10+
public:
11+
lTexture(SDL_Renderer* Renderer=NULL);
12+
lTexture(std::string path,SDL_Renderer* Renderer=NULL); //Initialization constructor
13+
~lTexture();
14+
bool setRenderer(SDL_Renderer* Renderer);
15+
bool loadfromFile(std::string path);
16+
// #ifdef _SDL_TTF_H
17+
bool loadfromText(std::string text,SDL_Color textcolor = {0,0,0},TTF_Font* Font=NULL);
18+
// #endif
19+
void free();
20+
int getHeight();
21+
int getWidth();
22+
void render(int x, int y, SDL_Rect* clip=NULL,double angle=0,SDL_Point* center=NULL,SDL_RendererFlip flip=SDL_FLIP_NONE);
23+
void render(SDL_Rect* space, SDL_Rect* clip=NULL,double angle=0,SDL_Point* center=NULL,SDL_RendererFlip flip=SDL_FLIP_NONE);
24+
bool setFont(TTF_Font* Font);
25+
void setColor(Uint8 red,Uint8 green,Uint8 blue);
26+
void setAlpha(Uint8 alpha);
27+
void setBlendMode(SDL_BlendMode blending);
28+
private:
29+
SDL_Renderer* mRenderer;
30+
int mWidth;
31+
int mHeight;
32+
TTF_Font* mFont;
33+
SDL_Texture* mTexture;
34+
};
35+
#endif

0 commit comments

Comments
 (0)