Page 1 of 1

PutPixel2

Posted: Fri Sep 21, 2007 9:12 am
by Buckethead
Hello, I need an example with this function.
GetPixel2, PutPixel2
The same as above, but for locked buffers only.
I'm under the impression it's a faster way to plot a dot. I get a "memory access violation" when I use PutPixel2.
All I need it's to fill pixel per pixel the whole window without other function like a filler or a filled shape.

An exemple:

Code: Select all

SCREEN 320,200
Color 255,0,0
Repeat
Lock SCREEN()
For y = 0 To 200
For x = 0 To 320
c = Rnd(255)
PutPixel x,y,c
Next x
Next y
Unlock SCREEN()
Text 1,1,FPS()
DrawScreen
Forever 
I got 30 FPS
(I know it's the worst way for making noise, It's just I need a faster instruction)

Have you a short exemple like this routine (or not ;)) for PutPixel2 ?

Re: PutPixel2

Posted: Fri Sep 21, 2007 4:19 pm
by CCE
Found this one from the finnish manual.

Code: Select all

SCREEN 640,480

AddText "Press any key to copy..."

'First make a gradient 
For i=0 To 240
    Color 0,i+15,0
    Line 0,i,640,i
Next i

DrawScreen OFF

WaitKey

'Lock screen 
Lock

'make 154 481 iterations, copy each pixel 
For y=0 To 240
For x=0 To 640
    PutPixel2 x,480-y,GetPixel2(x, y)
Next x
Next y

'Unlock screen 
Unlock

DrawScreen

WaitKey

Re: PutPixel2

Posted: Fri Sep 21, 2007 5:07 pm
by Buckethead
There's still this "memory access violation" error msg.
The locked buffer works with usual putpixel, somehow I wonder what PutPixel2 do compare to PutPixel. Is it faster, or maybe it's something deeper with adresses etc ?
(from this exemple it's seem to be simple as PutPixel)

Now you put it I've seen this example before. First I didn't get it because I didn't test getpixel alone.
I think that getpixel is just a number, the color grabbed from a pixel. To me this value can be used directly in putpixel.

I saw a kind of formula like (0 Shl 8) + (0 Shl 16) + (255 Shl 24) inside the code on the finnish forum when I was looking for PutPixel2. I've done stuff with another language where 255 was red and in Coolbasic it's blue. RGB instruction was 65536*n + 256*n + n [n(0,255)]

Edit: it's seem that Shl is a function you find in plenty other langages, it cames from assembler, and is of course very fast. Shl and Shr (left/right)
12 Shl 2, should move 12 value (00001100) two bits on the left then the result is 48 (00110000)

Re: PutPixel2

Posted: Sun Sep 23, 2007 7:52 pm
by koodaaja
Buckethead wrote:There's still this "memory access violation" error msg.
The locked buffer works with usual putpixel, somehow I wonder what PutPixel2 do compare to PutPixel. Is it faster, or maybe it's something deeper with adresses etc ?
(from this exemple it's seem to be simple as PutPixel)

Now you put it I've seen this example before. First I didn't get it because I didn't test getpixel alone.
I think that getpixel is just a number, the color grabbed from a pixel. To me this value can be used directly in putpixel.

I saw a kind of formula like (0 Shl 8) + (0 Shl 16) + (255 Shl 24) inside the code on the finnish forum when I was looking for PutPixel2. I've done stuff with another language where 255 was red and in Coolbasic it's blue. RGB instruction was 65536*n + 256*n + n [n(0,255)]

Edit: it's seem that Shl is a function you find in plenty other langages, it cames from assembler, and is of course very fast. Shl and Shr (left/right)
12 Shl 2, should move 12 value (00001100) two bits on the left then the result is 48 (00110000)
Oh, well... I didn't get all the things you said. But yes, getpixel returns a number. It's calculated with the following formula: blue + ( green shl 8 ) + ( red shl 16 ) + (alpha(mostly you will use 255) shl 24). They seem to be in a reverse order, but in the number you get by that formula, bits are arranged like this: AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB wich is the normal order: r, g, b. And of course, you can write it as (alpha shl 24) + ( red shl 16 ) + ( green shl 8 ) + blue if you want. So, in ure code, replace c = Rnd(255) with c = (Rnd(255)) Shl 16. Then it'll be red. And I didn't get MAV when I changed the command to PutPixel2. It was just 4 times faster ;D

Re: PutPixel2

Posted: Mon Sep 24, 2007 4:54 pm
by m1c
I also seem to get memory access violation every time i try to use PutPixel2. Even the example presented in manual doesn't work. Also drawing to images with PutPixel or PutPixel2 always fails.

Re: PutPixel2

Posted: Mon Sep 24, 2007 7:02 pm
by Buckethead
Nice, now I can put the color I need. Thanks.
I've no idea for this "MAV", I'm running under XP pro SP2, the lastest version of direct X. Intel E2140, 2 Go , 8800 GTS

At least I can plot faster than dot x,y.. PutPixel works for me. But PutPixel2 4x fastest, amazing. I could run some 320*200 little effects done pixel per pixel almost smooth.

Now I'm trying to make combined effects and I've trouble with Unlock SCREEN(), but somehow I didn't test double buffering yet.

Re: PutPixel2

Posted: Mon Sep 24, 2007 8:14 pm
by koodaaja
Buckethead wrote:Nice, now I can put the color I need. Thanks.
I've no idea for this "MAV", I'm running under XP pro SP2, the lastest version of direct X. Intel E2140, 2 Go , 8800 GTS

At least I can plot faster than dot, PutPixel works for me. But PutPixel2 4x fastest, amazing. I could run some 320*200 little effects done pixel per pixel almost smooth.

Now I'm trying to make combined effects and I've trouble with Unlock SCREEN(), but somehow I didn't test double buffering yet.
Double buffering is a default in CB, u don't have to worry about things so complicated youreself. And you don't have to call SCREEN() after Unlock, it unlocks the current locked buffer anyway. (Wow, you are lucky, I wish I had a computer that supports Vista and DirectX 10 :O)

I got FPS 7 with the following code:

Code: Select all

SCREEN 320, 240

Color 255, 255, 255

Repeat
    ang = WrapAngle(ang + 2)
    Lock SCREEN()
        For x = 0 To 320
            For y = 0 To 240
                col = (Sin(WrapAngle(Distance(x, y, 160, 120)*4-ang))*3)*20
                col = Max(0, Min(col, 255))
                PutPixel2 x, y, Int(Min(255, col^2) + (col Shl 8) + (Min(255, col*2) Shl 16) + (255 Shl 24))
            Next y
        Next x
    Unlock
    Text 10, 10, FPS()
    DrawScreen
Forever
It's not too fast, but if you draw boxes of width and height 3 to 5 instead (and don't scan the whole screen), you will get FPS 20+.

Re: PutPixel2

Posted: Tue Sep 25, 2007 7:35 pm
by Buckethead
Nice, I've 12 FPS (without PutPixel2)
I was mainly thinking on how I could open a large buffer (i.e. 4000x4000) to grab the pixels into the main screen. To create simple scroller, page flipping stuffs, or precalculated animation of 60 (or more) frames.
Actually I'm far to need it, I tried CopyBox and tested weirds things with Lock/Unlock. It seem there are no functions to scroll (only) a region inside a specified rectangle.