Irregular polygon, using OS API

Just starting out? Need help? Post your questions and find answers here.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Irregular polygon, using OS API

Post by Shardik »

ken_anthony,

thank you for following Zach's advice and taking the effort to cut down your code down to the bare minimum to demonstrate your problem. The answer to your problem is a very simple one:
Your procedure Does_Not_Work() doesn't draw your polygon because your window isn't refreshed. The callback WidgetExposeHandler() is only activated if your window is changed by a system action like resizing your window. So your posted code already displays the polygon without any change as soon as you start resizing your window. To draw the polygon on each call to procedure Does_Not_Work() simply put this line

Code: Select all

WidgetExposeHandler(FixedBox, 0)
at the end of your procedure Does_Not_Work(). Additionally you have to define FixedBox as Global or Shared. This problem you would have seen if you would have used EnableExplicit. I advice you to always use EnableExplicit in all your PureBasic programs because lots of problems are solved if all variables are properly declared in their respective scope... :wink:
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Irregular polygon, using OS API

Post by Zach »

See, I'm helpful :mrgreen:

(I did think Scope might be involved but wasn't sure)
ken_anthony
User
User
Posts: 77
Joined: Thu Jun 20, 2013 5:41 am
Location: Springerville AZ USA

Re: Irregular polygon, using OS API

Post by ken_anthony »

I am in awe. You have no idea how grateful I am. I don't know how long, if ever, it would have taken me to stumble upon the solution. I had actually tested something close to that by moving a window over the polygon then away to see if it was persistent. Now I have to see if I would accidentally have seen that by doing the same after one click of my test...

So thank you both. Yes, you too Zach. Ya did good.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Irregular polygon, using OS API

Post by luis »

I know in this case was only part of the problem, but I think we should coin a name... something like EDS maybe ?

EnableExplicit Deficiency Syndrome.

This is an illness that can be stopped.

Act now !

:)
"Have you tried turning it off and on again ?"
A little PureBasic review
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: Irregular polygon, using OS API

Post by LuCiFeR[SD] »

luis wrote:I know in this case was only part of the problem, but I think we should coin a name... something like EDS maybe ?

EnableExplicit Deficiency Syndrome.

This is an illness that can be stopped.

Act now !

:)
Yes! EnableExplicit... better than a condom :)
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Irregular polygon, using OS API

Post by Zach »

EnableExplicit: The only brand that guarantees no unwanted children, even if it breaks
ken_anthony
User
User
Posts: 77
Joined: Thu Jun 20, 2013 5:41 am
Location: Springerville AZ USA

Re: Irregular polygon, using OS API

Post by ken_anthony »

EnableExplicit is now part of my project. May god have mercy on my soul.

Thanks to this solution for polygons I have gone ahead and paid for a year of hosting. Nothing to see now, but...

http://www.galaxy-is-waiting.com

...is my new domain. After I've learned all I can about my host and established a test ProgreSQL database I intend to purchase Navicat for managing my database.

It's just become real. One year from now I hope to publish. I hope to share the ride with some of my new friends here.

Elements of this program existed in 1975 when I was in high school and kids used to whisper and point, "That's the guy!"

I'm hoping my multi-user game will handle thousands of players per server. It will be like nothing I've ever seen others do. I'm hoping I haven't bitten off more than I can chew. I will have a lot to learn between now and then. If I wondered about PureBasic being the right choice, the people on this forum have convinced me.

My next project will be a staid business application, but that's for another day. Actually, I have a few boring business applications in mind, but again, those will have to wait until after I publish this game.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Irregular polygon, using OS API

Post by Shardik »

For comparsion this is an example using the deprecated GDK function gdk_draw_polygon():

Code: Select all

EnableExplicit

Define *FixedBox.GtkWidget

Dim PolygonPoints.GdkPoint(4)

