]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/texture.cpp
Add files via upload
[dragonblocks-bedrock.git] / src / texture.cpp
1 #include <GL/freeglut.h>
2 #include <png.h>
3 #include <string>
4 #include <iostream>
5 #include <cstring>
6 #include <cassert>
7 #include <cstdio>
8
9 #include "game.h"
10 #include "graphics.h"
11 #include "texture.h"
12
13
14 using namespace std;
15
16
17 Texture::Texture(string filename, bool t){
18         translucent = t;
19         dummyimage = false;
20         FILE *file = fopen(filename.c_str(), "rb");
21         if(!file){
22                 Game::log("Failed to Load Image " + filename + ": File not found. Using a dummy Image.", WARNING);
23                 dummyimage = true;
24                 return;
25         }
26         png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
27         assert(png);
28         png_infop info = png_create_info_struct(png);
29         assert(info);
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);
36         if(bit_depth == 16)
37                 png_set_strip_16(png);
38         if(color_type == PNG_COLOR_TYPE_PALETTE)
39                 png_set_palette_to_rgb(png);
40         #ifndef _WIN32
41         if(color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
42                 png_set_expand_gray_1_2_4_to_8(png);
43         #endif
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));
54         }
55         png_read_image(png, row_pointers);
56         fclose(file);
57         png_destroy_read_struct(&png, &info, NULL);
58 }
59 void Texture::draw(int displayX, int displayY, int displayWidth, int displayHeight){
60         if(dummyimage){
61                 Graphics::drawRectangle(displayX, displayY, displayWidth, displayHeight, {0, 0, 0});
62                 return;
63         }
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});
72                 }
73         }
74 }