Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 menu runs slow, but fast sometimes
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
michaelp




PostPosted: Mon Mar 16, 2009 3:53 pm   Post subject: menu runs slow, but fast sometimes

I have a menu for my G9 final project. Everything seems to working okay, but the menu runs really slow sometimes, and other times, very fast, and I have no idea as to why. Most of the time it will run slower, but it will speed up occasionally.
I am gonna attach the files and images necessary to run the menu, could someone take a look at it?



TechGame.zip
 Description:
Files for the menu

Download
 Filename:  TechGame.zip
 Filesize:  57.87 KB
 Downloaded:  56 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
BigBear




PostPosted: Mon Mar 16, 2009 4:40 pm   Post subject: Re: menu runs slow, but fast sometimes

I didn't find it slow. I did find it annoying that is exits if incorrect input. Also you should use setscreen("noecho")
michaelp




PostPosted: Mon Mar 16, 2009 5:48 pm   Post subject: RE:menu runs slow, but fast sometimes

Really? What was the number that was displayed on the bottom right hand corner of the screen?
And for me, it will run fast occasionally.
Like, I will work with it for 5 min, and when I run it, it will be slow. But sometimes, when I come back to it, it will be a helluva lot faster than before. Next time I work on it, it will be slow again.

And I'll fix the exiting thing.
Wolf_Destiny




PostPosted: Mon Mar 16, 2009 6:28 pm   Post subject: Re: menu runs slow, but fast sometimes

I found that a fairly hack way of speeding up turing's image loading is to load them all at the beginning of the program. I'd assume that it loads them into the ram instead of having to read from the hard drive each time. So maybe that's an option?
BigBear




PostPosted: Mon Mar 16, 2009 6:43 pm   Post subject: RE:menu runs slow, but fast sometimes

The number starts at 0 then goes to 86 then I hit number 1 and return it is 45 then 86.
michaelp




PostPosted: Mon Mar 16, 2009 6:57 pm   Post subject: RE:menu runs slow, but fast sometimes

Holy crap.
The fastest it's been on my computer is 50 something, and that's when it's going fast. For me, it is usually at 15 or 16.
And Wolf_Destiny: my images are loaded at the start of the program.
copthesaint




PostPosted: Mon Mar 16, 2009 8:28 pm   Post subject: RE:menu runs slow, but fast sometimes

Well Michaelp If you run Turing a bunch of times, It will eventually take up more and more of your computers ram. Eventually it can take up to 2 GB of ram and more. Sooner or later you will eventually have to press Ctrl-Alt-Delete and end the Turing process. That will eliminate the build up of ram for Turing, Then run Turing again. For me I don't have to worry about this as much with 4 GB of ram on my laptop, but for computers with less, well Turing can take up a lot of ram.
michaelp




PostPosted: Tue Mar 17, 2009 8:43 am   Post subject: RE:menu runs slow, but fast sometimes

I see. When I run the menu while looking at the RAM usage for the Turing process, it ate up about 6 more megabytes of RAM/second. Neutral
I don't know if the school computers will be able to handle it!
Does anyone know why though? I free the rotatedCD pic at the end of each loop. Sad
Sponsor
Sponsor
Sponsor
sponsor
copthesaint




PostPosted: Tue Mar 17, 2009 9:11 am   Post subject: RE:menu runs slow, but fast sometimes

You free the rotatedCD pic after each loop because you don't want to declare a new picture for each change in angle. If you remove The Pic.Free (CDROTATED) then for every angle it changes you will create a new picture, then you will max out you max number of picture witch is 1000 btw.
michaelp




PostPosted: Tue Mar 17, 2009 10:55 am   Post subject: Re: menu runs slow, but fast sometimes

So, are you saying something like creating an array at the beginning of the program, with all the necessary rotated CD's would be better?
I just did that. Here is the new code for menu.t:
code:
%menu.t
%Menu for final game

%Include the needed files
include "name.t" %For the credits procedure
include "game.t" %For the game procedure

