View previous topic :: View next topic |
Author |
Message |
Pappons
Joined: 18 Jul 2008 Posts: 2 Location: Copenhagen, Denmark
|
Posted: Wed Sep 29, 2010 2:21 pm Post subject: setpixel? |
|
|
Hi guys,
I see there's a getpixel() function in the module arc.graphics.routines - but what about setpixel() ie. a function to write a pixel to a texture in memory?
I tried rolling my own:
Code: |
void SetPixel ( SDL_Surface* pSurface , Uint32 col , int x , int y )
{
if ( SDL_MUSTLOCK ( pSurface ) )
{
SDL_LockSurface( pSurface ) ;
}
//determine position
char* pPosition = cast( char* ) pSurface.pixels ;
//offset by y
pPosition += ( pSurface.pitch * y ) ;
//offset by x
pPosition += ( pSurface.format.BytesPerPixel * x ) ;
//copy pixel data
* pPosition = col ;
if ( SDL_MUSTLOCK ( pSurface ) )
{
SDL_UnlockSurface( pSurface ) ;
}
}
|
But it's not working - has anyone got any ideas?
(on a side note, i'm trying to draw on top of a texture i used with my getpixel() test - getpixel works fine, by the way!) |
|
Back to top |
|
|
SirAlaran
Joined: 19 Feb 2007 Posts: 84 Location: Silicon Valley
|
Posted: Wed Sep 29, 2010 8:40 pm Post subject: Re: setpixel? |
|
|
Code: |
uint* pixels = cast(uint*)(pSurface.pixels);
pixels[(y * pSurface.w) + x] = col;
|
This code assumes a 32-bit color depth for pSurface. Also keep in mind that when you modify the SDL_Surface the changes are not automatically uploaded to the graphics card memory. Arc uses OpenGL for the actual drawing. I think the updateTexture should do that.
Also, D has byte and ubyte types. Use those when you need a byte. char is for characters.
Pappons wrote: | Hi guys,
I see there's a getpixel() function in the module arc.graphics.routines - but what about setpixel() ie. a function to write a pixel to a texture in memory?
I tried rolling my own:
Code: |
void SetPixel ( SDL_Surface* pSurface , Uint32 col , int x , int y )
{
if ( SDL_MUSTLOCK ( pSurface ) )
{
SDL_LockSurface( pSurface ) ;
}
//determine position
char* pPosition = cast( char* ) pSurface.pixels ;
//offset by y
pPosition += ( pSurface.pitch * y ) ;
//offset by x
pPosition += ( pSurface.format.BytesPerPixel * x ) ;
//copy pixel data
* pPosition = col ;
if ( SDL_MUSTLOCK ( pSurface ) )
{
SDL_UnlockSurface( pSurface ) ;
}
}
|
But it's not working - has anyone got any ideas?
(on a side note, i'm trying to draw on top of a texture i used with my getpixel() test - getpixel works fine, by the way!) |
_________________ Current projects: Project Fermitas, Arctographer tile map editor, Arclib game library.
Gentoo | Textadept |
|
Back to top |
|
|
Pappons
Joined: 18 Jul 2008 Posts: 2 Location: Copenhagen, Denmark
|
Posted: Thu Sep 30, 2010 10:44 am Post subject: |
|
|
Wonderful - thanks for your quick reply!
Now the setpixel works and looks great as well:
Code: | /**
Sets a 32bit color pixel in the texture memory
*/
void setpixel32( Texture t , uint x , uint y , Color c )
{
updateTexture(t, Point(x,y), Size(1,1), cast( ubyte[] ) [c.r,c.g,c.b,c.a] );
}
|
|
|
Back to top |
|
|
mutable
Joined: 22 Jun 2010 Posts: 87
|
Posted: Thu Sep 30, 2010 4:37 pm Post subject: |
|
|
The SDL_Lock and -Unlock Operations are predestinated for the in/out block.
Just as a little tip. |
|
Back to top |
|
|
SirAlaran
Joined: 19 Feb 2007 Posts: 84 Location: Silicon Valley
|
Posted: Thu Sep 30, 2010 8:16 pm Post subject: |
|
|
mutable wrote: | The SDL_Lock and -Unlock Operations are predestinated for the in/out block.
Just as a little tip. |
http://www.digitalmars.com/d/1.0/dbc.html wrote: | The assert's in the in and out bodies are called contracts. Any other D statement or expression is allowed in the bodies, but it is important to ensure that the code has no side effects, and that the release version of the code will not depend on any effects of the code. For a release build of the code, the in and out code is not inserted. |
It is a much better idea to use the scope statement for such things, as the contracts are not present when you pass the -release switch to the compiler. _________________ Current projects: Project Fermitas, Arctographer tile map editor, Arclib game library.
Gentoo | Textadept |
|
Back to top |
|
|
mutable
Joined: 22 Jun 2010 Posts: 87
|
Posted: Fri Oct 01, 2010 7:53 am Post subject: |
|
|
SirAlaran wrote: | mutable wrote: | The SDL_Lock and -Unlock Operations are predestinated for the in/out block.
Just as a little tip. |
http://www.digitalmars.com/d/1.0/dbc.html wrote: | The assert's in the in and out bodies are called contracts. Any other D statement or expression is allowed in the bodies, but it is important to ensure that the code has no side effects, and that the release version of the code will not depend on any effects of the code. For a release build of the code, the in and out code is not inserted. |
It is a much better idea to use the scope statement for such things, as the contracts are not present when you pass the -release switch to the compiler. |
You mean
Code: |
scope(exit) {
SDL_UnlockSurface(pSurface);
}
|
or?
But which scope Block can i use for the lock command?
But thanks, i don't know before, that the -release switch turns contracts off But i love the in and out blocks for methods |
|
Back to top |
|
|
SirAlaran
Joined: 19 Feb 2007 Posts: 84 Location: Silicon Valley
|
Posted: Fri Oct 01, 2010 7:50 pm Post subject: |
|
|
mutable wrote: |
or?
But which scope Block can i use for the lock command?
But thanks, i don't know before, that the -release switch turns contracts off But i love the in and out blocks for methods |
Code: |
import std.stdio;
void unlock() {
writeln("unlocking");
}
void lock() {
writeln("locking");
}
void process() {
writeln("process");
}
void theFunction(bool needsLocking) {
if (needsLocking) lock();
scope(exit) if(needsLocking) unlock();
process();
}
void main(string[] args)
{
theFunction(true);
writeln("------");
theFunction(false);
}
|
stdout wrote: |
locking
process
unlocking
------
process
|
_________________ Current projects: Project Fermitas, Arctographer tile map editor, Arclib game library.
Gentoo | Textadept |
|
Back to top |
|
|
SirAlaran
Joined: 19 Feb 2007 Posts: 84 Location: Silicon Valley
|
Posted: Sat Oct 02, 2010 5:09 pm Post subject: |
|
|
I've added direct pixel access as a feature to Arclib 2. It can be used like this:
Code: | // Disables drawing with this texture until endPixelAccess is called
texture.beginPixelAccess();
for(uint i = 0; i != 20; ++i)
{
// Write pixels
texture.setPixel(i, 16, Color(0xff00ffff));
}
// Ends pixel access mode and copies pixel data to OpenGL.
texture.endPixelAccess();
|
http://www.dsource.org/projects/arclib/browser/branches/arc2-alpha/arc/graphics/texture.d _________________ Current projects: Project Fermitas, Arctographer tile map editor, Arclib game library.
Gentoo | Textadept |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|