]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/texture.cpp
Some structure Changes
[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         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));
50         }
51         png_read_image(png, row_pointers);
52         fclose(file);
53         png_destroy_read_struct(&png, &info, NULL);
54 }
55 void Texture::draw(int displayX, int displayY, int displayWidth, int displayHeight){
56         if(dummyimage){
57                 Graphics::drawRectangle(displayX, displayY, displayWidth, displayHeight, {0, 0, 0});
58                 return;
59         }
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});
68                 }
69         }
70 }