%Include the necessary images
var eye : int := Pic.FileNew ("images/eye.bmp")
var scaledEye := Pic.Scale (eye, 160, 80)
Pic.Free (eye)
var catCD : int := Pic.FileNew ("images/catcd.bmp")
var character : int := Pic.FileNew ("images/character.bmp")
var scaledCharacter : int := Pic.Scale (character, 250, 375)
Pic.Free (character)

%Set the screen
View.Set ("offscreenonly;graphics:1000;700,nobuttonbar,noecho,nocursor,title: Michael's Game")
colorback (21)
cls

%Font for menu
var menuFont : int := Font.New ("mono:48:bold,italic,underline")
var selectionFont : int := Font.New ("mono:30:bold,italic")
var fontColour : int
%Variables for cat spinning
var degrees : int := 0
%Colours for the stars
var starColour : int
%Where to put the character
var x : int := 0
%Process for getting option
var choice : string (1) := '0'
process getOption
    getch (choice)
end getOption
fork getOption
cls

%Procedure for doing x  =0, degree = 0, choice = 0
proc setZeroes
    choice := '0'
    degrees := 0
    x := 0
    cls
    View.Update
    fork getOption
end setZeroes

var rotatedCD : array 1 .. 36 of int
var currentCD : int := 0
for i : 1 .. 36
    rotatedCD( i ) := Pic.Rotate( catCD, i * 10, Pic.Width( catCD ) div 2, Pic.Height( catCD ) div 2 )
end for

%Input array
var chars : array char of boolean
%FPS Counter
var count : int := 0
var loopStart : int := Time.Elapsed
var FPS : int := 0
%Menu
loop
    %FPS count
    if Time.Elapsed - loopStart >= 1000 then
        FPS := count
        count := 0
        loopStart := Time.Elapsed
    end if
    count := count + 1
    Font.Draw (intstr (FPS), 0, 0, menuFont, white)

    %The main menu
    if choice = '0' then
        %Rotate the cd
        %degrees := degrees + 10
        %rotatedCD := Pic.Rotate (catCD, degrees, Pic.Width (catCD) div 2, Pic.Height (catCD) div 2)
        currentCD := currentCD + 1
        if currentCD > 36 then
            currentCD := 1
        end if
        %Move the character
        if x + Pic.Width (scaledCharacter) < maxx then
            x := x + 15
        end if
        %Draw the images
        %Pic.Draw (rotatedCD, 0, maxy - Pic.Height (catCD), picMerge)
        %Pic.Draw (rotatedCD, 800, maxy - Pic.Height (catCD), picMerge)
        Pic.Draw( rotatedCD( currentCD ), 0, maxy - Pic.Height( catCD ), picMerge )
        Pic.Draw( rotatedCD( currentCD ), 800, maxy - Pic.Height( catCD ), picMerge )
        fontColour := Rand.Int (20, 30)
        Font.Draw ("Michael's Game", 250, maxy - 48, menuFont, fontColour) %48 = size of menuFont
        %Draw the eyes
        Pic.Draw (scaledEye, 300, 520, picMerge)
        Pic.Draw (scaledEye, 540, 520, picMerge)
        %Draw a nose
        Draw.Line (510, 550, 460, 430, black)
        Draw.Line (460, 430, 530, 430, black)
        %SMILE!
        Draw.Arc (490, 420, 95, 25, 180, 360, black)
        %Draw a line to separate the menu
        Draw.ThickLine (0, 380, maxx, 380, 5, black)
        %Draw the menu options
        Font.Draw ("1. Play Michael's Game", 0, 200, selectionFont, brightred)
        Font.Draw ("2. View the credits", 0, 160, selectionFont, brightred)
        Font.Draw ("3. Quit", 0, 120, selectionFont, brightred)
        Font.Draw ("Enter your choice:", 0, 60, selectionFont, yellow)
        %Decoration
        starColour := Rand.Int (1, 7)
        Draw.FillStar (220, 400, 340, 520, starColour)
        Draw.FillStar (650, 400, 770, 520, starColour)
        %Draw the character in the middle
        Pic.Draw (scaledCharacter, x, 0, picMerge)
        %Finish the loop
        View.UpdateArea (0, 380, maxx, maxy)
        View.UpdateArea (0, 0, x + Pic.Width (scaledCharacter) - 1, Pic.Height (scaledCharacter) + 2)
        cls
        Time.DelaySinceLast( round( 1000/18 ) )
        %Free the picture so we don't run out of picID's
        %Pic.Free (rotatedCD)
    elsif choice = '1' then
        playGame
        setZeroes
    elsif choice = '2' then
        colorback( black )
        makeName
        setZeroes
        colorback( 21 )
        cls
        View.Update
    elsif choice = '3' then
        exit
    else
        choice := '0'
        fork getOption
    end if
