• Welcome to Dark Bit Factory & Gravity.
 

Nehe tutorial examples problem!

Started by jace_stknights, October 04, 2011

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

jace_stknights

Hi all,

I've download the Nehe Opengl tutorial examples for PureBasic (ftp://ftp-developpez.com/purebasic/sources/fichiers/3d/opengl/nehe-tutoriels.zip) for testing first (I gonna learn soon) and got and error for the examples 7 up to 12 with the FreeMemory in this procedure (it is nearly the same in each example):

Procedure.l LoadGLTextures() ;Load Bitmaps And Convert To Textures

Protected Status.l=#False ;Status Indicator
Protected Dim *TextureImage.AUX_RGBImageRec(1) ;Create Storage Space For The Texture

;Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
*TextureImage(0)=LoadBMP("Data/Crate.bmp")
If *TextureImage(0)
  Status=#True ;Set The Status To TRUE

  glGenTextures_(3,@texture(0)) ;Create Three Textures

  ;Create Nearest Filtered Texture
  glBindTexture_(#GL_TEXTURE_2D,texture(0))
  glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MAG_FILTER,#GL_NEAREST)
  glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MIN_FILTER,#GL_NEAREST)
  glTexImage2D_(#GL_TEXTURE_2D,0,3,*TextureImage(0)\sizeX,*TextureImage(0)\sizeY,0,#GL_RGB,#GL_UNSIGNED_BYTE,*TextureImage(0)\Data)

  ;Create Linear Filtered Texture
  glBindTexture_(#GL_TEXTURE_2D,texture(1))
  glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MAG_FILTER,#GL_LINEAR)
  glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MIN_FILTER,#GL_LINEAR)
  glTexImage2D_(#GL_TEXTURE_2D,0,3,*TextureImage(0)\sizeX,*TextureImage(0)\sizeY,0,#GL_RGB,#GL_UNSIGNED_BYTE,*TextureImage(0)\Data)

  ;Create MipMapped Texture
  glBindTexture_(#GL_TEXTURE_2D,texture(2))
  glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MAG_FILTER,#GL_LINEAR)
  glTexParameteri_(#GL_TEXTURE_2D,#GL_TEXTURE_MIN_FILTER,#GL_LINEAR_MIPMAP_NEAREST)
  gluBuild2DMipmaps_(#GL_TEXTURE_2D,3,*TextureImage(0)\sizeX,*TextureImage(0)\sizeY,#GL_RGB,#GL_UNSIGNED_BYTE,*TextureImage(0)\Data)
EndIf

If *TextureImage(0) ;If Texture Exists
  If *TextureImage(0)\Data ;If Texture Image Exists
   FreeMemory(*TextureImage(0)\Data) ;Free The Texture Image Memory
  EndIf
   FreeMemory(*TextureImage(0)) ;Free The Image Structure
EndIf

ProcedureReturn Status ;Return The Status

EndProcedure


The compiler gave the error:

The specified "MemoryId" is not valid for the two FreeMemory at the end.

If I comment the lines, everything runs fine. But as I'm a serious programmer ;D, if I create a memory struct, you should free it after  :whack:

Someone got an idea?
Challenge Trophies Won:

Jim

You need the source for LoadBMP to know what to free...unless there's a FreeBMP?
Jim
Challenge Trophies Won:

jace_stknights

Hmmm yep, but as the source is "like this" it should work, no?

I didn't really take time to "enter the code", I just test it for the moment. And I've found strange that this part don't work.
Challenge Trophies Won:

energy

Maybe it not created by Purebasics AllocateMem.

Just change to:
GlobalFree_(*TextureImage(0)\Data)

This should work.
coding: jwasm,masm
hobby: www.scd2003.de

padman

Why do you bother...  ;) Go and finish your remake instead! :P

QuoteSyntax

    FreeMemory(*MemoryID)

Description

    Free the memory previously allocated with AllocateMemory() or ReAllocateMemory().

    Note: all remaining allocated memory blocks are automatically freed when the program ends.

Supported OS

    All
Challenge Trophies Won:

Jim

That's only true if it actually uses AllocateMemory. :P  If someone points me to source or binary of LoadBMP I'll go and work it out.  Either the nehe example has always been wrong on this point or the details of LoadBMP have changed since it was written :)

Jim
Challenge Trophies Won:

energy

Normally as i wrote above:

GlobalFree_(*TextureImage(0)\Data)  :whisper:



coding: jwasm,masm
hobby: www.scd2003.de

jace_stknights

Quote from: padman on October 04, 2011
Why do you bother...  ;) Go and finish your remake instead! :P
No, nooooo don't slap me!!! :whack: I'm was just... ok I'm back on my remake!!!


Quote from: padman on October 04, 2011

    Note: all remaining allocated memory blocks are automatically freed when the program ends.


Yes I know, but as I told first : I am supposed to be a good coder!!! ;D ;D ;D ;D ;D ;D So I free the allocated memory ;P

Quote from: energy on October 05, 2011
Normally as i wrote above:
GlobalFree_(*TextureImage(0)\Data)  :whisper:

Thank you, it is working!!!!  :cheers:
Challenge Trophies Won:

padman

Just for completeness and for Jim the LoadBMP procedure. Seems like FreeMemory was used by mistake (?) when converting the code to Purebasic and the compiler just didn't care in earlier versions...

Anyway, here's the code: (the underscore after Readfile_ and CloseFile_ is there because the server wouldn't let me post it without it...)




Import "glaux.lib"
auxDIBImageLoad.l(filename.s) As "_auxDIBImageLoadA@4" ;loads a 24-bit Windows DIB
EndImport

Procedure.l LoadBMP(Filename.s) ;Loads A Bitmap Image

Protected File.l=#Null ;File Handle

If Filename="" ;Make Sure A Filename Was Given
  ProcedureReturn #Null ;If Not Return NULL
EndIf

File=ReadFile_(#PB_Any,Filename) ;Check To See If The File Exists

If File ;Does The File Exist?
  CloseFile_(File) ;Close The Handle
  ProcedureReturn auxDIBImageLoad(Filename) ;Load The Bitmap And Return A Pointer
EndIf

ProcedureReturn #Null ;If Load Failed Return NULL



QuoteI am supposed to be a good coder!!!

* padman PokeQ's Jace with a stick...  :P
Challenge Trophies Won:

jace_stknights

Quote from: padman on October 05, 2011

QuoteI am supposed to be a good coder!!!

* padman PokeQ's Jace with a stick...  :P

I hope I was not in a bad posture at this moment... A misplaced POKEQ could cause damage!!!  ;D ;D ;D
Challenge Trophies Won: