码迷,mamicode.com
首页 > 其他好文 > 详细

LoadTGA

时间:2015-02-05 17:39:44      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:

LoadTGA.h

 1 #include <windows.h>
 2 
 3 #pragma pack(push, 1)
 4 
 5 typedef struct Header_s
 6 {
 7     unsigned char        descLen;
 8     unsigned char        cmapType;
 9     unsigned char        imageType;
10     short                campStart;
11     unsigned short        cmapEntries;
12     unsigned char        cmapBits;
13     unsigned short        xOffset;
14     unsigned short        yOffset;
15 
16 }Header_t;
17 
18 typedef struct TGAHeader_s
19 {
20     Header_t            head;
21     unsigned short        width;
22     unsigned short        height;
23     unsigned char        bpp;
24     unsigned char        attrib;
25 
26 }TGAHeader_t;
27 
28 typedef struct TextureImage_s
29 {
30     unsigned char    *imageData;
31     unsigned int    width;
32     unsigned int    height;
33     unsigned int    bpp;
34     unsigned int    texId;
35     unsigned int    imageType;
36     bool            bCompressed;
37 
38     TextureImage_s():imageData(0){}
39 
40 }TextureImage_t;
41 
42 #pragma pack(pop)
43 
44 class TGAImage
45 {
46 public:
47     TGAImage(void);
48     ~TGAImage(void);
49 
50 public:
51     bool    LoadTGA(TextureImage_t *tex, char *fileName);
52 
53 protected:
54     bool    LoadUncompressedTGA(TextureImage_t *tex, HANDLE file);
55     bool    LoadCompressedTGA(TextureImage_t *tex, HANDLE file);
56 };

