Sign in to follow this  
tsulas

Access Violation...

Recommended Posts

Hi All, I've been trying to get my menus to show up for a game I am making, and I got all the menus to work and everything, but then I moved it to another file, and now it doesn't work... When I run it, it comes back pointing to the first of the glGenTextures lines in the InitMenus() function with: 'Unhandled exception at 0x6958a250 in program.exe: 0xC0000005: Access Violation writing location 0x00000004 Here is the header file: #ifndef _CMENUS_H #define _CMENUS_H #define PI 3.14159 #define DEG2RAD(x) (x*PI)/180.0f #define TWO_PI PI*2.0 #define HALF_PI PI/2.0 class CTargaImage; class CreateMenus { private: // Texture objects for menu textures GLuint m_MenuTextures[32]; // IDs for the spots GLuint SpotNames[16]; // Menu Drawing Functions void DrawMainMenu(); void DrawNewMenu(); void DrawLoadMenu(); void DrawOptionsMenu(); void DrawCreditsMenu(); void DrawExitMenu(); // Boolean for if menu should be drawn bool ShowMain; bool ShowNew; bool ShowLoad; bool ShowOptions; bool ShowCredits; bool ShowExit; bool StartCredits; public: CreateMenus(); virtual ~CreateMenus(); bool InitMenus(); bool DeInit(); void DrawRightMenu(); void DealWithThis(int objectID); GLfloat CreditsY; }; #endif And here is the .cpp File: #include <windows.h> #include <gl/gl.h> #include <gl/glu.h> #include "CMenus.h" #include "CTargaImage.h" CreateMenus::CreateMenus() // Constructor { } CreateMenus:: ~CreateMenus() // Destructor { } bool CreateMenus::InitMenus() { // Create an instance of the Targa (TGA) Image maker CTargaImage image; // Each of the following loads a different texture for the menus image.Load("data/textures/menus/main/NewGame.tga"); glGenTextures(1, &m_MenuTextures[0]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[0]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/LoadGame.tga"); glGenTextures(1, &m_MenuTextures[1]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[1]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/Options.tga"); glGenTextures(1, &m_MenuTextures[2]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[2]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/Credits.tga"); glGenTextures(1, &m_MenuTextures[3]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[3]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/Exit.tga"); glGenTextures(1, &m_MenuTextures[4]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[4]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/Title.tga"); glGenTextures(1, &m_MenuTextures[5]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[5]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/Biped.tga"); glGenTextures(1, &m_MenuTextures[6]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[6]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/Yes.tga"); glGenTextures(1, &m_MenuTextures[7]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[7]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/No.tga"); glGenTextures(1, &m_MenuTextures[8]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[8]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/ExitReally.tga"); glGenTextures(1, &m_MenuTextures[9]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[9]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/CreditsScroll.tga"); glGenTextures(1, &m_MenuTextures[10]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[10]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/Back.tga"); glGenTextures(1, &m_MenuTextures[11]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[11]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); image.Load("data/textures/menus/main/CreditsTitle.tga"); glGenTextures(1, &m_MenuTextures[12]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[12]); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage()); image.Release(); // Set the Credits Starting Position, used on the credits menu CreditsY=-15.0f; // Initialize the Name Stack glInitNames(); // Initialize the array of IDs For Picking buttons for (int i=0; i<16; i++) { SpotNames[i]=i+1; } glEnable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); return true; } bool CreateMenus::DeInit() { // Delete the textures from memory glDeleteTextures(32, &m_MenuTextures[32]); return true; } void CreateMenus::DealWithThis(int objectID) { ShowMain=false; ShowNew=false; ShowLoad=false; ShowOptions=false; ShowCredits=false; ShowExit=false; switch(objectID) { case 0: // Main Menu ShowMain=true; break; case 1: // New Game Menu ShowNew=true; break; case 2: // Load Game Menu ShowLoad=true; break; case 3: // Options Menu ShowOptions=true; break; case 4: // Credits Menu ShowCredits=true; CreditsY=-15.0f; break; case 5: // Exit Menu ShowExit=true; break; default: // Something Went really Wrong... break; } } void CreateMenus::DrawRightMenu() { if (ShowMain==true) { DrawMainMenu(); } if (ShowNew==true) { DrawNewMenu(); } if (ShowLoad==true) { DrawLoadMenu(); } if (ShowOptions==true) { DrawOptionsMenu(); } if (ShowCredits==true) { DrawCreditsMenu(); } if (ShowExit==true) { DrawExitMenu(); } } void CreateMenus::DrawMainMenu() { glPushMatrix(); // Preserve our current Matrix glTranslatef(0.0f, 0.0f, -10.0f); // Move the scene back // New Game glTranslatef(3.25f, 4.25f, 0.0f); glPushName(SpotNames[0]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[0]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(2.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(2.0f, 0.5f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 0.5f, 0.0f); glEnd(); glPopName(); // Load Game glTranslatef(0.0f, -0.75f, 0.0f); glPushName(SpotNames[1]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[1]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(2.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(2.0f, 0.5f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 0.5f, 0.0f); glEnd(); glPopName(); // Options glTranslatef(-0.05f, -0.75f, 0.0f); glPushName(SpotNames[2]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[2]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(2.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(2.0f, 0.5f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 0.5f, 0.0f); glEnd(); glPopName(); // Credits glTranslatef(0.05f, -0.75f, 0.0f); glPushName(SpotNames[3]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[3]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(2.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(2.0f, 0.5f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 0.5f, 0.0f); glEnd(); glPopName(); // Exit glTranslatef(-0.05f, -0.75f, 0.0f); glPushName(SpotNames[4]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[4]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(2.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(2.0f, 0.5f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 0.5f, 0.0f); glEnd(); glPopName(); // Title glTranslatef(-10.0f, -6.0f, 0.0f); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[5]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(9.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(9.0f, 3.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 3.0f, 0.0f); glEnd(); // Character glTranslatef(3.0f, 3.0f, 0.0f); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[6]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(3.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(3.0f, 6.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 6.0f, 0.0f); glEnd(); glPopMatrix(); } void CreateMenus::DrawExitMenu() { glPushMatrix(); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -10.0f); // Move the scene back // New Game glTranslatef(-3.25f, 0.0f, 0.0f); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[9]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(6.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(6.0f, 3.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 3.0f, 0.0f); glEnd(); glTranslatef(1.0f, -2.0f, 0.0f); glPushName(SpotNames[5]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[7]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(2.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(2.0f, 0.5f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 0.5f, 0.0f); glEnd(); glPopName(); glTranslatef(2.0f, 0.0f, 0.0f); glPushName(SpotNames[6]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[8]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(2.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(2.0f, 0.5f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 0.5f, 0.0f); glEnd(); glPopName(); glPopMatrix(); } void CreateMenus::DrawCreditsMenu() { if (CreditsY<5.0f) { CreditsY+=0.02f; } else { ShowCredits=false; ShowMain=true; } glLoadIdentity(); glTranslatef(0.0f, 0.0f, -10.0f); glTranslatef(-3.0f, CreditsY, 0.0f); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[10]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(5.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(5.0f, 10.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 10.0f, 0.0f); glEnd(); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -10.0f); glTranslatef(4.0f, -4.5f, 0.01f); glPushName(SpotNames[7]); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[11]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(2.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(2.0f, 0.5f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 0.5f, 0.0f); glEnd(); glPopName(); glTranslatef(-10.0f, 8.0f, 0.0f); glBindTexture(GL_TEXTURE_2D, m_MenuTextures[3]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f);glVertex3f(0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f);glVertex3f(3.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f);glVertex3f(3.0f, 0.75f, 0.0f); glTexCoord2f(0.0f, 1.0f);glVertex3f(0.0f, 0.75f, 0.0f); glEnd(); } void CreateMenus::DrawOptionsMenu() { } void CreateMenus::DrawLoadMenu() { } void CreateMenus::DrawNewMenu() { } Anyone see why this would be happening?

Share this post


Link to post
Share on other sites
What's the code where you call CreateMenus::InitMenus() and where you create the CreateMenus instance look like? It looks like your this pointer is invalid. You're probably calling createMenus->InitMenus() while createMenus==NULL.

Share this post


Link to post
Share on other sites
Sorry, I have figured it out now...

When I created the instance, it was a pointer, and set to null, and then I tried to use it... so I made it just an instance, as there can only be one running anyway, and used the . instead of ->

Thanks joanusdmentia.

Share this post


Link to post
Share on other sites
Quote:
Original post by tsulas
The instance is created using the following:

CreateMenus *menuClass=NULL;

And when InitMenus is called it is:

menuClass->InitMenus();

Oh noes, you're accessing a null pointer! You first need to tell that pointer where to point, so do the following:

CreateMenus *menuClass=new CreateMenus();
menuClass->InitMenus();

//Do stuff

delete menuClass;
menuClass=0;





If you create it with new, don't forget to delete it! Alternativly, since you're not using inheritance, you could simply do this:


CreateMenus menuClass;
menuClass.InitMenus();


That way you don't have to worry about memory leaks.

BTW, edit your first post and put [ source ][ /source ] tag's (without the spaces) around your code!

EDIT: You already figured it out, too slow again!

Share this post


