Преглед изворни кода

Removed duplicated TGA writing code

Thomas Buck пре 11 година
родитељ
комит
f66275b65c
5 измењених фајлова са 33 додато и 130 уклоњено
  1. 1
    0
      ChangeLog
  2. 7
    7
      include/utils/tga.h
  3. 1
    1
      src/OpenRaider.cpp
  4. 10
    108
      src/Texture.cpp
  5. 14
    14
      src/utils/tga.cpp

+ 1
- 0
ChangeLog Прегледај датотеку

7
 
7
 
8
 	[ 20140307 ]
8
 	[ 20140307 ]
9
 	* Removed duplicated GL initialization code
9
 	* Removed duplicated GL initialization code
10
+	* Removed duplicated TGA writing code from Texture
10
 
11
 
11
 	[ 20140306 ]
12
 	[ 20140306 ]
12
 	* Created utility library
13
 	* Created utility library

+ 7
- 7
include/utils/tga.h Прегледај датотеку

46
  * \param f file to be checked
46
  * \param f file to be checked
47
  * \returns 0 if valid, else error condition
47
  * \returns 0 if valid, else error condition
48
  */
48
  */
49
-int tga_check(FILE *f);
49
+int tgaCheck(FILE *f);
50
 
50
 
51
 /*!
51
 /*!
52
  * \brief Load a TGA image from file
52
  * \brief Load a TGA image from file
57
  * \param type where the type will be stored (tga_type_t)
57
  * \param type where the type will be stored (tga_type_t)
58
  * \returns 0 on success, else error condition
58
  * \returns 0 on success, else error condition
59
  */
59
  */
60
-int tga_load(FILE *f, unsigned char **image,
60
+int tgaLoad(FILE *f, unsigned char **image,
61
                 unsigned int *width, unsigned int *height, char *type);
61
                 unsigned int *width, unsigned int *height, char *type);
62
 
62
 
63
 /*!
63
 /*!
66
  * \param image pixmap to be stored
66
  * \param image pixmap to be stored
67
  * \param width width of pixmap/image
67
  * \param width width of pixmap/image
68
  * \param height height of pixmap/image
68
  * \param height height of pixmap/image
69
- * \param type tga_type_t to use
69
+ * \param type tga type to use
70
  * \returns 0 on success, else error condition
70
  * \returns 0 on success, else error condition
71
  */
71
  */
72
-int tga_save(FILE *f, unsigned char *image,
72
+int tgaSave(FILE *f, unsigned char *image,
73
                 unsigned int width, unsigned int height, char type);
73
                 unsigned int width, unsigned int height, char type);
74
 
74
 
75
 /*!
75
 /*!
77
  * \param image pixmap to be stored
77
  * \param image pixmap to be stored
78
  * \param width width of pixmap/image
78
  * \param width width of pixmap/image
79
  * \param height height of pixmap/image
79
  * \param height height of pixmap/image
80
- * \param type tga_type_t to use
80
+ * \param type tga type to use
81
  * \param s format string for file path/name
81
  * \param s format string for file path/name
82
  * \returns 0 on success, else error condition
82
  * \returns 0 on success, else error condition
83
  */
83
  */
84
-int tga_save_filename(unsigned char *image,
84
+int tgaSaveFilename(unsigned char *image,
85
                         unsigned int width, unsigned int height,
85
                         unsigned int width, unsigned int height,
86
-                        char type, char *s, ...)
86
+                        char type, const char *s, ...)
87
     __attribute__((format(printf, 5, 6)));
87
     __attribute__((format(printf, 5, 6)));
88
 
88
 
89
 #endif
89
 #endif

+ 1
- 1
src/OpenRaider.cpp Прегледај датотеку

3218
         m_render.screenShot(sfilename);
3218
         m_render.screenShot(sfilename);
3219
         delete [] tmp;
3219
         delete [] tmp;
3220
         delete [] sfilename;
3220
         delete [] sfilename;
3221
-        print(true, "Took screenshot");
3221
+        print(false, "Took screenshot");
3222
     }
3222
     }
3223
     else if (rc_command("fullscreen", cmd))
3223
     else if (rc_command("fullscreen", cmd))