LoadTGA.cpp

  1 #include "LoadTGA.h"
  2 
  3 TGAImage::TGAImage(void)
  4 {
  5 }
  6 
  7 TGAImage::~TGAImage(void)
  8 {
  9 }
 10 
 11 bool TGAImage::LoadTGA(TextureImage_t *tex, char *fileName)
 12 {
 13     Header_t uTGAcompare = { 0, 0,  2, 0, 0, 0, 0, 0 };
 14     Header_t cTGAcompare = { 0, 0, 10, 0, 0, 0, 0, 0 };
 15 
 16     TGAHeader_t header;
 17 
 18     HANDLE file = CreateFile(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
 19 
 20     if(file == INVALID_HANDLE_VALUE)
 21     {
 22         return FALSE;
 23     }
 24 
 25     DWORD dwRead;
 26 
 27     ReadFile(file, &header, sizeof(TGAHeader_t), &dwRead, NULL);
 28 
 29     if(dwRead != sizeof(TGAHeader_t))
 30     {
 31         CloseHandle(file);
 32         return FALSE;
 33     }
 34 
 35     tex->width = header.width;
 36     tex->height = header.height;
 37     tex->bpp = header.bpp;
 38 
 39     if(tex->bpp != 32)
 40     {
 41         CloseHandle(file);
 42         return FALSE;
 43     }
 44 
 45     if(!memcmp(&uTGAcompare, &header.head, sizeof(header.head)))
 46     {
 47         tex->bCompressed = FALSE;
 48 
 49         if(!LoadUncompressedTGA(tex, file))
 50         {
 51             CloseHandle(file);
 52             return FALSE;
 53         }
 54     }
 55     else if(!memcmp(&cTGAcompare, &header.head, sizeof(header.head)))
 56     {
 57         tex->bCompressed = TRUE;
 58 
 59         if(!LoadCompressedTGA(tex, file))
 60         {
 61             CloseHandle(file);
 62             return FALSE;
 63         }
 64     }
 65 
 66     return TRUE;
 67 }
 68 
 69 bool TGAImage::LoadUncompressedTGA(TextureImage_t *tex, HANDLE file)
 70 {
 71     unsigned int bytePerPixel = tex->bpp / 8;
 72     unsigned int imgSize = tex->width * tex->height * bytePerPixel;
 73 
 74     tex->imageData = new unsigned char [ imgSize ];
 75 
 76     DWORD dwRead;
 77 
 78     ReadFile(file, tex->imageData, imgSize, &dwRead, NULL);
 79 
 80     if(dwRead != imgSize)
 81     {
 82         return FALSE;
 83     }
 84 
 85     for(int i = 0; i < (int)imgSize; i += bytePerPixel)
 86     {
 87         tex->imageData[i] ^= tex->imageData[i + 2] ^= tex->imageData[i] ^= tex->imageData[i + 2];
 88     }
 89 
 90     CloseHandle(file);
 91     return TRUE;
 92 }
 93 
 94 bool TGAImage::LoadCompressedTGA(TextureImage_t *tex, HANDLE file)
 95 {
 96     unsigned int bytePerPixel = tex->bpp / 8;
 97     unsigned int imgSize = tex->width * tex->height * bytePerPixel;
 98 
 99     tex->imageData = new unsigned char [ imgSize ];
100 
101     unsigned int pixelcount = tex->width * tex->height;
102     unsigned int currentPixel = 0;
103     unsigned int currentByte = 0;
104     unsigned char *colorbuffer = (unsigned char *)malloc(bytePerPixel);
105 
106     do
107     {
108         unsigned char chunkHeader = 0;
109         DWORD dwRead = 0;
110 
111         ReadFile(file, &chunkHeader, sizeof(unsigned char), &dwRead, NULL);
112 
113         if(chunkHeader < 128)
114         {
115             chunkHeader++;
116 
117             for(int i = 0; i < chunkHeader; ++i)
118             {
119                 ReadFile(file, colorbuffer, sizeof(bytePerPixel), &dwRead, NULL);
120 
121                 tex->imageData[currentByte + 0] = colorbuffer[2];
122                 tex->imageData[currentByte + 1] = colorbuffer[1];
123                 tex->imageData[currentByte + 2] = colorbuffer[0];
124 
125                 if(bytePerPixel == 4)
126                 {
127                     tex->imageData[currentByte + 3] = colorbuffer[3];
128                 }
129 
130                 currentPixel++;
131                 currentByte += bytePerPixel;
132             }
133         }
134         else
135         {
136             chunkHeader -= 127;
137 
138             ReadFile(file, colorbuffer, sizeof(bytePerPixel), &dwRead, NULL);
139 
140             for(int i = 0; i < chunkHeader; ++i)
141             {
142                 tex->imageData[currentByte + 0] = colorbuffer[2];
143                 tex->imageData[currentByte + 1] = colorbuffer[1];
144                 tex->imageData[currentByte + 2] = colorbuffer[0];
145 
146                 if(bytePerPixel == 4)
147                 {
148                     tex->imageData[currentByte + 3] = colorbuffer[3];
149                 }
150 
151                 currentPixel++;
152                 currentByte += bytePerPixel;
153             }
154         }
155 
156     }
157     while(currentPixel < pixelcount);
158 
159     free(colorbuffer);
160     CloseHandle(file);
161     return TRUE;
162 }

 exportfuncs.cpp

 1 #include <metahook.h>
 2 #include "Surface.h"
 3 #include "DrawTGA.h"
 4 #include <triangleapi.h>
 5 #include "LoadTGA.h"
 6 
 7 cl_enginefunc_t gEngfuncs;
 8 TGAImage TGALoad;
 9 
10 
11 int Initialize(struct cl_enginefuncs_s *pEnginefuncs, int iVersion)
12 {
13     memcpy(&gEngfuncs, pEnginefuncs, sizeof(gEngfuncs));
14     return gExportfuncs.Initialize(pEnginefuncs, iVersion);
15 }
16 
17 int HUD_Init(void)
18 {
19     return gExportfuncs.HUD_Init();
20 }
21 
22 int HUD_VidInit(void)
23 {
24     if(g_pSurface == NULL)
25     {
26         CreateInterfaceFn fnEngineCreateInterface = g_pMetaHookAPI->GetEngineFactory();
27         vgui::ISurface *pSurface = (vgui::ISurface *)fnEngineCreateInterface(VGUI_SURFACE_INTERFACE_VERSION, NULL);
28         Surface_InstallHook(pSurface);
29     }
30 
31     /*g_TextureId = g_pSurface->CreateNewTextureID();
32     g_pSurface->DrawSetTextureFile(g_TextureId, "TEST", true, true);*/
33 
34     TextureImage_t TGA;
35     TGALoad.LoadTGA(&TGA, "cstrike/TEST1.tga");
36 
37     g_TextureId = g_pSurface->CreateNewTextureID();
38     g_pSurface->DrawSetTextureRGBA(g_TextureId, TGA.imageData, TGA.width, TGA.height, true, true);
39 
40     return gExportfuncs.HUD_VidInit();
41 }
42 
43 int HUD_Redraw(float time, int intermission)
44 {
45     if(g_TextureId != NULL)
46     {
47         gEngfuncs.pTriAPI->RenderMode(kRenderTransTexture);
48         g_pSurface->DrawSetColor(255, 255, 255, 255);
49         g_pSurface->DrawSetTexture(g_TextureId);
50         g_pSurface->DrawTexturedRect(115, 115, 512, 512);
51     }
52 
53     return gExportfuncs.HUD_Redraw(time, intermission);
54 }

 

LoadTGA

标签:

原文地址:http://www.cnblogs.com/crsky/p/4275124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!