FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

setpixel?

 
Post new topic   Reply to topic     Forum Index -> ArcLib
View previous topic :: View next topic  
Author Message
Pappons



Joined: 18 Jul 2008
Posts: 2
Location: Copenhagen, Denmark

PostPosted: Wed Sep 29, 2010 2:21 pm    Post subject: setpixel? Reply with quote

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? Smile

(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
View user's profile Send private message
SirAlaran



Joined: 19 Feb 2007
Posts: 84
Location: Silicon Valley

PostPosted: Wed Sep 29, 2010 8:40 pm    Post subject: Re: setpixel? Reply with quote

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? Smile

(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
View user's profile Send private message
Pappons



Joined: 18 Jul 2008
Posts: 2
Location: Copenhagen, Denmark

PostPosted: Thu Sep 30, 2010 10:44 am    Post subject: Reply with quote

Wonderful - thanks for your quick reply! Smile

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
View user's profile Send private message
mutable



Joined: 22 Jun 2010
Posts: 87

PostPosted: Thu Sep 30, 2010 4:37 pm    Post subject: Reply with quote

The SDL_Lock and -Unlock Operations are predestinated for the in/out block.
Just as a little tip.
Back to top
View user's profile Send private message
SirAlaran



Joined: 19 Feb 2007
Posts: 84
Location: Silicon Valley

PostPosted: Thu Sep 30, 2010 8:16 pm    Post subject: Reply with quote

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
View user's profile Send private message
mutable



Joined: 22 Jun 2010
Posts: 87

PostPosted: Fri Oct 01, 2010 7:53 am    Post subject: Reply with quote

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 Smile But i love the in and out blocks for methods Smile
Back to top
View user's profile Send private message
SirAlaran



Joined: 19 Feb 2007
Posts: 84
Location: Silicon Valley

PostPosted: Fri Oct 01, 2010 7:50 pm    Post subject: Reply with quote

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 Smile But i love the in and out blocks for methods Smile


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
View user's profile Send private message
SirAlaran



Joined: 19 Feb 2007
Posts: 84
Location: Silicon Valley

PostPosted: Sat Oct 02, 2010 5:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> ArcLib All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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