Jump to content
  • Advertisement

Archived

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

neonoblivion

Source code problem... Again :(

This topic is 5986 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

well... thanks to Anthracks & jallen I got my filler working, but now when I try my texture code it compiles but gives me the "You have performed an illigal action" box, dies, and stays in 800X600 ,so I have to manually change back to high rez . can anyone help.
  
#include <windows.h>
#include <GL\gl.h>
#include <GL\glu.h>	
#include <GL\glaux.h>
#include <stdio.h>

#define width	800
#define height	600
#define bpp		16

#define WINDOW_CLASS_NAME "WINXCLASS"

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)


HGLRC           hrc=NULL;						
HDC             hdc=NULL;						
HWND            hwnd=NULL;
HINSTANCE       hin;

bool	keys[256];
bool	active=TRUE;
bool	fullscreen=TRUE;


GLfloat		xrot;							
GLfloat		yrot;							
GLfloat		zrot;


GLuint	texture[1];


LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int init(void);
int gmain(void);
void shutdown(void);
AUX_RGBImageRec *LoadBMP(char *filename);
void ResizeWnd(int Width, int Height);

int WINAPI WinMain(	HINSTANCE	hInstance,			
			HINSTANCE	hPrevInstance,				
			LPSTR		lpCmdLine,			
			int		nCmdShow){

	MSG	msg;	
	BOOL	done=FALSE;	

	GLuint		PixelFormat;
	WNDCLASS winclass;

	
	RECT		WindowRect;
	WindowRect.left=(long)0;	
	WindowRect.right=(long)width;	
	WindowRect.top=(long)0;			
	WindowRect.bottom=(long)height;
	
	
	hin					= GetModuleHandle(NULL);
	winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    winclass.lpfnWndProc = WndProc;
    winclass.cbClsExtra = 0;
    winclass.cbWndExtra = 0;
    winclass.hInstance = hin;
    winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    winclass.hbrBackground = NULL;
    winclass.lpszMenuName = NULL; 
    winclass.lpszClassName = WINDOW_CLASS_NAME;
	
	RegisterClass(&winclass);
	
	
	if (!(hwnd = CreateWindowEx(WS_EX_APPWINDOW,
	             WINDOW_CLASS_NAME,
			     "ha ha ha",	
			     WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
			     0,0,	   
			     width,height,
			     NULL,	 
			     NULL,	
			     hin,
			     NULL))){	
   return(0);
   }
	
	
	
	DEVMODE dms;					
	memset(&dms,0,sizeof(dms));		
	dms.dmSize=sizeof(dms);		
	dms.dmPelsWidth	= width;	
	dms.dmPelsHeight	= height;	
	dms.dmBitsPerPel	= bpp;		
	dms.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

	ChangeDisplaySettings(&dms,CDS_FULLSCREEN);
						
	ShowCursor(FALSE);

	
	static	PIXELFORMATDESCRIPTOR pfd=					// pfd Tells Windows How We Want Things To Be

	{
		sizeof(PIXELFORMATDESCRIPTOR),					// Size Of This Pixel Format Descriptor

		1,								// Version Number

		PFD_DRAW_TO_WINDOW |						// Format Must Support Window

		PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL

		PFD_DOUBLEBUFFER,						// Must Support Double Buffering

		PFD_TYPE_RGBA,							// Request An RGBA Format

		bpp,								// Select Our Color Depth

		0, 0, 0, 0, 0, 0,						// Color Bits Ignored

		0,								// No Alpha Buffer

		0,								// Shift Bit Ignored

		0,								// No Accumulation Buffer

		0, 0, 0, 0,							// Accumulation Bits Ignored

		16,								// 16Bit Z-Buffer (Depth Buffer)

		0,								// No Stencil Buffer

		0,								// No Auxiliary Buffer

		PFD_MAIN_PLANE,							// Main Drawing Layer

		0,								// Reserved

		0, 0, 0								// Layer Masks Ignored

	};

	
	hdc=GetDC(hwnd);

	PixelFormat=ChoosePixelFormat(hdc,&pfd);

	SetPixelFormat(hdc,PixelFormat,&pfd);

	hrc=wglCreateContext(hdc);

	wglMakeCurrent(hdc,hrc);

	ShowWindow(hwnd,SW_SHOW);	
	SetForegroundWindow(hwnd);	
	SetFocus(hwnd);				
	ResizeWnd(width, height);
	init();

	///load texture

	
	
	////////////////textures

	
	
	
	AUX_RGBImageRec *tex[1];
	memset(tex,0,sizeof(void *)*1);	
	tex[0]=LoadBMP("c:\\happy.bmp");
	glGenTextures(1, &texture[0]);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, tex[0]->sizeX, 
		tex[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, 
		tex[0]->data);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	if(tex[0]){
		if(tex[0]->data){
			free(tex[0]->data);
		}
		free(tex[0]);
	}

	
	
	/////////////////textures end

	
	
	
	///end load texture

	
	while(1){
      if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)){ 
	 if (msg.message == WM_QUIT)
            break;
	
	 TranslateMessage(&msg);

         DispatchMessage(&msg);
      } 
    
      if(gmain()==0)
		  break;
	  SwapBuffers(hdc);	
   
	} 	


	shutdown();


	return (msg.wParam);						

}







LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){

PAINTSTRUCT ps;	
HDC hdc;	 

 
switch(msg){	
	
	case WM_CREATE:{
             return(0);
	} break;

        case WM_PAINT:{
             hdc = BeginPaint(hwnd,&ps);
             EndPaint(hwnd,&ps);
             return(0);
        } break;

	case WM_DESTROY:{
             PostQuitMessage(0);
	     return(0);
	} break;

	default:break;

}

 
return (DefWindowProc(hwnd, msg, wparam, lparam));

}



int init(void){
	glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);						
	glEnable(GL_DEPTH_TEST);				
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	return(0);
}

void shutdown(void){
	ChangeDisplaySettings(NULL,0);					
	ShowCursor(TRUE);
	wglMakeCurrent(NULL,NULL);
	if(hrc){
		wglDeleteContext(hrc);
		hrc=NULL;
	}
	if(hdc){
		ReleaseDC(hwnd,hdc);
		hdc=NULL;
	}
	if(hwnd){
		DestroyWindow(hwnd);
		hwnd=NULL;
	}	
	
	UnregisterClass("WINDOWS_CLASS_NAME",hin);
	hin=NULL;
}




int gmain(void){
	if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE)){
		PostMessage(hwnd, WM_DESTROY,0,0);
		return(0);
	}
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(-1.5f,0.0f,-6.0f);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	//glColor3f(1.0f,0.0f,0.0f);

	glBegin(GL_TRIANGLES);
		glVertex3f( 0.0f, 1.0f, 0.0f);
		glVertex3f(-1.0f,-1.0f, 0.0f);
		glVertex3f( 1.0f,-1.0f, 0.0f);
	glEnd();	

	//glTranslatef(3.0f,0.0f,0.0f);	

	return(1);
}



void ResizeWnd(int Width, int Height){ 
	 GLfloat fAspect; 
	 if (!Height) 
	 Height = 1; 
	 glViewport(0, 0, Width, Height); 
	 fAspect = (float)Width/(float)Height;
	 glMatrixMode(GL_PROJECTION);
	 glLoadIdentity();
	 gluPerspective(45.0, fAspect, 1.0, 400.0);
	 glMatrixMode(GL_MODELVIEW);
	 glLoadIdentity();
}

AUX_RGBImageRec *LoadBMP(char *filename){
	FILE *file;
	if(!(file=fopen("filename","r")))
		return(0);
	fclose(file);
	return auxDIBImageLoad(filename);
}

  
The sadness... ;(

Share this post


Link to post
Share on other sites
Advertisement
I just fixed this same problem for someone else the other day. Was it you or somebody else?

Anyhow look at your function call:

glGenTextures(1, &texture[0]);


It should be the following:

glGenTextures(1, texture);

glGenTextures takes an address to the beginning of an array. If you do the following: &texture[0]. You are passing it the address of the first value in the array.

------

Shop for the Lowest Price!
Then, Check a Reseller''s
I''m ran the debugger on your code, and I''m finding the error occurring on this function call:


glTexImage2D(GL_TEXTURE_2D, 0, 3, tex[0]->sizeX,
tex[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, tex[0]->data);


I''m still checking to see what is causing the error. I think it might have something to do with tex[0]. I''m still checking though.

Share this post


Link to post
Share on other sites
Also, just a little bug I noticed. You might want to change your function call to fopen, so your are passing in the filename, and not the string "filename".

The code below is correct. Notice the absence of quotes around filename in then fopen function call.


  
AUX_RGBImageRec *LoadBMP(char *filename)
{
FILE *file;

if(!(file=fopen(filename, "r")))
return(0);

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

Share this post


Link to post
Share on other sites
quote:
Original post by aNonamuss
glGenTextures(1, &texture[0]);

It should be the following:

glGenTextures(1, texture);

glGenTextures takes an address to the beginning of an array. If you do the following: &texture[0]. You are passing it the address of the first value in the array.


That is exactly the same.

The problem is that the image is not being loaded, so LoadBMP is returning 0. Therefore, there is a NULL pointer dereferenced in the tex [0]->data: !!tex [0] == NULL!!

The cause of that is:

  
if(!(file=fopen("filename","r")))
return(0);

Which is wrong. You''re trying to open a file called "filename" instead of a file with the name specified by the filename variable, which would be:

  
if(!(file=fopen(filename,"r")))
return(0);


Oh, and did you know glaux is evil? It''s buggy like hell.

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!