Thanks for the reply, how does this gluScaleImage function works?
I´m trying to make a game where I can modify the content just with txt files, like what images to load and where, where i can click, display a matrix of images and later i want to try to put some text with opengl, with i have miserable failed until now, =P
Here is the code and thank you for your attetion:
#include <windows.h>
#include "gl/glut.h"
#include <stdlib.h>
#include <fstream.h>
#include "png/png.h"
char *screen=new char[40];
char *buttons=new char[40];
char *map=new char[40];
GLuint *texture;
GLuint *texturemap;
static GLuint pngBind(char *filename, int wrapst, int minf, int magf, int alpha)
{
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
FILE *fp;
if ((fp = fopen(filename, "rb")) == NULL)
return false;
png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if (png_ptr == NULL)
{
fclose(fp);
return false;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return false;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
return false;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, sig_read);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
int height = png_get_image_height(png_ptr, info_ptr);
int width = png_get_image_width(png_ptr, info_ptr);
int row=png_get_rowbytes(png_ptr, info_ptr);
int channels=png_get_channels(png_ptr, info_ptr);
channels++;
GLubyte image[height][width][channels];
bool putalpha=false;
for (int i =0; i<height; i++)
for (int j =0; j<width; j++)
for (int k =0; k<channels; k++)
if (alpha==1)
{
if (k!=3)
{
image[i][j][k]=(GLubyte)info_ptr->row_pointers[i][(j*(channels-1))+k];
if (k==2)
if (image[i][j][k]==10 && image[i][j][k-1]==10 && image[i][j][k-2]==10)
putalpha=true;
}
else if (putalpha==true)
{
image[i][j][k]=0;
putalpha=false;
}
else
image[i][j][k]=255;
}
else
image[i][j][k]=(GLubyte)info_ptr->row_pointers[i][(j*(channels-1))+k];
if (alpha==0)
{
for (int i =0; i<height; i++)
for (int j =0; j<width; j++)
image[i][j][3]=255;
}
static GLuint texName;
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapst);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapst);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,magf);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,minf);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width,
height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
fclose(fp);
return texName;
}
void BindTexture(const char* name, int tipo)
{
ifstream file;
int temp;
char *newname=new char[40];
strcpy(newname,name);
strcat(newname,"_image.txt");
file.open(newname);
file>>temp;
if (tipo==0)
{
free(texture);
texture=new GLuint[temp];
glGenTextures(temp, texture);
}
else if (tipo==1)
{
free(texturemap);
texturemap=new GLuint[temp];
glGenTextures(temp, texturemap);
}
int alpha;
char text[40];
for (int i=0;i<temp;i++)
{
file>>alpha;
file>>text;
if (tipo==0)
texture[i]=pngBind(text,GL_CLAMP,GL_LINEAR,GL_LINEAR,alpha);
else if (tipo==1)
texturemap[i]=pngBind(text,GL_CLAMP,GL_LINEAR,GL_LINEAR,alpha);
}
file.close();
}
void DisplayMap(void)
{
ifstream file;
int x,y;
char *mapposition=new char[40];
strcpy(mapposition,map);
strcat(mapposition,"_position.txt");
file.open(mapposition);
file>>x;
file>>y;
int matrix[x][y];
for (int i=0;i<x;i++)
for (int j=0;j<y;j++)
{
file>>matrix[i][j];
}
glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_NOTEQUAL,0);
for (int i=0;i<x;i++)
for (int j=0;j<y;j++)
{
glBindTexture(GL_TEXTURE_2D, texturemap[matrix[i][j]]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f (50+32*i ,440-(32*(j+1)));
glTexCoord2f(0,1); glVertex2f (50+32*i ,440-(32*j) );
glTexCoord2f(1,1); glVertex2f (50+32*(i+1),440-(32*j) );
glTexCoord2f(1,0); glVertex2f (50+32*(i+1),440-(32*(j+1)));
glEnd();
}
glDisable(GL_ALPHA_TEST);
glDisable(GL_TEXTURE_2D);
file.close();
}
void Clear(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
}
void Update(void)
{
glutPostRedisplay();
}
void Display(void)
{
ifstream file;
int temp;
char *screenposition=new char[40];
strcpy(screenposition,screen);
strcat(screenposition,"_position.txt");
file.open(screenposition);
file>>temp;
glEnable(GL_TEXTURE_2D);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_NOTEQUAL,0);
float temp2[4];
for (int i=0;i<temp;i++)
{
glBindTexture(GL_TEXTURE_2D, texture[i]);
glBegin(GL_QUADS);
for (int k=0;k<4;k++)
{
for (int j=0;j<4;j++)
file>>temp2[j];
glTexCoord2f(temp2[0],temp2[1]); glVertex2f (temp2[2],temp2[3]);
}
glEnd();
}
glDisable(GL_ALPHA_TEST);
glDisable(GL_TEXTURE_2D);
file.close();
glutSwapBuffers();
}
void KeyPress(unsigned char key, int x, int y)
{
switch (key)
{
case 27: /*ESC*/ glutDestroyWindow(glutGetWindow()); exit(0); break;
}
}
void Mouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
ifstream file;
file.open(buttons);
int temp;
file>>temp;
for (int i=0;i<temp;i++)
{
int tipo;
file>>tipo;
int temp2[4];
for (int j=0;j<4;j++)
file>>temp2[j];
if (x>temp2[0] && x<temp2[1] && y>temp2[2] && y<temp2[3])
{
if (tipo==0)
{
free(screen);screen=new char[40];
file>>screen;
free(buttons);buttons=new char[40];
file>>buttons;
BindTexture(screen,0);
Clear();
Update();
return;
}
else if (tipo==1)
exit(0);
else if (tipo==2)
{
free(screen);screen=new char[40];
file>>screen;
free(buttons);buttons=new char[40];
file>>buttons;
free(map);map=new char[40];
file>>map;
BindTexture(screen,0);
BindTexture(map,1);
Clear();
DisplayMap();
Update();
return;
}
}
else if (tipo==0)
{
char temp3[40];
file>>temp3;
file>>temp3;
}
else if (tipo==2)
{
char temp3[40];
file>>temp3;
file>>temp3;
file>>temp3;
}
}
file.close();
}
break;
default:
break;
}
}
int main(int argc, char * argv[])
{
screen="display/menu";
buttons="mouse/menu.txt";
map=NULL;
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE);
glutGameModeString( "640x480:16@60" );
glutEnterGameMode();
gluOrtho2D(0.0,640,0.0,480);
glutDisplayFunc( Display );
glutKeyboardFunc( KeyPress );
glutMouseFunc( Mouse );
// glutIdleFunc( Update );
BindTexture(screen,0);
Clear();
glutMainLoop();
return 0;
}