1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
SDL_Texture* TextureClass::CreateTexture_TGA (char* filename, SDL_Renderer* Renderer)
{
SDL_Texture* Texture = nullptr;
SDL_Surface* Surface = nullptr;
int error, width, height, bpp, imageSize;
FILE* filePtr;
unsigned int count;
TargaHeader targaFileHeader;
unsigned char* targaImage;
error = fopen_s(&filePtr, filename, "rb");
if(error != 0)
{
return nullptr;
}
count = fread(&targaFileHeader, sizeof(TargaHeader), 1, filePtr);
if(count != 1)
{
return nullptr;
}
width = (int)targaFileHeader.width;
height = (int)targaFileHeader.height;
bpp = (int)targaFileHeader.bpp;
if(bpp != 32)
{
return nullptr;
}
if (bpp == 32)
{
imageSize = width * height * 4;
}
targaImage = new unsigned char[imageSize];
if(!targaImage)
{
return nullptr;
}
count = fread(targaImage, 1, imageSize, filePtr);
if(count != imageSize)
{
return nullptr;
}
error = fclose(filePtr);
if(error != 0)
{
return nullptr;
}
Surface = SDL_CreateRGBSurfaceFrom (targaImage, width, height, 32, 0, 0, 0, 0, 0);
Texture = SDL_CreateTextureFromSurface (Renderer ,Surface);
SDL_FreeSurface (Surface);
return Texture;
}
|