Author Topic: Nehe tutorial examples problem!  (Read 2772 times)

0 Members and 1 Guest are viewing this topic.

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Nehe tutorial examples problem!
« on: October 04, 2011 »
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):
Code: [Select]
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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Nehe tutorial examples problem!
« Reply #1 on: October 04, 2011 »
You need the source for LoadBMP to know what to free...unless there's a FreeBMP?
Jim
Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: Nehe tutorial examples problem!
« Reply #2 on: October 04, 2011 »
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:

Offline energy

  • Amiga 1200
  • ****
  • Posts: 280
  • Karma: 25
    • View Profile
Re: Nehe tutorial examples problem!
« Reply #3 on: October 04, 2011 »
Maybe it not created by Purebasics AllocateMem.

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

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

Offline padman

  • Senior Member
  • Pentium
  • ********
  • Posts: 978
  • Karma: 258
    • View Profile
Re: Nehe tutorial examples problem!
« Reply #4 on: October 04, 2011 »
Why do you bother...  ;) Go and finish your remake instead! :P

Quote
Syntax

    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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Nehe tutorial examples problem!
« Reply #5 on: October 04, 2011 »
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:

Offline energy

  • Amiga 1200
  • ****
  • Posts: 280
  • Karma: 25
    • View Profile
Re: Nehe tutorial examples problem!
« Reply #6 on: October 05, 2011 »
Normally as i wrote above:

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



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

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: Nehe tutorial examples problem!
« Reply #7 on: October 05, 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!!!



    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

Normally as i wrote above:
GlobalFree_(*TextureImage(0)\Data)  :whisper:

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

Offline padman

  • Senior Member
  • Pentium
  • ********
  • Posts: 978
  • Karma: 258
    • View Profile
Re: Nehe tutorial examples problem!
« Reply #8 on: October 05, 2011 »
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...)

Code: [Select]


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


Quote
I am supposed to be a good coder!!!

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

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Re: Nehe tutorial examples problem!
« Reply #9 on: October 06, 2011 »

Quote
I 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: