1 #include <GL/freeglut.h>
17 Texture::Texture(string filename, bool t){
20 FILE *file = fopen(filename.c_str(), "rb");
22 Game::log("Failed to Load Image " + filename + ": File not found. Using a dummy Image.", WARNING);
26 png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
28 png_infop info = png_create_info_struct(png);
30 png_init_io(png, file);
31 png_read_info(png, info);
32 width = png_get_image_width(png, info);
33 height = png_get_image_height(png, info);
34 color_type = png_get_color_type(png, info);
35 bit_depth = png_get_bit_depth(png, info);
37 png_set_strip_16(png);
38 if(color_type == PNG_COLOR_TYPE_PALETTE)
39 png_set_palette_to_rgb(png);
41 if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
42 png_set_expand_gray_1_2_4_to_8(png);
44 if(png_get_valid(png, info, PNG_INFO_tRNS))
45 png_set_tRNS_to_alpha(png);
46 if(color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_PALETTE)
47 png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
48 if(color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
49 png_set_gray_to_rgb(png);
50 png_read_update_info(png, info);
51 row_pointers = (png_bytep*)malloc(sizeof(png_bytep) *height);
52 for(int y = 0; y < height; y++) {
53 row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png,info));
55 png_read_image(png, row_pointers);
57 png_destroy_read_struct(&png, &info, NULL);
59 void Texture::draw(int displayX, int displayY, int displayWidth, int displayHeight){
61 Graphics::drawRectangle(displayX, displayY, displayWidth, displayHeight, {0, 0, 0});
64 int scaleX = (float)displayWidth/width;
65 int scaleY = (float)displayHeight/height;
66 for(int y = 0; y < height; y++) {
67 png_bytep row = row_pointers[y];
68 for(int x = 0; x < width; x++) {
69 png_bytep px = &(row[x * 4]);
70 if(!translucent || px[0] || px[1] || px[2])
71 Graphics::drawRectangle(x*scaleX + displayX, y*scaleY + displayY, scaleX, scaleY, {(float)px[0]/255, (float)px[1]/255, (float)px[2]/255});