Author Topic: OpenGl Pong: simple demonstration of OpenGl 2D graphic vs standard graphic  (Read 6565 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi friends
here a demo that I was doing some time ago.
For now I post the essential module in which I show how to manage data and pass it to OpenGl function that are constrained into _GL() sub.

If I get some feedback of interest I'll complete all the demonstration to get the status of simple complete demo adding sounds, scores, etc etc etc.

Help instructions:
press Enter key to swap between Standard and OpenGl mode (you can recognize it from the shape of ball like hexagon) move pad of players with left/right arrows and A / S.

waiting feedbacks
Thanks to try
* 1OpenGl Pong v.1.0.bas (Filesize: 10.2 KB, Downloads: 210)
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Hi @TempodiBasic

Not seeing any gl advantage, are you?

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi Bplus
glad to get your feedback

Yes I confirm the demonstration hasn't the goal to show differences and advantages of OpenGl using GPU vs standard graphic using CPU.
In fact this code shows simply how to accomplish one of the easiest game (Pong) in minimalistic graphic structure using both OpenGl features both standard graphic features.
Why this so simple example?
It seems that in our section of forum dedicated to OpenGl there are so many wonderful 3D graphic examples and no about basic steps with OpenGl...https://www.qb64.org/forum/index.php?board=21.0  so here a skeleton of 2D game engine using OpenGl.
The user beginner in OpenGl can go on in OpenGl practice starting from a simpler position and then can approach to 3D graphic tecniques. Before squareroot he can learn sum and multiplication. ;-)

Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Well I think it noble and admirable to get _gl conversation going. And it makes sense to me to start with less complex 2D programming. Got to say I don't like your example too much and for discussion lets post the code (even though at moment code boxes suck, let us assume or hope that will be fixed):
Code: QB64: [Select]
  1. ' OpenGl Pong
  2. DefLng L ' Longs start with L
  3. DefInt I ' Integers start with I
  4. DefStr S ' Strings start with S
  5. DefSng N 'Singles start with N
  6.  
  7. ' type of data called OBJECT
  8. Type obj
  9.     X As Integer
  10.     Y As Integer
  11.     Width1 As Integer
  12.     Height As Integer
  13.     Color1 As _Unsigned Long
  14.     Active As Integer
  15.     SpeedX As Integer
  16.     SpeedY As Integer
  17.  
  18. Const True = -1, False = 0, Left = 19200, Right = 19712, Escape = 27
  19. Const Enter = 13, iPlayer1 = 1, iPlayer2 = 2
  20. Dim lScreen
  21. Dim Shared iKeyb(1 To 2) As Integer
  22. Dim Shared iActiveGl, myScreen As obj, Pad(1 To 2) As obj, Ball As obj
  23. Dim Shared InitGl As Integer, iBallGl As Integer
  24. Dim Shared iPadGl1 As Integer, iPadGl2 As Integer
  25.  
  26. lScreen = _NewImage(800, 600, 32) ' graphic
  27. Screen lScreen ' graphic  creating the window of the program
  28. '  wait that window has been created
  29. _Delay 2 'Do While Not _ScreenExists: Loop
  30. _Title "OpenGl Pong v.1.0: press Enter to switch between normal vs OpenGl mode"
  31.  
  32.  
  33. Init
  34.  
  35. Do While (iKeyb(iPlayer1) <> Escape) Or (iKeyb(iPlayer2) <> Escape)
  36.     GetInput
  37.     ' stardard graphic mode
  38.     If iActiveGl = False Then
  39.         Border
  40.         Showpad iPlayer1
  41.         Showpad iPlayer2
  42.         ShowBall
  43.         Call Message(1, 1, "press Escape to quit, A/S player 1 Left/Right, cursor Left/Right player 2")
  44.         '    Else
  45.         '  GL() graphic mode
  46.  
  47.         ' Nothing to do here  go to _GL()
  48.     End If
  49.  
  50.     ' calculations for new screenshot
  51.     MovePad
  52.     MoveBall
  53.  
  54.     ' this toggles between standard graphic mode and  Gl graphic mode
  55.     If iKeyb(iPlayer1) = Enter Or iKeyb(iPlayer2) = Enter Then iActiveGl = Not iActiveGl
  56.     ' saving CPU usage
  57.     _Limit 20
  58.  
  59. ' area GL--------------------------------------------------------------
  60.  
  61. Sub _GL ()
  62.     If iActiveGl = 0 Then Exit Sub
  63.     If InitGl Then InitGlSub
  64.     ClearScreen
  65.  
  66.     If iBallGl = True Then BallGl
  67.     If iPadGl1 Then PADgL1
  68.     If iPadGl2 Then PADgL2
  69.     _glFlush
  70.  
  71. Sub BallGl
  72.     Dim nGc, nBc, nRc, nAc
  73.     Dim e As Single, gXb As Single, gYb As Single
  74.     ComponentColor Ball.Color1
  75.     If SetColorGL!(nGc, nBc, nRc, nAc) Then
  76.         _glBegin _GL_TRIANGLE_FAN
  77.         For e = 1 To (_Pi(2)) Step (2 * _Pi / 6)
  78.             gXb = Proportion!(0, _Width, -1, 1, Ball.X + (Ball.Width1 * Cos(e)))
  79.             gYb = Proportion!(0, _Height, 1, -1, Ball.Y + (Ball.Height * Sin(e)))
  80.             _glVertex2f gXb, gYb
  81.         Next e
  82.         _glEnd
  83.     End If
  84.  
  85. Sub PADgL1
  86.     Dim nGc, nBc, nRc, nAc
  87.     Dim nx1, nx2, ny1, ny2, dummy
  88.     ComponentColor Pad(iPlayer1).Color1
  89.     If SetColorGL!(nGc, nBc, nRc, nAc) Then
  90.         nx1 = Proportion(0, _Width, -1, 1, Pad(iPlayer1).X)
  91.         ny1 = Proportion(0, _Height, 1, -1, Pad(iPlayer1).Y)
  92.         ny2 = Proportion(0, _Height, 1, -1, Pad(iPlayer1).Y + Pad(iPlayer1).Height)
  93.         nx2 = Proportion(0, _Width, -1, 1, Pad(iPlayer1).X + Pad(iPlayer1).Width1)
  94.         _glBegin _GL_QUADS
  95.         _glVertex2f nx1, ny1
  96.         _glVertex2f nx1, ny2
  97.         _glVertex2f nx2, ny2
  98.         _glVertex2f nx2, ny1
  99.         _glEnd
  100.         ComponentColor _RGB32(0)
  101.         dummy = SetColorGL!(nGc, nBc, nRc, nAc)
  102.         nx1 = Proportion(0, _Width, -1, 1, Pad(iPlayer1).X + 2)
  103.         ny1 = Proportion(0, _Height, 1, -1, Pad(iPlayer1).Y + 2)
  104.         ny2 = Proportion(0, _Height, 1, -1, Pad(iPlayer1).Y + Pad(iPlayer1).Height - 2)
  105.         nx2 = Proportion(0, _Width, -1, 1, Pad(iPlayer1).X + Pad(iPlayer1).Width1 - 2)
  106.         _glBegin _GL_LINE_LOOP
  107.         _glVertex2f nx1, ny1
  108.         _glVertex2f nx1, ny2
  109.         _glVertex2f nx2, ny2
  110.         _glVertex2f nx2, ny1
  111.         _glEnd
  112.  
  113.     End If
  114.  
  115. Sub PADgL2
  116.     Dim nGc, nBc, nRc, nAc
  117.     Dim nx1, nx2, ny1, ny2, dummy
  118.     ComponentColor Pad(iPlayer2).Color1
  119.     If SetColorGL!(nGc, nBc, nRc, nAc) Then
  120.         nx1 = Proportion(0, _Width, -1, 1, Pad(iPlayer2).X)
  121.         ny1 = Proportion(0, _Height, 1, -1, Pad(iPlayer2).Y)
  122.         ny2 = Proportion(0, _Height, 1, -1, Pad(iPlayer2).Y + Pad(iPlayer2).Height)
  123.         nx2 = Proportion(0, _Width, -1, 1, Pad(iPlayer2).X + Pad(iPlayer2).Width1)
  124.         _glBegin _GL_QUADS
  125.         _glVertex2f nx1, ny1
  126.         _glVertex2f nx1, ny2
  127.         _glVertex2f nx2, ny2
  128.         _glVertex2f nx2, ny1
  129.         _glEnd
  130.         ComponentColor _RGB32(0)
  131.         dummy = SetColorGL!(nGc, nBc, nRc, nAc)
  132.         nx1 = Proportion(0, _Width, -1, 1, Pad(iPlayer2).X + 2)
  133.         ny1 = Proportion(0, _Height, 1, -1, Pad(iPlayer2).Y + 2)
  134.         ny2 = Proportion(0, _Height, 1, -1, Pad(iPlayer2).Y + Pad(iPlayer2).Height - 2)
  135.         nx2 = Proportion(0, _Width, -1, 1, Pad(iPlayer2).X + Pad(iPlayer2).Width1 - 2)
  136.         _glBegin _GL_LINE_LOOP
  137.         _glVertex2f nx1, ny1
  138.         _glVertex2f nx1, ny2
  139.         _glVertex2f nx2, ny2
  140.         _glVertex2f nx2, ny1
  141.         _glEnd
  142.     End If
  143.  
  144. Sub ClearScreen
  145.     _glClear _GL_COLOR_BUFFER_BIT
  146.  
  147. Function SetColorGL! (nGc, nRc, nBc, nAc)
  148.     SetColorGL! = False
  149.     nGc = Proportion(1, 255, 0, 1, Gc)
  150.     nRc = Proportion(1, 255, 0, 1, Rc)
  151.     nBc = Proportion(1, 255, 0, 1, Bc)
  152.     nAc = Proportion(1, 255, 0, 1, Ac)
  153.     _glColor3f nRc, nGc, nBc
  154.     SetColorGL! = True
  155.  
  156. Sub InitGlSub
  157.     'settings viewport for GL screen
  158.     _glViewport myScreen.X, myScreen.Y, myScreen.Width1, myScreen.Height
  159.     Dim nGc, nBc, nRc, nAc
  160.     ComponentColor myScreen.Color1
  161.     If SetColorGL!(nGc, nRc, nBc, nAc) Then
  162.         _glClearColor nRc, nGc, nBc, nAc
  163.     Else
  164.         Print "Error"
  165.     End If
  166.     InitGl = False
  167.  
  168.  
  169. ' area sub/functions*****************************************************
  170. Function Proportion (imin1, imax1, imin2, imax2, ivalue)
  171.     Dim iDelta1, iDelta2
  172.     iDelta1 = (imax1 - imin1)
  173.     iDelta2 = (imax2 - imin2)
  174.     Proportion = ((ivalue * iDelta2) / iDelta1) + imin2
  175.  
  176.  
  177.  
  178. Sub ComponentColor (C As _Unsigned Long)
  179.     Gc = _Green32(C)
  180.     Rc = _Red32(C)
  181.     Bc = _Blue32(C)
  182.     Ac = _Alpha32(C)
  183.  
  184. Sub Init
  185.     ' window of program
  186.     myScreen.X = 0
  187.     myScreen.Y = 0
  188.     myScreen.Width1 = _Width
  189.     myScreen.Height = _Height
  190.     myScreen.Color1 = _RGB32(200, 200, 50)
  191.     myScreen.Active = True
  192.  
  193.     ' ball
  194.     Ball.X = 400
  195.     Ball.Y = 300
  196.     Ball.Active = True
  197.     Ball.Width1 = 10
  198.     Ball.Height = 10
  199.     Ball.SpeedX = 3
  200.     Ball.SpeedY = 3
  201.     Ball.Color1 = _RGB32(200, 10, 10)
  202.  
  203.     'PAD player1
  204.     Pad(iPlayer1).X = 350
  205.     Pad(iPlayer1).Y = 400
  206.     Pad(iPlayer1).Width1 = 100
  207.     Pad(iPlayer1).Height = 30
  208.     Pad(iPlayer1).Color1 = _RGB32(40, 230, 200)
  209.     Pad(iPlayer1).SpeedX = 0
  210.     Pad(iPlayer1).SpeedY = 0
  211.     Pad(iPlayer1).Active = True
  212.  
  213.  
  214.     'PAD player2
  215.     Pad(iPlayer2).X = 350
  216.     Pad(iPlayer2).Y = 150
  217.     Pad(iPlayer2).Width1 = 100
  218.     Pad(iPlayer2).Height = 30
  219.     Pad(iPlayer2).Color1 = _RGB32(230, 130, 220)
  220.     Pad(iPlayer2).SpeedX = 0
  221.     Pad(iPlayer2).SpeedY = 0
  222.     Pad(iPlayer2).Active = True
  223.  
  224.     'Gl
  225.     InitGl = True
  226.     iBallGl = True
  227.     iPadGl1 = True
  228.     iPadGl2 = True
  229.     iActiveGl = False ' at the start the graphic is normal
  230.  
  231. ' Graphic normal---------------
  232. Sub Showpad (P As Integer)
  233.     If Pad(P).Active Then
  234.         Line (Pad(P).X, Pad(P).Y)-Step(Pad(P).Width1, Pad(P).Height), Pad(P).Color1, BF
  235.         Line (Pad(P).X, Pad(P).Y)-Step(Pad(P).Width1, Pad(P).Height), _RGB32(0), B
  236.     End If
  237.  
  238. Sub ShowBall
  239.     Circle (Ball.X, Ball.Y), Ball.Width1, Ball.Color1
  240.     Paint Step(0, 0), Ball.Color1, Ball.Color1
  241.  
  242. Sub Border
  243.     Line (myScreen.X + 1, myScreen.Y + 1)-(myScreen.X + myScreen.Width1 - 1, myScreen.Y + myScreen.Height - 1), myScreen.Color1, BF
  244.  
  245. ' movement and I/O area-----------------------------
  246. Sub Message (iWhereX As Integer, iWhereY As Integer, sMessage As String)
  247.     Locate iWhereY, iWhereX
  248.     Print sMessage;
  249.  
  250. Sub MovePad
  251.     Dim i
  252.     For i = iPlayer1 To iPlayer2 Step 1
  253.         If iKeyb(i) = Right Then Pad(i).X = Pad(i).X + 10
  254.         If Pad(i).X > myScreen.Width1 - Pad(i).Width1 Then Pad(i).X = myScreen.Width1 - Pad(i).Width1
  255.         If iKeyb(i) = Left Then Pad(i).X = Pad(i).X - 10
  256.         If Pad(i).X < myScreen.X Then Pad(i).X = myScreen.X
  257.     Next
  258.  
  259. Sub MoveBall
  260.     Ball.X = Ball.X + Ball.SpeedX
  261.     Ball.Y = Ball.Y + Ball.SpeedY
  262.     ' bouncing control
  263.     'vs walls
  264.     If Ball.X < 0 Then Ball.X = Int(Ball.Width1 / 2) + 1: Ball.SpeedX = -Ball.SpeedX
  265.     If Ball.X > myScreen.Width1 - Int(Ball.Width1 / 2) + 1 Then Ball.X = myScreen.Width1 - Int(Ball.Width1 / 2) + 1: Ball.SpeedX = -Ball.SpeedX
  266.     If Ball.Y < 0 Then Ball.Y = Int(Ball.Height / 2) + 1: Ball.SpeedY = -Ball.SpeedY
  267.     If Ball.Y > myScreen.Height - Int(Ball.Height / 2) + 1 Then Ball.Y = myScreen.Height - Int(Ball.Height / 2) + 1: Ball.SpeedY = -Ball.SpeedY
  268.     ' vs player1
  269.     If Abs(Pad(iPlayer1).Y + Int(Pad(iPlayer1).Height / 2) - (Ball.Y + Int(Ball.Height / 2))) < Int((Pad(iPlayer1).Height + Ball.Height) / 2) Then
  270.         If Abs((Pad(iPlayer1).X + Int(Pad(iPlayer1).Width1 / 2)) - (Ball.X + Int(Ball.Width1 / 2))) < Int((Pad(iPlayer1).Width1 + Ball.Width1) / 2) Then
  271.             Ball.SpeedY = -Ball.SpeedY
  272.         End If
  273.     End If
  274.     ' vs player2
  275.     If Abs(Pad(iPlayer2).Y + Int(Pad(iPlayer2).Height / 2) - (Ball.Y + Int(Ball.Height / 2))) < Int((Pad(iPlayer2).Height + Ball.Height) / 2) Then
  276.         If Abs((Pad(iPlayer2).X + Int(Pad(iPlayer2).Width1 / 2)) - (Ball.X + Int(Ball.Width1 / 2))) < Int((Pad(iPlayer2).Width1 + Ball.Width1) / 2) Then
  277.             Ball.SpeedY = -Ball.SpeedY
  278.         End If
  279.     End If
  280.  
  281. Sub GetInput
  282.     ' it resets command variables
  283.     iKeyb(iPlayer1) = 0
  284.     iKeyb(iPlayer2) = 0
  285.     ' keyboard input
  286.     iKeyb(iPlayer1) = _KeyHit
  287.     If _KeyDown(115) Or _KeyDown(83) Then iKeyb(iPlayer2) = Right Else If iKeyb(iPlayer2) = Right Then iKeyb(iPlayer2) = False
  288.     If _KeyDown(65) Or _KeyDown(97) Then iKeyb(iPlayer2) = Left Else If iKeyb(iPlayer2) = Left Then iKeyb(iPlayer2) = False
  289.     If _KeyDown(27) Then iKeyb(iPlayer2) = Escape
  290.     If _KeyDown(13) Then iKeyb(iPlayer2) = Enter
  291.     '  Do: Loop Until _KeyHit = 0
  292.     ' mouse input
  293.     ' -->in developing
  294.     'joystick input
  295.     '-->in developing
  296.  
  297.  

