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

Username:   Password: 
 RegisterRegister   
 Crumbling Picture *optimized like I never believed possible*
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Lapsus Antepedis




PostPosted: Mon May 16, 2005 11:53 am   Post subject: Crumbling Picture *optimized like I never believed possible*

The code has been re optimized to the point where putting the variables in the array actually takes longer than making the stuff fall! Shocked
It will work with a picture of any size, and should be compatibe with Turing for 3.1.1 for people like me who have bad install disks...
Thank you Mr. Gault!


code:


% By:Lapsus Antepedis and heavily edited by Mr.Gault
% (he optimized the h*** out of it!)
var windoe : int := Window.Open ("graphics:500;100")
var pixl : array 0 .. maxx, 0 .. maxy of int
var pictor : int := Pic.FileNew ("falling.bmp")
% fallPoint and y are Mr.Gault's variables
var fallPoint : int
var y : int

Pic.Draw (pictor, 0, 0, picCopy)
Pic.Free (pictor)

for x : 0 .. maxx
    for l : 0 .. maxy
        pixl (x, l) := whatdotcolour (x, l)
    end for
end for

% The rest has been rewritten by Mr.Gault, the above is by Lapsus Antepedis
for x : 0 .. maxx
    y := 0
    loop
        y += 1
        if pixl (x, y) = 0 then
            fallPoint := y
        end if
        exit when pixl (x, y) = 0 or y = maxy - 1
    end loop
    for l : fallPoint .. maxy
        if pixl (x, l) not= 0 then
            pixl (x, fallPoint) := pixl (x, l)
            pixl (x, l) := 0
            drawdot (x, l, 0)
            drawdot (x, fallPoint, pixl (x, fallPoint))
            fallPoint += 1
        end if
    end for
end for



I wrote this at home one day because one of my friends made a remake of scorched earth that had falling dirt, so I waned to make something fall too. It's fully commented because I felt like it... Beware, you will need a super-ultra-mega fast computer to make more than a few columns fall at a reasonable speed, it's just too hard for a processor to do alone... (my school's brand new P4 dualies can do 25 at once... ^^; ) Anyway, enough of me, here's the code:
(you also need the bitmap at the bottom so it actually does something...)

code:


/* This program was written by Lapsus Antepedis as an attempt to
 make graphics fall quickly, it was inspired by Mr. Gault's
 tank game that has a fairly good ability to make dirt fall...
 it also works with 256 colours, but it tends to eat any images
 from higher colour settings. (Die gradients! DIE!) */

% Opens a window that is 500px wide and 100 px tall
var window : int := Window.Open ("graphics:500;100")

% Opens the file that the picture to be "falling" is in
var pictor : int := Pic.FileNew ("falling.BMP")

/* sets how many columns of pixels will attempt to fall at once
 the program runs fastest when this is set to one, but it looks
 best (to me) set at five */
var fallatonce : int := 1

% Draws the picture
Pic.Draw (pictor, 0, 0, picCopy)

/* Draws a line along the bottom of the window so that the pixels
 will not fall below the window */
drawline (0, 0, maxx, 0, 7)

/* *This was used to create a random pattern of dots to test the*
 *program before I added a picture*
 for x : 0 .. maxx
 for y : 0 .. maxy
 if Rand.Int (0, 1) = 1 then
 drawdot (x, y, 7)
 end if
 end for
 end for */

% This is the main process
process falling (blah, ack : int)

    /* this chooses the column that the process will make fall
     it uses the variable "ack" to make sure that it does not
     go over a place that has already fallen, or go over a place
     that will be taken care of by another instance of this process */
    for x : blah .. maxx by ack

        /* this makes sure that the process will not check higher
         than it has to */
        for z : 0 .. maxy

            % this is the bit that does all of the work
            for y : 1 .. maxy - z

                /* this checks to see if the current pixel has something in it,
                 as well as if the pixel below is empty */
                if whatdotcolour (x, y) not= 0 and whatdotcolour (x, y - 1) = 0 then

                    % this draws a dot in the empty space below in the colour of the dot to be removed
                    drawdot (x, y - 1, whatdotcolour (x, y))

                    % this erases the current dot
                    drawdot (x, y, 0)

                    % closing all the open statements
                end if
            end for
        end for
    end for
end falling

% this draws the picture
Pic.Draw (pictor, 0, 0, picCopy)

/* this draws a line across the bottom of the screen
 to ensure that the pixels do not fall off */
drawline (0, 0, maxx, 0, 7)

% this starts however many columns falling at once
for i : 0 .. fallatonce - 1
    fork falling (i, fallatonce)
end for





falling.bmp
 Description:
This is the picture you need
 Filesize:  146.54 KB
 Viewed:  2445 Time(s)

falling.bmp


Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Mon May 16, 2005 2:45 pm   Post subject: (No subject)

Very nicely done. A couple of suggestions:
Recode without the use of processes. You'll find your programme will run faster. OOT does not handle processes too well. But if you prefer to use them, read up Tony's tutorials on Process Control in the Tutorials section.

Otherwise:
Begin by breaking the picture down into pixels. Each pixel will be considered a single object. These objects will have x- and y- coords, and of course a colour.
Since you have the entire 'picture' stored as these particles, you can control them as you see fit. You could create a function that moves each of them down by some value in the y-direction, then update the screen to show the effects. This would translate to the same effect as what you already have. Of coures, in this method you'd be able to choose whether all of them fall at the same time, or whether they fall one column at a time.

Basically, what you have here is the rough, unpolished beginnings of a particle engine. Look around Source Code (and Apps as well mayhaps) for a couple examples. Specifically zylum's and Catalyst's.

Good work. + bits.
[Gandalf]




PostPosted: Mon May 16, 2005 8:11 pm   Post subject: (No subject)

Really nice effect - I might use it someday (with your permission, of course Smile).

It looks really good when you have at least 20 columns going at once. Try doing all of them at once (I used 500) - cool text effect, and potentially, graphics effect.

Anyway, hope too see scorched earth sometime - I really loved that game, played it for like 5 years straight, and then on and off for a while after that Very Happy.
Notoroge




PostPosted: Mon May 16, 2005 8:55 pm   Post subject: Re: Crumbling Picture

Lapsus Antepedis wrote:
my school's brand new P4 dualies can do 25 at once... ^^;
Not trying to be rude, but it seems like we have a slight case of sloppy programming on our hands. I recall seeing some stuff by catalyst that ran on my school's ugilest computers quite nicely, and most likely had more stuff flying around the screen than that.
Lapsus Antepedis




PostPosted: Tue May 17, 2005 7:13 am   Post subject: Re: Crumbling Picture

Notoroge wrote:
Not trying to be rude, but it seems like we have a slight case of sloppy programming on our hands. I recall seeing some stuff by catalyst that ran on my school's ugilest computers quite nicely, and most likely had more stuff flying around the screen than that.


Oops... I just remembered the amount of spyware and adware on the school network... ^^; Maybe I'll attempt to run ad-aware to kill some... Razz
the older computers actually work at the same performance level most of the time. Evil or Very Mad (they are all pentium2 @ 400MHz)

EDIT: I don't really try for fast, efficient code. I just throw these king of things together whenever the idea hits me... That program, for example came into existance a few minutes after I thought of it... I'm not really aiming for clean, fast code. (I'm not that good at it anyways Razz )
Delos




PostPosted: Tue May 17, 2005 2:14 pm   Post subject: (No subject)

Again, despite the cumbersome amounts of adware entangling your computers at school, this particular piece of code could have been done quite some more efficiently.
But then, most code is not perfect on a first try. Or a twenty-third for that matter.
Two words: Particle Engine.
Notoroge




PostPosted: Tue May 17, 2005 3:20 pm   Post subject: (No subject)

I concur. But yeah, first try is just showing off the idea and making it work like you want. After that you sit there and recode everything bit-by-bit, replacing everything with more efficient code. That's what I do anyways. Usually I just write completely unstructured the first time around. Everything that goes in the file is essentially as-I-go-along programming. Once it's working perfectly, I open a new window and have both sitting side-by-side. Look at every piece of code, and optimize it. Once I'm done, I do it again, then again, and again, etc.

Usually, for example a class assignment, I'll go through the code about twenty times. And every time I could find something to optimize.
Lapsus Antepedis




PostPosted: Wed May 18, 2005 7:41 pm   Post subject: (No subject)

I rewrote the whole program to use a 2D array and calculate it that way, but I have no I dea if it is faster or not... Also, no processes...

code:

var windoe : int := Window.Open ("graphics:500;100, nocursor, noecho")
var pixl : array 0 .. maxx, 0 .. maxy of int
var pictor : int := Pic.FileNew ("pictor.BMP")
Pic.Draw (pictor, 0, 0, picCopy)
Pic.Free (pictor)
for x : 0 .. maxx
    for y : 0 .. maxy
        pixl (x, y) := whatdotcolour (x, y)
    end for
end for

for x : 0 .. maxx
    for z : 0 .. maxy
        for y : 1 .. maxy - z
            if pixl (x, y) not= 0 and pixl (x, y - 1) = 0 then
                pixl (x, y - 1) := (pixl (x, y))
                pixl (x, y) := 0
                drawdot (x, y, pixl (x, y))
                drawdot (x, y - 1, pixl (x, y - 1))
            end if
        end for
    end for
end for



Here is my attempt to make more than one pixel move at once, but it just makes the pixels fall together into a little thing at the bottom that is one pixel tall...

code:

var windoe : int := Window.Open ("graphics:500;100, nocursor, noecho")
var pixl : array 0 .. maxx, 0 .. maxy of int
var pictor : int := Pic.FileNew ("pictor.BMP")
Pic.Draw (pictor, 0, 0, picCopy)
Pic.Free (pictor)

for x : 0 .. maxx
    for y : 0 .. maxy
        pixl (x, y) := whatdotcolour (x, y)
    end for
end for

proc dropit (x, y : int)
    var dist : int := 0
    for z : 1 .. y
        if pixl (x, z - 1) not= 0 then
            exit
        end if
        dist += 1
    end for
    pixl (x, y - dist) := pixl (x, y)
    pixl (x, y) := 0
    drawdot (x, y, pixl (x, y))
    drawdot (x, y - dist, pixl (x, y - dist))
end dropit

for y : 1 .. maxy
    for x : 0 .. maxx
        dropit (x, y)
    end for
    delay (10)
end for


does anyone have any ways to make these work?
or any suggestions on something to make of it?

feel free to use this code for your own use, but I would like to know what you use it for...
Sponsor
Sponsor
Sponsor
sponsor
Lapsus Antepedis




PostPosted: Thu May 19, 2005 12:01 pm   Post subject: (No subject)

See the first post for info...
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: