adamdruppe: Drawing scaled image

Pie? via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 1 21:01:03 PDT 2016


On Thursday, 2 June 2016 at 03:37:01 UTC, Adam D. Ruppe wrote:
> On Thursday, 2 June 2016 at 03:19:20 UTC, Pie? wrote:
>> I'm curious about how to draw a scaled image.
>
> There's a few general options:
>
> 1) Scale it yourself in-memory then draw. This is a pain, I 
> don't think my public libraries have a scale method....
>
> 2) If on MS Windows, you can resize the window as a user and it 
> will stretch for you automatically. X on Linux doesn't support 
> this though.
>
> 3) Again, on MS Windows, you could call StretchBlt yourself to 
> scale and draw to it.
>
> 4) You can make an OpenGL texture and draw that at any 
> scale/rotation. This is fairly involved and uses a totally 
> different drawing system, but you can do it all with 
> simpledisplay.d too.
>
>> Also, png.d doesn't seem to work with alpha channel?
>
> png does, but simpledisplay doesn't (except for the OpenGL 
> stuff, of course).
>
> Both the MemoryImage implementations (TrueColorImage and 
> IndexedImage) have a rgba thing internally.... so you could 
> blend yourself before drawing (color.d has an alphaBlend 
> function, undocumented though), but if you want it done 
> automatically or in hardware, gotta use the opengl stuff.
>
>> Which also leads me to, how to draw one image on to another?
>
> Gotta DIY there, I haven't written a function for that yet.
>
> It's not hard to do though, at least with MemoryImages. The 
> simpledisplay Image is different - it is optimized for the 
> platform so the bits change around (it doesn't have an alpha 
> channel in all cases either).
>
> My terminal emulator does this.. here's the function:
>
> https://github.com/adamdruppe/terminal-emulator/blob/master/main.d#L296
>
> so basically you copy the bits yourself, using the offset 
> members to handle the platform-specific stuff. I didn't do 
> alpha blending there though... but the blend function from 
> color.d could do it.
>
>
>
> Doing alpha blending as we draw on the window needs new 
> functions used... AlphaBlend on Windows and XRender on Linux 
> IIRC. But since I tend to use OpenGL when I want to do lots of 
> the blending I haven't been in a big rush to do it.

Thanks, I'll look into it. I have tried OpenGL with simpledisplay 
but I cannot draw to the window. I assume other drawing methods 
are required?

	auto window = new SimpleWindow(600, 400, "Pong game!", 
OpenGlOptions.yes);
	
	window.setAsCurrentOpenGlContext();
	//auto img = imageFromPng(readPng(cast(ubyte[]) read(args[1])));
	// newer api, simpler but less control
	auto img = readPng(`x.png`);
	auto img2 = readPng(`y.png`);
	window.redrawOpenGlScene = delegate() {
			auto painter = window.draw();
			painter.clear();
			painter.drawImage(Point(10, 10), Image.fromMemoryImage(img2));
			painter.drawImage(Point(10, 10), Image.fromMemoryImage(img));
	};

	//displayImage(Image.fromMemoryImage(img), window);
	window.eventLoop(50, // set a 50 ms timer pulls
		// This runs once per timer pulse
		delegate () {
			//auto painter = window.draw();
			//painter.clear();
			//painter.drawImage(Point(10, 10), 
Image.fromMemoryImage(img2));
			//painter.drawImage(Point(10, 10), Image.fromMemoryImage(img));
			window.redrawOpenGlSceneNow();

		},
		delegate (KeyEvent event) {
		},
		delegate (MouseEvent event) {
		}
	);


If so, I can use gamehelpers.d to do the drawing of the images?

Thanks.



More information about the Digitalmars-d-learn mailing list