I think I have enough experience with Ping Pong games that we dont have to compare and complicate code by showing regular version versus gl. I suggest pure stripped down gl version. I also suggest you can use one sub to draw a paddle for either player and is using proportion to find coordinates of paddle really necessary? yuck

It would be nice to play against AI because 2 player is stupid (=not fun, how can 2 people sit at one computer who wants to play against them self?) IMHO.

Also I find a bug with Help getting a blank screen when trying to checkout this:
_glVertex2f



Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Is this also necessary? and again blank on _glColor3f from Help

Code: QB64: [Select]
  1. Function SetColorGL! (nGc, nRc, nBc, nAc)
  2.     SetColorGL! = False
  3.     nGc = Proportion(1, 255, 0, 1, Gc)
  4.     nRc = Proportion(1, 255, 0, 1, Rc)
  5.     nBc = Proportion(1, 255, 0, 1, Bc)
  6.     nAc = Proportion(1, 255, 0, 1, Ac)
  7.     _glColor3f nRc, nGc, nBc  ' ................................. what's 3f for?
  8.     SetColorGL! = True
  9.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
  ' ................................. what's 3f for?

In GL, the numbers and letters at the end specify what you’re passing to your routine.

GlColor3f — the 3 says you’re passing 3 values, the f says they’re floating point values.
GlColor4f — 4 values (Red, Green, Blue, Alpha vs RGB), and they’re floats.
GlColor3i — 3 values (RGB), and they’re integers.

And so on…
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
OK so the color values 0 to 255 in _RGB32 function have to be converted to 0 to 1 for GL?

And the same goes for coordinates?

If so then maybe yes it is helpful to put the 2 ways side by side in single example. :)
« Last Edit: August 28, 2021, 12:36:39 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
OK so the color values 0 to 255 in _RGB32 function have to be converted to 0 to 1 for GL?

And the same goes for coordinates?

Or you can use GlColor*UB for unsigned byte values, which is basically the RGB32 values that you’d plug into QB64 normally.  (Replace * with 3 for RGB, 4 for RGBA)

Float (*f) is a value from 0.0 to 1.0.  Integer (*i) …. I don’t remember off the top of my head.   Unsigned Byte (*ub) is a value from 0 to 255, which we normally use in QB64.

« Last Edit: August 28, 2021, 12:39:55 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Yeah wow! I just did quick scan of GL in Help, like a whole other language.

Maybe @FellippeHeitor would like to do some Intro programs for GL?

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
You can avoid the conversion between normal QB screen coordinates to GL coordinates by using some transformation before
drawing.
for example -
Code: QB64: [Select]
  1. Screen _NewImage(600, 600, 32)
  2.  
  3.     _Limit 1
  4.  
  5. Sub _GL ()
  6.     _glMatrixMode _GL_MODELVIEW
  7.     _glTranslatef -1, 1, 0
  8.     _glScalef 1 / (0.5 * _Width(0)), -1 / (0.5 * _Height(0)), 1
  9.  
  10.     _glBegin _GL_TRIANGLES 'normal qb screen coordinates
  11.     _glVertex2f 300, 0
  12.     _glVertex2f 600, 600
  13.     _glVertex2f 0, 600
  14.     _glEnd
  15.  
  16.     _glFlush
  17.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi
my pleasure to get the attention and the joining in thread of all you!

 
All the info showed by you are very important for a beginner of Opengl.

1. Yes Opengl uses own screen dimensions and Cartesian axis, own range value for _RGB system, own coordinates to map images to use as sprite or as textures.

2. (i dunno if it is different in other languages of programming) but in QB64 you can use Opengl commands only into _GL() sub

3. In running mode _GL sub runs about at 60FPS (frequency or rate for second  and NOT frame for second)

4. So you can imagine that you cannot make I/O or calculations or loops or _delays in it except for those that don't  break this its nature to run always fast. (so you get that without a blocking loop you can use Inkey$ or _Keyhit  or _Keydown in _GL sub)

5.  Ashish  has already posted an alternative solution to the problem of translate the coordinates between Screen setting and Opengl metric system.  Other 2 solutions are A) that used in the code that translates on fly at need the coordinates or
B) set the Screen coordinates equal to Opengl cohordinates by using WINDOW  command of QB. For example
Code: QB64: [Select]
  1. S = _Newimage (800,600,32)
  2. Window (-1, - 1) - (1,1)
  3.  

A bit more complex is to use a matrix (bidimensionale array) that stores value already calculated. It is evident when you try to make a Plasma code into Opengl.

5. The screen of Opengl is always void! This means that all those commands and techniques that relies their own efficacy on the information stored in graphic memory (think about point() or screen and all the others commands)  are without importance here. The easy workaround is to store those informations into a matrix of 2 dimensional array) of unsigned long for a 32bit color screen.


@Ashish
your Opengl tutorial is very good, but as the general tutorial, it doesn't cover all the features of Opengl commands nor all Opengl commands  nor how using Opengl  to do.....
Nevertheless I find it not only for beginners because it is plenty of informations.
As I said you some time ago, if it is your interest and your will, you can make video tutorials or a more step by step tutorial on Opengl.

@bplus
Yes
1. surely the Opengl subs to draw pad can be unified.
2. Ai Vs human is more interesting I'll do an algorithm
3. Wiki is so much void about _GL commands
4. Easily the standard graphic mode can be stripped out to show the only Opengl version and the beginner must be focused to see the difference between this mode and standard mode to do graphic.

Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Quick question on this:
Code: QB64: [Select]
  1. B) set the Screen coordinates equal to Opengl cohordinates by using WINDOW  command of QB. For example
  2. Code: QB64: [Select]
  3. S = _NEWIMAGE (800,600,32)
  4. WINDOW (-1, - 1) - (1,1)
  5.  
  6.  

To avoid distortion of circles, wouldn't you want S to be square? ie 600 x 600, not 800 x 600.

Note how Ashish starts with square screen dimensions.
« Last Edit: August 29, 2021, 10:45:51 am by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
@bplus
Here the first step
1. unifying the 2 SUB DrawPad in OpenGl
2. make a more rounded ball :-)
3. have more control in the input section

Code: QB64: [Select]
  1. ' OpenGl Pong
  2. DefLng L ' Longs start with L
  3. DefInt I ' Integers start with I
  4. DefStr S ' Strings start with S
  5. DefSng N 'Singles start with N
  6.  
  7. ' type of data called OBJECT
  8. Type obj
  9.     X As Integer
  10.     Y As Integer
  11.     Width1 As Integer
  12.     Height As Integer
  13.     Color1 As _Unsigned Long
  14.     Active As Integer
  15.     SpeedX As Integer
  16.     SpeedY As Integer
  17.  
  18. Const True = -1, False = 0, Left = 19200, Right = 19712, Escape = 27
  19. Const Enter = 13, iPlayer1 = 1, iPlayer2 = 2
  20. Dim lScreen
  21. Dim Shared iKeyb(1 To 2) As Integer
  22. Dim Shared iActiveGl, myScreen As obj, Pad(1 To 2) As obj, Ball As obj
  23. Dim Shared InitGl As Integer, iBallGl As Integer
  24. Dim Shared iPadGl1 As Integer, iPadGl2 As Integer
  25.  
  26. lScreen = _NewImage(800, 600, 32) ' graphic
  27. Screen lScreen ' graphic  creating the window of the program
  28. '  wait that window has been created
  29. _Delay 2 'Do While Not _ScreenExists: Loop
  30. _Title "OpenGl Pong v.1.0: press Enter to switch between normal vs OpenGl mode"
  31.  
  32.  
  33. Init
  34.  
  35. Do While (iKeyb(iPlayer1) <> Escape) Or (iKeyb(iPlayer2) <> Escape)
  36.     GetInput
  37.     ' stardard graphic mode
  38.     If iActiveGl = False Then
  39.         Border
  40.         Showpad iPlayer1
  41.         Showpad iPlayer2
  42.         ShowBall
  43.         Call Message(1, 1, "press Escape to quit, A/S player 1 Left/Right, cursor Left/Right player 2")
  44.         '    Else
  45.         '  GL() graphic mode
  46.  
  47.         ' Nothing to do here  go to _GL()
  48.     End If
  49.  
  50.     ' calculations for new screenshot
  51.     MovePad
  52.     MoveBall
  53.  
  54.     ' this toggles between standard graphic mode and  Gl graphic mode
  55.     If iKeyb(iPlayer1) = Enter Or iKeyb(iPlayer2) = Enter Then iActiveGl = Not iActiveGl
  56.     Do: Loop Until _KeyHit = 0 ' waiting that user releases the key
  57.     ' saving CPU usage
  58.     _Limit 20
  59.  
  60. ' area GL--------------------------------------------------------------
  61.  
  62. Sub _GL ()
  63.     If iActiveGl = 0 Then Exit Sub
  64.     If InitGl Then InitGlSub
  65.     ClearScreen
  66.  
  67.     If iBallGl = True Then BallGl
  68.     If iPadGl1 Then PADgL iPlayer1 'Thanks to Bplus suggestion
  69.     If iPadGl2 Then PADgL iPlayer2 ' one SUB with a flag at the place of 2 subs
  70.     _glFlush
  71.  
  72. Sub BallGl
  73.     Dim nGc, nBc, nRc, nAc
  74.     Dim e As Single, gXb As Single, gYb As Single
  75.     ComponentColor Ball.Color1
  76.     If SetColorGL!(nGc, nBc, nRc, nAc) Then
  77.         _glBegin _GL_TRIANGLE_FAN
  78.         For e = 1 To (_Pi(2)) Step (2 * _Pi / 10) ' <-- increasing the number we get a more suitable ball
  79.             gXb = Proportion!(0, _Width, -1, 1, Ball.X + (Ball.Width1 * Cos(e)))
  80.             gYb = Proportion!(0, _Height, 1, -1, Ball.Y + (Ball.Height * Sin(e)))
  81.             _glVertex2f gXb, gYb
  82.         Next e
  83.         _glEnd
  84.     End If
  85.  
  86. Sub PADgL (F As Integer)
  87.     Dim nGc, nBc, nRc, nAc
  88.     Dim nx1, nx2, ny1, ny2, dummy
  89.     ComponentColor Pad(F).Color1
  90.     If SetColorGL!(nGc, nBc, nRc, nAc) Then
  91.         nx1 = Proportion(0, _Width, -1, 1, Pad(F).X)
  92.         ny1 = Proportion(0, _Height, 1, -1, Pad(F).Y)
  93.         ny2 = Proportion(0, _Height, 1, -1, Pad(F).Y + Pad(F).Height)
  94.         nx2 = Proportion(0, _Width, -1, 1, Pad(F).X + Pad(F).Width1)
  95.         _glBegin _GL_QUADS
  96.         _glVertex2f nx1, ny1
  97.         _glVertex2f nx1, ny2
  98.         _glVertex2f nx2, ny2
  99.         _glVertex2f nx2, ny1
  100.         _glEnd
  101.         ComponentColor _RGB32(0)
  102.         dummy = SetColorGL!(nGc, nBc, nRc, nAc)
  103.         nx1 = Proportion(0, _Width, -1, 1, Pad(F).X + 2)
  104.         ny1 = Proportion(0, _Height, 1, -1, Pad(F).Y + 2)
  105.         ny2 = Proportion(0, _Height, 1, -1, Pad(F).Y + Pad(F).Height - 2)
  106.         nx2 = Proportion(0, _Width, -1, 1, Pad(F).X + Pad(F).Width1 - 2)
  107.         _glBegin _GL_LINE_LOOP
  108.         _glVertex2f nx1, ny1
  109.         _glVertex2f nx1, ny2
  110.         _glVertex2f nx2, ny2
  111.         _glVertex2f nx2, ny1
  112.         _glEnd
  113.  
  114.     End If
  115.  
  116.  
  117. Sub ClearScreen
  118.     _glClear _GL_COLOR_BUFFER_BIT
  119.  
  120. Function SetColorGL! (nGc, nRc, nBc, nAc)
  121.     SetColorGL! = False
  122.     nGc = Proportion(1, 255, 0, 1, Gc)
  123.     nRc = Proportion(1, 255, 0, 1, Rc)
  124.     nBc = Proportion(1, 255, 0, 1, Bc)
  125.     nAc = Proportion(1, 255, 0, 1, Ac)
  126.     _glColor3f nRc, nGc, nBc
  127.     SetColorGL! = True
  128.  
  129. Sub InitGlSub
  130.     'settings viewport for GL screen
  131.     _glViewport myScreen.X, myScreen.Y, myScreen.Width1, myScreen.Height
  132.     Dim nGc, nBc, nRc, nAc
  133.     ComponentColor myScreen.Color1
  134.     If SetColorGL!(nGc, nRc, nBc, nAc) Then
  135.         _glClearColor nRc, nGc, nBc, nAc
  136.     Else
  137.         Print "Error"
  138.     End If
  139.     InitGl = False
  140.  
  141.  
  142. ' area sub/functions*****************************************************
  143. Function Proportion (imin1, imax1, imin2, imax2, ivalue)
  144.     Dim iDelta1, iDelta2
  145.     iDelta1 = (imax1 - imin1)
  146.     iDelta2 = (imax2 - imin2)
  147.     Proportion = ((ivalue * iDelta2) / iDelta1) + imin2
  148.  
  149.  
  150.  
  151. Sub ComponentColor (C As _Unsigned Long)
  152.     Gc = _Green32(C)
  153.     Rc = _Red32(C)
  154.     Bc = _Blue32(C)
  155.     Ac = _Alpha32(C)
  156.  
  157. Sub Init
  158.     ' window of program
  159.     myScreen.X = 0
  160.     myScreen.Y = 0
  161.     myScreen.Width1 = _Width
  162.     myScreen.Height = _Height
  163.     myScreen.Color1 = _RGB32(200, 200, 50)
  164.     myScreen.Active = True
  165.  
  166.     ' ball
  167.     Ball.X = 400
  168.     Ball.Y = 300
  169.     Ball.Active = True
  170.     Ball.Width1 = 10
  171.     Ball.Height = 10
  172.     Ball.SpeedX = 3
  173.     Ball.SpeedY = 3
  174.     Ball.Color1 = _RGB32(200, 10, 10)
  175.  
  176.     'PAD player1
  177.     Pad(iPlayer1).X = 350
  178.     Pad(iPlayer1).Y = 400
  179.     Pad(iPlayer1).Width1 = 100
  180.     Pad(iPlayer1).Height = 30
  181.     Pad(iPlayer1).Color1 = _RGB32(40, 230, 200)
  182.     Pad(iPlayer1).SpeedX = 0
  183.     Pad(iPlayer1).SpeedY = 0
  184.     Pad(iPlayer1).Active = True
  185.  
  186.  
  187.     'PAD player2
  188.     Pad(iPlayer2).X = 350
  189.     Pad(iPlayer2).Y = 150
  190.     Pad(iPlayer2).Width1 = 100
  191.     Pad(iPlayer2).Height = 30
  192.     Pad(iPlayer2).Color1 = _RGB32(230, 130, 220)
  193.     Pad(iPlayer2).SpeedX = 0
  194.     Pad(iPlayer2).SpeedY = 0
  195.     Pad(iPlayer2).Active = True
  196.  
  197.     'Gl
  198.     InitGl = True
  199.     iBallGl = True
  200.     iPadGl1 = True
  201.     iPadGl2 = True
  202.     iActiveGl = False ' at the start the graphic is normal
  203.  
  204. ' Graphic normal---------------
  205. Sub Showpad (P As Integer)
  206.     If Pad(P).Active Then
  207.         Line (Pad(P).X, Pad(P).Y)-Step(Pad(P).Width1, Pad(P).Height), Pad(P).Color1, BF
  208.         Line (Pad(P).X, Pad(P).Y)-Step(Pad(P).Width1, Pad(P).Height), _RGB32(0), B
  209.     End If
  210.  
  211. Sub ShowBall
  212.     Circle (Ball.X, Ball.Y), Ball.Width1, Ball.Color1
  213.     Paint Step(0, 0), Ball.Color1, Ball.Color1
  214.  
  215. Sub Border
  216.     Line (myScreen.X + 1, myScreen.Y + 1)-(myScreen.X + myScreen.Width1 - 1, myScreen.Y + myScreen.Height - 1), myScreen.Color1, BF
  217.  
  218. ' movement and I/O area-----------------------------
  219. Sub Message (iWhereX As Integer, iWhereY As Integer, sMessage As String)
  220.     Locate iWhereY, iWhereX
  221.     Print sMessage;
  222.  
  223. Sub MovePad
  224.     Dim i
  225.     For i = iPlayer1 To iPlayer2 Step 1
  226.         If iKeyb(i) = Right Then Pad(i).X = Pad(i).X + 10
  227.         If Pad(i).X > myScreen.Width1 - Pad(i).Width1 Then Pad(i).X = myScreen.Width1 - Pad(i).Width1
  228.         If iKeyb(i) = Left Then Pad(i).X = Pad(i).X - 10
  229.         If Pad(i).X < myScreen.X Then Pad(i).X = myScreen.X
  230.     Next
  231.  
  232. Sub MoveBall
  233.     Ball.X = Ball.X + Ball.SpeedX
  234.     Ball.Y = Ball.Y + Ball.SpeedY
  235.     ' bouncing control
  236.     'vs walls
  237.     If Ball.X < 0 Then Ball.X = Int(Ball.Width1 / 2) + 1: Ball.SpeedX = -Ball.SpeedX
  238.     If Ball.X > myScreen.Width1 - Int(Ball.Width1 / 2) + 1 Then Ball.X = myScreen.Width1 - Int(Ball.Width1 / 2) + 1: Ball.SpeedX = -Ball.SpeedX
  239.     If Ball.Y < 0 Then Ball.Y = Int(Ball.Height / 2) + 1: Ball.SpeedY = -Ball.SpeedY
  240.     If Ball.Y > myScreen.Height - Int(Ball.Height / 2) + 1 Then Ball.Y = myScreen.Height - Int(Ball.Height / 2) + 1: Ball.SpeedY = -Ball.SpeedY
  241.     ' vs player1
  242.     If Abs(Pad(iPlayer1).Y + Int(Pad(iPlayer1).Height / 2) - (Ball.Y + Int(Ball.Height / 2))) < Int((Pad(iPlayer1).Height + Ball.Height) / 2) Then
  243.         If Abs((Pad(iPlayer1).X + Int(Pad(iPlayer1).Width1 / 2)) - (Ball.X + Int(Ball.Width1 / 2))) < Int((Pad(iPlayer1).Width1 + Ball.Width1) / 2) Then
  244.             Ball.SpeedY = -Ball.SpeedY
  245.         End If
  246.     End If
  247.     ' vs player2
  248.     If Abs(Pad(iPlayer2).Y + Int(Pad(iPlayer2).Height / 2) - (Ball.Y + Int(Ball.Height / 2))) < Int((Pad(iPlayer2).Height + Ball.Height) / 2) Then
  249.         If Abs((Pad(iPlayer2).X + Int(Pad(iPlayer2).Width1 / 2)) - (Ball.X + Int(Ball.Width1 / 2))) < Int((Pad(iPlayer2).Width1 + Ball.Width1) / 2) Then
  250.             Ball.SpeedY = -Ball.SpeedY
  251.         End If
  252.     End If
  253.  
  254. Sub GetInput
  255.     ' it resets command variables
  256.     iKeyb(iPlayer1) = 0
  257.     iKeyb(iPlayer2) = 0
  258.     ' keyboard input
  259.     iKeyb(iPlayer1) = _KeyHit
  260.     If _KeyDown(115) Or _KeyDown(83) Then iKeyb(iPlayer2) = Right Else If iKeyb(iPlayer2) = Right Then iKeyb(iPlayer2) = False
  261.     If _KeyDown(65) Or _KeyDown(97) Then iKeyb(iPlayer2) = Left Else If iKeyb(iPlayer2) = Left Then iKeyb(iPlayer2) = False
  262.     If _KeyDown(27) Then iKeyb(iPlayer2) = Escape
  263.     If _KeyDown(13) Then iKeyb(iPlayer2) = Enter
  264.     '  Do: Loop Until _KeyHit = 0
  265.     ' mouse input
  266.     ' -->in developing
  267.     'joystick input
  268.     '-->in developing
  269.  
  270.  
  271.  

PS I'm delaying for AI because OpenGl is a graphic issue, so I'm putting it in a second order.
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Here the code with only OpenGl version
Code: QB64: [Select]
  1. ' OpenGl Pong
  2. DEFLNG L ' Longs start with L
  3. DEFINT I ' Integers start with I
  4. DEFSTR S ' Strings start with S
  5. DEFSNG N 'Singles start with N
  6.  
  7. ' type of data called OBJECT
  8. TYPE obj
  9.     X AS INTEGER
  10.     Y AS INTEGER
  11.     Width1 AS INTEGER
  12.     Height AS INTEGER
  13.     Color1 AS _UNSIGNED LONG
  14.     Active AS INTEGER
  15.     SpeedX AS INTEGER
  16.     SpeedY AS INTEGER
  17.  
  18. CONST True = -1, False = 0, Left = 19200, Right = 19712, Escape = 27
  19. CONST Enter = 13, iPlayer1 = 1, iPlayer2 = 2
  20. DIM lScreen
  21. DIM SHARED iKeyb(1 TO 2) AS INTEGER
  22. DIM SHARED iActiveGl, myScreen AS obj, Pad(1 TO 2) AS obj, Ball AS obj
  23. DIM SHARED InitGl AS INTEGER, iBallGl AS INTEGER
  24. DIM SHARED iPadGl1 AS INTEGER, iPadGl2 AS INTEGER
  25.  
  26. lScreen = _NEWIMAGE(800, 600, 32) ' graphic
  27. SCREEN lScreen ' graphic  creating the window of the program
  28. '  wait that window has been created
  29. _DELAY 2 'Do While Not _ScreenExists: Loop
  30. _TITLE "OpenGl Pong v.1.0: press Enter to switch between normal vs OpenGl mode"
  31.  
  32.  
  33. Init
  34.  
  35. DO WHILE (iKeyb(iPlayer1) <> Escape) OR (iKeyb(iPlayer2) <> Escape)
  36.     GetInput
  37.     ' stardard graphic mode
  38.     IF iActiveGl = False THEN
  39.         Border
  40.         Showpad iPlayer1
  41.         Showpad iPlayer2
  42.         ShowBall
  43.         CALL Message(1, 1, "press Escape to quit, A/S player 1 Left/Right, cursor Left/Right player 2")
  44.         '    Else
  45.         '  GL() graphic mode
  46.  
  47.         ' Nothing to do here  go to _GL()
  48.     END IF
  49.  
  50.     ' calculations for new screenshot
  51.     MovePad
  52.     MoveBall
  53.  
  54.     ' this toggles between standard graphic mode and  Gl graphic mode
  55.     IF iKeyb(iPlayer1) = Enter OR iKeyb(iPlayer2) = Enter THEN iActiveGl = NOT iActiveGl
  56.     DO: LOOP UNTIL _KEYHIT = 0 ' waiting that user releases the key
  57.     ' saving CPU usage
  58.     _LIMIT 20
  59.  
  60. ' area GL--------------------------------------------------------------
  61.  
  62. SUB _GL ()
  63.     IF iActiveGl = 0 THEN EXIT SUB
  64.     IF InitGl THEN InitGlSub
  65.     ClearScreen
  66.  
  67.     IF iBallGl = True THEN BallGl
  68.     IF iPadGl1 THEN PADgL iPlayer1 'Thanks to Bplus suggestion
  69.     IF iPadGl2 THEN PADgL iPlayer2 ' one SUB with a flag at the place of 2 subs
  70.     _GLFLUSH
  71.  
  72. SUB BallGl
  73.     DIM nGc, nBc, nRc, nAc
  74.     DIM e AS SINGLE, gXb AS SINGLE, gYb AS SINGLE
  75.     ComponentColor Ball.Color1
  76.     IF SetColorGL!(nGc, nBc, nRc, nAc) THEN
  77.         _GLBEGIN _GL_TRIANGLE_FAN
  78.         FOR e = 1 TO (_PI(2)) STEP (2 * _PI / 10) ' <-- increasing the number we get a more suitable ball
  79.             gXb = Proportion!(0, _WIDTH, -1, 1, Ball.X + (Ball.Width1 * COS(e)))
  80.             gYb = Proportion!(0, _HEIGHT, 1, -1, Ball.Y + (Ball.Height * SIN(e)))
  81.             _GLVERTEX2F gXb, gYb
  82.         NEXT e
  83.         _GLEND
  84.     END IF
  85.  
  86. SUB PADgL (F AS INTEGER)
  87.     DIM nGc, nBc, nRc, nAc
  88.     DIM nx1, nx2, ny1, ny2, dummy
  89.     ComponentColor Pad(F).Color1
  90.     IF SetColorGL!(nGc, nBc, nRc, nAc) THEN
  91.         nx1 = Proportion(0, _WIDTH, -1, 1, Pad(F).X)
  92.         ny1 = Proportion(0, _HEIGHT, 1, -1, Pad(F).Y)
  93.         ny2 = Proportion(0, _HEIGHT, 1, -1, Pad(F).Y + Pad(F).Height)
  94.         nx2 = Proportion(0, _WIDTH, -1, 1, Pad(F).X + Pad(F).Width1)
  95.         _GLBEGIN _GL_QUADS
  96.         _GLVERTEX2F nx1, ny1
  97.         _GLVERTEX2F nx1, ny2
  98.         _GLVERTEX2F nx2, ny2
  99.         _GLVERTEX2F nx2, ny1
  100.         _GLEND
  101.         ComponentColor _RGB32(0)
  102.         dummy = SetColorGL!(nGc, nBc, nRc, nAc)
  103.         nx1 = Proportion(0, _WIDTH, -1, 1, Pad(F).X + 2)
  104.         ny1 = Proportion(0, _HEIGHT, 1, -1, Pad(F).Y + 2)
  105.         ny2 = Proportion(0, _HEIGHT, 1, -1, Pad(F).Y + Pad(F).Height - 2)
  106.         nx2 = Proportion(0, _WIDTH, -1, 1, Pad(F).X + Pad(F).Width1 - 2)
  107.         _GLBEGIN _GL_LINE_LOOP
  108.         _GLVERTEX2F nx1, ny1
  109.         _GLVERTEX2F nx1, ny2
  110.         _GLVERTEX2F nx2, ny2
  111.         _GLVERTEX2F nx2, ny1
  112.         _GLEND
  113.  
  114.     END IF
  115.  
  116.  
  117. SUB ClearScreen
  118.     _GLCLEAR _GL_COLOR_BUFFER_BIT
  119.  
  120. FUNCTION SetColorGL! (nGc, nRc, nBc, nAc)
  121.     SetColorGL! = False
  122.     nGc = Proportion(1, 255, 0, 1, Gc)
  123.     nRc = Proportion(1, 255, 0, 1, Rc)
  124.     nBc = Proportion(1, 255, 0, 1, Bc)
  125.     nAc = Proportion(1, 255, 0, 1, Ac)
  126.     _GLCOLOR3F nRc, nGc, nBc
  127.     SetColorGL! = True
  128.  
  129. SUB InitGlSub
  130.     'settings viewport for GL screen
  131.     _GLVIEWPORT myScreen.X, myScreen.Y, myScreen.Width1, myScreen.Height
  132.     DIM nGc, nBc, nRc, nAc
  133.     ComponentColor myScreen.Color1
  134.     IF SetColorGL!(nGc, nRc, nBc, nAc) THEN
  135.         _GLCLEARCOLOR nRc, nGc, nBc, nAc
  136.     ELSE
  137.         PRINT "Error"
  138.     END IF
  139.     InitGl = False
  140.  
  141.  
  142. ' area sub/functions*****************************************************
  143. FUNCTION Proportion (imin1, imax1, imin2, imax2, ivalue)
  144.     DIM iDelta1, iDelta2
  145.     iDelta1 = (imax1 - imin1)
  146.     iDelta2 = (imax2 - imin2)
  147.     Proportion = ((ivalue * iDelta2) / iDelta1) + imin2
  148.  
  149.  
  150.  
  151. SUB ComponentColor (C AS _UNSIGNED LONG)
  152.     Gc = _GREEN32(C)
  153.     Rc = _RED32(C)
  154.     Bc = _BLUE32(C)
  155.     Ac = _ALPHA32(C)
  156.  
  157. SUB Init
  158.     ' window of program
  159.     myScreen.X = 0
  160.     myScreen.Y = 0
  161.     myScreen.Width1 = _WIDTH
  162.     myScreen.Height = _HEIGHT
  163.     myScreen.Color1 = _RGB32(200, 200, 50)
  164.     myScreen.Active = True
  165.  
  166.     ' ball
  167.     Ball.X = 400
  168.     Ball.Y = 300
  169.     Ball.Active = True
  170.     Ball.Width1 = 10
  171.     Ball.Height = 10
  172.     Ball.SpeedX = 3
  173.     Ball.SpeedY = 3
  174.     Ball.Color1 = _RGB32(200, 10, 10)
  175.  
  176.     'PAD player1
  177.     Pad(iPlayer1).X = 350
  178.     Pad(iPlayer1).Y = 400
  179.     Pad(iPlayer1).Width1 = 100
  180.     Pad(iPlayer1).Height = 30
  181.     Pad(iPlayer1).Color1 = _RGB32(40, 230, 200)
  182.     Pad(iPlayer1).SpeedX = 0
  183.     Pad(iPlayer1).SpeedY = 0
  184.     Pad(iPlayer1).Active = True
  185.  
  186.  
  187.     'PAD player2
  188.     Pad(iPlayer2).X = 350
  189.     Pad(iPlayer2).Y = 150
  190.     Pad(iPlayer2).Width1 = 100
  191.     Pad(iPlayer2).Height = 30
  192.     Pad(iPlayer2).Color1 = _RGB32(230, 130, 220)
  193.     Pad(iPlayer2).SpeedX = 0
  194.     Pad(iPlayer2).SpeedY = 0
  195.     Pad(iPlayer2).Active = True
  196.  
  197.     'Gl
  198.     InitGl = True
  199.     iBallGl = True
  200.     iPadGl1 = True
  201.     iPadGl2 = True
  202.     iActiveGl = False ' at the start the graphic is normal
  203.  
  204. ' Graphic normal---------------
  205. SUB Showpad (P AS INTEGER)
  206.     IF Pad(P).Active THEN
  207.         LINE (Pad(P).X, Pad(P).Y)-STEP(Pad(P).Width1, Pad(P).Height), Pad(P).Color1, BF
  208.         LINE (Pad(P).X, Pad(P).Y)-STEP(Pad(P).Width1, Pad(P).Height), _RGB32(0), B
  209.     END IF
  210.  
  211. SUB ShowBall
  212.     CIRCLE (Ball.X, Ball.Y), Ball.Width1, Ball.Color1
  213.     PAINT STEP(0, 0), Ball.Color1, Ball.Color1
  214.  
  215. SUB Border
  216.     LINE (myScreen.X + 1, myScreen.Y + 1)-(myScreen.X + myScreen.Width1 - 1, myScreen.Y + myScreen.Height - 1), myScreen.Color1, BF
  217.  
  218. ' movement and I/O area-----------------------------
  219. SUB Message (iWhereX AS INTEGER, iWhereY AS INTEGER, sMessage AS STRING)
  220.     LOCATE iWhereY, iWhereX
  221.     PRINT sMessage;
  222.  
  223. SUB MovePad
  224.     DIM i
  225.     FOR i = iPlayer1 TO iPlayer2 STEP 1
  226.         IF iKeyb(i) = Right THEN Pad(i).X = Pad(i).X + 10
  227.         IF Pad(i).X > myScreen.Width1 - Pad(i).Width1 THEN Pad(i).X = myScreen.Width1 - Pad(i).Width1
  228.         IF iKeyb(i) = Left THEN Pad(i).X = Pad(i).X - 10
  229.         IF Pad(i).X < myScreen.X THEN Pad(i).X = myScreen.X
  230.     NEXT
  231.  
  232. SUB MoveBall
  233.     Ball.X = Ball.X + Ball.SpeedX
  234.     Ball.Y = Ball.Y + Ball.SpeedY
  235.     ' bouncing control
  236.     'vs walls
  237.     IF Ball.X < 0 THEN Ball.X = INT(Ball.Width1 / 2) + 1: Ball.SpeedX = -Ball.SpeedX
  238.     IF Ball.X > myScreen.Width1 - INT(Ball.Width1 / 2) + 1 THEN Ball.X = myScreen.Width1 - INT(Ball.Width1 / 2) + 1: Ball.SpeedX = -Ball.SpeedX
  239.     IF Ball.Y < 0 THEN Ball.Y = INT(Ball.Height / 2) + 1: Ball.SpeedY = -Ball.SpeedY
  240.     IF Ball.Y > myScreen.Height - INT(Ball.Height / 2) + 1 THEN Ball.Y = myScreen.Height - INT(Ball.Height / 2) + 1: Ball.SpeedY = -Ball.SpeedY
  241.     ' vs player1
  242.     IF ABS(Pad(iPlayer1).Y + INT(Pad(iPlayer1).Height / 2) - (Ball.Y + INT(Ball.Height / 2))) < INT((Pad(iPlayer1).Height + Ball.Height) / 2) THEN
  243.         IF ABS((Pad(iPlayer1).X + INT(Pad(iPlayer1).Width1 / 2)) - (Ball.X + INT(Ball.Width1 / 2))) < INT((Pad(iPlayer1).Width1 + Ball.Width1) / 2) THEN
  244.             Ball.SpeedY = -Ball.SpeedY
  245.         END IF
  246.     END IF
  247.     ' vs player2
  248.     IF ABS(Pad(iPlayer2).Y + INT(Pad(iPlayer2).Height / 2) - (Ball.Y + INT(Ball.Height / 2))) < INT((Pad(iPlayer2).Height + Ball.Height) / 2) THEN
  249.         IF ABS((Pad(iPlayer2).X + INT(Pad(iPlayer2).Width1 / 2)) - (Ball.X + INT(Ball.Width1 / 2))) < INT((Pad(iPlayer2).Width1 + Ball.Width1) / 2) THEN
  250.             Ball.SpeedY = -Ball.SpeedY
  251.         END IF
  252.     END IF
  253.  
  254. SUB GetInput
  255.     ' it resets command variables
  256.     iKeyb(iPlayer1) = 0
  257.     iKeyb(iPlayer2) = 0
  258.     ' keyboard input
  259.     iKeyb(iPlayer1) = _KEYHIT
  260.     IF _KEYDOWN(115) OR _KEYDOWN(83) THEN iKeyb(iPlayer2) = Right ELSE IF iKeyb(iPlayer2) = Right THEN iKeyb(iPlayer2) = False
  261.     IF _KEYDOWN(65) OR _KEYDOWN(97) THEN iKeyb(iPlayer2) = Left ELSE IF iKeyb(iPlayer2) = Left THEN iKeyb(iPlayer2) = False
  262.     IF _KEYDOWN(27) THEN iKeyb(iPlayer2) = Escape
  263.     IF _KEYDOWN(13) THEN iKeyb(iPlayer2) = Enter
  264.     '  Do: Loop Until _KeyHit = 0
  265.     ' mouse input
  266.     ' -->in developing
  267.     'joystick input
  268.     '-->in developing
  269.  
  270.  
  271.  
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
@Ashish
I have an issue with your code!
It was flickering!
Code: QB64: [Select]
  1. Screen _NewImage(600, 600, 32)
  2.     _Limit 1
  3.  
  4. Sub _GL ()
  5.     _glMatrixMode _GL_MODELVIEW
  6.     _glTranslatef -1, 1, 0
  7.     _glScalef 1 / (0.5 * _Width(0)), -1 / (0.5 * _Height(0)), 1
  8.  
  9.     _glBegin _GL_TRIANGLES 'normal qb screen coordinates
  10.     _glVertex2f Rnd * 300, 0
  11.     _glVertex2f Rnd * 600, Rnd * 600
  12.     _glVertex2f 0, Rnd * 600
  13.     _glEnd
  14.  
  15.     _glFlush
  16.  
  17.  
Programming isn't difficult, only it's  consuming time and coffee