ProcedureC WidgetExposeHandler(*Widget.GtkWidget, *Event.GdkEventExpose)
  Shared PolygonPoints()

  Protected RedColor.GdkColor

  gdk_color_parse_("red", @RedColor)

  ; WARNING:
  ; gdk_gc_set_rgb_fg_color() has been deprecated since GDK 2.22 and should not be used
  ; in newly-written code. You should use gdk_cairo_set_source_color() instead.

  gdk_gc_set_rgb_fg_color_(*Widget\style\fg_gc, @RedColor) 

  ; WARNING:
  ; gdk_draw_polygon() has been deprecated since GDK 2.22 and should not be used in
  ; newly-written code. You should use cairo_line_to() or cairo_append_path() and
  ; cairo_fill() or cairo_stroke() instead.

  gdk_draw_polygon_(*Widget\window, *Widget\style\fg_gc, #True, @PolygonPoints(), 5)
EndProcedure

PolygonPoints(0)\x = 10
PolygonPoints(0)\y = 10
PolygonPoints(1)\x = 160
PolygonPoints(1)\y = 10
PolygonPoints(2)\x = 185
PolygonPoints(2)\y = 85
PolygonPoints(3)\x = 160
PolygonPoints(3)\y = 210
PolygonPoints(4)\x = 60
PolygonPoints(4)\y = 135

OpenWindow(0, 270, 100, 200, 220, "Polygon example")
*FixedBox.GtkWidget = g_list_nth_data_(gtk_container_get_children_(gtk_bin_get_child_(WindowID(0))), 0)
g_signal_connect_(*FixedBox, "expose-event", @WidgetExposeHandler(), 0)
WidgetExposeHandler(*FixedBox, 0)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Irregular polygon, using OS API

Post by Zach »

Multiplayer Elite ? 8)

I still haven't played that game but that's what comes to mind.
ken_anthony
User
User
Posts: 77
Joined: Thu Jun 20, 2013 5:41 am
Location: Springerville AZ USA

Re: Irregular polygon, using OS API

Post by ken_anthony »

I don't want to be influenced by looking at other games at this point but I did see this blurb...
Space in Frontier was especially deep, with a full-scale galaxy containing 100 billion stars and several empires with their own legal systems and trading outposts. Players could choose to raid other ships or play it straight, mining moons, scooping fuel from gas giants, and landing on planets to survey them for materials.
I will not have 100 billion stars. 500 stars (on a non interactive background of more) with all the rocks flying around them should be more than enough space for a few thousand players. No empires other than what the players make. There will be non player AI both good and bad, but they will be mostly background. There will be some AI trading, but mostly it will be between players. Every visible star will start with one planet having a population, but I haven't decided how I will handle political systems yet. No gas giants or double star systems. Yes, there will be brown dwarf stars you will have to detect before you know there locations.

All rocks will have a range of habitability, size and density, but again, no gas giants. Ships that don't have enough thrust to overcome surface gravity or enough energy to achieve orbit can't on their own.

Alliances between players will be very complex due to economic ties. How is my secret for now.

Ships will not come from a list. Players will both design them functionally and visually (even by not artists... and it's fun. I've gotten myself, a very non artist, lost in making ships rather than programming as I should be.) A good design will profit the player as others build that class of ship for themselves.

Most ships will not be star ships. They will stay in one solar system unless transported. The biggest ships will not be transported and only some of them will have FTL engines. 18 different star ship engines may be available if someone builds them. Worm holes are also possible but not as you might imagine. I'm keeping that to myself as well for now.

I'm no artist, so most of the artwork will be supplied by the players. Things will get bigger the closer you are to them (this is where vector graphics works well.)
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Irregular polygon, using OS API

Post by Zach »

Yeah I'm not an artists either.. That's why my first game is going to be a Text RPG! ...I if can ever get my development tools finished :|

It certainly sounds like an ambitious project, I hope you have success with it.
ken_anthony
User
User
Posts: 77
Joined: Thu Jun 20, 2013 5:41 am
Location: Springerville AZ USA

Re: Irregular polygon, using OS API

Post by ken_anthony »

Did I mention you start the game like Christina Hendricks left Malcolm Reynolds in one episode of firefly. A firefly is a small to medium sized ship with two shuttles. Yes, they are in the game.
ken_anthony
User
User
Posts: 77
Joined: Thu Jun 20, 2013 5:41 am
Location: Springerville AZ USA