end loop

%End the game
var bigFont : int := Font.New ("palatino:90:italic,underline")
for i : 1 .. 300
    Draw.Dot (Rand.Int (0, maxx), Rand.Int (0, maxy), white)
end for
Font.Draw ("Hope you had fun!", 0, 300, bigFont, brightpurple)
View.Update
copthesaint




PostPosted: Tue Mar 17, 2009 11:19 am   Post subject: RE:menu runs slow, but fast sometimes

No I didn't What you had before was right
I was just trying to explain why you use Pic.Free
The_Bean




PostPosted: Wed Mar 18, 2009 6:21 pm   Post subject: Re: menu runs slow, but fast sometimes

On my computer your original programs runs around 62
The update you posted ran around 18

Using some of my tricks I got it to average at 124 and sometimes peek at 152
So it is possible to greatly increase the speed.

1)Creating and freeing images takes a lot of time. So hold the rotated CD images in an array.
2)Avoid using processes, use Input.KeyDown() to get the input instead.
3)picMerge is really really slow. Change the image backgrounds from white to grey and use picCopy instead.
4)View.UpdateArea() works great for speeding things up, but not if you use it more than once in a loop, just use View.Update.
michaelp




PostPosted: Thu Mar 19, 2009 10:08 am   Post subject: RE:menu runs slow, but fast sometimes

The update ran around 18 because of this line:
code:
Time.DelaySinceLast( round( 1000/18 ) )

That means it was working. Razz
Out of the things you just suggested, I did the array thing. I'll see what I can do to implement the other things.

edit: Okay, I've done most of the things you said, and I got it to run at 100.
I didn't change the View.UpdateArea's because:
1. They are used for effect.
2. There wasn't a significant speed-up.(or any,really)
Also, I tried to picCopy the rotating CD's: didn't work out well!
But everything else is working great, got rid of the process and what not.
Here is the new code:(The Time.DelaySinceLast line is commented out)
Turing:
%menu.t
%Menu for final game

%Include the needed files
include "name.t" %For the credits procedure
include "game.t" %For the game procedure

%Include the necessary images
var eye : int := Pic.FileNew ("images/eye.bmp")
var scaledEye := Pic.Scale (eye, 160, 80)
Pic.Free (eye)
var catCD : int := Pic.FileNew ("images/catcd.bmp")
var character : int := Pic.FileNew ("images/character.bmp")
var scaledCharacter : int := Pic.Scale (character, 250, 375)
Pic.Free (character)

%Set the screen
View.Set ("offscreenonly;graphics:1000;700,nobuttonbar,noecho,nocursor,title: Michael's Game")
colorback (21)
cls
View.Update

%Font for menu
var menuFont : int := Font.New ("mono:48:bold,italic,underline")
var selectionFont : int := Font.New ("mono:30:bold,italic")
var fontColour : int
%Variables for cat spinning
var degrees : int := 0
%Colours for the stars
var starColour : int
%Where to put the character
var x : int := 0
%Process for getting option
var choice : string (1) := '0'

%Procedure for doing x  =0, degree = 0, choice = 0
proc setZeroes
    choice := '0'
    degrees := 0
    x := 0
    cls
    View.Update
end setZeroes

var rotatedCD : array 1 .. 36 of int
var currentCD : int := 0
for i : 1 .. 36
    rotatedCD (i) := Pic.Rotate (catCD, i * 10, Pic.Width (catCD) div 2, Pic.Height (catCD) div 2)
end for

%Input array
var chars : array char of boolean
%FPS Counter
var count : int := 0
var loopStart : int := Time.Elapsed
var FPS : int := 0
%Menu
loop
    %FPS count
    if Time.Elapsed - loopStart >= 1000 then
        FPS := count
        count := 0
        loopStart := Time.Elapsed
    end if
    count := count + 1
    Font.Draw (intstr (FPS), 0, 0, menuFont, white)
    %Input
    Input.KeyDown( chars )
    if chars( chr( ORD_1 ) ) then
        choice := '1'
    end if
    if chars( chr( ORD_2 ) ) then
        choice := '2'
    end if
    if chars( chr( ORD_3 ) ) then
        choice := '3'
    end if

    %The main menu
    if choice = '0' then
        %Rotate the cd
        currentCD := currentCD + 1
        if currentCD > 36 then
            currentCD := 1
        end if
        %Move the character
        if x + Pic.Width (scaledCharacter) < maxx then
            x := x + 15
        end if
        %Draw the images
        Pic.Draw (rotatedCD (currentCD), 0, maxy - Pic.Height (catCD), picMerge)
        Pic.Draw (rotatedCD (currentCD), 800, maxy - Pic.Height (catCD), picMerge)
        fontColour := Rand.Int (20, 30)
        Font.Draw ("Michael's Game", 250, maxy - 48, menuFont, fontColour)         %48 = size of menuFont
        %Draw the eyes
        Pic.Draw (scaledEye, 300, 520, picCopy)
        Pic.Draw (scaledEye, 540, 520, picCopy)
        %Draw a nose
        Draw.Line (510, 550, 460, 430, black)
        Draw.Line (460, 430, 530, 430, black)
        %SMILE!
        Draw.Arc (490, 420, 95, 25, 180, 360, black)
        %Draw a line to separate the menu
        Draw.ThickLine (0, 380, maxx, 380, 5, black)
        %Draw the menu options
        Font.Draw ("1. Play Michael's Game", 0, 200, selectionFont, brightred)
        Font.Draw ("2. View the credits", 0, 160, selectionFont, brightred)
        Font.Draw ("3. Quit", 0, 120, selectionFont, brightred)
        Font.Draw ("Enter your choice:", 0, 60, selectionFont, yellow)
        %Decoration
        starColour := Rand.Int (1, 7)
        Draw.FillStar (220, 400, 340, 520, starColour)
        Draw.FillStar (650, 400, 770, 520, starColour)
        %Draw the character in the middle
        Pic.Draw (scaledCharacter, x, 0, picCopy)
        %Finish the loop
        View.UpdateArea (0, 375, maxx, maxy)
        View.UpdateArea (0, 0, x + Pic.Width (scaledCharacter) - 1, Pic.Height( scaledCharacter ))
        cls
        %Time.DelaySinceLast (round (1000 / 5))
    elsif choice = '1' then
        playGame
        setZeroes
    elsif choice = '2' then
        colorback (black)
        makeName
        setZeroes
        colorback (21)
        cls
        View.Update
    elsif choice = '3' then
        exit
    end if
end loop

%End the game
var bigFont : int := Font.New ("palatino:90:italic,underline")
var starX, starY : int
for i : 1 .. 150
    starX := Rand.Int( 0, maxx )
    starY := Rand.Int( 0, maxy )
    Draw.FillStar ( starX, starY, starX + 10, starY + 10white)
end for
Font.Draw ("Hope you had fun!", 0, 300, bigFont, brightpurple)
View.Update
The_Bean




PostPosted: Thu Mar 19, 2009 11:19 am   Post subject: Re: menu runs slow, but fast sometimes

You can get even faster by using the picCopy on the CD, but you need to set the transparent colour of the main CD image to grey before you rotate it. This way the new white sections it makes will actually be grey.
It made for a gain 20 on my comp.
michaelp




PostPosted: Thu Mar 19, 2009 11:37 am   Post subject: RE:menu runs slow, but fast sometimes

Awesome, doing that gets it up to 115-120. Thanks for the help, I don't need it going any faster now. Razz
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: