Re: Help with Simple Graphics Demo

new topic     » goto parent     » topic index » view thread      » older message » newer message

This is a multi-part message in MIME format.
--------------040805020909040804040603

Derek Parnell wrote:

>Yes, a lot of the demo programs are crap. So do something about it, people.
>  
>
I think the demo programs serve a very good purpose. Maybe some could be
combined to cut down on the volume, but I use the demo programs
extensively while I'm learning to program win32lib. It's taking a long
time to learn because I have precious few minutes to devote to the
task... of course, the IDE makes it disgustingly easy... heheh.

>I've already added the dice-throw demo to the collection.
>  
>
I'm attaching the latest version of Brian's and Evan's
die-rolling-graphing code... Derek, if you made any modifications
yourself, would you send me a copy?

Thanks!
-ck




--------------040805020909040804040603
Content-Type: text/plain;
 name="rolling_die_1.exw"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="rolling_die_1.exw"

include Win32lib.ew
without warning

sequence last_graph_size
	last_graph_size = {0,0}
--------------------------------------------------------------------------------

-- Window Window1
constant Window1 = createEx( Window, "Thrown Dice Chart", 0, 0, 0, 276, 113, 0,
0 ),
  txt_Sides = createEx( EditText, "6", Window1, 48, 8, 48, 20, 0, 0 ),
  LText3 = createEx( LText, "Sides", Window1, 12, 12, 36, 20, 0, 0 ),
  LText4 = createEx( LText, "QTY", Window1, 12, 36, 36, 20, 0, 0 ),
  txt_QTY = createEx( EditText, "3", Window1, 48, 32, 48, 20, 0, 0 ),
  LText6 = createEx( LText, "Rolls", Window1, 12, 60, 36, 20, 0, 0 ),
  txt_Rolls = createEx( EditText, "1000", Window1, 48, 56, 48, 20, 0, 0 ),
  bttn_Roll = createEx( PushButton, "Roll", Window1, 104, 8, 156, 68, 0, 0 ),
  Graph = createEx( Pixmap, "Bitmap9", Window1, 8, 84, 252, 184, 0, 0 )
--------------------------------------------------------------------------------

procedure update_roll_button()   
    setText( bttn_Roll, getText( txt_QTY ) & "d" & getText( txt_Sides ) 
& " x " & getText( txt_Rolls ) )
end procedure

update_roll_button()

  ------------------------------

procedure bttn_Roll_onClick (integer self, integer event, sequence 
params)--params is ()
	atom width, gWidth, height
    integer dice_num, dice_sides, dice_rolls, roll, slot, maxVal
    sequence dice_results, graph_size, rolls

	-- clear last graph
	if not equal( last_graph_size, {0,0}) then -- clear it
		setPenColor( Graph, getSysColor( COLOR_BTNFACE ) )
drawRectangle ( Graph, W32_True, 0, 0, last_graph_size[1], last_graph_size[2]
)
		copyBlt( Window1,8,84,Graph)
	end if

    dice_num = getNumber( txt_QTY )
    dice_sides = getNumber( txt_Sides )
    dice_rolls = getNumber( txt_Rolls )
    dice_results = repeat(0,dice_sides*dice_num-dice_num+1)

--	first get a set of numbers
	rolls = {}
	
    for i = 1 to dice_rolls do
      roll = 0
      for j = 1 to dice_num do
        roll += rand( dice_sides )
      end for
      slot = roll-dice_num+1
      dice_results[slot] += 1
      rolls &= slot
	end for
	
	maxVal = 0
	for t=1 to length(dice_results) do
		if maxVal < dice_results[t] then
			maxVal = dice_results[t]
		end if
	end for

	-- now set window height
	graph_size = { 30*(dice_sides*dice_num-dice_num+1)+25, maxVal }

	gWidth = graph_size[1]+20
	if gWidth < 276 then
		gWidth = 276
	end if
	
	setCtlSize( Graph, graph_size[1], graph_size[2] )
    setCtlSize( Window1, gWidth, graph_size[2]+113 )

      setPenColor( Graph, Black )
    for i = dice_num to dice_num*dice_sides do
      setPenPos( Graph, 30*(i-dice_num), graph_size[2]-15 )
      wPrintf( Graph, "%d", {i} )
    end for

    dice_results = repeat(0,dice_sides*dice_num-dice_num+1)
    for i=1 to dice_rolls do
      slot = rolls[i]
      dice_results[slot] += 1
      drawLine(Graph,30*(slot)-30,graph_size[2]-20-dice_results[slot],
                     30*(slot)-20,graph_size[2]-20-dice_results[slot])
      copyBlt(Window1,8,84,Graph)
    end for
    
    last_graph_size = graph_size