3224
     {
3224
     {

+ 10
- 108
src/Texture.cpp Прегледај датотеку

232
     if (texture)
232
     if (texture)
233
     {
233
     {
234
 #ifdef DUMP_TTF_TGA
234
 #ifdef DUMP_TTF_TGA
235
-        FILE *f = fopen("ttf_font.tga", "wb");
236
-        if (f)
237
-        {
238
-            tga_save(f, texture->texture, 256, 256, 4);
239
-            fclose(f);
240
-        }
241
-        else
242
-        {
243
-            perror("ttf_font.tga");
244
-        }
235
+        tgaSaveFilename(texture->texture, 256, 256, 4, "ttf_font.tga");
245
 #endif
236
 #endif
246
 
237
 
247
         gFontTest = generateFont(texture);
238
         gFontTest = generateFont(texture);
792
 }
783
 }
793
 
784
 
794
 
785
 
795
-void Texture::glScreenShot(char *base, unsigned int width, unsigned int height)
796
-{
786
+void Texture::glScreenShot(char *base, unsigned int width, unsigned int height) {
797
     FILE *f;
787
     FILE *f;
798
-    int sz = width*height;
799
-    unsigned char *image = new unsigned char[sz*3];
800
-    unsigned char *swap_row = new unsigned char[width*3];
788
+    int sz = width * height;
789
+    unsigned char *image = new unsigned char[sz * 3];
801
     char filename[1024];
790
     char filename[1024];
802
     static int count = 0;
791
     static int count = 0;
803
     bool done = false;
792
     bool done = false;
804
-    int i, j, size;
805
-    unsigned char comment_lenght;
806
-    unsigned char colormap_type;
807
-    unsigned char image_type;
808
-    unsigned short colormap_index;
809
-    unsigned short colormap_lenght;
810
-    unsigned char colormap_bbp;
811
-    unsigned short origin_x;
812
-    unsigned short origin_y;
813
-    unsigned short swidth;
814
-    unsigned short sheight;
815
-    char comment[32] = "Mongoose TGA 0.0.1\0";
816
-    unsigned char tmp, bpp, desc_flags;
817
-
818
 
793
 
819
-    if (!image || !width || !height)
820
-    {
794
+    if (!image || !width || !height) {
821
         if (image)
795
         if (image)
822
-        {
823
             delete [] image;
796
             delete [] image;
824
-        }
825
-        delete [] swap_row;
826
 
797
 
827
         printf("glScreenShot> ERROR: Couldn't allocate image!\n");
798
         printf("glScreenShot> ERROR: Couldn't allocate image!\n");
828
         return;
799
         return;
841
             done = true;
812
             done = true;
842
     }
813
     }
843
 
814
 
844
-    f = fopen(filename, "wb");
845
-
846
-    if (!f)
847
-    {
848
-        printf("glScreenShot> ERROR: Couldn't write screenshot.\n");
849
-        perror("glScreenShot> ERROR: ");
850
-        delete [] image;
851
-        delete [] swap_row;
852
-        return;
853
-    }
854
-
855
     // Capture frame buffer
815
     // Capture frame buffer
856
-    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
857
-
858
-    // Flip vertical
859
-    for (i = 0, j = (int)height-1; i < (int)height/2; ++i, --j)
860
-    {
861
-        memcpy(swap_row, &image[i*width*3], width*3);
862
-        memcpy(&image[i*width*3], &image[j*width*3], width*3);
863
-        memcpy(&image[j*width*3], swap_row, width*3);
864
-    }
816
+    glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, image);
865
 
817
 
866
-    delete [] swap_row;
867
-
868
-    comment_lenght = (unsigned char)strlen(comment);
869
-    colormap_type = 0;
870
-    image_type = 2;
871
-    colormap_index = 0;
872
-    colormap_lenght = 0;
873
-    colormap_bbp = 0;
874
-    origin_x = origin_y = 0;
875
-    swidth = (unsigned short)width;
876
-    sheight = (unsigned short)height;
877
-    bpp = 24;
878
-    desc_flags = 32;
879
-
880
-    // Write TGA header
881
-    fwrite(&comment_lenght, 1, 1, f);
882
-    fwrite(&colormap_type, 1, 1, f);
883
-    fwrite(&image_type, 1, 1, f);
884
-    fwrite(&colormap_index, 2, 1, f);
885
-    fwrite(&colormap_lenght, 2, 1, f);
886
-    fwrite(&colormap_bbp, 1, 1, f);
887
-    fwrite(&origin_x, 2, 1, f);
888
-    fwrite(&origin_y, 2, 1, f);
889
-    fwrite(&swidth, 2, 1, f);
890
-    fwrite(&sheight, 2, 1, f);
891
-    fwrite(&bpp, 1, 1, f);
892
-    fwrite(&desc_flags, 1, 1, f);
893
-
894
-    // Write comment
895
-    fwrite(&comment, 1, comment_lenght, f);
896
-
897
-    size = width * height * 3;
898
-
899
-    for (i = 0; i < size; i += 3)
900
-    {
901
-        tmp = image[i];
902
-        image[i] = image[i + 2];
903
-        image[i + 2] = tmp;
904
-    }
905
-
906
-    // Write image data
907
-    if (fwrite(image, size, 1, f) < 1)
908
-    {
909
-        perror("glScreenShot> Disk write failed.\n");
910
-        fclose(f);
911
-        delete [] image;
912
-        return;
913
-    }
914
-
915
-    fclose(f);
818
+    tgaSaveFilename(image, width, height, 0, "%s", filename);
819
+    printf("Took screenshot '%s'.\n", filename);
916
 
820
 
917
     delete [] image;
821
     delete [] image;
918
-
919
-    printf("Took screenshot '%s'.\n", filename);
920
 }
822
 }
921
 
823
 
922
 
824
 
936
     {
838
     {
937
         perror("Couldn't load file");
839
         perror("Couldn't load file");
938
     }
840
     }
939
-    else if (!tga_check(f))
841
+    else if (!tgaCheck(f))
940
     {
842
     {
941
-        tga_load(f, &image, &w, &h, &type);
843
+        tgaLoad(f, &image, &w, &h, &type);
942
 
844
 
943
         type += 2;
845
         type += 2;
944
 
846
 

+ 14
- 14
src/utils/tga.cpp Прегледај датотеку

14
 
14
 
15
 #include "utils/tga.h"
15
 #include "utils/tga.h"
16
 
16
 
17
-int tga_check(FILE *f) {
17
+int tgaCheck(FILE *f) {
18
     char buffer[10];
18
     char buffer[10];
19
 
19
 
20
     if (!f) {
20
     if (!f) {
21
-        perror("tga_check> Passed invalid file.\n");
21
+        perror("tgaCheck> Passed invalid file.\n");
22
         return -1;
22
         return -1;
23
     }
23
     }
24
 
24
 
30
     if (!(buffer[1] == 0 && (buffer[2] == TGA_TYPE__COLOR ||
30
     if (!(buffer[1] == 0 && (buffer[2] == TGA_TYPE__COLOR ||
31
                     //buffer[2] == TGA_TYPE__GREYSCALE ||
31
                     //buffer[2] == TGA_TYPE__GREYSCALE ||
32
                     buffer[2] == TGA_TYPE__COLOR_RLE))) {
32
                     buffer[2] == TGA_TYPE__COLOR_RLE))) {
33
-        printf("tga_check> Inavlid or unknown TGA format.\n");
33
+        printf("tgaCheck> Inavlid or unknown TGA format.\n");
34
         return -2;
34
         return -2;
35
     }
35
     }
36
     return 0;
36
     return 0;
37
 }
37
 }
38
 
38
 
39
-int tga_load(FILE *f, unsigned char **image, unsigned int *width, unsigned int *height, char *type) {
39
+int tgaLoad(FILE *f, unsigned char **image, unsigned int *width, unsigned int *height, char *type) {
40
     tga_t header;
40
     tga_t header;
41
     char comment[256];
41
     char comment[256];
42
     unsigned char pixel[4];
42
     unsigned char pixel[4];
47
     unsigned int i, j;
47
     unsigned int i, j;
48
 
48
 
49
     if (!f) {
49
     if (!f) {
50
-        fprintf(stderr, "tga_load> Invalid parameters.\n");
50
+        fprintf(stderr, "tgaLoad> Invalid parameters.\n");
51
         return -1;
51
         return -1;
52
     }
52
     }
53
 
53
 
122
     size = header.width * header.height;
122
     size = header.width * header.height;
123
 
123
 
124
     if (!size || (!(header.colormap_type == 0 && (header.image_type == 2 || header.image_type == 10)))) {
124
     if (!size || (!(header.colormap_type == 0 && (header.image_type == 2 || header.image_type == 10)))) {
125
-        fprintf(stderr, "tga_load> Unknown image format.\n");
125
+        fprintf(stderr, "tgaLoad> Unknown image format.\n");
126
         return -2;
126
         return -2;
127
     }
127
     }
128
 
128
 
162
                     break;
162
                     break;
163
                 case TGA_TYPE__COLOR:
163
                 case TGA_TYPE__COLOR:
164
                     if (fread((*image), size, 1, f) < 1) {
164
                     if (fread((*image), size, 1, f) < 1) {
165
-                        fprintf(stderr, "tga_load> Image fread failed.\n");
165
+                        fprintf(stderr, "tgaLoad> Image fread failed.\n");
166
                         delete [] *image;
166
                         delete [] *image;
167
                         return -4;
167
                         return -4;
168
                     }
168
                     }
214
                     break;
214
                     break;
215
                 case TGA_TYPE__COLOR:
215
                 case TGA_TYPE__COLOR:
216
                     if (fread((*image), size, 1, f) < 1) {
216
                     if (fread((*image), size, 1, f) < 1) {
217
-                        fprintf(stderr, "tga_load> Image fread failed.\n");
217
+                        fprintf(stderr, "tgaLoad> Image fread failed.\n");
218
                         delete [] *image;
218
                         delete [] *image;
219
                         return -4;
219
                         return -4;
220
                     }
220
                     }
239
             }
239
             }
240
             break;
240
             break;
241
         case 8:
241
         case 8:
242
-            printf("tga_load> 8bpp Not implemented\n");
242
+            printf("tgaLoad> 8bpp Not implemented\n");
243
             break;
243
             break;
244
         default:
244
         default:
245
             ;
245
             ;
257
     return 0;
257
     return 0;
258
 }
258
 }
259
 
259
 
260
-int tga_save(FILE *f, unsigned char *image, unsigned int width, unsigned int height, char type) {
260
+int tgaSave(FILE *f, unsigned char *image, unsigned int width, unsigned int height, char type) {
261
     tga_t header;
261
     tga_t header;
262
     unsigned int size;
262
     unsigned int size;
263
     char comment[64];
263
     char comment[64];
265
     //unsigned char tmp;
265
     //unsigned char tmp;
266
 
266
 
267
     if (!f || !image || !width || !height) {
267
     if (!f || !image || !width || !height) {
268
-        fprintf(stderr, "tga_save> Invalid parameters.\n");
268
+        fprintf(stderr, "tgaSave> Invalid parameters.\n");
269
         return -1;
269
         return -1;
270
     }
270
     }
271
 
271
 
350
 
350
 
351
     // Write image data
351
     // Write image data
352
     if (fwrite(image, size, 1, f) < 1) {
352
     if (fwrite(image, size, 1, f) < 1) {
353
-        perror("tga_save> Disk write failed.\n");
353
+        perror("tgaSave> Disk write failed.\n");
354
         return -2;
354
         return -2;
355
     }
355
     }
356
     return 0;
356
     return 0;
357
 }
357
 }
358
 
358
 
359
-int tga_save_filename(unsigned char *image, unsigned int width, unsigned int height, char type, char *s, ...) {
359
+int tgaSaveFilename(unsigned char *image, unsigned int width, unsigned int height, char type, const char *s, ...) {
360
     char buffer[1024];
360
     char buffer[1024];
361
     FILE *f;
361
     FILE *f;
362
     int v;
362
     int v;
369
         perror(buffer);
369
         perror(buffer);
370
         return -1;
370
         return -1;
371
     }
371
     }
372
-    v = tga_save(f, image, width, height, type);
372
+    v = tgaSave(f, image, width, height, type);
373
     fclose(f);
373
     fclose(f);
374
     return v;
374
     return v;
375
 }
375
 }

Loading…
Откажи
Сачувај