123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #include <png.h>
- #include "client/engine/Texture.h"
- Texture::Texture(const char* path)
- {
- if(!load(path))
- {
- loaded = true;
- cout << "cannot load texture " << path << endl;
- }
- }
- Texture::~Texture()
- {
- if(data != nullptr)
- {
- delete[] data;
- }
- if(texture != 0)
- {
- glDeleteTextures(1, &texture);
- }
- }
- bool Texture::load(const char* path)
- {
- FILE* file = fopen(path, "r");
- if(file == NULL)
- {
- cerr << "image '" << path << "' does not exist" << endl;
- return false;
- }
- bool b = load(path, file);
- fclose(file);
- if(b)
- {
- initGL();
- }
- return b;
- }
- bool Texture::load(const char* path, FILE* file)
- {
- // check signature of png
- unsigned char buffer[8];
- if(fread(buffer, sizeof(char), 8, file) != 8)
- {
- cerr << "cannot read signature of image '" << path << "'" << endl;
- return false;
- }
-
- if(png_sig_cmp(buffer, 0, 8))
- {
- cerr << "file '" << path << "' is not an image" << endl;
- return false;
- }
-
- // create structures for data
- png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if(png == NULL)
- {
- cerr << "cannot create image data structure" << endl;
- return false;
- }
-
- png_infop info = png_create_info_struct(png);
- if(info == NULL)
- {
- cerr << "cannot create image info structure" << endl;
- return false;
- }
- unsigned int** rowPointers = NULL;
-
- // set callback for errors
- if(setjmp(png_jmpbuf(png)))
- {
- if(rowPointers != NULL)
- {
- png_free(png, rowPointers);
- }
- png_destroy_read_struct(&png, &info, NULL);
- cerr << "image '" << path << "' has used error callback" << endl;
- return false;
- }
- // set reading function
- png_init_io(png, file);
- // notify about already used signature bytes
- png_set_sig_bytes(png, 8);
-
- // read info data
- png_read_info(png, info);
- width = png_get_image_width(png, info);
- height = png_get_image_height(png, info);
-
- // read image data
- data = new unsigned int[width * height];
-
- // allocate and set row pointer to correct places in block
- rowPointers = (unsigned int**) png_malloc(png, height * (sizeof(unsigned int*)));
- for(unsigned int i = 0; i < height; i++)
- {
- rowPointers[i] = (data + i * width);
- }
- png_set_rows(png, info, (png_bytepp) rowPointers);
-
- png_read_image(png, (png_bytepp) rowPointers);
-
- png_free(png, rowPointers);
- png_destroy_read_struct(&png, &info, NULL);
- return true;
- }
- void Texture::initGL()
- {
- glActiveTexture(GL_TEXTURE0);
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
-
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-
- /*for(int i = 0; i < width * height; i++)
- {
- if(data[i] != 0)
- {
- cout << "X";
- }
- else
- {
- cout << " ";
- }
-
- if(i != 0 && i % width == 0)
- {
- cout << endl;
- }
- }*/
-
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
- delete[] data;
- data = nullptr;
- }
- void Texture::bind()
- {
- glBindTexture(GL_TEXTURE_2D, texture);
- }
- bool Texture::isLoaded()
- {
- return loaded;
- }
|