Re: Irregular polygon, using OS API

Post by ken_anthony »

BTW, next year I will need beta testers and later I anticipate paid hosts that start with a constellation class starship to help the weak players get started and enforce galactic law which has no silly prime directive. Would you like to get paid for playing my game? You've already demonstrated the qualities my hosts will require. But again, I'm not there yet.

Regarding naked Mal, players aren't going to be specific characters like in a sim, but you do start with nothing, not even a starter ship. You will get resources as you play. New players can get some advantages and income from contacting more advanced players.

Shardik, I am so glad for Cairo. I don't even want to think about GTK (ok, other than for the intellectual stimulation.) From a business standpoint, having Cairo on every Linux distribution means one less head ache for me.
ken_anthony
User
User
Posts: 77
Joined: Thu Jun 20, 2013 5:41 am
Location: Springerville AZ USA

Re: Irregular polygon, using OS API

Post by ken_anthony »

Shardik,

I believe I've found a couple of oops.

The most severe for me is that only RGB values of 0 or 255 work. Any value from 1 to 254 is treated as zero.

The second oops requires me to put together an example which will take a bit of time, but I will do so and post it here later.
ken_anthony
User
User
Posts: 77
Joined: Thu Jun 20, 2013 5:41 am
Location: Springerville AZ USA

Re: Irregular polygon, using OS API

Post by ken_anthony »

Alright, I've put together this little test. Question: Why is the green polygon showing up on the second form?

Originally, I had an error producing a polygon on a canvas on a second form, but I haven't reproduced it yet.

Main file

Code: Select all

EnableExplicit

Global wMain, wSecond

Enumeration
#m1    
EndEnumeration

Structure Point
  x.D
  y.D
EndStructure

Structure PolygonEntry
  ColorRed.D
  ColorGreen.D
  ColorBlue.D
  Fill.L
  List PolygonPoint.Point()
EndStructure

Global MainForm.I, SecondForm.l

Declare WidgetExposeHandler(*a,*b)
Global NewList Polygon.PolygonEntry()
Global NewList PolygonPoint.Point()
IncludeFile "/home/ken/PureBasic/polygon.Shardik"   ;This file hasn't changed from first test. Use the same one.
IncludeFile "/home/ken/PureBasic/Second.form"

Procedure Load_data()
Define i.I
For i = 1 To 5
  AddElement(PolygonPoint())
  Read.D PolygonPoint()\x
  Read.D PolygonPoint()\y
Next i
EndProcedure

Load_data()
DataSection
  ; -- Pentagon
  Data.D   0,   0
  Data.D 150,   0
  Data.D 175,  75
  Data.D 150, 200
  Data.D  50, 125
EndDataSection

