PutPixel2

Post your coding questions such as 'how to' here.
Post Reply
Buckethead
Newcomer
Posts: 16
Joined: Thu Sep 20, 2007 9:53 am

PutPixel2

Post by Buckethead » Fri Sep 21, 2007 9:12 am

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 ?

User avatar
CCE
Artist
Artist
Posts: 650
Joined: Mon Aug 27, 2007 9:53 pm

Re: PutPixel2

Post by CCE » Fri Sep 21, 2007 4:19 pm

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

Buckethead
Newcomer
Posts: 16
Joined: Thu Sep 20, 2007 9:53 am

Re: PutPixel2

Post by Buckethead » Fri Sep 21, 2007 5:07 pm

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)

User avatar
koodaaja
Moderator
Moderator
Posts: 1583
Joined: Mon Aug 27, 2007 11:24 pm
Location: Otaniemi - Mikkeli -pendelöinti

Re: PutPixel2

Post by koodaaja » Sun Sep 23, 2007 7:52 pm

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

m1c
Member
Posts: 65
Joined: Tue Aug 28, 2007 5:10 pm
Location: \o

Re: PutPixel2

Post by m1c » Mon Sep 24, 2007 4:54 pm

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.

Buckethead
Newcomer
Posts: 16
Joined: Thu Sep 20, 2007 9:53 am

Re: PutPixel2

Post by Buckethead » Mon Sep 24, 2007 7:02 pm

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.

User avatar
koodaaja
Moderator
Moderator
Posts: 1583
Joined: Mon Aug 27, 2007 11:24 pm
Location: Otaniemi - Mikkeli -pendelöinti

Re: PutPixel2

Post by koodaaja » Mon Sep 24, 2007 8:14 pm

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+.

Buckethead
Newcomer
Posts: 16
Joined: Thu Sep 20, 2007 9:53 am

Re: PutPixel2

Post by Buckethead » Tue Sep 25, 2007 7:35 pm

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.

Post Reply