Hi i am having problems with my assignment. i am supposed to make a 2d top down view game and i started on doing the main menu. i tried at making a main menu and it worked. after that i changed the codes and put them into classes and this is where the problem starts to come. the textures dont seem so appear on my output window. when compiling there is no error at all. just a black blank screen. i have checked my textures, they worked and they were loaded properly. i have also checked all my header files to see if they were called into main.cpp. last bu not least i have also checked the camera position. its in the correct position to view the mainmenu.
this is my mainmenu.h
#include "myengine.h"
class Mainmenu {
private:
//Boolean for clicking start button
bool Start;
//Boolean for clicking end button
bool End;
//Boolean for mouseover at certain button
bool AtButton[2];
GLfloat color;
public:
Mainmenu();
void Update(float);
void Buttons(void);
void MouseClick(int);
void MouseoverButton(int, int, float, float);
void StartPage(void);
void Render(void);
bool GetStart(void);
bool GetEnd(void);
};
this is my mainmenu.cpp
#include "mainmenu.h"
#include "enum.h"
#include <iostream>
using namespace std;
extern TextureImage textures[20];
Mainmenu::Mainmenu(void){
Start = false;
End = false;
color = 1.0f;
AtButton[ StartButton ] = false;
AtButton[ QuitButton ] = false;
}
void Mainmenu::Buttons(void)
{
//Start Button
GLfloat size = 0.2f;
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glPushMatrix();
if( AtButton[ StartButton ] )
{
glColor4f( 0.0f, 1.0f, 1.0f, color );
}
else
{
glColor4f( 1.0f, 1.0f, 1.0f, color );
}
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, textures[ 1 ].id );
glTranslatef( 0.0f, -0.5f, 0.0f );
glBegin(GL_QUADS);
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( size, size, 0.0f );
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -size, size, 0.0f );
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -size, 0.0f, 0.0f );
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( size, 0.0f, 0.0f );
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
//Quit Button
glPushMatrix();
if( AtButton[ QuitButton ] )
{
glColor4f( 0.0f, 1.0f, 1.0f, color );
}
else
{
glColor4f( 1.0f, 1.0f, 1.0f, color );
}
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, textures[ 3 ].id );
glTranslatef( 0.0f, -0.75f, 0.0f );
glBegin(GL_QUADS);
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( size, size, 0.0f );
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -size, size, 0.0f );
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -size, 0.0f, 0.0f );
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( size, 0.0f, 0.0f );
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glColor3f( 1.0f, 1.0f, 1.0f );
glDisable(GL_BLEND);
}
void Mainmenu::MouseClick(int state)
{
if( state == GLUT_DOWN )
{
if( AtButton[ StartButton ] == true && Start == false && End != true )
{
Start = true;
}
if( AtButton[ QuitButton ] == true && Start == false )
{
End = true;
}
}
}
void Mainmenu::MouseoverButton(int MouseX, int MouseY, float Window_Width, float Window_Height)
{
//Checking if MouseOver both buttons
if( MouseX >= 320*(Window_Width/800) && MouseX <= 480*(Window_Width/800) && MouseY >= 390*(Window_Height/600) && MouseY <= 450*(Window_Height/600))
{
AtButton[ StartButton ] = true;
}
else if( MouseX >= 320*(Window_Width/800) && MouseX <= 480*(Window_Width/800) && MouseY >= 460*(Window_Height/600) && MouseY <= 520*(Window_Height/600) )
{
AtButton[ QuitButton ] = true;
}
else
{
AtButton[ StartButton ] = false;
AtButton[ QuitButton ] = false;
}
}
void Mainmenu::Update(float frameDeltaTime)
{
// Menu fade away when Start is pressed
if( Start == true ){
color -= frameDeltaTime;
if( color <= 0 ){
color = 0;
}
}
if( End == true){
glutLeaveMainLoop();
}
}
void Mainmenu::StartPage(void)
{
//Background
glPushMatrix();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBindTexture( GL_TEXTURE_2D, textures[ 0 ].id );
glColor4f( 1.0f, 1.0f, 1.0f, color );
glBegin(GL_QUADS);
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( 1.0f, 1.0f, -0.1f );
glTexCoord2f( 1.0f, 1.0f ); glVertex3f(-1.0f, 1.0f, -0.1f );
glTexCoord2f( 1.0f, 0.0f ); glVertex3f(-1.0f,-1.0f, -0.1f );
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 1.0f,-1.0f, -0.1f );
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
Buttons();
}
void Mainmenu::Render(void){
StartPage();
}
bool Mainmenu::GetStart(void){
return Start;
}
bool Mainmenu::GetEnd(void){
return End;
}
my texture loading function is in myengine.h and myengine.cpp respectively
////////////////////////////////////////////////////////////////////////////////
// Program : My Engine
// File : myengine.h
////////////////////////////////////////////////////////////////////////////////
#ifndef MYENGINE_H
#define MYENGINE_H
#include <string>
#include <time.h>
#include "freeglut.h"
#include <iostream>
using namespace std;
typedef struct
{
GLubyte *imageData; // image data
GLuint bpp; // image color depth ( bits per pixel )
GLuint width; // image width
GLuint height; // image height
GLuint id; // texture id
} TextureImage;
// only for uncompressed file
void LoadTGA( TextureImage *texture, char *filename, bool clamp ) ;
#endif
#include "myengine.h"
void LoadTGA( TextureImage *texture, char *filename, bool clamp ) // load TGA file to memory
{
GLubyte TGAheader[ 12 ] = {0,0,2,0,0,0,0,0,0,0,0,0}; // uncompressed TGA header
GLubyte TGAcompare[ 12 ]; // for comparing TGA header
GLubyte header[ 6 ]; // first 6 useful header bytes
GLuint bytesPerPixel; // number of bytes per pixel in TGA gile
GLuint imageSize; // for setting memory
GLuint temp; // temporary variable
GLuint type=GL_RGBA; // default GL mode (32 bits per pixel)
FILE *file;
fopen_s(&file, filename, "rb" ); // open TGA file
if( file == NULL || // check file exist
fread( TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare ) || // are there 12 bytes to read?
memcmp( TGAheader,TGAcompare,sizeof(TGAheader) )!=0 || // does header match what we want
fread(header,1,sizeof(header),file)!=sizeof(header)) // if so read next 6 header bytes
{
if (file != NULL) // check file exist
fclose(file); // close file on failure
cout << "Error : Loading file '" << filename << "' !" << endl;
return;
}
texture->width = header[1] * 256 + header[0]; // determine TGA width (highbyte*256+lowbyte)
texture->height = header[3] * 256 + header[2]; // determine TGA height (highbyte*256+lowbyte)
if( texture->width <=0 || // is width <= 0
texture->height <=0 || // is height <=0
(header[4]!=24 && header[4]!=32)) // is TGA 24 or 32 Bit
{
fclose(file); // close file on failure
cout << "Error : Loading file '" << filename << "' !" << endl;
return;
}
texture->bpp = header[4]; // grab TGA's bits per pixel (24 or 32)
bytesPerPixel = texture->bpp/8; //divide by 8 to get bytes per pixel
imageSize = texture->width*texture->height*bytesPerPixel; // calculate memory required for TGA data
texture->imageData=(GLubyte *)malloc(imageSize); // reserve memory to hold TGA data
if( texture->imageData==NULL || // does storage memory exist?
fread(texture->imageData, 1, imageSize, file)!=imageSize) // does image size match memory reserved?
{
if(texture->imageData!=NULL) // check image data loaded
free(texture->imageData); // if yes, release image data
fclose(file); // close file
cout << "Error : Loading file '" << filename << "' !" << endl;
return;
}
for(int i=0; i<int(imageSize); i+=bytesPerPixel) // loop through image data
{ // swaps 1st and 3rd bytes ('R'ed and 'B'lue)
temp=texture->imageData[i]; // temporarily store value at image data 'i'
texture->imageData[i] = texture->imageData[i + 2]; // set 1st byte to value of 3rd byte
texture->imageData[i + 2] = temp; // set 3rd byte to value in 'temp' (1st byte value)
}
fclose (file); // close file
// Build A Texture From The Data
glGenTextures(1, &texture->id); // generate texture id
glBindTexture(GL_TEXTURE_2D, texture->id);
int channels;
if (type==GL_RGB) channels = 3;
else channels = 4;
if (texture[0].bpp==24) // was TGA 24 bits
type=GL_RGB; // if yes set 'type' to GL_RGB
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
if (clamp)
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, true ? ((GLenum)0x812F) : GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, true ? ((GLenum)0x812F) : GL_REPEAT );
} else
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
}
// build Mipmaps for various distances
gluBuild2DMipmaps(GL_TEXTURE_2D, channels, texture->width, texture->height, type, GL_UNSIGNED_BYTE, texture->imageData);
}
finally this is my main.cpp
#include<iostream>
#include <math.h>
#include "freeglut.h"
#include "irrklang/irrKlang.h"
#include "SCamera.h"
#include "sound.h"
#include "mainmenu.h"
#include "myengine.h"
using namespace std;
using namespace irrklang;
#pragma comment(lib, "irrKlang.lib"); // link with irrKlang.dll
sound *Sound;
Mainmenu *menu;
TextureImage textures[10];
SCamera camera;
SMouse mouse;
bool keys[256] = { false };
bool full_screen;
float timediff;
float timeprec;
float timeex = glutGet(GLUT_ELAPSED_TIME);
float Window_Width, Window_Height;
int MouseX, MouseY;
void InitTexture (void) //initialise all the textures used
{
LoadTGA (&textures[0], "images/zombie.tga", false);
LoadTGA (&textures[1], "images/start_button.tga", false);
LoadTGA (&textures[2], "images/quit_button.tga", false);
LoadTGA (&textures[3], "images/help_button.tga", false);
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0); // set background colour to black
glShadeModel(GL_SMOOTH);
InitTexture();
}
void KeyDown( unsigned char key, int x, int y ){
keys[key] = true;
switch (key)
{
case 27:
camera.escape();
break;
}
}
void KeyUp( unsigned char key, int x, int y )
{
keys[key] = false;
}
void update_keyboard (void)
{
}
void ProcessMouseMove( int x, int y )
{
MouseX = x;
MouseY = y;
}
void Resize( int width, int height )
{
Window_Width = width;
Window_Height = height;
//glViewport ( 0, 0, width, height );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ();
}
void ProcessMouseClick( int button, int state, int x, int y ){
switch( button ){
case GLUT_LEFT_BUTTON :
if( menu->GetStart() != true) {
menu->MouseClick(state);
}
break;
case GLUT_RIGHT_BUTTON : break;
case GLUT_MIDDLE_BUTTON : break;
}
}
void UpdateScene (void)
{
timeex = glutGet(GLUT_ELAPSED_TIME);
timediff=float (timeex - timeprec);
timeprec=timeex;
menu->Update(timediff);
}
void RenderScene (void)
{
//Sound->Play2DSound("sounds/bgm.mp3", true);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearDepth( 1.0 );
glEnable(GL_TEXTURE_2D);
glDepthFunc( GL_LESS );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (camera.mEyeX, camera.mEyeY, camera.mEyeZ,
camera.mEyeX + camera.mLookX, camera.mEyeY + camera.mLookY, camera.mEyeZ + 0, 0 , 1, 0);
UpdateScene();
//cout << MouseX << endl;
//cout << MouseY << endl;
menu->MouseoverButton(MouseX, MouseY, Window_Width, Window_Height);
menu->Render();
glutSwapBuffers ();
glutPostRedisplay ();
}
void InitApp()
{
full_screen = false;
menu = new Mainmenu();
for( int i = 0; i < 256; i++ ){
keys[i] = false;
}
}
void DeInitApp()
{
delete Sound;
delete menu;
}
void main (int argc, char * argv[])
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (800, 600);
glutCreateWindow( "Studio Project 3" );
glutInitWindowPosition (100, 100);
glutDisplayFunc (RenderScene);
glutReshapeFunc (Resize); // to be called when window size has changed
InitApp();
timeprec = glutGet(GLUT_ELAPSED_TIME);
glutPassiveMotionFunc (ProcessMouseMove);
glutMouseFunc( ProcessMouseClick );
glutIdleFunc (update_keyboard);
glutKeyboardFunc (KeyDown);
glutKeyboardUpFunc(KeyUp);
init ();
DeInitApp();
glutMainLoop ();
}