Link to post
Share on other sites
Quote:

Hi All,

I've been trying to get my menus to show up for a game I am making, and I got all the menus to work and everything, but then I moved it to another file, and now it doesn't work... When I run it, it comes back pointing to the first of the glGenTextures lines in the InitMenus() function with:

'Unhandled exception at 0x6958a250 in program.exe: 0xC0000005: Access Violation writing location 0x00000004

Anyone see why this would be happening?


The others found your problem, but... oi. Kind of a needle in a haystack yes? Hasn't it occurred to you that maybe you shouldn't need this much code to do basically the same thing over and over? If you know enough about programming to be interacting with OpenGL, you should know better about writing things like this (else I suggest you may be in over your head). Control-V is not your friend!

Here, I'll take a stab at fixing the worst things...


#ifndef _CMENUS_H
#define _CMENUS_H

extern static const int PI;
extern static const int DEG2RAD(x);
extern static const int TWO_PI;
extern static const int HALF_PI;

// class CTargaImage; <-- shouldn't need the forward declaration here.

// I'm not too sure where to put this. But in any case, let the compiler check
// your values for you...

typedef enum { MAIN = 0, NEW, LOAD, OPTIONS, CREDITS, EXIT } menuType;

class MenuHandler { // Classes should have "noun" names.
private:
// Texture objects for menu textures
// Why 32? You have only 13 textures. If you want room for expansion, why
// aren't you using a std::vector?
GLuint m_MenuTextures[32];
// IDs for the spots
GLuint SpotNames[16];

// Menu Drawing Functions
void DrawMainMenu();
void DrawNewMenu();
void DrawLoadMenu();
void DrawOptionsMenu();
void DrawCreditsMenu();
void DrawExitMenu();

menuType whichMenu; // which one should be drawn...

// Some helpers.
void DrawMenuItem(float x, float y, float z, int nameId, int texOffset = 0);
void DrawBox(float width = 2.0f, float height = 0.5f);

public:
CreateMenus();
virtual ~CreateMenus();

bool InitMenus();
bool DeInit();

void DrawRightMenu();

void DealWithThis(int objectID);

GLfloat CreditsY;

};
#endif





#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "CMenus.h"

#include "CTargaImage.h"

// Unless perhaps there's a better place to put the definitions:
static const int PI = 3.14159;
static const int DEG2RAD(x) = (x*PI)/180.0f;
static const int TWO_PI = PI*2.0;
static const int HALF_PI = PI/2.0;

using namespace std;

// Don't write empty constructors and destructors when the defaults are fine.

// This can't fail currently except by throwing an exception in one of the
// called methods, so I removed the return value as it adds no information.
void MenuHandler::InitMenus() {
// Create an instance of the Targa (TGA) Image maker
CTargaImage image;

// I only use an array because the number of textures is known at compile time.
// std::vector is a Good Thing generally...
// In nicely refactored code, you would have a vector<MenuItem> in each
// Menu, and their constructors would add the appropriate MenuItems, calling
// those constructors to do the texture loading.

int num_textures = 13;
string[num_textures] textureNames = {
"NewGame", "LoadGame", "Options", "Credits", "Exit", "Title", "Biped",
"Yes", "No", "ExitReally", "CreditsScroll", "Back", "CreditsTitle"
}
string folder = "data/textures/menus/main/";

for(int i = 0; i < num_textures; i++) {
image.Load((folder + textureNames[i] + ".tga").c_str());
glGenTextures(1, &m_MenuTextures[i]);
glBindTexture(GL_TEXTURE_2D, m_MenuTextures[i]);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
image.Release();
}

// Set the Credits Starting Position, used on the credits menu
CreditsY=-15.0f;

// Initialize the Name Stack
glInitNames();

// Initialize the array of IDs For Picking buttons
for (int i=0; i<16; i++) {
SpotNames[i]=i+1;
}

glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}

void MenuHandler::DeInit() {
// Delete the textures from memory
glDeleteTextures(32, &m_MenuTextures[32]);
}

void MenuHandler::DealWithThis(menuType objectID) {
assert(objectID >= 0 && objectID <= 5);
if (objectID == CREDITS) CreditsY=-15.0f; // special handling for credits menu
whichMenu = objectID;
}

void MenuHandler::DrawRightMenu() {
// THIS is where we want to do the switching...
switch(whichMenu) {
case MAIN: DrawMainMenu(); break;
case NEW: DrawNewMenu(); break;
case LOAD: DrawLoadMenu(); break;
case OPTIONS: DrawOptionsMenu(); break;
case CREDITS: DrawCreditsMenu(); break;
case EXIT: DrawExitMenu();
}
}

void MenuHandler::DrawMenuItem(float x, float y, float z, int nameId, int texOffset = 0) {
glTranslatef(x, y, z);
glPushName(SpotNames[nameId]);
glBindTexture(GL_TEXTURE_2D, m_MenuTextures[nameId + texOffset]);
DrawBox();
glPopName();
}

void MenuHandler::DrawBox(float width = 2.0f, float height = 0.5f) {
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(width, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(width, height, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, height, 0.0f);
glEnd();
}

void MenuHandler::DrawMainMenu() {
glPushMatrix();
glTranslatef(0.0f, 0.0f, -10.0f); // Move the scene back
DrawMenuItem(3.25f, 4.25f, 0.0f, 0); // New Game
DrawMenuItem(0.0f, -0.75f, 0.0f, 1); // Load Game
DrawMenuItem(-0.05f, -0.75f, 0.0f, 2); // Options
DrawMenuItem(0.05f, -0.75f, 0.0f, 3); // Credits
DrawMenuItem(-0.05f, -0.75f, 0.0f, 4); // Exit
// Title
glTranslatef(-10.0f, -6.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, m_MenuTextures[5]);
DrawBox(9.0f, 3.0f);
// Character
glTranslatef(3.0f, 3.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, m_MenuTextures[6]);
DrawBox(3.0f, 6.0f);
glPopMatrix();
}

void MenuHandler::DrawExitMenu() {
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f); // Move the scene back

glTranslatef(-3.25f, 0.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, m_MenuTextures[9]);
DrawBox(6.0f, 3.0f);

DrawMenuItem(1.0f, -2.0f, 0.0f, 5, 2);

DrawMenuItem(2.0f, 0.0f, 0.0f, 6, 2);
glPopMatrix();
}

void MenuHandler::DrawCreditsMenu() {
if (CreditsY<5.0f) {
CreditsY+=0.02f;
} else {
whichMenu == MAIN;
}

glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);

glTranslatef(-3.0f, CreditsY, 0.0f);
glBindTexture(GL_TEXTURE_2D, m_MenuTextures[10]);
DrawBox(5.0f, 10.0f);

glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);

DrawMenuItem(4.0f, -4.5f, 0.01f, 7, 4);

glTranslatef(-10.0f, 8.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, m_MenuTextures[3]);
DrawBox(3.0, 0.75f);
}

// These will be implemented later, I assume.
void MenuHandler::DrawOptionsMenu() {
}

void MenuHandler::DrawLoadMenu() {
}

void MenuHandler::DrawNewMenu() {
}




Which is only a start, really. You really should be implementing each menu as a separate Menu object, which stores its associated button information (so that you can get rid of that ugly switch logic). And making a class to represent the actual buttons (their texture and size; and give *them* the DrawMenuItem() method - except now it becomes MenuItem.draw()). And possibly making use of some data-driven design. Oh, and there are a *lot* of magic numbers to clean up...

Anyway. I don't mean to be too harsh; but it really is best to get in the habit ASAP. A lot of real-world code resembles this, and there's no reason for it. Nightmare to debug, too. ("Oops, am I supposed to change the first parameter of glGenTextures() to match the m_MenuTextures[10] array index?") Copy and paste programming breeds copy-and-paste errors (of two kinds: repeating the same error, and failing to change the pasted version in the right way).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this