Page 1 of 2

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Sun Feb 14, 2010 11:52 pm
by Demivec
Olliv wrote:Please decelerate! Code by code...
@Olliv: I will go slower.
Olliv wrote:Is it better? :D
No.

The code does not display properly using DirectX9 at any speed.

I modified it to display the same things using DirectX9 as it does using DirectX7 by removing the alpha channel from the image (it was completely opaque), the alphaBlend from the sprite and by not using a 3D Sprite. The working version for Code 1 follows: :)

Code: Select all

Procedure CreateTileTest(No.I, Wi.I, He.I)
  CreateImage(No, Wi, He, 24)
  StartDrawing(ImageOutput(No) )
    Box(0, 0, Wi, He, RGB(255, 255, 255) )
    Box(1, 1, Wi - 2, He - 2, RGB(0, 0, 0) )
  StopDrawing()
  CreateSprite(No, Wi, He)
  StartDrawing(SpriteOutput(No) )
    DrawImage(ImageID(No), 0, 0)
  StopDrawing()
EndProcedure

InitSprite()
InitKeyboard()
ExamineDesktops()
Dw = DesktopWidth(0)
Dh = DesktopHeight(0)
Dd = DesktopDepth(0)
 
Wi = 32
He = 32
OpenScreen(Dw, Dh, Dd, "")
CreateTileTest(1, Wi, He)

Repeat
  Delay(1)
  For XS3 = 0 To Dw - 1 Step 32
    For YS3 = 0 To Dh - 1 Step 32
      DisplaySprite(1, XS3 + OffsetX, YS3)
    Next
  Next
  FlipBuffers()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Right)
    If OffsetXAccel > -8
      OffsetXAccel - 2
    EndIf
  EndIf
  
  If KeyboardPushed(#PB_Key_Left)
    If OffsetXAccel < 8
      OffsetXAccel + 2
    EndIf
  EndIf
  
  OffsetX + OffsetXAccel
  If OffsetX > 31
    OffsetX - 32
  EndIf
  If OffsetX < 0
    OffsetX + 32
  EndIf
  If OffsetXAccel > 0
    OffsetXAccel - 1
  EndIf
  If OffsetXAccel < 0
    OffsetXAccel + 1
  EndIf
Until KeyboardPushed(#PB_Key_Escape)
CloseScreen()

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Mon Feb 15, 2010 3:20 am
by Demivec
@Olliv: Code 3 does not work with DirectX9 because of UseBuffer().
The UseBuffer() isn't supported with the DirectX9 subsystem, only with DirectX7.
That is something that has been true for at least the past 8 months or so (since DirectX9 was made the default). I don't think there is a solution to that problem yet.

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Mon Feb 15, 2010 2:01 pm
by Olliv
Demivec wrote:
Olliv wrote:Please decelerate! Code by code...
@Olliv: I will go slower.
Olliv wrote:Is it better? :D
No.

The code does not display properly using DirectX9 at any speed.

I modified it to display the same things using DirectX9 as it does using DirectX7 by removing the alpha channel from the image (it was completely opaque), the alphaBlend from the sprite and by not using a 3D Sprite. The working version for Code 1 follows: :)

Code: Select all

;Code
@Demivec

I thank you for your answer, the full explanation and the corrected code.

For this first code, is there other problems, or technical remarks? (the uglyness of the displayed grid being excepted...)

Ollivier

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Mon Feb 15, 2010 10:03 pm
by Demivec
Olliv wrote:@Demivec

I thank you for your answer, the full explanation and the corrected code.

For this first code, is there other problems, or technical remarks? (the uglyness of the displayed grid being excepted...)
Just one problem. The gridlines on the left border are not overwritten properly unless OffsetX is zero. When doing fine scrolling (scrolling that is less than a tile's dimensions) an additional partial tile must also be drawn at the border. This can be corrected with a small change also:

Code: Select all

  For XS3 = -32 To Dw - 1 Step 32 ;start one tile to the left
    For YS3 = 0 To Dh - 1 Step 32 
      DisplaySprite(1, XS3 + OffsetX, YS3)
    Next
  Next

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Tue Feb 16, 2010 12:12 am
by Olliv
Index
Previous example

@Demivec

You are right. Thank you for you to loose time to explain me different problems or compatibility.
After these corrections, I updated the first code. (I didn't remove the alpha channel neither the using of sprite 3D. Because sprites 3D get specific transformations which could be very useful for the future effects. If we remove also alpha channel and sprite 3D, processing ressources are different and allow us to use parallax which is executed in the 3rd code.)

Even if it's not electronic, this scrolling could be a good occasion to create a game, studying the basic mechanism.

Code: Select all

; >>>>  /!\ ONLY WITH DX7 (PB4.40)  <<<<

Procedure RedStone(X.I, Y.I, W.I, H.I)
    Box(X + 0, Y + 0, W - 1, H - 1, RGBA(255, 0, 0, 255) )
    Box(X + 1, Y + 1, W - 1, H - 1, RGBA(64, 0, 0, 255) )
    Box(X + 1, Y + 1, W - 2, H - 2, RGBA(128, 0, 0, 255) )
EndProcedure

Procedure CreateTileTest(No.I, Wi.I, He.I)
  CreateImage(No, Wi, He, 32)
  StartDrawing(ImageOutput(No) )
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0, 0, Wi, He, RGBA(0, 0, 0, 0) )
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    For K = 0 To 1
      For J = -2 To 2 Step 2
        For I = 0 To 1
          RedStone((I + J) * 8, I * 8 + K * 16, 16, 8)
        Next
      Next
    Next
  StopDrawing()
  CreateSprite(No, Wi, He, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(No) )
    DrawAlphaImage(ImageID(No), 0, 0)
  StopDrawing()
  CreateSprite3D(No, No)
  Start3D()
    DisplaySprite3D(No, 0, 0)
  Stop3D()
EndProcedure

Procedure CreateTileMono(No.I, Wi.I, He.I, Coef.I)
  CreateImage(No, Wi, He, 32)
  StartDrawing(ImageOutput(No) )
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0, 0, Wi, He, RGBA(0, 0, 0, 0) )
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    For I = 0 To 15
      Color = RGB(0, Coef * 8 + (7 - I / 2), Coef * 16 + (15 - I) )
      Box(0, I * 2, Wi, 2, RGBA(Red(Color), Green(Color), Blue(Color), 255) )
    Next
  StopDrawing()
  CreateSprite(No, Wi, He, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(No) )
    DrawAlphaImage(ImageID(No), 0, 0)
  StopDrawing()
  CreateSprite3D(No, No)
  Start3D()
    DisplaySprite3D(No, 0, 0)
  Stop3D()
EndProcedure

  InitSprite()
  InitSprite3D()
  InitKeyboard()
  ExamineDesktops()
  Dw = DesktopWidth(0)
  Dh = DesktopHeight(0)
  Dd = DesktopDepth(0)
  
  Wi = 32
  He = 32
  OpenScreen(Dw, Dh, Dd, "")
    CreateTileTest(1, Wi, He)
    For I = 0 To 15
      CreateTileMono(I + 2, Wi, He, I)
    Next I
    Global Dim Tile(255, 31)
    For I = 0 To 31
      For X = 0 To 255
        Tile(X, I) = 1
      Next
    Next
    For I = 0 To 15
      For X = 0 To 255
        Tile(X, 15 - I) = I + 2
      Next
    Next
    For X = 0 To 255
      For I = Int(10. + Cos(X / 8.) * 8.) To 15
        Tile(X, I) = 1
      Next
    Next

    Repeat
      Delay(1)
      Start3D()
        If OffsetTileX <= 0
          OffsetTileX = 0
          OffsetX = 0
        EndIf
        If OffsetTileX => ((255 - (Dw / 32) ) )
          OffsetTileX = ((255 - (Dw / 32) ) )
          OffsetX = 32 - 1
        EndIf
        For XS3 = 0 - 32 To Dw - 1 Step 32
          For YS3 = 0 To Dh - 1 Step 32
            ;Debug Str(XS3 / 32 + OffsetTileX) + "; " + Str(YS3 / 32)
            XX = XS3 / 32 + OffsetTileX
            If XX => 0
              Tile = Tile(XX, YS3 / 32)
              If Tile
                DisplaySprite3D(Tile, XS3 + OffsetX, YS3)
              EndIf
            EndIf
          Next
        Next
      Stop3D()
      FlipBuffers()
      ExamineKeyboard()
      If KeyboardPushed(#PB_Key_Right)
        If OffsetXAccel > -16
          OffsetXAccel - 2
        EndIf
      EndIf
      
      If KeyboardPushed(#PB_Key_Left)
        If OffsetXAccel < 16
          OffsetXAccel + 2
        EndIf
      EndIf
      OffsetX + OffsetXAccel
      If OffsetX > 31
        OffsetX - 32
        OffsetTileX - 1
      EndIf
      If OffsetX < 0
        OffsetX + 32
        OffsetTileX + 1
      EndIf
      If OffsetXAccel > 0
        OffsetXAccel - 1
      EndIf
      If OffsetXAccel < 0
        OffsetXAccel + 1
      EndIf
    Until KeyboardPushed(#PB_Key_Escape)
  CloseScreen()
  

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Tue Feb 16, 2010 9:17 am
by Michael Vogel
Looks nice, I got remembered to 1980 when I saw the video game scramble and tried to implement that on my PET 2001...

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Tue Feb 16, 2010 11:01 am
by Demivec
@Olliv: Here is a small adjustment needed for the example posted for large view screens. I present it merely to keep your example working and hopefully it is not considered an irritant. :wink:

It is presented in two parts to avoid posting all the intervening code

Code: Select all

Wi = 32
He = 32
OpenScreen(Dw, Dh, Dd, "")
;Add the following lines to test if screen is larger vertically than needed and set maxheight 'Dh2' appropriately
If Dh > ((He - 1) * 32)
  Dh2 = ((He - 1) * 32)
Else
  Dh2 = Dh
EndIf 
This portion modifies only the line at the start of the inner display loop with YS3 (changing 'Dh' to 'Dh2').

Code: Select all

For XS3 = 0 - 32 To Dw - 1 Step 32
      For YS3 = 0 To Dh2 - 1 Step 32 ; <== use maxheight based on screen and map size
        ;Debug Str(XS3 / 32 + OffsetTileX) + "; " + Str(YS3 / 32)
        XX = XS3 / 32 + OffsetTileX
        If XX => 0
          Tile = Tile(XX, YS3 / 32)
          If Tile
            DisplaySprite3D(Tile, XS3 + OffsetX, YS3)
          EndIf
        EndIf
      Next
    Next
The difference is shown in two thumbnails, the left one is before the change.

Image Image

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Tue Feb 16, 2010 11:48 am
by Kaeru Gaman
could a Moderator please cut the oldskool lessons off the discussing of the book?

I love oldskool and everything about tile-engines is my profession, but these are two topics, the original topic was the book.

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Tue Feb 16, 2010 11:54 am
by Demivec
Kaeru Gaman wrote:could a Moderator please cut the oldskool lessons off the discussing of the book?

I love oldskool and everything about tile-engines is my profession, but these are two topics, the original topic was the book.
I second the request.

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Tue Feb 16, 2010 2:31 pm
by Olliv
I understand nothing but I second the request too :mrgreen:

We could create an other topic in the coding question section about the problems a coder meets when he want to create a scrolling game. For the 3 codes I publish, there are already in the forum.

Re: 2D Scrolling (Tiles) Eingine examples

Posted: Tue Feb 16, 2010 3:03 pm
by Rings
splitted ;)

Re: 2D Scrolling (Tiles) Eingine examples

Posted: Tue Feb 16, 2010 6:24 pm
by Olliv
Thank you Rings!

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Wed Feb 17, 2010 9:51 pm
by Olliv
Demivec wrote:@Olliv: Here is a small adjustment needed for the example posted for large view screens. I present it merely to keep your example working and hopefully it is not considered an irritant. :wink:

It is presented in two parts to avoid posting all the intervening code

Code: Select all

Wi = 32
He = 32
OpenScreen(Dw, Dh, Dd, "")
;Add the following lines to test if screen is larger vertically than needed and set maxheight 'Dh2' appropriately
If Dh > ((He - 1) * 32)
  Dh2 = ((He - 1) * 32)
Else
  Dh2 = Dh
EndIf 
This portion modifies only the line at the start of the inner display loop with YS3 (changing 'Dh' to 'Dh2').

Code: Select all

For XS3 = 0 - 32 To Dw - 1 Step 32
      For YS3 = 0 To Dh2 - 1 Step 32 ; <== use maxheight based on screen and map size
        ;Debug Str(XS3 / 32 + OffsetTileX) + "; " + Str(YS3 / 32)
        XX = XS3 / 32 + OffsetTileX
        If XX => 0
          Tile = Tile(XX, YS3 / 32)
          If Tile
            DisplaySprite3D(Tile, XS3 + OffsetX, YS3)
          EndIf
        EndIf
      Next
    Next
The difference is shown in two thumbnails, the left one is before the change.

Image Image
@Demivec

Well... Excuse me if come back again to you, but I understand nothing about your last observation.

I have two questions I hope you have time to answer:

1) What are the conditions of your last observation?
> Only for unidirectionnal scrolling (just vertical or just horizontal)? Or for multi-directionnal scrolling?

2) Is the left picture really what you see on your computer? Or is it a representative (symbolic) picture which announces a possible bug depending the specific dimensions on different computers?

Thank you.

Ollivier

Re: Where can I get the <Programming 2D scrolling games>?

Posted: Thu Feb 18, 2010 2:42 am
by Demivec
Olliv wrote:Image Image
@Demivec

Well... Excuse me if come back again to you, but I understand nothing about your last observation.

I have two questions I hope you have time to answer:

1) What are the conditions of your last observation?
> Only for unidirectionnal scrolling (just vertical or just horizontal)? Or for multi-directionnal scrolling?

2) Is the left picture really what you see on your computer? Or is it a representative (symbolic) picture which announces a possible bug depending the specific dimensions on different computers?
1: It is for your horizontal scrolling example only. The simple fix I posted could be improved further by possibly centering the map vertically on the visible screen.
2: Yes. I have a larger screen view than you did and thus I see the map repeating (with an offset column) to fill the larger screen view. The same would happen if the map being used had been narrower than the screen.