Page 1 of 1

ADD CLASS

Posted: Thu Mar 03, 2016 12:26 pm
by mypub123

Code: Select all



;- Klassen

Class GetFrameRate
	FPS.l
	lFPS.l
	sFPS.l
	
	Public Method Get()
		This\FPS + 1
	  If ElapsedMilliseconds() - This\lFPS >= 1000
	    This\sFPS = This\FPS
	    This\FPS = 0
	    This\lFPS = ElapsedMilliseconds()
	  EndIf
	  MethodReturn This\sFPS
  EndMethod
EndClass

Class ScreenSize
	
	Public Width.l
	Public Height.l
	
	Public Method Init(ScreenWidth.l, ScreenHeight.l)
		This\Width = ScreenWidth
		This\Height = ScreenHeight
	EndMethod
EndClass

Class WindowScreen
	*Screen.ScreenSize
	WindowID.l
	
	Public Method Init(*ScreenSize)
		
		This\Screen = *ScreenSize
		
		If InitSprite() = 0
		  MessageRequester("Fehler","Konnte Sprite nicht initialisieren!",0)
		EndIf
		
		If InitSound() = 0
		  MessageRequester("Fehler","Konnte Sound nicht initialisieren!",0)
		EndIf
		
		If InitKeyboard()=0
		  MessageRequester("Fehler","Konnte Keyboard nicht initialisieren!",0)
		EndIf
		
		This\WindowID = OpenWindow(#PB_Any, 0, 0, This\Screen\Width, This\Screen\Height, "SimpleOOP - DrawExample1", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
			If OpenWindowedScreen(WindowID(This\WindowID), 0, 0, This\Screen\Width, This\Screen\Height, 1, 0, 0) = 0
			  MessageRequester("Fehler","Konnte Screen nicht initialisieren!",0)
			EndIf             
	EndMethod

	Public Method Event(WindowEvent.l)
		If EventWindow() <> This\WindowID
			MethodReturn 0
		EndIf
		
		Select WindowEvent
			Case #PB_Event_CloseWindow
				End
		EndSelect
		
	EndMethod
	
	Public Method UpdateScreen()
		FlipBuffers()
	  ClearScreen(RGB(0,0,0))
	EndMethod
	
EndClass

Class Line
	*Screen.ScreenSize
	xStart.l
	yStart.l
	xEnd.l
	yEnd.l
	Direction.l
	Color.l
	
	Public Method Init(*ScreenSize)
		This\Screen = *ScreenSize
		
		This\xStart = 0
		This\yStart = 0
		This\xEnd = Random(This\Screen\Width) ;This\ScreenWidth-50 
		This\YEnd = This\Screen\Height
		This\Direction = 1
		This\Color = RGB(Random(255),Random(255),Random(255))
	EndMethod

	Public Method Move()

		This\xEnd+This\Direction
		
		If This\xEnd > This\Screen\Width
			This\xEnd = This\Screen\Width
			This\Direction = -This\Direction
		EndIf
		
		If This\xEnd < 0
			This\xEnd = 0
			This\Direction = -This\Direction
		EndIf
		
	EndMethod

	Public Method Draw()
		Line(This\xStart, This\yStart, This\xEnd, This\yEnd , This\Color) 
	EndMethod

EndClass

;- Schleife

Define *FrameRate.GetFrameRate = NewObject.GetFrameRate
Define *ScreenSize.ScreenSize = NewObject.ScreenSize(800, 600)
Define *WindowScreen.WindowScreen = NewObject.WindowScreen(*ScreenSize)

NewList *Lines.Line()
	For I=0 To 10
		AddElement(*Lines())
			*Lines() = NewObject.Line(*ScreenSize)
	Next
	
Repeat

	ExamineKeyboard()
	WindowEvent = WindowEvent()
	*WindowScreen\Event(WindowEvent)
	
	StartDrawing(ScreenOutput())
	ForEach *Lines()
		*Lines()\Move()
		*Lines()\Draw()
	Next
	DrawingMode(#PB_2DDrawing_Transparent)
	DrawText(10, 10, Str(*FrameRate\Get())+" FPS")
	DrawingMode(#PB_2DDrawing_Default)
	StopDrawing()
	
	*WindowScreen\UpdateScreen()

ForEver

Add CLASS syntax

And it can automatically prompt

And FreeClass

CLass is often used to

In order to join the official function convenient please
He has
Default property
default member
Can be destroyed
Similar to the VB CLass

Re: ADD CLASS

Posted: Thu Mar 03, 2016 6:11 pm
by sys64802
Ohhhhhh finally !
It was about time someone asked for this !
:lol:

Re: ADD CLASS

Posted: Thu Mar 03, 2016 6:55 pm
by User_Russian
Probably it will not be done!
Already many times asked to add classes in the PB, but Fred does not agree!

Re: ADD CLASS

Posted: Thu Mar 03, 2016 7:43 pm
by Lunasole
User_Russian wrote:Probably it will not be done!
Already many times asked to add classes in the PB, but Fred does not agree!
... and I'm becoming more agree with him, after recently worked both with C++ class code and clear C code (BOX2D engine vs Chipmunk2D engine) and nicely compared one vs second.
Classes are really damn mad mess, the code written in true POO-style with all that shit like polymorphism etc is so damn dirty and hard to work with and understand, so it is really better not allow this at all.

But I still like very much the VB6-style "class modules" (totally limited OOP variation), they would be perfect and useful, PB lacks it 8)

Re: ADD CLASS

Posted: Thu Mar 03, 2016 7:55 pm
by User_Russian
Lunasole wrote:But I still like very much the VB6-style "class modules" (totally limited OOP variation), they would be perfect and useful, PB lacks it 8)
I have proposed a variant classes, syntactically similar to the modules. http://www.purebasic.fr/english/viewtop ... 95#p454795

Re: ADD CLASS

Posted: Thu Mar 03, 2016 9:09 pm
by HanPBF
I like the smalltalk idea of having all in a big environment but I also can imagine the truth in "In smalltalk everything happens somewhere else!".

Beyond architecture, the most amoung us like working with text based programs in an editor, don't they?


We urgently need a statistics in this forum for the OOP-discussion waves...

Re: ADD CLASS

Posted: Thu Mar 03, 2016 9:36 pm
by StarBootics
Module are not enough ?

Code: Select all

DeclareModule FrameRate
  
  Declare.l Get()
  
EndDeclareModule

Module FrameRate
  
  Structure Instance
    
    FPS.l
    lFPS.l
    sFPS.l
    
  EndStructure
  
  Global Instance.Instance

  Procedure.l Get()
    Instance\FPS + 1
    If ElapsedMilliseconds() - Instance\lFPS >= 1000
      Instance\sFPS = Instance\FPS
      Instance\FPS = 0
      Instance\lFPS = ElapsedMilliseconds()
    EndIf
    ProcedureReturn Instance\sFPS
  EndProcedure
  
EndModule

FrameRate::Get()
Best regards
StarBootics

Re: ADD CLASS

Posted: Thu Mar 03, 2016 9:59 pm
by User_Russian
StarBootics, module <> class.
In your example, there is only one instance.

Re: ADD CLASS

Posted: Thu Mar 03, 2016 10:48 pm
by StarBootics
User_Russian wrote:StarBootics, module <> class.
In your example, there is only one instance.
What your point ?

Nothing id preventing you do use a linked list to have as many instance as you want. If you don't want to use a linked list you can declare a Structure in the DeclareModule/EndDeclareModule and create as many "instance" as you want. Just like that :

Code: Select all

DeclareModule Version

  Structure Version

    Major.a
    Minor.a
    Patch.a

  EndStructure
  
  Declare Update(*VersionA.Version, P_Major.a, P_Minor.a, P_Patch.a)
  Declare Reset(*VersionA.Version)
  Declare Compare(*VersionA.Version, *VersionB.Version)
  Declare ReadVersion(FileID.l, *VersionA.Version)
  Declare WriteVersion(FileID.l, *VersionA.Version)
  Declare.s Format(*VersionA.Version)
  Declare Unformat(*VersionA.Version, P_Version.s)
  Declare.l To_Long(*VersionA.Version)
  Declare From_Long(*VersionA.Version, LongVar.l)
  
EndDeclareModule

Module Version

  Procedure Update(*VersionA.Version, P_Major.a, P_Minor.a, P_Patch.a)

    *VersionA\Major = P_Major
    *VersionA\Minor = P_Minor
    *VersionA\Patch = P_Patch

  EndProcedure

  Procedure Reset(*VersionA.Version)
    
    *VersionA\Major = 0
    *VersionA\Minor = 0
    *VersionA\Patch = 0
    
  EndProcedure

  Procedure Compare(*VersionA.Version, *VersionB.Version)

    If *VersionA\Major <> *VersionB\Major
      ProcedureReturn #False
    EndIf

    If *VersionA\Minor <> *VersionB\Minor
      ProcedureReturn #False
    EndIf

    If *VersionA\Patch <> *VersionB\Patch
      ProcedureReturn #False
    EndIf
    
    ProcedureReturn #True
  EndProcedure

  Procedure ReadVersion(FileID.l, *VersionA.Version)
    
    *VersionA\Major = ReadAsciiCharacter(FileID)
    *VersionA\Minor = ReadAsciiCharacter(FileID)
    *VersionA\Patch = ReadAsciiCharacter(FileID)
    
  EndProcedure

  Procedure WriteVersion(FileID.l, *VersionA.Version)
    
    WriteAsciiCharacter(FileID, *VersionA\Major)
    WriteAsciiCharacter(FileID, *VersionA\Minor)
    WriteAsciiCharacter(FileID, *VersionA\Patch)
    
  EndProcedure

  Procedure.s Format(*VersionA.Version)
    
    ProcedureReturn "V" + Str(*VersionA\Major) + "." + Str(*VersionA\Minor) + "." + Str(*VersionA\Patch)
    
  EndProcedure

  Procedure Unformat(*VersionA.Version, P_Version.s)
    
    *VersionA\Major = Val(StringField(StringField(P_Version, 2, "V"), 1, "."))
    *VersionA\Minor = Val(StringField(StringField(P_Version, 2, "V"), 2, "."))
    *VersionA\Patch = Val(StringField(StringField(P_Version, 2, "V"), 3, "."))
    
  EndProcedure

  Procedure.l To_Long(*VersionA.Version)
    
    ProcedureReturn (*VersionA\Patch) << 16 + (*VersionA\Minor) << 8 + (*VersionA\Major)
  EndProcedure

  Procedure From_Long(*VersionA.Version, LongVar.l)
    
    *VersionA\Major = (LongVar) & $FF
    *VersionA\Minor = (LongVar >> 8) & $FF
    *VersionA\Patch = (LongVar >> 16) & $FF
    
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  Version::Update(Alpha.Version::Version, 1, 2, 3)
  Ver.l = Version::To_Long(Alpha)
  FormatedVersion.s = Version::Format(Alpha)
  Version::Unformat(Beta.Version::Version, FormatedVersion)
  Version::From_Long(Gamma.Version::Version, Ver)
  
  Debug FormatedVersion
  Debug Version::Format(Beta)
  Debug Ver
  Debug Version::Format(Gamma)
  
CompilerEndIf
Best regards
StarBootics