Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

PepsiPlease

What's wrong with this code?

This topic is 5983 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, Whenever I try to compile this code, it compiles fine, links fine, so I execute it. As the program begins (in a window, not fullscreen) an error pops up saying I did an illegal command or something. In fullscreen, it just doesn't load. I don't see why it compiles but not runs, and I can't figure out the debugger (just got msvc++). Anyhow, heres the code...
      int load_gl_textures()
{
  AUX_RGBImageRec *TextureImage[3];

  memset(TextureImage,0,sizeof(void *)*1);

  
  TextureImage[3] = loadbmp("blue.bmp");
  TextureImage[1] = loadbmp("cement.bmp");
  TextureImage[2] = loadbmp("52.bmp");
  

  glGenTextures(3, &texture[3]); 

  glBindTexture(GL_TEXTURE_2D, texture[3]);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[3]->sizeX,
               TextureImage[3]->sizeY, 0, GL_RGB,
               GL_UNSIGNED_BYTE, TextureImage[3]->data);

  glBindTexture(GL_TEXTURE_2D, texture[1]);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX,
               TextureImage[1]->sizeY, 0, GL_RGB,
               GL_UNSIGNED_BYTE, TextureImage[1]->data);

  glBindTexture(GL_TEXTURE_2D, texture[2]);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[2]->sizeX,
               TextureImage[2]->sizeY, 0, GL_RGB,
               GL_UNSIGNED_BYTE, TextureImage[2]->data);

	for (int t = 0; t < 2; t++)
	{
		if (TextureImage[t])
		{
		if (TextureImage[t]->data)
		{
			free(TextureImage[t]->data);
		}

		free(TextureImage[t]);
		}
	}

    
Any help would be appreciated. Thanks EDIT - got the damn source tags messed up [edited by - PepsiPlease on May 29, 2002 9:19:14 PM] [edited by - PepsiPlease on May 29, 2002 9:20:27 PM]

Share this post


Link to post
Share on other sites
Advertisement
You are overflowing, arrays start on 0 so drop the value by one on each of those assignments.

Share this post


Link to post
Share on other sites
Oops, I posted the wrong code. The first time I tried it I had 3 textures, 0 1 and 2, and the same thing happened, so I tried to change it, to no avail. This is the same exact thing, just after my failed attempt to fix the problem, so there must be another reason it''s happening.

Share this post


Link to post
Share on other sites
If it helps, here is what the debugger said:

//Lots of DLL''s
//More DLL''s

First-chance exception in lesson1.exe: 0xC0000005: Access Violation.





This is probably just a simple mistake, but I can''t seem to figure it out.

Share this post


Link to post
Share on other sites
What line does the code break on? Or, if it breaks into some assembly code hit Alt-7 and bring up the call stack and double click on the function that is not part of Win32, and see where it is being called from there.

Usually when I get this error it is from trying to read something that is not there. Like a pointer that points to nothing, or an array that has no information inside of it.



HighEnd
----
"Becoming a game developer from scratch..."
http://www.highendgaming.com

Share this post


Link to post
Share on other sites
Your right. Except this time it was trying to read a bitmap file from a .psp file.

But when I tryed to compile it, it still didn't work. This time it said "Release Device Context Failed". Here is the revised code.


    #include <windows.h>		// Header File For #include <gl\gl.h>			// Header File For The #include <gl\glu.h>			// Header File For The #include <gl\glaux.h>
#include <stdio.h> // Header File For The Glaux


HDC hDC=NULL; // Private GDI

HGLRC hRC=NULL; // Permanent Rendering

HWND hWnd=NULL; // Holds Our Window e

HINSTANCE hInstance; // Holds The Instance Of


bool keys[256]; // Array Used For The

bool active=TRUE; // Window Active Flag Set To

bool fullscreen=TRUE;


#define MAP_SIZEX 10
#define MAP_SIZEY 10


GLuint texture[3];


char map[10][10] = {
{1, 1, 1, 2, 2, 2, 2, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 2},
{1, 1, 1, 1, 1, 1, 1, 0, 0, 2},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 2},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc


GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window

{
if (height==0) // Prevent A Divide By Zero By

{
height=1; // Making Height Equal One

}

glViewport(0,0,width,height); // Reset The Current Viewport


glMatrixMode(GL_PROJECTION); // Select The Projection Matrix

glLoadIdentity(); // Reset The Projection Matrix


// Calculate The Aspect Ratio Of The Window

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix

glLoadIdentity(); // Reset The Modelview Matrix

}

AUX_RGBImageRec *loadbmp(char *filename)
{
FILE *file = NULL;

if (!filename)
return NULL;

file = fopen(filename, "r");

if (file)
{
fclose(file);
return auxDIBImageLoad(filename);
}

return NULL;
}



int load_gl_textures()
{
AUX_RGBImageRec *TextureImage[3];

memset(TextureImage,0,sizeof(void *)*1);


TextureImage[0] = loadbmp("blue.bmp");
TextureImage[1] = loadbmp("cement.bmp");
TextureImage[2] = loadbmp("52.bmp");


glGenTextures(3, &texture[3]);

glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,
TextureImage[0]->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE, TextureImage[0]->data);

glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX,
TextureImage[1]->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE, TextureImage[1]->data);

glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[2]->sizeX,
TextureImage[2]->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE, TextureImage[2]->data);

for (int t = 0; t < 3; t++)
{
if (TextureImage[t])
{
if (TextureImage[t]->data)
{
free(TextureImage[t]->data);
}

free(TextureImage[t]);
}
}

return(1);
}


int draw_tiles(GLvoid)
{
int tile;
for (int y = 0; y < MAP_SIZEY; y++)
{
for (int x = 0; x < MAP_SIZEX; x++)
{
tile = map[y][x];



glBindTexture(GL_TEXTURE_2D, texture[tile]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x), float(y), 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1), float(y), 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1), float(y + 1), 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x), float(y + 1), 0.0f);
glEnd();

}
}

return (1);
}

int InitGL(GLvoid) // All Setup For OpenGL Goes Here

{
if(!load_gl_textures())
{
return FALSE;
}
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH); // Enable Smooth Shading

glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background

glClearDepth(1.0f); // Depth Buffer Setup

glEnable(GL_DEPTH_TEST); // Enables Depth Testing

glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

return TRUE; // Initialization Went OK

}

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing

{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(5.0f, 8.0f, 20.0f, 5.0f, 8.0f, 0.0f, 0.0f, 1.0f, 0.0f);
draw_tiles();
return TRUE;
}


//And the rest of the code...






Why does it say that message?


Thanks

[edited by - PepsiPlease on May 29, 2002 10:07:18 PM]

Share this post


Link to post
Share on other sites
...and here is the new debug output:

The thread 0xFAE123ED has exited with code 0 (0x0).
The program ''C:\OpenGL\Lesson1\Debug\lesson1.exe'' has exited with code 0 (0x0).

Share this post


Link to post
Share on other sites
You're still accessing memory that is out-of-bounds


glGenTextures(3, &texture[3]);


Also, if your trying to pass in an array (as the function calls for). Try the following:


glGenTextures(3, texture);


[edited by - aNonamuss on May 29, 2002 10:40:54 PM]

Share this post


Link to post
Share on other sites
It works!

Thanks a lot for to help. I am just learning opengl, so a lot of this stuff is confusing.

monkey - I am just messing around with opengl, trying to learn the basics and such. If you have seen my other posts, I was trying to make a map loading function (which I never got working).



Again, thanks for the help

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!