Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setpixel(x,y) with examples #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Drawing functions:
* text(x,y,string): print the specified text at x,y using a bitmap font.
* triangle(x1,y1,x2,y2,x3,y3): draw a triangle with the specified vertex.
* getpixel(x,y): return the red,gree,blue value of the specified pixel.
* setpixel(x,y): draw a pixel at x,y.
* sprite(file,x,y,[rotation],[antialiasing]): draw sprite at coordinates with the specified rotation (in degrees, default 0) and antialiasing (default false).

Control functions:
Expand Down
33 changes: 33 additions & 0 deletions examples/astropixel.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- astropixel - getpixel/setpixel demo / k255 | 3x /

function setup()
px,py=WIDTH/2,HEIGHT/2
dpx,dpy=4,4
sx,sy={},{}
sn=1024
for i=1,sn do
sx[i],sy[i]=math.random(1,WIDTH),math.random(1,HEIGHT)
end
pc,sc={ 255,255,255,1 },{ 255,0,255,1 }
tick=0
setFPS(60)
end

function draw()
tick=tick+1
fill(0,0,0,0.05)
rect(0,0,WIDTH,HEIGHT)
fill(unpack(sc))
for i=1,sn do
if tick%60==0 and math.random(0,10)==10 then
ellipse(sx[i],sy[i],2,2)
end
ellipse(sx[i],sy[i],1,1)
end
fill(unpack(pc))
if getpixel(px,py) == unpack(sc) then
dpx,dpy=math.random(-10,10)/5,math.random(-10,10)/5
end
setpixel(px,py)
px,py=(px+dpx)%WIDTH,(py+dpy)%HEIGHT
end
19 changes: 19 additions & 0 deletions examples/pixels.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- pixels - setpixel demo / k255 | 3x /

function setup()
tick=0
d=1000
s=50
rgba={ 255,255,255,.75 }
end

function draw()
tick=tick+1
fill(unpack(rgba))
if tick%s==0 then
rgba[math.random(1,3)] = math.random(0,255)
end
for i=0,d,1 do
setpixel(math.random(0,WIDTH),math.random(0,HEIGHT))
end
end
12 changes: 12 additions & 0 deletions load81.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ int getpixelBinding(lua_State *L) {
return 3;
}

int setpixelBinding(lua_State *L) {
int x, y;

x = lua_tonumber(L,-2);
y = lua_tonumber(L,-1);

setPixelWithAlpha(l81.fb,x,y,l81.r,l81.g,l81.b,l81.alpha);
return 0;
}

int spriteBinding(lua_State *L) {
const char *filename;
int x, y, angle, antialiasing;
Expand Down Expand Up @@ -491,6 +501,8 @@ void resetProgram(void) {
lua_setglobal(l81.L,"setFPS");
lua_pushcfunction(l81.L,getpixelBinding);
lua_setglobal(l81.L,"getpixel");
lua_pushcfunction(l81.L,setpixelBinding);
lua_setglobal(l81.L,"setpixel");
lua_pushcfunction(l81.L,spriteBinding);
lua_setglobal(l81.L,"sprite");

Expand Down