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);
40 if(png_get_valid(png, info, PNG_INFO_tRNS))
41 png_set_tRNS_to_alpha(png);
42 if(color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_PALETTE)
43 png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
44 if(color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
45 png_set_gray_to_rgb(png);
46 png_read_update_info(png, info);
47 row_pointers = (png_bytep*)malloc(sizeof(png_bytep) *height);
48 for(int y = 0; y < height; y++) {
49 row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png,info));
51 png_read_image(png, row_pointers);
53 png_destroy_read_struct(&png, &info, NULL);
55 void Texture::draw(int displayX, int displayY, int displayWidth, int displayHeight){
57 Graphics::drawRectangle(displayX, displayY, displayWidth, displayHeight, {0, 0, 0});
60 int scaleX = (float)displayWidth/width;
61 int scaleY = (float)displayHeight/height;
62 for(int y = 0; y < height; y++) {
63 png_bytep row = row_pointers[y];
64 for(int x = 0; x < width; x++) {
65 png_bytep px = &(row[x * 4]);
66 if(!translucent || px[0] || px[1] || px[2])
67 Graphics::drawRectangle(x*scaleX + displayX, y*scaleY + displayY, scaleX, scaleY, {(float)px[0]/255, (float)px[1]/255, (float)px[2]/255});