memory access violation

Blitz3D Forums/Blitz3D Programming/memory access violation

Amanda Dearheart(Posted 2007) [#1]
Ok guys, here is my problem!
I'm getting a memory access violation! when i run my program. don't worry about not seeing anything, I know the program is not complete!. Another thing, I program in modules. This makes correcting mistakes a lot easier!!!

There are three files altogether

1. The main file
Include "Collisions.bb"
Include "Setup.bb"
Include "World.bb"
;Include "Player.bb"
;
; ===============================================
;
;   These files are not necessary for this particular game-script but they help out in coding
Include "key function.bb"



;
;        Main Routines
;

SetBuffer BackBuffer()
Setup
landscape("terrain-1.bmp", "water-2_mip.bmp", "sky", "heightmap_256.bmp")
Flip
UpdateWorld
RenderWorld
WaitKey()


2. A sub-program
;  SetUp file

Type MainCamera
		Field entity,camera,target,heading,sky
End Type
Type Player
	Field entity,model
	Field anim_speed#,player_y#,roll#
End Type


Function SetUp()
	Graphics3D 800, 600, 32, 0
	Player1.Player = CreatePlayer(0,10,0)
	Player1Camera.MainCamera = CreateMainCamera(Player1\entity)
End Function

Function CreateMainCamera.MainCamera(entity)
	c.MainCamera=New MainCamera
	c\entity=entity
	c\camera=CreateCamera()
	
	c\target=CreatePivot( entity )
	PositionEntity c\target,0,3,-10
	EntityType c\target,TYPE_TARGET
	
	c\heading=CreatePivot( entity )
	PositionEntity c\heading,0,0,20
	c\sky=CopyEntity( sky )
	Return c
	
End Function
Function UpdateMainCamera ( c.MainCamera)
	If KeyDown(200)
		TranslateEntity c\heading,0,-3,0
	Else If KeyDown(208)
		TranslateEntity c\heading,0,+3,0
	EndIf
	
	dx#=EntityX(c\target,True)-EntityX(c\camera,True)
	dy#=EntityY(c\target,True)-EntityY(c\camera,True)
	dz#=EntityZ(c\target,True)-EntityZ(c\camera,True)
	
	TranslateEntity c\camera,dx*.1,dy*.1,dz*.1
	
	PointEntity c\camera,c\heading
		
	PositionEntity c\target,0,0,0
	ResetEntity c\target
	PositionEntity c\target,0,3,-10

	PositionEntity c\sky,EntityX(c\camera),EntityY(c\camera),EntityZ(c\camera)
	
End Function

Function CreatePlayer.Player(x#, y#, z#)
	
	p.Player=New Player
	p\entity=CreatePivot()
	p\model=CopyEntity( player_model,p\entity )
	p\player_y=y
	PositionEntity p\entity,x,y,z
	EntityType p\entity,TYPE_PLAYER
	EntityRadius p\entity,1.5
	ResetEntity p\entity
	Return p

End Function


3. Another sub-program
Function skybox(file$)
	
	m=CreateMesh()
	;front face
	b=LoadBrush( file$+"_FR.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,-1,0,0:AddVertex s,+1,+1,-1,1,0
	AddVertex s,+1,-1,-1,1,1:AddVertex s,-1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;right face
	b=LoadBrush( file$+"_LF.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,-1,0,0:AddVertex s,+1,+1,+1,1,0
	AddVertex s,+1,-1,+1,1,1:AddVertex s,+1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;back face
	b=LoadBrush( file$+"_BK.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,+1,0,0:AddVertex s,-1,+1,+1,1,0
	AddVertex s,-1,-1,+1,1,1:AddVertex s,+1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;left face
	b=LoadBrush( file$+"_RT.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,0:AddVertex s,-1,+1,-1,1,0
	AddVertex s,-1,-1,-1,1,1:AddVertex s,-1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;top face
	b=LoadBrush( file$+"_UP.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,1:AddVertex s,+1,+1,+1,0,0
	AddVertex s,+1,+1,-1,1,0:AddVertex s,-1,+1,-1,1,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;bottom face	
	b=LoadBrush( file$+"_DN.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,-1,-1,1,0:AddVertex s,+1,-1,-1,1,1
	AddVertex s,+1,-1,+1,0,1:AddVertex s,-1,-1,+1,0,0
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	ScaleMesh m,100,100,100
	FlipMesh m
	EntityFX m,1

End Function
Function landscape(land$,water$,sky$,heightmap$)
	
	light=CreateLight()
	TurnEntity light,45,45,0
	
	land_tex=LoadTexture( land$,1 )
	ScaleTexture land_tex,10,10

	land=LoadTerrain( heightmap$ )
	EntityTexture land,land_tex
	
	TerrainShading land,True
	PositionEntity land,-1000,-100,-1000
	ScaleEntity land,2000.0/256,100,2000.0/256
	EntityType land,TYPE_TERRAIN
	TerrainDetail land,750,True
	
	For k=1 To n_trees
		Repeat
			tx#=Rnd(-70,70)-150
			tz#=Rnd(-70,70)+400
			ty#=TerrainY( land,tx,0,tz )
		Until ty>water_level
		t=CopyEntity( tree_sprite )
		PositionEntity t,tx,ty,tz
		ScaleSprite t,Rand(2,3),Rand(4,6)
	Next
	
	ground=CreatePlane()
	EntityTexture ground,land_tex
	PositionEntity ground,0,-100,0
	EntityOrder ground,9
	
	water_tex=LoadTexture( water$,3 )
	ScaleTexture water_tex,20,20
	
	water=CreatePlane()
	EntityTexture water,water_tex
	PositionEntity water,0,water_level,0
		
	sky=skybox( sky$ )
	EntityOrder sky,10
	HideEntity sky

End Function



Amanda Dearheart(Posted 2007) [#2]
OH, I forgot to explain!!

Sub-programs 2 and 3 are meant to be loaded by the Main Program 1.


John Blackledge(Posted 2007) [#3]
dx#=EntityX(c\target,True)-EntityX(c\camera,True)
dy#=EntityY(c\target,True)-EntityY(c\camera,True)
dz#=EntityZ(c\target,True)-EntityZ(c\camera,True)

TranslateEntity c\camera,dx*.1,dy*.1,dz*.1

d* ?


markcw(Posted 2007) [#4]
Maybe when you do
p\model=CopyEntity( player_model,p\entity )

the "player_model" variable isn't a valid entity.


Yo! Wazzup?(Posted 2007) [#5]
EDIT: Amanda Dearheart fixed it.


DJWoodgate(Posted 2007) [#6]
Run it in debug mode and see where the error occurs.


Mattizzle(Posted 2007) [#7]
Hehehe you like the famous castle demo I see. :-P Like DJWoodgate said, use debug mode. That goes for any MAV error as well. Most of the time it's just a name type-o and running in debug mode will show you a highlighted line in your code where something went wrong. Also, when you need to post alot of code, use the tag 'codebox' (with brackets of course) to conserve space and to allow less scrolling to the latest post.

Just out of curiousity in your user name, are you of the female gender? :D

~Matt


Amanda Dearheart(Posted 2007) [#8]
> Mattizzle

Ah, so you noticed. There is a reason for my methods, besides learning my around Blitz. I also liked Sky plateau and Wing Pilot by Rob Hutchinson. Sky Plateau Has a nice atmosphere and rotating feature as well as music that makes it goood for RPG type games. On the Nintendo platforms, StarFox has alwasys been my favorite game, which is why I liked Pilot. Thanks for the 'codebox' suggestion. Thanks for the 'debug' suggestion. It helped me solve that particular 'MAV' problem.. Now I have to solve other problems in the code.

> Markcw

Thanks for the player_model suggestion. While there was inddeed A problem, I forgot to loto issue the 'LoadAnimmesh' inststruction., it didn't solve my MAV problem

> Matt, again!

Yes, I am female.


Doiron(Posted 2007) [#9]
As others suggested, running the debug helps recognizing the issue.

First of all, be aware that the functions in the castle demo are not as reusable as they could seem since there are references in them which point to global variables containing the required 3d models.

The first issue was noticed by markcw...
In the CreatePlayer() function you are referring to an entity which doesn't exist.

In order to call the entity from within the function you could either associate an entity to the global variable 'player_model' (which would make the function less modular and not reusable) or add a parameter which asks for the player model:
Function CreatePlayer.Player(player_model, x#, y#, z#)
	
	p.Player=New Player
	p\entity=CreatePivot()
	p\model=CopyEntity( player_model,p\entity )
	p\player_y=y
	PositionEntity p\entity,x,y,z
	EntityType p\entity,TYPE_PLAYER
	EntityRadius p\entity,1.5
	ResetEntity p\entity
	Return p

End Function


There is also a similar problem within the skybox function, which looks for the 'sky' entity which doesn't exist.

The skybox is created by the function Skybox(file$). However, the function doesn't return anything (in that case the mesh 'm'), so it doesn't output the required 3D skybox model:

Function skybox(file$)
	
	m=CreateMesh()
	;front face
	b=LoadBrush( file$+"_FR.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,-1,0,0:AddVertex s,+1,+1,-1,1,0
	AddVertex s,+1,-1,-1,1,1:AddVertex s,-1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;right face
	b=LoadBrush( file$+"_LF.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,-1,0,0:AddVertex s,+1,+1,+1,1,0
	AddVertex s,+1,-1,+1,1,1:AddVertex s,+1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;back face
	b=LoadBrush( file$+"_BK.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,+1,0,0:AddVertex s,-1,+1,+1,1,0
	AddVertex s,-1,-1,+1,1,1:AddVertex s,+1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;left face
	b=LoadBrush( file$+"_RT.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,0:AddVertex s,-1,+1,-1,1,0
	AddVertex s,-1,-1,-1,1,1:AddVertex s,-1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;top face
	b=LoadBrush( file$+"_UP.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,1:AddVertex s,+1,+1,+1,0,0
	AddVertex s,+1,+1,-1,1,0:AddVertex s,-1,+1,-1,1,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;bottom face	
	b=LoadBrush( file$+"_DN.jpg",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,-1,-1,1,0:AddVertex s,+1,-1,-1,1,1
	AddVertex s,+1,-1,+1,0,1:AddVertex s,-1,-1,+1,0,0
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	ScaleMesh m,100,100,100
	FlipMesh m
	EntityFX m,1
	return m

End Function


Then, in the SetUp() function you call the CreateMainCamera() function which in turn needs a valid skybox model (which again doesn't exist). It looks like you are creating the skybox in the Landscape() function which is located in the next include... however, the skybox is needed before the CreateMainCamera() function.

So, for the sake of simplicity, I removed the lines related to the skybox creation from the Landscape() function, and put them in the setup() function right before the CreateMainCamera(). By now, I have also added a call to the character model from the Castle demo to the CreatePlayer() function call:

Function SetUp()
	Graphics3D 800, 600, 32, 0
	Player1.Player = CreatePlayer(LoadAnimMesh("MARIORUN.X"),0,10,0)

	sky=skybox( sky$ )
	EntityOrder sky,10
	HideEntity sky

	Player1Camera.MainCamera = CreateMainCamera(Player1\entity)
End Function


The "world.bb" include must be put before the "setup.bb" include.

Now remove the 'sky' parameter from the Landscape() function, and compile.


It's absolutely not the best way to work (I was just ironing the bugs out), but at least it runs.


Be very aware of the use globals, types and where to put includes before trying to achieve a modular framework: in particular avoid to call global variables from within functions (use parameters instead), and always call functions which you have already included before.


Amanda Dearheart(Posted 2007) [#10]
> Doiron
Thanks for the help!!


Reans(Posted 2014) [#11]
How about posting on BlitzMax proper instead about your peeing