|
Post by leopardpm on Jan 19, 2009 21:25:13 GMT -5
My first stupid question: Way back when I first learned Basic, I could turn on and off individual pixels, draw lines, circles, and whatnot directly to the screen. But I can't find any commands for these 'basic' functions in B4GL.
My problem: I want to create a screen full of hexagons as a background 'map' in a game where figures are moved from one hex to another. I tried making the hex as a graphic and loading it as a texture for a tileset but the problem is that I can't control where each hex prints so the screen looks like a table full of squares with each one having an individual hex in it... they need to touch and line up correctly. I have a routine to physically draw the hexmap using line drawing functions... but no such thing in B4GL.
Is there a new (I am real old school - 20-30 yrs ago) way to do such a thing or do I need to draw the entire screen and load it as a single object?
|
|
|
Post by Darkjester on Jan 19, 2009 23:05:41 GMT -5
You can use opengl to draw lines, circles, points.. etc Here is a simple line GlClear(Gl_color_buffer_bit) gltranslatef(0,0,-2) glbegin(gl_lines) glvertex2f(1,1) glvertex2f(1,0) glend() swapbuffers()
For more information, such as coloring and doing other things take a look at This tutorial series for basic opengl useHope this helps -Darkjester
|
|
|
Post by Tom Mulgrew on Jan 20, 2009 2:30:06 GMT -5
Basic4GL is built on top of the OpenGL graphics library, so its graphics commands are a little bit different. This page describes how to do basic drawing (lines, circles etc) basic4gl.wikispaces.com/2D+Drawing+in+OpenGLA useful project would be to take those commands and wrap them up in functions with more traditional names like "Line", "Circle" etc. Something I've meant to do, but never quite got around to it. -Tom
|
|
|
Post by Nicky Peter Hollyoake on Jan 20, 2009 13:43:52 GMT -5
Heres Tom's program in function. Sub Line (__X#, __Y#, __X1#, __Y1#) glBegin (GL_LINES) glVertex2f(__X#, __Y#) glVertex2f(__X1#, __Y1#) glEnd() EndSub
Sub Screen2D (__w, __h) glMatrixMode (GL_PROJECTION) glLoadIdentity () glOrtho (0, __w, __h, 0, 0, 1) glDisable(GL_DEPTH_TEST) glMatrixMode (GL_MODELVIEW) glLoadIdentity() EndSub
Sub Circle (MODE, X#, Y#, Radius#) Dim __i glPolygonMode (GL_FRONT_AND_BACK, MODE) glBegin (GL_TRIANGLE_FAN) For __i = 0 To 360 glVertex2f(X# + Cosd(__i) * Radius#, Y# + Sind(__i) * Radius#) Next glEnd() EndSub
Sub Rectangle (MODE, X#, Y#, XS#, YS#) glPolygonMode (GL_FRONT_AND_BACK, MODE) glBegin (GL_QUADS) glVertex2f(X#, Y#) glVertex2f(X#, Y#+YS#) glVertex2f(X#+XS#, Y#+YS#) glVertex2f(X#+XS#, Y#) glEnd() EndSub
Sub Point (__X#, __Y#) glBegin (GL_POINTS) glVertex2f(__X#, __Y#) glEnd() EndSub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim i
Screen2D (640, 480)
glTranslatef(0.375, 0.375, 0) glClearColor(.3, .3, .3, 0) glClear(GL_COLOR_BUFFER_BIT)
Rectangle (GL_LINE, 20, 20, 600, 440) Line (20, 40, 620, 40) Rectangle (GL_FILL, 602, 22, 16, 15) Line (580, 460, 620, 420)
For i = 1 To 5000 Point (RND() % 600 + 20, RND() % 420 + 40) Next
Circle (GL_FILL, 320, 240, 20) Circle (GL_POINT, 320, 240, 40) Circle (GL_POINT, 320, 240, 60) Circle (GL_POINT, 320, 240, 80) Swapbuffers() - Nicky
|
|
|
Post by leopardpm on Jan 20, 2009 20:39:13 GMT -5
ok... Great! I got the lines working correctly and have drawn the background map...but....
now next stupid question! Now, after drawing using the GLlines, when I activate a sprite, the screen clears to blank. I tried swapping buffers again and redrawing the background... but no go. Do you need to see the code?
Thanks for the help BTW - that is some quick response!!!
michael
|
|
|
Post by Darkjester on Jan 20, 2009 21:22:36 GMT -5
The code would be helpful, makesure you dont have any "Cls" commands where they shouldnt be also, turn on textmode(text_overlaid) and call drawtext() at the end of the loop, and that may fix your problem.
-Darkjester
|
|
|
Post by leopardpm on Jan 20, 2009 21:54:43 GMT -5
here is code (sloppy, sorry, copied/pasted most of it from other 'example' programs):
' Program to draw a hexmap and bounce a Sprite around on top of it
' Setup a 2D projection const XSize = 640, YSize = 480 glMatrixMode (GL_PROJECTION) glLoadIdentity () glOrtho (0, XSize, YSize, 0, 0, 1) glDisable(GL_DEPTH_TEST) glMatrixMode (GL_MODELVIEW) glLoadIdentity() ' Displacement trick for exact pixelization glTranslatef(0.375, 0.375, 0)
' Variables dim angle#, x1, y1, x2, y2, radius# dim i, xx1, xx2, xx3, yy2, yy3, x, y, ny dim texture, sprite
' Draw a scene glClearColor(0, 0, 255, 0) ' Mack Background blue glClear(GL_COLOR_BUFFER_BIT) gosub DrawHexMap SwapBuffers() gosub MakeSprite gosub BounceBall End
BounceBall: while true AnimateSprites ()
if SprLeft () < 0 or SprRight () > SpriteAreaWidth () then SprSetXVel (-SprXVel ()) endif if SprTop () < 0 or SprBottom () > SpriteAreaHeight () then SprSetYVel (-SprYVel ()) endif wend return
Hex: xx1 = x1 + 6 : xx2 = x1 + 21 : xx3 = x1 + 27 yy2 = y1 + 11 : yy3 = y1 + 22 glBegin(GL_LINE_LOOP) glVertex2f(xx1, y1): glVertex2f(xx2, y1): glVertex2f(xx3, yy2): glVertex2f(xx2, yy3): glVertex2f(xx1, yy3): glVertex2f(x1, yy2) glEnd() return
MakeSprite: texture = LoadTex("data\ball.png") sprite = NewSprite (texture) SprSetVel (vec2 (3, 3)) SprSetPos (100, 100) return DrawHexMap: for x = 2 to 29 for y = 1 to 19 x1 = ((x-1)*21) ny = 20+((y-1)*22) ' if it is an 'odd' column then offset it down half a hex if int(x/2) <> (x/2.0) then ny = ny + 11 : endif y1 = ny gosub Hex next next return
|
|
|
Post by Nicky Peter Hollyoake on Jan 20, 2009 22:03:57 GMT -5
Had to move a few things about, added a 'TextMode (TEXT_OVERLAID)', and 'DrawText()'. But here it is .. ' Program to draw a hexmap and bounce a Sprite around on top of it
' Setup a 2D projection const XSize = 640, YSize = 480 glMatrixMode (GL_PROJECTION) glLoadIdentity () glOrtho (0, XSize, YSize, 0, 0, 1) glDisable(GL_DEPTH_TEST) glMatrixMode (GL_MODELVIEW) glLoadIdentity() ' Displacement trick for exact pixelization glTranslatef(0.375, 0.375, 0)
' Variables dim angle#, x1, y1, x2, y2, radius# dim i, xx1, xx2, xx3, yy2, yy3, x, y, ny dim texture, sprite
' Draw a scene glClearColor(0, 0, 255, 0) ' Mack Background blue
TextMode (TEXT_OVERLAID)
gosub DrawHexMap gosub MakeSprite gosub BounceBall End
BounceBall: while true glClear(GL_COLOR_BUFFER_BIT) gosub DrawHexMap AnimateSprites ()
if SprLeft () < 0 or SprRight () > SpriteAreaWidth () then SprSetXVel (-SprXVel ()) endif if SprTop () < 0 or SprBottom () > SpriteAreaHeight () then SprSetYVel (-SprYVel ()) endif drawtext() SwapBuffers() wend return
Hex: xx1 = x1 + 6 : xx2 = x1 + 21 : xx3 = x1 + 27 yy2 = y1 + 11 : yy3 = y1 + 22 glBegin(GL_LINE_LOOP) glVertex2f(xx1, y1): glVertex2f(xx2, y1): glVertex2f(xx3, yy2): glVertex2f(xx2, yy3): glVertex2f(xx1, yy3): glVertex2f(x1, yy2) glEnd() return
MakeSprite: texture = LoadTex("data\ball.png") sprite = NewSprite (texture) SprSetVel (vec2 (3, 3)) SprSetPos (100, 100) return
DrawHexMap: for x = 2 to 29 for y = 1 to 19 x1 = ((x-1)*21) ny = 20+((y-1)*22) ' if it is an 'odd' column then offset it down half a hex if int(x/2) <> (x/2.0) then ny = ny + 11 : endif y1 = ny gosub Hex next next return - Nicky
|
|
|
Post by leopardpm on Jan 21, 2009 7:56:38 GMT -5
Thanks Nicki! It works, but, it is way slow to clear and redraw with each animation of the sprites - is there a better way to do this? I thought that one of the nice things about sprite routines is that they overlay or underlay the background without disturbing it? Seems like there is something missing here...
Once again you have proved invaluable to my 're-education'!
michael
|
|
|
Post by Darkjester on Jan 21, 2009 8:00:00 GMT -5
What do you mean 2 slow? Also you can set the "depth" of the sprite using sprsetzorder(), Plz loook in the sprite library guide for more information on sprite functions
-Darkjester
|
|
|
Post by Adam on Jan 21, 2009 8:37:10 GMT -5
here, i sped it up a little bit using a gl list
' Program to draw a hexmap and bounce a Sprite around on top of it
' Setup a 2D projection const XSize = 640, YSize = 480 glMatrixMode (GL_PROJECTION) glLoadIdentity () glOrtho (0, XSize, YSize, 0, 0, 1) glDisable(GL_DEPTH_TEST) glMatrixMode (GL_MODELVIEW) glLoadIdentity() ' Displacement trick for exact pixelization glTranslatef(0.375, 0.375, 0)
' Variables dim angle#, x1, y1, x2, y2, radius# dim i, xx1, xx2, xx3, yy2, yy3, x, y, ny dim texture, sprite Dim HexMap = glGenLists(1)
' Draw a scene glClearColor(0, 0, 255, 0) ' Mack Background blue
TextMode (TEXT_OVERLAID)
gosub DrawHexMap gosub MakeSprite gosub BounceBall End
BounceBall: while true glClear(GL_COLOR_BUFFER_BIT) AnimateSprites ()
if SprLeft () < 0 or SprRight () > SpriteAreaWidth () then SprSetXVel (-SprXVel ()) endif if SprTop () < 0 or SprBottom () > SpriteAreaHeight () then SprSetYVel (-SprYVel ()) endif GLCallList(HexMap) drawtext() SwapBuffers() wend return
Hex: xx1 = x1 + 6 : xx2 = x1 + 21 : xx3 = x1 + 27 yy2 = y1 + 11 : yy3 = y1 + 22 glBegin(GL_LINE_LOOP) glVertex2f(xx1, y1): glVertex2f(xx2, y1): glVertex2f(xx3, yy2): glVertex2f(xx2, yy3): glVertex2f(xx1, yy3): glVertex2f(x1, yy2) glEnd() return
MakeSprite: texture = LoadTex("data\ball.png") sprite = NewSprite (texture) SprSetVel (vec2 (3, 3)) SprSetPos (100, 100) return
DrawHexMap: GLNewList(HexMap, GL_COMPILE) for x = 2 to 29 for y = 1 to 19 x1 = ((x-1)*21) ny = 20+((y-1)*22) ' if it is an 'odd' column then offset it down half a hex if int(x/2) <> (x/2.0) then ny = ny + 11 : endif y1 = ny gosub Hex next next GLEndList() return
|
|
|
Post by leopardpm on Jan 21, 2009 8:41:30 GMT -5
I have been trying to research the glcopypixels command and other related functions in an effort to just redraw the background where a sprite is to achieve a faster speed, but can't figure it out yet (and it still feels like I am recreating the wheel)... maybe the problem lies in trying to use glsprites in the first place since everything is hooked up to be 3D - but do I really have to make an entire faux sprite drawing routine of my own which leaves the background intact? Or is there perhaps a 2d sprite/graphics library somewhere that I can use? I don't need to scale, spin, rotate, move camera, or anything 3D for this project. Thanks again for your input!
|
|
|
Post by Darkjester on Jan 21, 2009 11:36:41 GMT -5
what exactly are you trying to do?
|
|
|
Post by Nicky Peter Hollyoake on Jan 21, 2009 13:59:52 GMT -5
Synctimer can help speed up your programs.
- Nicky
|
|
|
Post by leopardpm on Jan 21, 2009 20:53:37 GMT -5
Thanks odama! The GLlist thing does speed it up a bit (10%?) - I will have to understand what it is doing exactly. Darkjester - I am just making a stupid game that is based on www.windowsgames.co.uk/slay.html - its nothing special but a good and interesting way to get back into programming... plus a game for the kiddos etc. Now, such a game doesn't really need 'speed' as I am desiring, but I am always looking for the most efficient/fast way to achieve things... and clunky things (such as having to redraw a background whenever sprites are activated) really get my goat - just a pet peeve. I saw a reference to another basic that was also compiled and it has 'pixel blitting' commands which I am more used too (I like directly accessing the screen pixel buffer and all off-screen buffers), also allows inline machine code, etc, to speed up any bottleneck points. Now I realize that most programs (or even games) don't require that kind of speed or control, but I like to know the possibilities (and limitations) of the language I am using. The game will also be one of my first attempts at doing any sort of network programming so I figured the simpler the better.
|
|