; ----- Display window and define expose handler
Procedure OpenMain()
  wMain = OpenWindow(#PB_Any, 270, 100, 500, 500, "Polygon demo", #PB_Window_SizeGadget)
  SetWindowColor(wMain, 0)
  MainForm = g_list_nth_data_(gtk_container_get_children_(gtk_bin_get_child_(WindowID(wMain))), 0)
  g_signal_connect_data_(MainForm, "expose-event", @WidgetExposeHandler(), 0, 0, #G_CONNECT_AFTER)
  CreatePolygon(PolygonPoint(), 250, 200, RGB(254, 255, 0), #True) ; THIS SHOULD BE YELLOW, NOT GREEN.
  WidgetExposeHandler(MainForm, 0)
  
   CreateMenu(#PB_Any, WindowID(wMain))
   MenuTitle("Open")
   MenuItem(#m1, "2nd form")
EndProcedure  

Procedure wMain_Events(event)
  Select event
    Case #PB_Event_CloseWindow
        End
        
    Case #PB_Event_Menu
        Select EventMenu()
            Case #m1
                OpenSecondForm()
        EndSelect
  EndSelect
EndProcedure

OpenMain()
Repeat  :  wMain_Events(WaitWindowEvent()) : ForEver
Second file: "Second.form"

Code: Select all

Procedure wSecond_Events(event)
  Select event
    Case #PB_Event_CloseWindow
        End
        
    Case #PB_Event_Menu
        Select EventMenu()
            Case #m1
                CreatePolygon(PolygonPoint(), 200, 150, RGB(255, 254, 0), #True) ; THIS SHOULD BE YELLOW, NOT RED.
                WidgetExposeHandler(SecondForm, 0)
        EndSelect
  EndSelect
EndProcedure

Procedure OpenSecondForm()
    wSecond = OpenWindow(#PB_Any, 350, 150, 500, 500, "Second Form", #PB_Window_SizeGadget)
    SetWindowColor(wSecond, 0)
    SecondForm = g_list_nth_data_(gtk_container_get_children_(gtk_bin_get_child_(WindowID(wSecond))), 0)
    g_signal_connect_data_(SecondForm, "expose-event", @WidgetExposeHandler(), 0, 0, #G_CONNECT_AFTER)
  
    CreateMenu(#PB_Any, WindowID(wSecond))
    MenuTitle("Test")
    MenuItem(#m1, "Do_test")
    
    Repeat  :  wSecond_Events(WaitWindowEvent()) : ForEver
    
EndProcedure
Third file is same as first test.

Code: Select all

ImportC ""
  gdk_cairo_create(*Drawable.GdkDrawable)
EndImport

ImportC "-lcairo"
  cairo_close_path(*CairoContext)
  cairo_destroy(*CairoContext)
  cairo_fill(*CairoContext)
  cairo_line_to(*CairoContext, x.D, y.D)
  cairo_set_line_width(*CairoContext, LineWidth.D)
  cairo_set_source_rgb(*CairoContext, Red.D, Green.D, Blue.D)
  cairo_stroke(*CairoContext)
EndImport

Procedure CreatePolygon(List PolygonPoint.Point(), x.D, y.D, Color.L, Fill.L)
  Shared Polygon.PolygonEntry()

  Protected ColorPart.L
  Protected i.I

  AddElement(Polygon())

  ForEach PolygonPoint()
    AddElement(Polygon()\PolygonPoint())
    Polygon()\PolygonPoint()\x = x + PolygonPoint()\x
    Polygon()\PolygonPoint()\y = y + PolygonPoint()\y
  Next

  ColorPart = (Color & $FF) / 255
  Polygon()\ColorRed = ColorPart
  ColorPart = ((Color >> 8) & $FF) / 255
  Polygon()\ColorGreen = ColorPart
  ColorPart = ((Color >> 16) & $FF) / 255
  Polygon()\ColorBlue = ColorPart
  Polygon()\Fill = Fill
EndProcedure

Procedure DrawPolygons(*Widget.GtkWidget)
  Shared Polygon.PolygonEntry()

  Protected CairoContext.I = gdk_cairo_create(*Widget\window)

  ForEach Polygon()
    If Polygon()\Fill
      ForEach Polygon()\PolygonPoint()
        cairo_line_to(CairoContext, Polygon()\PolygonPoint()\x, Polygon()\PolygonPoint()\y)
      Next

      cairo_close_path(CairoContext)
      cairo_set_source_rgb(CairoContext, Polygon()\ColorRed, Polygon()\ColorGreen, Polygon()\ColorBlue)
      cairo_fill(CairoContext)
    Else
      cairo_set_line_width(CairoContext, 1)
      cairo_set_source_rgb(CairoContext, Polygon()\ColorRed, Polygon()\ColorGreen, Polygon()\ColorBlue)

      ForEach Polygon()\PolygonPoint()
        cairo_line_to(CairoContext, Polygon()\PolygonPoint()\x, Polygon()\PolygonPoint()\y)
      Next

      cairo_close_path(CairoContext)
      cairo_stroke(CairoContext)
    EndIf
  Next

  cairo_destroy(CairoContext)
EndProcedure

ProcedureC WidgetExposeHandler(*Widget.GtkWidget, *Event.GdkEventExpose)
  DrawPolygons(*Widget.GtkWidget)
EndProcedure
Post Reply