--    ? dice_results
end procedure
setHandler( bttn_Roll, w32HClick, routine_id("bttn_Roll_onClick"))

  ------------------------------

procedure txt_onChange (integer self, integer event, sequence 
params)--params is ()
    -- update button when txt_Sides, txt_Rolls, or txt_QTY changes
    update_roll_button()
end procedure
setHandler( {txt_Sides,txt_Rolls,txt_QTY}, w32HChange, 
routine_id("txt_onChange"))

  ------------------------------
  
procedure paintWindow( integer self, integer event, sequence params )
    copyBlt(Window1,8,84,Graph)
end procedure
setHandler( Window1, w32HPaint, routine_id("paintWindow") )

  ------------------------------
  WinMain( Window1,Normal )
  ------------------------------

--------------040805020909040804040603
Content-Type: text/plain;
 name="rolling_die_2.exw"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="rolling_die_2.exw"

include Win32lib.ew
without warning

-- 
-- Window Window1
constant Window1 = createEx( Window, "Thrown Dice Chart", 0, Default,
Default, 276, 300, 0, 0 )
constant txt_Sides = createEx( EditText, "6", Window1, 48, 8, 48, 20, 0, 0 )
constant LText3 = createEx( LText, "Sides", Window1, 12, 12, 36, 20, 0, 0 )
constant LText4 = createEx( LText, "QTY", Window1, 12, 36, 36, 20, 0, 0 )
constant txt_QTY = createEx( EditText, "3", Window1, 48, 32, 48, 20, 0, 0 )
constant LText6 = createEx( LText, "Rolls", Window1, 12, 60, 36, 20, 0, 0 )
constant txt_Rolls = createEx( EditText, "1000", Window1, 48, 56, 48,20, 0, 0 )
constant bttn_Roll = createEx( PushButton, "Roll", Window1, 104, 8, 156,68, 0, 0
)
constant Bitmap9 = createEx( Pixmap, "Bitmap9", Window1, 8, 84, 252,184, 0, 0 )

-- 


procedure update_roll_button()
    setText( bttn_Roll, getText( txt_QTY ) & "d" & getText( txt_Sides )
& " x " & getText( txt_Rolls ) )
end procedure

update_roll_button()

-- 

procedure txt_Sides_onChange (integer self, integer event, sequence
params)--params is ()
    update_roll_button()
end procedure
setHandler( txt_Sides, w32HChange, routine_id("txt_Sides_onChange"))

-- 

procedure bttn_Roll_onClick (integer self, integer event, sequence
params)--params is ()
integer dice_num, dice_sides, dice_rolls, roll
sequence dice_results

integer BIGGEST, WIDTH, HEIGHT

    BIGGEST = 0
    dice_num = getNumber( txt_QTY )
    dice_sides = getNumber( txt_Sides )
    dice_rolls = getNumber( txt_Rolls )
    dice_results = repeat(0,dice_sides*dice_num)

    WIDTH = floor(252/(dice_num*dice_sides))
    HEIGHT = floor(100/(dice_num*dice_sides))

    for t=1 to dice_rolls do
        roll = 0
        for loop = 1 to dice_num do
            roll += floor(rand( (dice_sides-1)* 1000)/1000)+1
        end for
        dice_results[roll] += 1
        if dice_results[roll] > BIGGEST then
            BIGGEST = dice_results[roll]
       end if
    end for
    setPenColor(Bitmap9,Black)
    drawRectangle(Bitmap9,1,0,0,244,183)
    setPenColor(Bitmap9,BrightRed)
    for loop = 1 to length(dice_results) do
drawRectangle(Bitmap9,1,(loop-1) * WIDTH,183,((loop-1) *
       WIDTH)+WIDTH,183-(floor((dice_results[loop]/BIGGEST)*183)))
    end for
    copyBlt(Window1,8,80,Bitmap9)

end procedure
setHandler( bttn_Roll, w32HClick, routine_id("bttn_Roll_onClick"))

-- 

procedure txt_QTY_onChange (integer self, integer event, sequence
params)--params is ()
    update_roll_button()
end procedure
setHandler( txt_QTY, w32HChange, routine_id("txt_QTY_onChange"))

-- 

procedure txt_Rolls_onChange (integer self, integer event, sequence
params)--params is ()
    update_roll_button()
end procedure
setHandler( txt_Rolls, w32HChange, routine_id("txt_Rolls_onChange"))


WinMain( Window1,Normal ) 

--------------040805020909040804040603--

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu