PDA

View Full Version : Swapping between textures on a sprite sheet issue (C++)



PocketTNT
05-12-2016, 05:21 AM
I'm working on a project where I want to swap between two textures on a sprite sheet using OpenGL. I have both textures rendering in my window individually with no issues but I can't get them to switch with the key press I have set. Could someone tell me what I'm doing wrong/missing to get it to swap?


#include "LUtil.h"
#include <IL/il.h>
#include <IL/ilu.h>
#include "LTexture.h"

int gViewportMode = VIEWPORT_MODE_C;

//Sprite texture
LTexture gArrowTexture;

//Sprite area
LFRect gArrowClips[ 4 ];

bool initGL()
{
//Set the viewport
glViewport( 0.f, 0.f, SCREEN_WIDTH, SCREEN_HEIGHT );

//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, 1.0, -1.0 );

//Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

//Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f );

//Enable texturing
glEnable( GL_TEXTURE_2D );

//Check for error
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString(error);
return false;
}

//Initialize DevIL
ilInit();
ilClearColour( 255, 255, 255, 000 );

//Check for error
ILenum ilError = ilGetError();
if( ilError != IL_NO_ERROR )
{
printf( "Error initializing DevIL! %s\n", iluErrorString(ilError) );
return false;
}

return true;
}

bool loadMedia()
{
//Set clip rectangles
if ( gViewportMode == VIEWPORT_MODE_C) {
gArrowClips[ 0 ].x = 0.f;
gArrowClips[ 0 ].y = 0.f;
gArrowClips[ 0 ].w = 330.f;
gArrowClips[ 0 ].h = 355.f;

gArrowClips[ 1 ].x = 330.f;
gArrowClips[ 1 ].y = 0.f;
gArrowClips[ 1 ].w = 310.f;
gArrowClips[ 1 ].h = 480.f;

gArrowClips[ 2 ].x = 0.f;
gArrowClips[ 2 ].y = 355.f;
gArrowClips[ 2 ].w = 330.f;
gArrowClips[ 2 ].h = 125.f;
}
else if ( gViewportMode == VIEWPORT_MODE_N) {
gArrowClips[ 0 ].x = 0.f;
gArrowClips[ 0 ].y = 480.f;
gArrowClips[ 0 ].w = 330.f;
gArrowClips[ 0 ].h = 355.f;

gArrowClips[ 1 ].x = 330.f;
gArrowClips[ 1 ].y = 480.f;
gArrowClips[ 1 ].w = 310.f;
gArrowClips[ 1 ].h = 480.f;

gArrowClips[ 2 ].x = 0.f;
gArrowClips[ 2 ].y = 835.f;
gArrowClips[ 2 ].w = 330.f;
gArrowClips[ 2 ].h = 125.f;
}
//Load texture
if( !gArrowTexture.loadTextureFromFile( "SpriteSheet.png" ) )
{
printf( "Unable to load texture!\n" );
return false;
}
return true;
}

void update()
{

}

void render()
{
//Clear color buffer
glClear( GL_COLOR_BUFFER_BIT );

//Render arrows
gArrowTexture.render( 0.f, 0.f, &gArrowClips[ 0 ] );
gArrowTexture.render( SCREEN_WIDTH - gArrowClips[ 1 ].w, 0.f, &gArrowClips[ 1 ] );
gArrowTexture.render( 0.f, SCREEN_HEIGHT - gArrowClips[ 2 ].h, &gArrowClips[ 2 ] );




//Update screen
glutSwapBuffers();

}
void handleKeys( unsigned char key, int x, int y )
{
//If the user presses q
if( key == 'q' )
{
gViewportMode++;
if ( gViewportMode > VIEWPORT_MODE_N )
{
gViewportMode == VIEWPORT_MODE_C;
}
}
}
Like I said earlier I am able to get the different textures to render by swapping VIEWPORT_MODE_C and VIEWPORT_MODE_N at int gViewPortMode just under my includes.

My issue is that I cannot get them to swap using the key command that switches the viewport so I have to stop the program, change the code, then start it again to change the textures.

Does switching the viewport just not work for this?
If yes, is there an alternative?
If no, what do I need to fix/change?

Hermannicus
05-13-2016, 02:04 PM
Didn't check your whole code, but should this gViewportMode == VIEWPORT_MODE_C; not be gViewportMode = VIEWPORT_MODE_C;?

PocketTNT
05-14-2016, 07:23 AM
Didn't check your whole code, but should this gViewportMode == VIEWPORT_MODE_C; not be gViewportMode = VIEWPORT_MODE_C;?

Yeah, I fixed that and ny issue entirely yesterday… Thank you for answering though.