199 lines
4.4 KiB
C++
199 lines
4.4 KiB
C++
#include <fstream>
|
|
#include <string.h>
|
|
#include <GL/glut.h>
|
|
|
|
#include "textures.h"
|
|
#include "archive.h"
|
|
|
|
void read_from_string(char *str, int &place, char* data, int32_t size)
|
|
{
|
|
// Iterate over the string
|
|
for(int i=0;(i<size)&&(i<place+size);i++)
|
|
{
|
|
// Set part of the c string as the data
|
|
data[i] = (char)str[i+place];
|
|
}
|
|
|
|
// Add to the place
|
|
place += size;
|
|
}
|
|
|
|
char* combineImages(char* image_1, int imagesize_1, char* image_2, int imagesize_2,
|
|
int &imageout_size)
|
|
{
|
|
// Setup the dimentions varibles
|
|
uint32_t w1, h1, w2, h2;
|
|
int it1 = 0;
|
|
int it2 = 0;
|
|
|
|
// Get the dimentions
|
|
read_from_string(image_1, it1, (char*)&w1, sizeof(w1));
|
|
read_from_string(image_1, it1, (char*)&h1, sizeof(h1));
|
|
read_from_string(image_2, it2, (char*)&w2, sizeof(w2));
|
|
read_from_string(image_2, it2, (char*)&h2, sizeof(h2));
|
|
|
|
// Setup the images varibles
|
|
char* pix1 = new char[w1*h1*4];
|
|
char* pix2 = new char[w2*h2*4];
|
|
|
|
// Get the images
|
|
read_from_string(image_1, it1, pix1, w1*h1*4);
|
|
read_from_string(image_2, it2, pix2, w2*h2*4);
|
|
|
|
// Create a texture
|
|
std::string image_R;
|
|
|
|
// Set the size
|
|
uint32_t wR = w1;
|
|
uint32_t hR = h1+h2;
|
|
|
|
// Add the size to the texture
|
|
image_R.append((char*)&wR, sizeof(wR));
|
|
image_R.append((char*)&hR, sizeof(hR));
|
|
|
|
// Loop over image 1
|
|
for(int i=0;i<w1*h1*4;i++)
|
|
{
|
|
// Add part of image 1 to the texture
|
|
image_R += pix1[i];
|
|
}
|
|
|
|
// Loop over image 2
|
|
for(int i=0;i<w2*h2*4;i++)
|
|
{
|
|
// Add part of image 2 to the texture
|
|
image_R += pix2[i];
|
|
}
|
|
|
|
// Free the pixel data
|
|
free(pix1);
|
|
free(pix2);
|
|
|
|
// Convert the string to char array
|
|
int returnsize = image_R.size();
|
|
char* returndata = new char[returnsize];
|
|
|
|
// Loop over the image
|
|
for(int i=0;i<returnsize;i++)
|
|
{
|
|
// Set part of the string to part of the c string
|
|
returndata[i] = image_R[i];
|
|
}
|
|
|
|
// Return the data
|
|
return returndata;
|
|
}
|
|
|
|
unsigned int loadPixels(char *cpixels, int width, int height, char filter, bool unbind_texture)
|
|
{
|
|
// Convert the pixels to GLubyte
|
|
GLubyte *pixels = (GLubyte*)cpixels;
|
|
|
|
// Create the texture
|
|
GLuint tex;
|
|
glGenTextures(1, &tex);
|
|
glBindTexture(GL_TEXTURE_2D, tex);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width,
|
|
height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
|
|
|
// Linear
|
|
if(filter == 'L') {
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
|
}
|
|
|
|
// Nearest
|
|
if(filter == 'N') {
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
|
|
}
|
|
|
|
// Does the user want to unbind the texture
|
|
if(unbind_texture)
|
|
{
|
|
// Unbind the texture
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
}
|
|
|
|
// Return the address
|
|
return tex;
|
|
}
|
|
|
|
char* loadImage(const char *dir, int &width, int &height)
|
|
{
|
|
// Open the file
|
|
std::ifstream file;
|
|
file.open(dir, std::ios::binary);
|
|
|
|
// Get the dimentions
|
|
int32_t w, h;
|
|
file.read((char*)&w, sizeof(w));
|
|
file.read((char*)&h, sizeof(h));
|
|
width = w;
|
|
height = h;
|
|
|
|
// Get the binary data
|
|
char *pixels = new char[width*height*4];
|
|
file.read(pixels, width*height*4);
|
|
|
|
// Close the file
|
|
file.close();
|
|
|
|
// Return the binary data
|
|
return pixels;
|
|
}
|
|
|
|
unsigned int loadImageGL(const char *dir, char filter, int &width, int &height)
|
|
{
|
|
// Get the data
|
|
char *pixels = loadImage(dir, width, height);
|
|
|
|
// Return the OpenGL texture
|
|
return loadPixels(pixels, width, height, filter);
|
|
}
|
|
|
|
unsigned int loadImageDataGL(char *data, int size, char filter, int &width, int &height)
|
|
{
|
|
// Setup the iterator
|
|
int i = 0;
|
|
|
|
// Get the width and height
|
|
int32_t w, h;
|
|
read_from_string(data, i, (char*)&w, sizeof(w));
|
|
read_from_string(data, i, (char*)&h, sizeof(h));
|
|
width = w;
|
|
height = h;
|
|
|
|
// Get the pixels
|
|
char* pixels = new char[w*h*4];
|
|
read_from_string(data, i, pixels, w*h*4);
|
|
|
|
// Load the image
|
|
int image = loadPixels(pixels, width, height, filter);
|
|
|
|
// Free the pixel data
|
|
free(pixels);
|
|
|
|
// Return the image
|
|
return image;
|
|
}
|
|
|
|
unsigned int loadArchiveImageGL(const char *dir, int filter, int &width, int &height)
|
|
{
|
|
// Get the file data position
|
|
ArchivePos apos = archiveGetPos(archive, dir);
|
|
|
|
// Get the file data
|
|
char* data = new char[apos.size];
|
|
archiveRead(archive, apos, data);
|
|
|
|
// Load the image
|
|
int image = loadImageDataGL(data, (int)apos.size, filter, width, height);
|
|
|
|
// Free the file data
|
|
free(data);
|
|
|
|
// Return the image
|
|
return image;
|
|
}
|